threads / discuss / 16866

git-difftool

Subject: git-difftool

## tl;dr

16 messages between Dec 26, 2008 and Jan 17, 2009.

replies: 15people: 6as markdown or json

David Aguilar· Dec 26, 2008, 01:30 UTC · lore

Several co-workers have found this script useful. It's basically git-mergetool modified to operate on the git index/worktree. There's some things about the script that aren't very gitish (for example, the way revargs are processed) but it's useful nonetheless.

The usual use case for this script is when you have either staged or unstaged changes and you'd like to see the changes in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).

	git difftool [<filename>*]

Another use case is when you'd like to see the same information but are comparing arbitrary commits (this is the part where the revarg parsing could be better)

	git difftool --start=HEAD^ --end=HEAD [-- <filename>*]

The last use case is when you'd like to compare your current worktree to something other than HEAD (e.g. a tag)

	git difftool --commit=v1.0.0 [-- <filename>*]

It's a generally useful script so I thought I'd share it here. If others find it useful then maybe we can fix it up and ship it along with the other useful goodies in contrib/.

enjoy,
-- 
	David
Matthieu Moy· Dec 31, 2008, 16:04 UTC · re: David Aguilar · lore

Re: git-difftool

David Aguilar <davvid@gmail.com> writes:
Show 5 quoted lines
> The usual use case for this script is when you have either
> staged or unstaged changes and you'd like to see the changes
> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
>
> 	git difftool [<filename>*]
Is it not a complex way of saying
        GIT_EXTERNAL_DIFF=xxdiff git diff
?
(My 2 cents, and happy new year ;-)
-- 
Matthieu
David Aguilar· Dec 31, 2008, 20:11 UTC · re: Matthieu Moy · lore

Re: git-difftool

On Wed, Dec 31, 2008 at 8:04 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 19 quoted lines
> David Aguilar <davvid@gmail.com> writes:
>
>> The usual use case for this script is when you have either
>> staged or unstaged changes and you'd like to see the changes
>> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
>>
>>       git difftool [<filename>*]
>
> Is it not a complex way of saying
>
>        GIT_EXTERNAL_DIFF=xxdiff git diff
>
> ?
>
> (My 2 cents, and happy new year ;-)
>
> --
> Matthieu
>

Hmm... in theory, yes, but in practice, no. xxdiff is too gimp to handle what 'git diff' hands it =)

For example:

$ GIT_EXTERNAL_DIFF=echo git diff test test /tmp/.diff_1dh4TW 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 100644 test 0000

$ GIT_EXTERNAL_DIFF=xxdiff git diff test xxdiff (cmdline.cpp:762): You can specify at most 3 filenames. Extra arguments: " 100644 test 0000000000000000000000000000000000000000 100644" Use 'xxdiff --help' for more information. external diff died, stopping at test.

I checked the git-diff documentation and could not find a way to inhibit the sha1, mode, etc. args that are sent to GIT_EXTERNAL_DIFF (nor should there be, I presume).

I'm all for finding the simplest way and GIT_EXTERNAL_DIFF seems like the right entry point. What I should do is change the script so that it handles all of the choosing-a-merge-tool-logic and just have git call it via GIT_EXTERNAL_DIFF. That would definitely simplify the script since 'git diff' would be handling all of the tmp file and option processing logic. wow, I really like that idea. Thanks for the tip!

Happy new year,
-- 
    David
Boyd Stephen Smith Jr.· Dec 31, 2008, 21:20 UTC · re: David Aguilar · lore

Re: git-difftool

On Wednesday 2008 December 31 14:11:07 you wrote:
Show 16 quoted lines
> On Wed, Dec 31, 2008 at 8:04 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
> > David Aguilar <davvid@gmail.com> writes:
> >> The usual use case for this script is when you have either
> >> staged or unstaged changes and you'd like to see the changes
> >> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
> >>
> >>       git difftool [<filename>*]
> >
> > Is it not a complex way of saying
> >
> >        GIT_EXTERNAL_DIFF=xxdiff git diff
> >
> > ?
>
> Hmm... in theory, yes, but in practice, no.
> xxdiff is too gimp to handle what 'git diff' hands it =)

What about something like: git diff | xxdiff -

I normally use: git diff | kompare -

and it works fine.
-- 
Boyd Stephen Smith Jr.                     ,= ,-_-. =. 
bss@iguanasuicide.net                     ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy           `-'(. .)`-' 
http://iguanasuicide.net/                      \_/     
Matthieu Moy· Jan 1, 2009, 17:58 UTC · re: David Aguilar · lore

Re: git-difftool

"David Aguilar" <davvid@gmail.com> writes:
> Hmm... in theory, yes, but in practice, no.
> xxdiff is too gimp to handle what 'git diff' hands it =)

As done with "vimdiff" in another message, simply write a one-liner wrapper script that calls xxdiff $2 $3, and call this wrapper script.

-- 
Matthieu
Markus Heidelberg· Jan 2, 2009, 00:13 UTC · re: Matthieu Moy · lore

Re: git-difftool

Matthieu Moy, 01.01.2009:
Show 7 quoted lines
> "David Aguilar" <davvid@gmail.com> writes:
> 
> > Hmm... in theory, yes, but in practice, no.
> > xxdiff is too gimp to handle what 'git diff' hands it =)
> 
> As done with "vimdiff" in another message, simply write a one-liner
> wrapper script that calls xxdiff $2 $3, and call this wrapper script.
This works with GUI tools, but not with console tools. GVim works, Vim
doesn't.
And invoking
    git difftool
is by far more convenient than
    GIT_EXTERNAL_DIFF=vimdiff git diff
Markus
Matthieu Moy· Jan 2, 2009, 16:10 UTC · re: Markus Heidelberg · lore

Re: git-difftool

Markus Heidelberg <markus.heidelberg@web.de> writes:
Show 10 quoted lines
> Matthieu Moy, 01.01.2009:
>> "David Aguilar" <davvid@gmail.com> writes:
>> 
>> > Hmm... in theory, yes, but in practice, no.
>> > xxdiff is too gimp to handle what 'git diff' hands it =)
>> 
>> As done with "vimdiff" in another message, simply write a one-liner
>> wrapper script that calls xxdiff $2 $3, and call this wrapper script.
>
> This works with GUI tools, but not with console tools.
Actually, it does if you call git --no-pager.
Show 6 quoted lines
> GVim works, Vim
> doesn't.
> And invoking
>     git difftool
> is by far more convenient than
>     GIT_EXTERNAL_DIFF=vimdiff git diff

Right, but a script "git-difftool" calling the later is a one-liner, so 2 one-liners give you the same result as the ~500 lines script proposed. And GIT_EXTERNAL_DIFF has the great advantage of being maintained together with git, and will most likely handle all cases (diff between index, working tree, arbitrary commit, ...) correctly.

-- 
Matthieu
David Aguilar· Jan 2, 2009, 17:39 UTC · re: Matthieu Moy · lore

Re: git-difftool

On Fri, Jan 2, 2009 at 8:10 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 17 quoted lines
>
> As done with "vimdiff" in another message, simply write a
> one-liner wrapper script that calls xxdiff $2 $3, and call this
> wrapper script.
>
> ...
>
> Right, but a script "git-difftool" calling the later is a one-liner,
> so 2 one-liners give you the same result as the ~500 lines script
> proposed. And GIT_EXTERNAL_DIFF has the great advantage
> of being maintained together with git, and will most likely handle
> all cases (diff between index, working tree, arbitrary commit, ...)
> correctly.
>
> --
> Matthieu
>
Thanks for the feedback.  I've done exactly what you suggested.

I now have a git-difftool wrapper script that basically just sets up the environment for git-difftool-helper. git-difftool-helper does all of the merge tool configuration stuff ala http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html (it uses the same git config variables and thus works with existing custom commands). If you drop them both into the same directory it should work as-is (it munges $PATH).

It's not a two-liner (they do all that git config stuff and handle more than just vimdiff) but it does use GIT_EXTERNAL_DIFF now, which makes the script infinitely more useful. This is much nicer now since you can pass any 'git diff' options to git-difftool and it'll handle it correctly.

The usage is simpler now too:
usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
Thanks for your help,
-- 
    David
Markus Heidelberg· Jan 15, 2009, 22:26 UTC · re: David Aguilar · lore

Re: git-difftool

David Aguilar, 02.01.2009:
Show 17 quoted lines
> I now have a git-difftool wrapper script that basically just sets up
> the environment for git-difftool-helper.  git-difftool-helper does all
> of the merge tool configuration stuff ala
> http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html (it
> uses the same git config variables and thus works with existing custom
> commands).  If you drop them both into the same directory it should
> work as-is (it munges $PATH).
> 
> It's not a two-liner (they do all that git config stuff and handle
> more than just vimdiff) but it does use GIT_EXTERNAL_DIFF now, which
> makes the script infinitely more useful.  This is much nicer now since
> you can pass any 'git diff' options to git-difftool and it'll handle
> it correctly.
> 
> The usage is simpler now too:
> 
> usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
Now isn't this something for contrib, David?
Markus
David Aguilar· Jan 16, 2009, 05:05 UTC · re: Markus Heidelberg · lore

Re: git-difftool

On Thu, Jan 15, 2009 at 2:26 PM, Markus Heidelberg <markus.heidelberg@web.de> wrote:

Show 24 quoted lines
> David Aguilar, 02.01.2009:
>> I now have a git-difftool wrapper script that basically just sets up
>> the environment for git-difftool-helper.  git-difftool-helper does all
>> of the merge tool configuration stuff ala
>> http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html (it
>> uses the same git config variables and thus works with existing custom
>> commands).  If you drop them both into the same directory it should
>> work as-is (it munges $PATH).
>>
>> It's not a two-liner (they do all that git config stuff and handle
>> more than just vimdiff) but it does use GIT_EXTERNAL_DIFF now, which
>> makes the script infinitely more useful.  This is much nicer now since
>> you can pass any 'git diff' options to git-difftool and it'll handle
>> it correctly.
>>
>> The usage is simpler now too:
>>
>> usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
>
> Now isn't this something for contrib, David?
>
> Markus
>
>
It sure is.
BTW the script worked beautifully on *nix.

I later decided to give it a try on Windows and of course it didn't work right the first time. Once I added a check to use "git.exe" instead of "git" then it started working again. I'll prepare some documentation and post a patch later tonight.

Thanks for reminding me,
-- 
    David


(p.s. Markus: sorry for the double-email -- I forgot to cc: the list)
Ping Yin· Jan 1, 2009, 07:38 UTC · re: Matthieu Moy · lore

Re: git-difftool

On Thu, Jan 1, 2009 at 12:04 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 11 quoted lines
> David Aguilar <davvid@gmail.com> writes:
>
>> The usual use case for this script is when you have either
>> staged or unstaged changes and you'd like to see the changes
>> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
>>
>>       git difftool [<filename>*]
>
> Is it not a complex way of saying
>
>        GIT_EXTERNAL_DIFF=xxdiff git diff

$ cat mydiff #!/bin/bash exec vimdiff $2 $5

then i run
$ GIT_EXTERNAL_DIFF=mydiff  git diff
but it gives me the error
Vim: Warning: Output is not to a terminal

$ ps -ef | grep vimdiff ping 20284 20283 1 15:37 pts/13 00:00:00 vimdiff /tmp/.diff_XXXQK6 index.php

Markus Heidelberg· Jan 2, 2009, 00:04 UTC · re: Ping Yin · lore

Re: git-difftool

Ping Yin, 01.01.2009:
Show 23 quoted lines
> On Thu, Jan 1, 2009 at 12:04 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
> > David Aguilar <davvid@gmail.com> writes:
> >
> >> The usual use case for this script is when you have either
> >> staged or unstaged changes and you'd like to see the changes
> >> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
> >>
> >>       git difftool [<filename>*]
> >
> > Is it not a complex way of saying
> >
> >        GIT_EXTERNAL_DIFF=xxdiff git diff
> 
> $ cat mydiff
> #!/bin/bash
> exec vimdiff $2 $5
> 
> then i run
> 
> $ GIT_EXTERNAL_DIFF=mydiff  git diff
> 
> but it gives me the error
> Vim: Warning: Output is not to a terminal

Just the warning and everything else works? For me the display is totally screwed up and the commands don't really work because of the pager. I have to add GIT_PAGER="" to get it working.

Markus
Ping Yin· Jan 2, 2009, 01:59 UTC · re: Markus Heidelberg · lore

Re: git-difftool

On Fri, Jan 2, 2009 at 8:04 AM, Markus Heidelberg <markus.heidelberg@web.de> wrote:

Show 29 quoted lines
> Ping Yin, 01.01.2009:
>> On Thu, Jan 1, 2009 at 12:04 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
>> > David Aguilar <davvid@gmail.com> writes:
>> >
>> >> The usual use case for this script is when you have either
>> >> staged or unstaged changes and you'd like to see the changes
>> >> in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).
>> >>
>> >>       git difftool [<filename>*]
>> >
>> > Is it not a complex way of saying
>> >
>> >        GIT_EXTERNAL_DIFF=xxdiff git diff
>>
>> $ cat mydiff
>> #!/bin/bash
>> exec vimdiff $2 $5
>>
>> then i run
>>
>> $ GIT_EXTERNAL_DIFF=mydiff  git diff
>>
>> but it gives me the error
>> Vim: Warning: Output is not to a terminal
>
> Just the warning and everything else works? For me the display is
> totally screwed up and the commands don't really work because of the
> pager. I have to add GIT_PAGER="" to get it working.
>
Thanks. With GIT_PAGER="" it works now.
enso· Jan 17, 2009, 03:47 UTC · re: Ping Yin · lore

Re: git-difftool

Ping Yin wrote:
> 
> Thanks. With GIT_PAGER="" it works now.
> 

Personally, I didn't like having to screw around with environment variables directly to get vimdiff to work. So I did the following:

1) Made a simple diff script gitvimdiff.sh:

#!/bin/env sh vimdiff "$2" "$5"

2) git config --global diff.external ~/path/to/gitvimdiff.sh
3) git config --global pager.diff ""

Now when I run git-diff it uses vimdiff (hooray) and automatically turns the pager off for me! Now only if I could get meld to work under cygwin.

-- 
View this message in context: http://n2.nabble.com/git-difftool-tp1814817p2171934.html
Sent from the git mailing list archive at Nabble.com.
David Aguilar· Jan 17, 2009, 04:41 UTC · re: enso · lore

Re: git-difftool

On Fri, Jan 16, 2009 at 7:47 PM, enso <a.calhoon@gmail.com> wrote:
Show 19 quoted lines
> Ping Yin wrote:
>>
>> Thanks. With GIT_PAGER="" it works now.
>>
>
> Personally, I didn't like having to screw around with environment variables
> directly to get vimdiff to work. So I did the following:
>
> 1) Made a simple diff script gitvimdiff.sh:
>
> #!/bin/env sh
> vimdiff "$2" "$5"
>
> 2) git config --global diff.external ~/path/to/gitvimdiff.sh
> 3) git config --global pager.diff ""
>
> Now when I run git-diff it uses vimdiff (hooray) and automatically turns the
> pager off for me! Now only if I could get meld to work under cygwin.
> --

Hello sir It sounds like you were looking for exactly the patch I sent last night =)

http://article.gmane.org/gmane.comp.version-control.git/105925
With 'git difftool' you just do it once:

git config --global merge.tool vimdiff git difftool [diff options]

The nice thing is that it supports the same configuration variables as 'git mergetool', so if you've already set things up to work correctly with mergetool then you're already setup for difftool.

Meld.. I can't say I've used it on windows. I have successfully used kdiff3 with msysgit, though. I just downloaded it and copied it to /bin.

difftool supports meld, though, so if you get a build of meld that works then you should in theory be able to use it just by setting merge.tool to meld.

-- 
    David
Ping Yin· Jan 17, 2009, 14:17 UTC · re: enso · lore

Re: git-difftool

On Sat, Jan 17, 2009 at 11:47 AM, enso <a.calhoon@gmail.com> wrote:
Show 19 quoted lines
>
>
>
> Ping Yin wrote:
>>
>> Thanks. With GIT_PAGER="" it works now.
>>
>
> Personally, I didn't like having to screw around with environment variables
> directly to get vimdiff to work. So I did the following:
>
> 1) Made a simple diff script gitvimdiff.sh:
>
> #!/bin/env sh
> vimdiff "$2" "$5"
>
> 2) git config --global diff.external ~/path/to/gitvimdiff.sh
> 3) git config --global pager.diff ""
>
I am not aware of these configurations, thanks.
What i current do is to use an alias to use vimdiff for diff

← back to recent threads