0

I have the following configuration for git difftool:

[diff]
    tool = any
[difftool]
    prompt = false
[difftool "any"]
    cmd = /maxkoretskyi/test/my.sh "$LOCAL" "$REMOTE"

inside cmd I have access to several variables like $LOCAL and $REMOTE that I pass to my.sh. Those are temp file paths and my script outputs them simply like this echo "$1" "$2":

$ git difftool git difftool 821d1b06 73a14711
Temp/NviQKc_f1.txt 
Temp/exkQKc_f1.txt

Is there any way to access the commit hashes in my.sh that a user passes to `git difftool command?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

2

difftool is not what you want; this program operates on file contents, and has no access to metadata. Rather, look into the GIT_EXTERNAL_DIFF environment variable

Quoting from the manpage:

GIT_EXTERNAL_DIFF

When the environment variable GIT_EXTERNAL_DIFF is set, the program named by it is called to generate diffs, and Git does not use its builtin diff machinery. For a path that is added, removed, or modified, GIT_EXTERNAL_DIFF is called with 7 parameters:

path old-file old-hex old-mode new-file new-hex new-mode

The hex values on the GIT_EXERNAL_DIFF command line are blob object id's, not commit id's; See the related question Which commit has this blob? (eg. git log --find-object=<blob>)

NOTE the same blob id can occur in multiple commits, so it's not possible to uniquely identify which of the commits was referred to on the "git diff <commit> <commit>" command line

Jaredo Mills
  • 159
  • 1
  • 8
  • thanks, so there's no way to get this info using `difftool`? – Max Koretskyi Mar 05 '22 at 14:15
  • 1
    I can't say **there is no way**. If you had to make the SHA1's available to the mergetool, you can modify the script `libexec/git-core/git-difftool--helper` to pass in extra arguments, define environment variables, etc. There are ways, although they are hacky and are likely to cause you headaches with future versions of git. Sticking to documented features would minimize such headaches. It's also possible to maintain your own fork of git which functions to your specifications. I think most users would avoid these ways. – Jaredo Mills Mar 05 '22 at 14:56
  • Maybe if you describe what you're trying to do, there may be a better solution. – Jaredo Mills Mar 05 '22 at 15:05
  • just tested, it outputs hash of the file, not the commit id that I pass, `$ GIT_EXTERNAL_DIFF="./my.sh" git diff 821d1 73a14` – Max Koretskyi Mar 05 '22 at 15:36
  • The hashes on the `GIT_EXTERNAL_DIFF` command line are blob object id's. You can search the repo for commits which contain these blobs (there may be more than one commit). See [Which commit has this blob?](https://stackoverflow.com/questions/223678/which-commit-has-this-blob) – Jaredo Mills Mar 05 '22 at 17:19
  • got it, worked for me, thanks! – Max Koretskyi Mar 05 '22 at 18:08