10

I am trying to show a git diff between two commits for a file. Basically, I did it as in https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowChangedFilesBetweenCommits.java

You can see my full code under https://github.com/svenhornberg/JGitDiff

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<DiffEntry> 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;
    }
}

the code results in this message:

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)

the following line must be false, but it is like in the example from jgit-cookbook

 ObjectId oldHead = repository.resolve("HEAD^^^^{tree}");   //here is my nullpointer

I tested HEAD^1{Tree} but this does not work, it looks like {tree} must be in the string for resolving a tree instead of a commit id. Any ideas to get it working?

1
  • 1
    As a side note: repository.getDirectory().getParent() does not necessarily return the work dir, use repository.getWorkTree() instead. And another note: you can also obtain the repository through git.getRepository() and thus spare the repository parameter in commitFileContent. Commented Dec 9, 2014 at 9:10

2 Answers 2

14

To obtain the tree of the head commit, call

git.getRepository().resolve( "HEAD^{tree}" )

and to obtain the tree of the parent of the HEAD commit, call

git.getRepository().resolve( "HEAD~1^{tree}" )

Search for 'Git caret and tilde' if you are interested in more details.

To summarize, here goes a snippet that computes the diff of two commits:

File file = new File( git.getRepository().getWorkTree(), "file.txt" );
writeFile( file, "first version" );
RevCommit newCommit = commitChanges();
writeFile( file, "second version" );
RevCommit oldCommit = commitChanges();

ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
ObjectId oldTree = git.getRepository().resolve( "HEAD^{tree}" ); // equals newCommit.getTree()
oldTreeIter.reset( reader, oldTree );
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
ObjectId newTree = git.getRepository().resolve( "HEAD~1^{tree}" ); // equals oldCommit.getTree()
newTreeIter.reset( reader, newTree );

DiffFormatter df = new DiffFormatter( new ByteArrayOutputStream() ); // use NullOutputStream.INSTANCE if you don't need the diff output
df.setRepository( git.getRepository() );
List<DiffEntry> entries = df.scan( oldTreeIter, newTreeIter );

for( DiffEntry entry : entries ) {
  System.out.println( entry );
}

private RevCommit commitChanges() throws GitAPIException {
  git.add().addFilepattern( "." ).call();
  return git.commit().setMessage( "commit message" ).call();
}

private static void writeFile( File file, String content ) throws IOException {
  FileOutputStream outputStream = new FileOutputStream( file );
  outputStream.write( content.getBytes( "UTF-8" ) );
  outputStream.close();
}

For further considerations about showing changes between commits, you may want to read this in-depth discussion of JGit's diff APIs that can be found here: http://www.codeaffine.com/2016/06/16/jgit-diff/

3
  • This works well, except if new commit is a merged commit with two or more parents. If I use this code with multiple parents then I get all diffs up to merge base commit. Any idea how to get diffs of a merge commit (mostly they are none, but sometimes there is some file changed) without including all diffs up to merge base? Note: normal git diff does the same so this is correct behaviour, just not behaviour I desire in this case. What I need is something like git show <merged_commit> which unfortunately does not exist in JGit. Any help is much appreciated. Commented Sep 22, 2015 at 15:16
  • The above code compares two trees, independant of their originating commits. How do you obtain the commits? I suggest to open a new question with the code (ideally a test case) attached that you have so far. Commented Sep 22, 2015 at 17:41
  • 3
    You can use new DiffFormatter(NullOutputStream.INSTANCE) if you don't need the output. org.eclipse.jgit.util.io.NullOutputStream
    – René Link
    Commented Oct 10, 2016 at 11:25
2

I am using the following code to print diffs between two commits. Using DiffFormatter.scan seems simpler than the other solutions I have seen on SO.

It might be that I am missing some case(s) where this is not supported, but it works fine for our use case.

public static void main(String[] args) throws Exception {
    Repository repository = new FileRepositoryBuilder()
            .setGitDir(new File("c:/temp/jgit-test/.git")).build();
    // Here we get the head commit and it's first parent.
    // Adjust to your needs to locate the proper commits.
    RevCommit headCommit = getHeadCommit(repository);
    RevCommit diffWith = headCommit.getParent(0);
    FileOutputStream stdout = new FileOutputStream(FileDescriptor.out);
    try (DiffFormatter diffFormatter = new DiffFormatter(stdout)) {
        diffFormatter.setRepository(repository);
        for (DiffEntry entry : diffFormatter.scan(diffWith, headCommit)) {
            diffFormatter.format(diffFormatter.toFileHeader(entry));
        }
    }
}

private static RevCommit getHeadCommit(Repository repository) throws Exception {
    try (Git git = new Git(repository)) {
        Iterable<RevCommit> history = git.log().setMaxCount(1).call();
        return history.iterator().next();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.