I want to get the changed files between two commits, not the diff information, how can I use JGit to make it?
Asked
Active
Viewed 468 times
0
-
I think this has already been answered here: https://stackoverflow.com/questions/28785364/list-of-files-changed-between-commits-with-jgit – Rüdiger Herrmann Nov 04 '20 at 15:22
-
Does this answer your question? [List of files changed between commits with JGit](https://stackoverflow.com/questions/28785364/list-of-files-changed-between-commits-with-jgit) – Rüdiger Herrmann Nov 04 '20 at 15:23
1 Answers
0
With two refs pointing at the two commits it should suffice to do the following to iterate all changes between the commits:
ObjectId oldHead = repository.resolve("HEAD^^^^{tree}");
ObjectId head = repository.resolve("HEAD^{tree}");
// prepare the two iterators to compute the diff between
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
// finally get the list of changed files
try (Git git = new Git(repository)) {
List<DiffEntry> diffs= git.diff()
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();
for (DiffEntry entry : diffs) {
System.out.println("Entry: " + entry);
}
}
}
}
There is a ready-to-run example snippet contained in the jgit-cookbook

centic
- 15,565
- 9
- 68
- 125
-
However, DiffEntry can only provide information like newPath and oldPath. How can I use the paths or other possible information provided by DiffEntry to get the files changed? – Allen Abner Nov 06 '20 at 06:20
-
Not sure what you mean with "files changed" missing. You get the new/old path as part of the DiffEntry. For non-moves it will be the same for both "old" and "new", so for simple modifications, either will be your "path" – centic Nov 06 '20 at 18:22