问题
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