When writing a git difftool, is there a way for the tool to know which file is being compared?

203 Views Asked by At

I tried writing a simple difftool for this question - Verify that Git commit only moves lines - and it works ... fine I guess, but there is one problem.

When asking it to do a diff between two commits, like this:

git difftool -t OnlyMovedLines HEAD~1 HEAD

Then for each modified file, two temporary files gets created with the snapshots from each of the commits, and then the tool is invoked.

This, however, means that the tool is unable to report which file is actually being diffed, as the filenames could be things like this:

C:\Users\lasse\AppData\Local\Temp/Ol8Kc9_a.txt
C:\Users\lasse\AppData\Local\Temp/WtSX78_a.txt

So my question is this, is there any way for me to add more to the configuration in the .gitconfig file, or are there things like environment variables or whatnot, that can be used to tell the tool which file is actually being compared?

The .gitconfig file looks like this for this particular tool:

[difftool "OnlyMovedLines"]
    cmd = 'D:\\path\\to\\netcoreapp3.1\\OnlyLinesMovedDiffTool.exe' $LOCAL $REMOTE
2

There are 2 best solutions below

2
On

One note about the filenames :

  • if you run git difftool alone : the original filename is presented on the terminal before each invocation of the tool (it even comes with a confirm message if you don't use -y), so you can consider the end user already has the information ;
  • if you run git difftool -d : the tool will be faced with two directories, and each file to be compared will have it full relative path, so you can use that.

Perhaps the answer is you don't have to bother after all :)

0
On

The answer was quite simple.

The .gitconfig configuration was missing a third parameter to my tool:

[difftool "OnlyMovedLines"]
    cmd = 'D:\\path\\to\\netcoreapp3.1\\OnlyLinesMovedDiffTool.exe' $LOCAL $REMOTE $BASE
                                                                                   ^^^^^

After adding that, the name of the file being compared was passed to my program.