当使用“git difftool”时,当其中一个文件是最新版本时,它会将相对路径传递给外部diff应用程序。
~/.gitconfig
[difftool "echo"]
cmd = echo $LOCAL $REMOTE
path =
[diff]
tool = echo
示例命令
git difftool e3d654bc65404b9229abc0251a6793ffbfccdee3 6c6b5fd34196402e4fa0e8cf42f681d9fbd5359f
Viewing: 'app/views/shared/_graph.html.slim'
Launch 'echo' [Y/n]: y
app/views/shared/_graph.html.slim /var/folders/fs/3pgvhwkj2vq4qt3q6n74s5880000gn/T//XGFoyj__graph.html.slim
在本例中
app/views/shared/_graph.html.slim
是传递给外部diff应用程序的相对路径,由于它是相对的,diff应用程序不知道如何打开它。如何使“git difftool”始终导出绝对路径?
最佳答案
将echo命令放在包装器脚本中
$ tail -5 ~/.gitconfig
[difftool "echo"]
cmd = /tmp/test/echo.sh $LOCAL $REMOTE
path =
[diff]
tool = echo
$ cat /tmp/test/echo.sh
#!/bin/sh
LOCAL="$1"
REMOTE="$2"
case "$LOCAL"
in
/*)
L="$LOCAL"
;;
*)
L=`git rev-parse --show-toplevel`/"$LOCAL"
;;
esac
case "$REMOTE"
in
/*)
R="$REMOTE"
;;
*)
R=`git rev-parse --show-toplevel`/"$REMOTE"
;;
esac
echo "$L" "$R"
$
然后它将输出文件的绝对路径:
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: dummy
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git difftool
Viewing: 'a/b/c/dummy'
Hit return to launch 'echo':
/tmp/vsDkDe_dummy /tmp/test/a/b/c/dummy
$
关于git - 如何使git difftool始终导出绝对路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22397845/