gpt4 book ai didi

git - 如何使 git difftool 始终导出绝对路径

转载 作者:太空狗 更新时间:2023-10-29 14:28:16 24 4
gpt4 key购买 nike

当使用“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com