How to get the file list for a commit with JGit

Each commit points to a tree that denotes all files that make up the commit.

Note, that this not only includes the files that were added, modified, or removed with this particular commit but all files contained in this revision.

If the commit is represented as a RevCommit, the ID of the tree can be obtained like this:

ObjectId treeId = commit.getTree().getId();

If the commit ID originates from another source, it needs to be resolved first to get the associated tree ID. See here, for example: How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

In order to iterate over a tree, use a TreeWalk:

try (TreeWalk treeWalk = new TreeWalk(repository)) {
  treeWalk.reset(treeId);
  while (treeWalk.next()) {
    String path = treeWalk.getPathString();
    // ...
  }
}

If you are only interested in the changes that were recorded with a certain commit, see here: Creating Diffs with JGit or here: File diff against the last commit with JGit

Tags:

Java

Jgit