1

I want to list the commits of a local Git repository in Java. So I am using below code to do so:

Iterable<RevCommit> commits = git.log().all().call();

But I also need the path of every file that was committed and listed in git log.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Rujuta S
  • 171
  • 1
  • 1
  • 11
  • For each commit in `commits` you can list the files as described in this post: https://stackoverflow.com/questions/40590039/how-to-get-the-file-list-for-a-commit-with-jgit – Rüdiger Herrmann Dec 29 '17 at 08:45
  • I am getting IncorrectObjectTypeException on line `treeWalk.reset(rev.getId());` in below code. `Iterable logs = git.log().call(); for (RevCommit rev : logs) { ` – Rujuta S Dec 29 '17 at 09:12
  • What is `rev`? Please edit your question to include the complete snippet that you are running. – Rüdiger Herrmann Dec 30 '17 at 09:11

2 Answers2

1

Solution worked for me:

ObjectReader or = repository.newObjectReader();
        RevWalk rw = new RevWalk(or);
        TreeWalk tw = new TreeWalk(or);
          tw.setFilter(TreeFilter.ANY_DIFF);
          tw.setRecursive(true);
          ObjectId start = repository.resolve(Constants.HEAD);
          startId = or.abbreviate(start);
          rw.markStart(rw.parseCommit(start));

          for(;;){
          RevCommit c = rw.next();
          if (c == null){
              break;
          }
          if (c.getParentCount() != 1){
              continue;
          }
          RevCommit p = c.getParent(0);
            rw.parseHeaders(p);
            tw.reset(p.getTree(), c.getTree());
            while (tw.next()) {
                System.out.println(tw.getPathString()+" id: "+c.getId());
            }
          }
Rujuta S
  • 171
  • 1
  • 1
  • 11
0

Regarding IncorrectObjectTypeException, you do have the TreeWalk.reset() throwing that exception because, from the Javadoc:

the given object id does not denote a tree, but instead names some other non-tree type of object. Note that commits are not trees, even if they are sometimes called a "tree-ish".

See those examples of TreeWalk.reset to see how to access the tree from a given commit. Like rev.getTree().

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250