0

I'm writing a program with Java using JGit. I want to find out the differences between two commits in a project. So I convert two commits from String to ObjectId, but it always shows me error that the commits which I put in are not trees. Can anyone please help me?

Hier is the code:

try (Git git = new Git(repository)) {

   ObjectId commit1 = ObjectId.fromString("123456........" );
   ObjectId commit2 = ObjectId.fromString("sfggdg........" );

   ObjectReader reader = repo.newObjectReader();

   CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
   oldTreeIter.reset(reader, commit1);
   CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
   newTreeIter.reset(reader, commit2);

   System.out.println("Differences between two commits: ");

   List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();

   for (DiffEntry diff : diffs) {

        System.out.println(diff.getPath(null);

    } 
}

Output:

Differences between two commits:
org.eclipse.jgit.errors.IncorrectObjectTypeException: Object 123456........ is not a tree.
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:150)
at org.eclipse.jgit.treewalk.CanonicalTreeParser.reset(CanonicalTreeParser.java:214)
at com.capgemini.fis.DiffsBetweenTwoCommits.main(DiffsBetweenTwoCommits.java:57)

Expected Output: (for example)

Differences between two commits:
README.txt
file.h
thuyanh
  • 23
  • 8
  • This has been answered here already: http://stackoverflow.com/questions/27361538/how-to-show-changes-between-commits-with-jgit. Just replace `HEAD` and `HEAD~1` with the respective commit ids. – Rüdiger Herrmann Jun 02 '16 at 08:39
  • Thank you Rüdiger! I've tried it before, but the program shows me the differences between another two commits, not the 2 commits that I put in. Can they be the parents of the two commits which I put in? – thuyanh Jun 02 '16 at 09:05
  • @RüdigerHerrmann you are right. I just need to replace HEAD and HEAD~1 with the respective commit ids. I misstook commit ids for tree id. I'll delete this question. – thuyanh Jun 02 '16 at 13:41

0 Answers0