Jgit比较更改返回空值

时间:2019-05-06 10:53:15

标签: java git jgit

我正在尝试提交文件,并比较两次提交之间的文件更改。我已经尝试了以下代码

public void changes() throws IOException, GitAPIException {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        repo = builder.setGitDir(new File("D:/Gitrepo" + "/.git")).setMustExist(true)
                        .build();
        git = new Git(repo);
        File file = new File( git.getRepository().getWorkTree(), "file.txt" );
        writeFile( file, "first version" );
        RevCommit newCommit = commitChanges();
        writeFile( file, "second version" );
        RevCommit oldCommit = commitChanges();

        ObjectReader reader = git.getRepository().newObjectReader();
        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        ObjectId oldTree = git.getRepository().resolve( "HEAD^{tree}" ); // equals newCommit.getTree()
        oldTreeIter.reset( reader, oldTree );
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        ObjectId newTree = git.getRepository().resolve( "HEAD~1^{tree}" ); // equals oldCommit.getTree()
        newTreeIter.reset( reader, newTree );

        DiffFormatter df = new DiffFormatter( new ByteArrayOutputStream() );
        df.setRepository( git.getRepository() );
        List<DiffEntry> entries = df.scan( oldTreeIter, newTreeIter );

        for( DiffEntry entry : entries ) {
          System.out.println( entry );
        }
        }

    private RevCommit commitChanges() throws GitAPIException {
      git.add().addFilepattern( "." ).call();
      return git.commit().setMessage( "commit message" ).call();
    }

    private static void writeFile( File file, String content ) throws IOException {
      FileOutputStream outputStream = new FileOutputStream( file );
      outputStream.write( content.getBytes( "UTF-8" ) );
      outputStream.close();
    }

当我执行以上代码时,我得到的输出为DiffEntry [MODIFY file.txt],而不是文件内容有所不同。 (即第一版和第二版)。

所以我试图使用下面的哈希码比较内容

 listDiff(repo, git, "b7ed64bb0356bf0e0a1d0b01fd7cd178deca01f5",
                            "e33dad5cbbf848bff3000b85acd9f5f9bd04102c");

private static void listDiff(Repository repository, Git git, String oldCommit, String newCommit) throws GitAPIException, IOException {
            final List<DiffEntry> diffs = git.diff()
                    .setOldTree(prepareTreeParser(repository, oldCommit))
                    .setNewTree(prepareTreeParser(repository, newCommit))
                    .call();

            System.out.println("Found: " + diffs.size() + " differences");
            for (DiffEntry diff : diffs) {
                System.out.println("Diff: " + diff.getChangeType() + ": " +
                        (diff.getOldPath().equals(diff.getNewPath()) ? diff.getNewPath() : diff.getOldPath() + " -> " + diff.getNewPath()));
            }
        }


 private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException {
        // from the commit we can build the tree which allows us to construct the TreeParser
        //noinspection Duplicates
        try (RevWalk walk = new RevWalk(repository)) {
            RevCommit commit = walk.parseCommit(repository.resolve(objectId));
            RevTree tree = walk.parseTree(commit.getTree().getId());

            CanonicalTreeParser treeParser = new CanonicalTreeParser();
            try (ObjectReader reader = repository.newObjectReader()) {
                treeParser.reset(reader, tree.getId());
            }

            walk.dispose();

            return treeParser;
        }
    }

但是这也不起作用,它返回Diff:0 我从“ D:\ Gitrepo.git \ logs \ refs \ heads \ master”中获取了哈希码。 请纠正我我要去哪里哪里

1 个答案:

答案 0 :(得分:0)

我通过重新编写如下代码解决了该问题

public static void main(String[] args) throws Exception {
    Repository repository = new FileRepositoryBuilder()
            .setGitDir(new File("D:/Gitrepo/.git")).build();

    RevCommit headCommit = getHeadCommit(repository);
    RevCommit diffWith = headCommit.getParent(0);
    FileOutputStream stdout = new FileOutputStream(FileDescriptor.out);
    try (DiffFormatter diffFormatter = new DiffFormatter(stdout)) {
        diffFormatter.setRepository(repository);
        for (DiffEntry entry : diffFormatter.scan(diffWith, headCommit)) {
            diffFormatter.format(diffFormatter.toFileHeader(entry));
        }
    }
}

private static RevCommit getHeadCommit(Repository repository) throws Exception {
    try (Git git = new Git(repository)) {
        Iterable<RevCommit> history = git.log().setMaxCount(1).call();
        return history.iterator().next();
    }
}

它像下面那样打印文件的内容

diff --git a/file.txt b/file.txt
index bde942a..506150f 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-first version
\ No newline at end of file
+second version
\ No newline at end of file

感谢@RüdigerHerrmann的指导