Search code examples
gitgit-diffrhapsodygit-difftool

Configuring multiple git mergetool and difftool so that appropriate tool will be chosen based on extension


When I do git difftool, recursively it runs default difftool for all files. I wrote a script to choose appropriate difftool based on extension and I call like git difftool -t mytool in the script. difftool redirects all arguments to new call, which is good. However, it somehow redirects all file names as well. So basically, my script becomes useless since custom difftool is passed with all files that is changed.

What I want to do is, limit difftool to one file, or at least call my diftool with one file, not the rest. Basically, this is my script and I have this as the default difftool in .gitconfig.

if is_sbs $1 $2 ; then
    #custom difftool which is also defined in .gitconfig
    git dt -t rhapsody 
else
    #for all files
    git dt -t kdiff3 
fi

Solution

  • Apparently, git inherently pipes all file information and it is not shown in the arguments to the script. Instead of defining other diff tools as "difftool"s in .gitconfig, I've just made a call to the executable file with appropriate arguments. Complete solution is the following:

    .gitconfig

    [difftool]
        prompt = false
    [mergetool]
        prompt = false
    [difftool "selectiveDiff"]
        cmd = difftool.sh $LOCAL $REMOTE $BASE $MERGED
        keepBackup = false
    [mergetool "selectiveMerge"]
        cmd = mergetool.sh $LOCAL $REMOTE $BASE $MERGED
        keepBackup = false
    

    Please keep in mind that scripts should be in PATH. Either create a directory and add it to your PATH or use one that is already in PATH.

    Define variables in somewhere

    RHAPSODY_PATH="C:/Program Files (x86)/IBM/Rational/Rhapsody/8.0.5/DiffMerge.exe"
    KDIFF3_PATH="C:/Program Files/KDiff3/kdiff3.exe"
    

    difftool.sh

    if is_sbs $1 $2 ; then
        "$RHAPSODY_PATH" -base $3 $1 $2 -xcompare 
    else
        "$KDIFF3_PATH" $1 $2
    fi
    

    mergetool.sh

    if is_sbs $1 $2 ; then
        #DiffMerge.exe -base BASE FILE1 FILE2 -out OUTPUT -xmerge
        "$RHAPSODY_PATH" -base $3 $1 $2 -out $4 -xmerge
    else
        #kdiff3 BASE FILE1 FILE2 -o OUTPUT
        "$KDIFF3_PATH" $3 $1 $2 -o $4
    fi
    

    It will choose the right diff and merge tool upon calls.