File diff against the last commit with JGit

I am trying to use JGit to get the differences of a file from the last commit to the most recent uncommitted changes. How can I do this with JGit? (using the command line would be the output of git diff HEAD)

Following several discussions (link1, link2) I come with a piece of code that is able to find the files that are uncommited, but it I cannot get the difference of the files

  • What of Eclipse project .metadata can I safely ignore in Git/Mercurial?
  • git tree (still) contains duplicates and an erroneous signal 13
  • How do I “git blame” a deleted line?
  • Is Perforce worth it?
  • Is git's implementation of the patience diff algorithm correct?
  • Git completion not working in zsh on OS X Yosemite with Homebrew
  • Repository db = new FileRepository("/path/to/git");
    Git git = new Git(db);
    
    AbstractTreeIterator oldTreeParser = this.prepareTreeParser(db, Constants.HEAD);
    
    List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).call();
    
    for (DiffEntry entry : diff) {
        System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
        DiffFormatter formatter = new DiffFormatter(System.out);
        formatter.setRepository(db);
        formatter.format(entry);
    
    }
    

    UPDATE

    This issue was a long time ago. My existing for does display the uncommitted code. The current code that I am using for prepareTreeParser, in the context of displaying the difference, is:

    public void gitDiff() throws Exception {
        Repository db = new FileRepository("/path/to/git" + DEFAULT_GIT);
        Git git = new Git(db);
    
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DiffFormatter formatter = new DiffFormatter( out );
        formatter.setRepository(git.getRepository());
        AbstractTreeIterator commitTreeIterator = prepareTreeParser(git.getRepository(), Constants.HEAD);
        FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );
        List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );
    
        for( DiffEntry entry : diffEntries ) {
            System.out.println("DIFF Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
            formatter.format(entry);
            String diffText = out.toString("UTF-8");
            System.out.println(diffText);
            out.reset();
        }
        git.close();
        db.close();
    
        // This code is untested. It is slighting different for the code I am using in production,
        // but it should be very easy to adapt it for your needs
    }
    private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws Exception {
        Ref head = repository.getRef(ref);
        RevWalk walk = new RevWalk(repository);
        RevCommit commit = walk.parseCommit(head.getObjectId());
        RevTree tree = walk.parseTree(commit.getTree().getId());
    
        CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
        ObjectReader oldReader = repository.newObjectReader();
        try {
            oldTreeParser.reset(oldReader, tree.getId());
        } finally {
            oldReader.release();
        }
        return oldTreeParser;
    }
    

  • git: import changes from non git repository
  • Bash script to extract filenames from git whatchanged
  • Structuring Go subpackages for teams
  • git error: RPC failed; curl 56 GnuTLS
  • Git + a large data set?
  • Can I use GitHub's 2-Factor Authentication with TortoiseGit?
  • One Solution collect form web for “File diff against the last commit with JGit”

    The following setup works for me:

    DiffFormatter formatter = new DiffFormatter( System.out );
    formatter.setRepository( git.getRepository() );
    AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(),  Constants.HEAD );
    FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() );
    List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator );
    
    for( DiffEntry entry : diffEntries ) {
      System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() );
      formatter.format( entry );
    }
    

    The uncommitted changes are made accessible trough the FileTreeIterator. Using formatter.scan() instead of the DiffCommand has the advantage that the formatter is set up properly to handle the FileTreeIterator. Otherwise you will get MissingObjectExceptions as the formatter tries to locate changes from the work tree in the repository.

    Git Baby is a git and github fan, let's start git clone.