2

I want to get the list of all the files which were part of a commit. I have the commit id available with me.

I looked into the following link

How to get the file list for a commit with JGit

and tried the following code.

TreeWalk treeWalk = new TreeWalk( repository );
treeWalk.reset( commit.getId() );
while( treeWalk.next() ) {
  String path = treeWalk.getPathString();
  // ...
}
treeWalk.close();

and following code

try( RevWalk walk = new RevWalk( git.getRepository() ) ) {
  RevCommit commit = walk.parseCommit( commitId );
  ObjectId treeId = commit.getTree().getId();
  try( ObjectReader reader = git.getRepository().newObjectReader() ) {
    return new CanonicalTreeParser( null, reader, tree );
  }
}

With the above code I get the list of all the files present in the branch. I need the list of files which are deleted, modified or added on a commit.

With the following git command I successfully get the list of files which were part of particular commit

git diff-tree --name-only -r <commitId>

I want the same thing from JGit.

Update : I don't want to get the difference between two commits but only the list of files changed as a part of commit.

pranay jain
  • 352
  • 1
  • 2
  • 15
  • Possible duplicate of [How to show changes between commits with JGit](https://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit) – Rüdiger Herrmann Oct 14 '17 at 09:38
  • @RüdigerHerrmann. I don't want the changes between two commits but changes which were part of commit with the help of commit id – pranay jain Oct 16 '17 at 03:57
  • @pranayjain Git commits record the state of all files, not just "what changed" (see [Git for Computer Scientists](http://eagain.net/articles/git-for-computer-scientists/)). To determine "what changed", you have to compare the state of files in one commit with the state in some other commit. In your case, it's likely you want to compare against a parent commit, but you'll have to decide what to do if there are multiple. – Brett Kail Oct 16 '17 at 04:13

1 Answers1

1

You can use the Git.diff() command. It requires two Tree-Iterators:

    final List<DiffEntry> diffs = git.diff()
            .setOldTree(prepareTreeParser(repository, oldCommit))
            .setNewTree(prepareTreeParser(repository, newCommit))
            .call();

A tree-iterator can be created from the commitId with the following:

    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(repository.resolve(objectId));
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        try (ObjectReader reader = repository.newObjectReader()) {
            treeParser.reset(reader, tree.getId());
        }

        walk.dispose();

        return treeParser;
    }

Then you can iterate over the resulting List and retrieve details for each changed/added/removed file.

See also this sample in my jgit-cookbook for a runable showcase.

centic
  • 15,565
  • 9
  • 68
  • 125
  • I have only 1 commit id available with me. If I need to get it working with the help of above snippet I need to get the next commit id. Is this the only way out ? – pranay jain Oct 16 '17 at 04:03
  • Your sample codes in jgit-cookbook solved my problem. – pranay jain Oct 16 '17 at 09:26
  • Yep, for others: the caret-syntax "^" will point to the previous commit, so you can use "^" in oldCommit and "" in newCommit to do this. – centic Oct 16 '17 at 15:14
  • I have checked jgit-cookbook but I am getting an exception like " Exception in thread "main" org.eclipse.jgit.errors.MissingObjectException: Missing unknown 3d857c0629b672f07d478d0e6a5ebc50bdde48f0". – Goutham Harshith Mar 20 '20 at 06:54