views:

1032

answers:

1

Is there a way to use Winmerge inside of Git to do Diffs?

Thanks

+14  A: 

(msysgit, 1.6.5, DOS session)

The first part (using winmerge) is described in "How do I view ‘git diff’ output with visual diff program?"

C:\myGitRepo>git config --replace --global diff.tool winmerge
C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --replace --global difftool.prompt false

With winmerge.sh stored in a directory part of your PATH:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$1" "$2"

(see WinMerge Command-line options)
Note: the above works in a DOS session, not a PowerShell session.
"Program Files (x86)" has an (x86) because I am using a Windows (7) 64bits

git difftool

will now launch WinMerge.
If you want git diff to launch WinMerge, just set:

set GIT_EXTERNAL_DIFF=winmerge.sh


But the real added value comes from the ability to use that same diff tool to present all differences in one batch instead of presenting them sequentially, forcing you to close the diff tool windows one file at a time.

As Seba Illingworth mentions in his answer, a script git-diffall.sh (also put in the path) can do just that:

#!/bin/sh
git diff --name-only "$@" | while read filename; do
    git difftool "$@" --no-prompt "$filename" &
done

But that only works by opening n windows for n files (if you try to use the -s option of WinMerge, it will not work because of the temp files being deleted by the difftool too early)


That is why I like the approach of GitDiff.bat - power-diffing with GI, which allows you to review the list of files with a difference, before selecting one to examine its internal differences.
I have tweaked it to use only DOS commands

@echo off

setlocal

if "%1" == "-?" (
    echo GitDiff - enables diffing of file lists, instead of having to serially
    echo diff files without being able to go back to a previous file.
    echo Command-line options are passed through to git diff.
    echo If GIT_FOLDER_DIFF is set, it is used to diff the file lists. Default is windff.
    goto END
)

if "%GIT_DIFF_COPY_FILES%"=="" (
    rd /s /q %TEMP%\GitDiff
    mkdir %TEMP%\GitDiff
    mkdir %TEMP%\GitDiff\old
    mkdir %TEMP%\GitDiff\new

    REM This batch file will be called by git diff. This env var indicates whether it is
    REM being called directly, or inside git diff
    set GIT_DIFF_COPY_FILES=1

    set GIT_DIFF_OLD_FILES=%TEMP%\GitDiff\old
    set GIT_DIFF_NEW_FILES=%TEMP%\GitDiff\new

    set GIT_EXTERNAL_DIFF=%~dp0\GitDiff.bat
    echo Please wait and press q when you see "(END)" printed in reverse color...
    call git diff %*

    if defined GIT_FOLDER_DIFF (
        REM This command using GIT_FOLDER_DIFF just does not work for some reason.
        %GIT_FOLDER_DIFF% %TEMP%\GitDiff\old %TEMP%\GitDiff\new
        goto END
    )

    if exist "%ProgramFiles%\Beyond Compare 2\BC2.exe" (
        set GIT_FOLDER_DIFF="%ProgramFiles%\Beyond Compare 2\BC2.exe"
        "%ProgramFiles%\Beyond Compare 2\BC2.exe" %TEMP%\GitDiff\old %TEMP%\GitDiff\new
        goto END
    )

    "%ProgramFiles(x86)%\WinMerge\WinMergeU.exe" -r -e -dl "Base" -dr "Mine"  %TEMP%\GitDiff\old %TEMP%\GitDiff\new
    goto END
)

REM diff is called by git with 7 parameters:
REM     path old-file old-hex old-mode new-file new-hex new-mode
copy %TEMP%\%~nx2 %GIT_DIFF_OLD_FILES%\%1
copy %5 %GIT_DIFF_NEW_FILES%

:END

It is not robust enough to handle files with same names in different directories, but it gives you a general idea of what is possible:
Here only one WinMerge will open up, with the list of files having internal differences. You can click on the ones you want to examines, then a simple ESC will close the all WinMerge-diff session.

VonC
Trying this with git version 1.6.5.1.1367.gcd48 and it doesn't seem to generate any files in those temp folders. Any ideas where to look for the problem? Thanks!
Art
@Art: in those debug cases, I try to isolate the problem by simplifying the script: 1/ if the script called with the 7 parameter (first pass of git diff, to copy the files) 2/ is the script at least called back at the end of the `git diff`?
VonC