How to use kdiff3 as a difftool / mergetool with cygwin git

First, install kdiff3 for windows.

Second, create the following script somewhere (such as ~/kdiff3.sh, and change the location of your kdiff3.exe to an appropriate cygwin-style path to where you executable really is). The script is based on Pete Goodliffe’s one he used for svn. I’ve expanded on the idea to make it more flexible. See my next post for a more general version that can be used for any windows program

#!/bin/sh
RESULT=""
for arg
do
if [[ "" != "$arg" ]] && [[ -e $arg ]];
then
OUT=`cygpath -wa $arg`
else
OUT=$arg
if [[ $arg == -* ]];
then
OUT=$arg
else
OUT="'$arg'"
fi
fi
RESULT=$RESULT" "$OUT
done
/cygdrive/d/Program\ Files\ \(x86\)/KDiff3/kdiff3.exe $RESULT

Finally, configure your ~/.gitconfig to:

[diff]
tool = kdiff3
[merge]
tool = kdiff3
[mergetool "kdiff3"]
path = ~/kdiff3.sh
keepBackup = false
trustExitCode = false

No thanks to wordpress for ruining the whitespace formatting… :(

Try running git difftool (make sure you have some modified files) to make sure it works.

About these ads

3 thoughts on “How to use kdiff3 as a difftool / mergetool with cygwin git

  1. If your path contains spaces, this script doesn’t work
    Add single quotes around it and it’s all better :)
    OUT=\’`cygpath -wa $arg`\’

    Otherwise, thanks for the help!

  2. Struggled with this for some time. Didn’t work at all. Finally realized that the kdiff3.sh must be saved in UNIX format. Thanks for the very general solution!
    in the .gitconfig file for me the path to kdiff3.exe needed to be absolute and not begin with ~.
    Also, just for general information, the specific solution when doing “git difftool” is this:

    /cygdrive/d/mypgms/KDiff3/kdiff3.exe “$1″ “$2″ “$3″ “$4″ “$5″ `cygpath -wa $6` `cygpath -wa $7`
    A kdiff3.sh containing just this line works too for “git difftool”.

    This expands to:
    /cygdrive/d/mypgms/KDiff3/kdiff3.exe –auto –L1 “alias1 (A)” –L2 “alias2 (B)” win_abs_path_to_A win_abs_path_to_B

    Thanks again,
    -gene

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s