gitconfig shell-命令别名从顶级而不是当前目录运行

gitconfig shell-command aliasing runs from top-level rather than current directory

我编写了一个名为 git_diff_parent 的 bash 脚本,当传递一个提交句柄时,它会执行 git diff commit~1 commit;换句话说,它无需在命令行上指定 commit~1 即可与其祖先进行区分。我通过解析 git 命令行,找到哪个参数是提交字符串(使用 git cat-file -t),将其替换为 commit~1 commit 然后将新参数列表传递给 git diff 来做到这一点.我在 /etc/gitconfig 中为这个脚本添加了别名,所以我可以将这个命令执行为 git diff-parent:

[alias]
   diff-parent = !git_diff_parent

这个 almost 工作,除非我试图从顶级存储库目录以外的地方区分单个文件,例如:

% pwd
/home/myhome/github/my-branch/rtl
% git diff-parent branch -- mycode.sv
<no output>
% git diff-parent branch -- rtl/mycode.sv
<diff output appears>

为了调试它,我在我的脚本中添加了 pwd 以查看脚本的工作目录。你瞧,它总是从顶级目录 (/home/myhome/github/my-branch) 而不是我所在的子目录启动。我什至尝试 git -C . diff-parent ... 无济于事。

那么有没有办法让我的别名从当前工作目录启动?

https://git-scm.com/docs/git-config#Documentation/git-config.txt-alias

Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory. GIT_PREFIX is set … from the original current directory.

(强调和剪切是我的 — phd

因此您可以在 git_diff_parent shell 脚本中执行 cd $GIT_PREFIX