title: How to show changes between commits with JGit
body: <p>I am trying to show a git diff between two commits for a file. Basically, I did it as in <a href="https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java" rel="noreferrer">https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java</a></p>
  
  <p>You can see my full code under <a href="https://github.com/svenhornberg/JGitDiff" rel="noreferrer">https://github.com/svenhornberg/JGitDiff</a></p>
  
  <pre><code>public class RepoDiff {
  
      public void compare(byte[] fileContentOld, byte[] fileContentNew) {
          try {
              Repository repository = createNewRepository();
              Git git = new Git(repository);
  
              // create the file
              commitFileContent(fileContentOld, repository, git, true);
              commitFileContent(fileContentNew, repository, git, false);
  
              // The {tree} will return the underlying tree-id instead of the commit-id itself!
              ObjectId oldHead = repository.resolve("HEAD^^^^{tree}");   //here is my nullpointer
              ObjectId head = repository.resolve("HEAD^{tree}");
  
              System.out.println("Printing diff between tree: " + oldHead + " and " + head);
  
              // prepare the two iterators to compute the diff between
              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
              List&lt;DiffEntry&gt; diffs= new Git(repository).diff()
                              .setNewTree(newTreeIter)
                              .setOldTree(oldTreeIter)
                              .call();
              for (DiffEntry entry : diffs) {
                  System.out.println("Entry: " + entry);
              }
              System.out.println("Done");
  
          } catch (Exception exception) {
              exception.printStackTrace();
          }
      }
  
  /**
   * Adds and optionally commits fileContent to a repository
   * @param commit True if file should be committed, False if not
   * @throws Exception catch all for testing
   */
  private void commitFileContent(byte[] fileContent, Repository repository, Git git, boolean commit) throws Exception {
  
      File myfile = new File(repository.getDirectory().getParent(), "testfile");
      myfile.createNewFile();
  
      FileOutputStream fos = new FileOutputStream (myfile); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      baos.write(fileContent);
      baos.writeTo(fos);
  
      // run the add
      git.add().addFilepattern("testfile").call();
  
      if(commit) {
          // and then commit the changes
          git.commit().setMessage("Added fileContent").call();
      }
  
      fos.close();
  }
  
  /**
   * Helperfunction from
   * https://github.com/centic9/jgit-cookbook
   */
  public static Repository createNewRepository() throws IOException {
      // prepare a new folder
      File localPath = File.createTempFile("TestGitRepository", "");
      localPath.delete();
  
      // create the directory
      Repository repository = FileRepositoryBuilder.create(new File(
              localPath, ".git"));
      repository.create();
  
      return repository;
      }
  }
  </code></pre>
  
  <p>the code results in this message: </p>
  
  <pre><code>Printing diff between tree: null and AnyObjectId[c11a3a58c23b0dd6e541c4bcd553197772626bc6]
  java.lang.NullPointerException
  at  org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.index(UnpackedObjectCache.java:146)
  at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.contains(UnpackedObjectCache.java:109)
  at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache.isUnpacked(UnpackedObjectCache.java:64)
  at org.eclipse.jgit.internal.storage.file.ObjectDirectory.openObject(ObjectDirectory.java:367)
  at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:145)
  at org.eclipse.jgit.treewalk.CanonicalTreeParser.reset(CanonicalTreeParser.java:202)
  at javadiff.RepoDiff.compare(RepoDiff.java:40)
  at javadiff.GitDiff.main(GitDiff.java:30)
  </code></pre>
  
  <p><strong>the following line must be false, but it is like in the example from jgit-cookbook</strong></p>
  
  <pre><code> ObjectId oldHead = repository.resolve("HEAD^^^^{tree}");   //here is my nullpointer
  </code></pre>
  
  <p>I tested <code>HEAD^1{Tree}</code> but this does not work, it looks like <code>{tree}</code> must be in the string for resolving a tree instead of a commit id. Any ideas to get it working?</p>