Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Creating the equivalent of "git diff"

On Mon, Sep 15, 2014 at 1:13 PM, Zach Oakes <zsoakes@xxxxxxxxx> wrote:
I'm just trying to implement "git diff", i.e. a diff between my latest commit and my working tree (if I am using the terms correctly). The only semi-solution I've found is to continue using DirCacheIterator, but ignore any DiffEntry object that have a change type of ADD. This isn't ideal, but it at least avoids seeing entries from ignored directories.

"git diff" is the same as "git diff HEAD". The (a) side of the diff is HEAD, and the (b) side is the working tree. In JGit this means you parse HEAD out of the Repository and the working tree is a FileTreeIterator.

If you've tried that and it's still not properly ignoring .gitignore, that's probably a bug. Share your code and we'll try to reproduce it.
Regarding org.eclipse.jgit.pgm, if that is shelling out to git, I would like to avoid that as I'd like my tool to not require git to be installed.

Every program in org.eclipse.jgit.pgm is a pure JGit reimplementation of a corresponding core git command. (Well, usually a subset-ish.) Those programs have a main() and are designed to be run from the command line with args.

There are also high-level API commands intended to be used from programs in org.eclipse.jgit.api. In particular DiffCommand looks like it will fit your use case. If it doesn't, I suggest using DiffCommand and/or Diff as a reference in your implementation.
On Mon, Sep 15, 2014 at 4:06 PM, Dave Borowitz <dborowitz@xxxxxxxxxx> wrote:
It depends what the arguments are. "git diff origin/master" diffs the working tree against origin/master; so in that case, one argument would be a FileTreeIterator and the other would be an AbstractTreeIterator over origin/master (which has no working tree, it's not checked out).

"git diff foo bar", on the other hand, diffs two non-working-tree arguments.

Tangential question: what's wrong with org.eclipse.jgit.pgm.Diff? Are there features that doesn't support that are better implemented upstream?

On Mon, Sep 15, 2014 at 1:01 PM, Zach Oakes <zsoakes@xxxxxxxxx> wrote:
So I should provide FileTreeIterators to both arguments of the DiffFormatter.scan method? It looks like the diff will always return nothing if I pass the exact same tree for both arguments. Maybe there is a way to set the first one to point of the HEAD of my repo...

On Mon, Sep 15, 2014 at 3:52 PM, Dave Borowitz <dborowitz@xxxxxxxxxx> wrote:
If you have a working directory, use a WorkingTreeIterator. From the WTI javadoc you can see it recommends using FileTreeIterator if you have a local FS directory.

On Mon, Sep 15, 2014 at 12:23 PM, Zach Oakes <zsoakes@xxxxxxxxx> wrote:
I've tried replacing DirCacheIterator with a CanonicalTreeParser set to "HEAD^{tree}", and that actually does obey gitignore. Unfortunately, DiffFormatter's scan method returns every file in the repo, whether they've been modified or not. Perhaps I can try to filter them out.

On Mon, Sep 15, 2014 at 3:16 PM, Zach Oakes <zsoakes@xxxxxxxxx> wrote:
I'm not sure if I have to use DirCacheIterator, but I don't know of any other way to do it. I need to diff the last commit with whatever is in my working directory, as "git diff" does. Is there a workaround? I would imagine someone has done it before.

On Mon, Sep 15, 2014 at 2:50 PM, Dave Borowitz <dborowitz@xxxxxxxxxx> wrote:
If you look at the implementation of DiffFilter#getDiffTreeFilterFor, it looks like you need to pass it a WorkingTreeIterator to get it to use a NotIgnoredFilter.

I suppose if you must use a DirCacheIterator it's a missing feature to read .gitignore from the index.

On Sun, Sep 14, 2014 at 9:50 PM, Zach Oakes <zsoakes@xxxxxxxxx> wrote:
I'm trying to make an exact equivalent to the plain "git diff" command programmatically. The only remaining issue is that it doesn't seem to be respecting the repository's gitignore file. I am getting numerous diffs for files in a build directory, which do not appear when I run it on the command line. I would appreciate help if anyone can provide it.

DirCacheIterator dci = new DirCacheIterator(repo.readDirCache());
FileTreeIterator fti = new FileTreeIterator(repo);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(out);
List<DiffEntry> entries = df.scan(dci, fti);

jgit-dev mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

jgit-dev mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

Back to the top