我正在使用JGit编写Java程序。 我想找出项目中两个提交之间的差异。 所以我将两个提交从String转换为ObjectId,但它总是显示我输入的提交不是树的错误。 有人可以帮帮我吗?
Hier是代码:
try (Git git = new Git(repository)) {
ObjectId commit1 = ObjectId.fromString("123456........" );
ObjectId commit2 = ObjectId.fromString("sfggdg........" );
ObjectReader reader = repo.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, commit1);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, commit2);
System.out.println("Differences between two commits: ");
List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();
for (DiffEntry diff : diffs) {
System.out.println(diff.getPath(null);
}
}
输出:
Differences between two commits:
org.eclipse.jgit.errors.IncorrectObjectTypeException: Object 123456........ is not a tree.
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:150)
at org.eclipse.jgit.treewalk.CanonicalTreeParser.reset(CanonicalTreeParser.java:214)
at com.capgemini.fis.DiffsBetweenTwoCommits.main(DiffsBetweenTwoCommits.java:57)
预期输出:(例如)
Differences between two commits:
README.txt
file.h