3

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?
}
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53

2 Answers2

3

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();
Community
  • 1
  • 1
centic
  • 15,565
  • 9
  • 68
  • 125
-1


$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

Mudassir Razvi
  • 1,783
  • 12
  • 33
  • I'm using [jgit](http://www.eclipse.org/jgit/) and not the normal git command line tool. – Davide Pastore Oct 01 '14 at 14:33
  • In that case see if this helps: http://stackoverflow.com/questions/8520682/how-do-i-do-the-equivalent-of-git-diff-name-status-with-jgit The solution is to use diff-tree, how you use it in jgit is given in the link above. – Mudassir Razvi Oct 01 '14 at 14:41