gitconfig别名使用!source在zsh中无法运行。

5

我有一个像这样的gitconfig:

[alias]
l = "!source ~/.githelpers && pretty_git_log"

当我运行它时,会得到这个结果:
[desktop] git l
source ~/.githelpers && pretty_git_log: 1: source: not found
error: cannot run source ~/.githelpers && pretty_git_log: No such file or directory
fatal: While expanding alias 'l': 'source ~/.githelpers && pretty_git_log': No such file or directory

当我添加任何其他shell内建命令进行测试时,它们都可以正常运行:
[alias]
l = "!echo running from the builtin"

[desktop] git l
running from the builtin

你有没有想过为什么在git中找不到source命令?我正在使用zsh,但是改成bash好像没有任何区别:

[desktop] bash
[desktop] git l
source ~/.githelpers && pretty_git_log: 1: source: not found
error: cannot run source ~/.githelpers && pretty_git_log: No such file or directory
fatal: While expanding alias 'l': 'source ~/.githelpers && pretty_git_log': No such file or directory

抱歉问题有点傻,但为什么需要在前面加上“source”? - CharlesB
这实际上是Gary Bernhardt的gitconfig中的一个示例,以及他的.githelpers文件中有一个名为pretty_git_log()的函数。我假设源调用将使pretty_git_log函数在命令行上作为命令可用,但不完全确定。 - user1244166
1个回答

6
失败是因为!<command>结构试图找到一个程序运行。有一个/bin/echo程序(与您的shell内置的echo不同,但这是另一回事),但没有/bin/source(或/usr/bin或任何其他地方)。由于source的本质,它不能成为一个独立的程序。
请尝试使用以下命令替换:
[alias]
l = "!sh -c 'source ~/.githelpers && pretty_git_log'"

根据需要将sh更改为bash或其他内容。


谢谢,这个方法可行。我之前也试过其他没有二进制版本的内置函数(至少我认为是这样),它们也能用,但无论如何,你的例子是有效的!谢谢。 - user1244166
1
l = "!bash -c 'source ~/.githelpers && pretty_git_log'" 对我有用,谢谢! - user2167582

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接