8

我想获取修订版 XXXXXX 和 HEAD 之间更改/添加/删除文件的列表。这是我到目前为止所拥有的:

String oldHash = "a97e5553e37a25bd1a3c99eab303145baed08dbd";
Git git = Git.open(new File("/tmp/jgit"));
Repository repository = git.getRepository();
ObjectId old = repository.resolve(oldHash);
ObjectId head = repository.resolve("HEAD");

// how do i get the trees from the obj. id?
List<<DiffEntry> diffs = git.diff().setNewTree(null).setOldTree(null).call();

for(DiffEntry diff : diffs) {
    // do stuff
}

这是正确的方法吗?如果是,我如何获得 git.diff() 所需的树?

4

1 回答 1

10

您可以通过调用获取 HEAD 的树 ID 和哈希:

ObjectId head = repository.resolve("HEAD^{tree}");

对于修订 ID:

ObjectId old = repository.resolve(oldHash + "^{tree}");

获得树 ID 后,您可以创建树迭代器并获取差异:

ObjectReader reader = repository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldId);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, headId);
List<DiffEntry> diffs= git.diff()
                        .setNewTree(newTreeIter)
                        .setOldTree(oldTreeIter)
                        .call();
于 2011-12-19T23:17:46.987 回答