1

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
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 1
    A side comment : another way to track "moved lines" is to detect them in the diff. The ["diff-highlight" contrib script](https://github.com/git/git/tree/master/contrib/diff-highlight) is an example : its intended usage is `git diff | diffhighlight` or `git log -p | diffhighlight` – LeGEC Jul 18 '20 at 21:09
  • I couldn't figure out from the code alone how the files are open, try to dump the environment variables to see what you can pick there. – LeGEC Jul 18 '20 at 21:27
  • 2
    Found this when looking for "diff viewer moved blocks of lines" : https://stackoverflow.com/questions/1380333/highlighting-added-deleted-lines-ignoring-moves-in-a-patch-file – LeGEC Jul 18 '20 at 21:44
  • @LeGEC Thanks, I'll take a look. I have my own diff engine written in C# though so the diff by itself was the most trivial piece of it all. – Lasse V. Karlsen Jul 19 '20 at 09:36

2 Answers2

1

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 :)

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • If I run `git difftool -t OnlyMovedLines HEAD~1 HEAD`, and the two commits being compared has changed 2 files, I just get the output from my program, no information about which file the changes come from. I will try dumping the environment variables as you suggested in your comment to my question. – Lasse V. Karlsen Jul 19 '20 at 09:38
  • Alas, no variable names that matter, there are some `GIT_*` variables present, and one of them changes, named `GIT_DIFF_PATH_COUNTER`, but it goes from 1 to 2, so it doesn't really tell me anything useful. – Lasse V. Karlsen Jul 19 '20 at 09:43
1

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825