gpt4 book ai didi

git - 获取运行 git alias 的路径

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

当通过 Git 别名运行外部命令时,它从存储库的 rood 目录(.git/通常位于该目录)运行。可以通过以下方式检查:

$ cd /some/path/inside/repo
$ echo $(git -c alias.root='!pwd' root)

现在,有没有办法获取运行别名的确切路径?`


在 XY 问题的情况下,这就是我正在做的事情:我想要一个启动 shell 函数的别名,它依次运行几个 Git 命令,比如 ls-files。它应该只在调用它的路径中工作,所以它应该知道它。

如果我只运行该函数,它会很好地工作,因为它不会更改路径并准确地在调用它的地方运行。但我想要更多的本地行为。我现在拥有的是

git-add-specificname -opt -opt2 --long-option -- path1 path2

我想要的是

git add-specificname -opt -opt2 --long-option -- path1 path2

Git 中不允许使用别名参数,因此 git add specificname... 没有机会

最佳答案

您需要添加 cd ${GIT_PREFIX:-.}在你的别名中。

( Git 1.7.6+ :

Processes spawned by "[alias] <name> = !process" in the configuration can inspect GIT_PREFIX environment variable to learn where in the working tree the original command was invoked.

git config :

'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix' from the original current directory.

)

如果GIT_PREFIX未定义(因为您正在从 repo 的根文件夹执行别名),它将被替换为 ' . '.

您可以看到 t/t1020-subdirectory.sh 中使用的变量

test_expect_success 'GIT_PREFIX for !alias' '
printf "dir/" >expect &&
(
git config alias.test-alias-directory "!sh -c \"printf \$GIT_PREFIX\"" &&
cd dir &&
git test-alias-directory >../actual
) &&
test_cmp expect actual
'

关于git - 获取运行 git alias 的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722119/

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