Git diff 文本内容获取方式

使用org.eclipse.jgit.diff;(org.eclipse.igit:org.eclipse.igit)

//以下是读取变更行
Git git = init(gitPath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(out);
// 设置比较器为忽略空白字符对比(Ignores all whitespace)
df.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
df.setRepository(git.getRepository());
df.format(diffEntry); // 打印文件差异具体内容
String diffText = new String(out.toByteArray(), "UTF-8");

public Git init(String remotePath) {
        String dirName = remotePath.replace("gitlab", "igit").replace("igit.58corp.com:", "igit.58corp.com/").split("igit.58corp.com/")[1].replace("/", "_").replace(".git", "").replace("http:", "https:");
        remotePath = remotePath.replace("http:", "https:");
        logger.debug("start git init:" + remotePath + "; " + dirName);
        // 设置远程服务器上的用户名和密码
        // 克隆代码库命令
        Git git = null;

        try {
            File localPath = new File(CoverageConstants.GIT_CODEBASE_ROOT + dirName);
            if (!localPath.exists()) {
                CloneCommand cloneCommand = Git.cloneRepository();
                git = cloneCommand.setURI(remotePath.replace("gitlab", "igit").replace("git@igit.58corp.com:", "https://igit.58corp.com/")) // 设置远程URI
                        //.setBranch("master") // 设置clone下来的分支
                        .setDirectory(localPath) // 设置下载存放路径
                        .setCredentialsProvider(usernamePasswordCredentialsProvider) // 设置权限验证
                        .call();
                git.fetch().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call();
            } else {
                String URL = CoverageConstants.GIT_CODEBASE_ROOT + dirName + "/.git";
                git = Git.open(new File(URL));
                git.fetch().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call();
            }

        } catch (TransportException e) {
            logger.error("init git error:" + remotePath + ":" + e.getMessage());
        } catch (IOException e) {
            logger.error("init git error:" + remotePath + ":" + e.getMessage());
        } catch (InvalidRemoteException e) {
            logger.error(e.getMessage());
        } catch (GitAPIException e) {
            logger.error("init git error:" + remotePath + ":" + e.getMessage());
        }
        logger.debug("init completed:");
        return git;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值