Get the list of changed files after a pull with JGit

自作多情 提交于 2019-12-10 20:28:31

问题


I would like to know how I can retrieve the list of changed files after a pull request.

I'm using this to get all the merged commits, but I want to know all the changed files.

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?
}

回答1:


Building on the previous comments/questions:

Get the current head before you pull:

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

And after the pull again:

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

Then you should be able to run the diff the same way as in How do I do the equivalent of "git diff --name-status" with jgit?:

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();



回答2:



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

My Approach that works for me:
1. Cat all the hashes into a file (Say tempfile1)
2. xargs -n1 git diff-tree --no-renames --no-commit-id --name-only -r < tempfile1 >> tempfile2
3. sort tempfile2 | uniq >> final (To remove duplicate entries)

final file will have all the changed files



来源:https://stackoverflow.com/questions/26143212/get-the-list-of-changed-files-after-a-pull-with-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!