如何在 git diff 工具脚本中访问提交哈希而不是临时文件路径

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

我对 git difftool 进行了以下配置:

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

cmd 中,我可以访问传递给 my.sh 的几个变量,例如 $LOCAL$REMOTE。这些是临时文件路径,我的脚本简单地输出它们 echo "" "":

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

有什么方法可以访问 my.sh 中用户传递给 `git difftool 命令的提交哈希值?

difftool不是你想要的;该程序对文件内容进行操作,无法访问元数据。相反,查看 GIT_EXTERNAL_DIFF 环境变量

引自联机帮助页:

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

GIT_EXERNAL_DIFF 命令行上的十六进制值是 blob 对象 ID,而不是提交 ID;请参阅相关问题 Which commit has this blob?(例如 git log --find-object=<blob>

注意 相同的 blob id 可以出现在多个提交中,因此无法唯一标识在“git diff <commit> <commit>”命令中引用了哪个提交行