使用JGit获取后获取已更改文件的列表

时间:2014-10-01 13:59:56

标签: java git jgit

我想知道如何在拉取请求后检索已更改文件的列表。

我使用它来获取所有合并的提交,但我想知道所有已更改的文件。

Git git = new Git(localRepo);
PullCommand pullCmd = git.pull();
PullResult pullResult = pullCmd.call();
MergeResult mergeResult = pullResult.getMergeResult();
ObjectId[] mergedCommits = mergeResult.getMergedCommits();

for (ObjectId mergedCommit : mergedCommits) {
  // And now?
}

2 个答案:

答案 0 :(得分:2)

以前的评论/问题为基础:

在拉之前获取当前头

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

之后再拉一次:

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

然后你应该能够像How do I do the equivalent of "git diff --name-status" with jgit?中那样运行diff:

ObjectReader reader = repository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
List<DiffEntry> diffs= git.diff()
                    .setNewTree(newTreeIter)
                    .setOldTree(oldTreeIter)
                    .call();

答案 1 :(得分:-1)


$git diff-tree --no-renames --no-commit-id --name-only -r

我的方法对我有用:
1.将所有哈希值转换成文件(说tempfile1)
2. xargs -n1 git diff-tree --no-renames --no-commit-id --name-only -r&lt; tempfile1&gt;&gt; tempfile2
3. sort tempfile2 | uniq&gt;&gt; final(删除重复的条目)

最终文件将包含所有已更改的文件