gitdifftool

how to make git difftool to always export absolute paths


When using 'git difftool' it passes relative path to external diff application when one of the files is the latest version.

~/.gitconfig

[difftool "echo"]
    cmd = echo $LOCAL $REMOTE
    path =
[diff]
    tool = echo

example command

git difftool e3d654bc65404b9229abc0251a6793ffbfccdee3 6c6b5fd34196402e4fa0e8cf42f681d9fbd5359f

Viewing: 'app/views/shared/_graph.html.slim'
Launch 'echo' [Y/n]: y
app/views/shared/_graph.html.slim /var/folders/fs/3pgvhwkj2vq4qt3q6n74s5880000gn/T//XGFoyj__graph.html.slim

In this example app/views/shared/_graph.html.slim is relative path which would be passed to external diff application and since it is relative the diff application doesn't know how to open it.

How can I make 'git difftool' to ALWAYS export absolute paths?


Solution

  • This is solution based on hlovdal and VonC answers which I've ended up using.

    ~/.git_absolute_path.sh

    #!/bin/sh
    
    FILE_PATH="$1"
    
    case "$FILE_PATH"
    in
            /*)
                    NEW_PATH="$FILE_PATH"
                    ;;
            *)
                    NEW_PATH=`git rev-parse --show-toplevel`/"$FILE_PATH"
                    ;;
    esac
    
    echo "$NEW_PATH"
    

    ~/.gitconfig

    [difftool "echo"]
        cmd = echo `~/.git_absolute_path.sh $LOCAL` `~/.git_absolute_path.sh $REMOTE`
        path =
    [mergetool "echo"]
        cmd = echo `~/.git_absolute_path.sh $LOCAL` `~/.git_absolute_path.sh $REMOTE` `~/.git_absolute_path.sh $BASE` `~/.git_absolute_path.sh $MERGED`
        trustExitCode = true
    [diff]
        tool = echo
    

    the difference here is that we are reusing the single script for each path.