Wednesday, April 13, 2022

[SOLVED] How to access commit hash instead of temp file path in git diff tool script

Issue

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?


Solution

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



Answered By - Jaredo Mills
Answer Checked By - Cary Denson (WPSolving Admin)