2

For a long time, I had the following alias in my aliases file:

ignore=!([ ! -e .gitignore ] && touch .gitignore) | echo $1 >>.gitignore

It wasn't original to me, and if you search for it, you'll see it a lot of places. Recently, however, I've started having a weird problem with the alias. Anything I ignore gets placed twice in the .gitignore file and on the same line (with a space only).

I did a bit of debugging and found that what is really happening is that the call to echo $1 is echoing $1 as you'd expect, but also echoing the entire string of arguments to the alias.

To test this, I made a new alias:

eo = !echo $1

> git eo test
test test    

> git eo test0 test1
test0 test0 test1

That last line is the most interesting because it clearly shows that the echo call is getting the entire set of arguments tacked on to it while $1 is getting evaluated correctly. In fact, if I mess with things and change $1 to $9 (and don't fill $9), I get:

> git eo test0 test1
test0 test1

I have confirmed that this happens on Git versions 1.8.5 to 1.9.0 and I have confirmed that it DOES NOT happen in Git version 1.7.1; unfortunately, I am unable to test between 1.7.1 and 1.8.5.

Does anyone have any insight to what is going on here? It breaks a few of my aliases...

4

1 回答 1

3

我认为扩展不是您所期望的。让我们从您的别名开始:

eo = !echo $1

我相信git eo test正在扩展到:

echo $1 test

然后扩展为:

echo test test

git 查看别名的典型方式是说当我说“eo”时,我希望你运行“echo $1”并将所有参数传递给该命令。这使您可以执行以下操作

[alias]
k = !gitk

并且仍然能够将参数传递给git k. 如果你想让它表现不同,你应该通过这样的 shell 调用来传递它:

[alias]
eo = !sh -c 'echo $1' -

然后你会得到预期的结果。

我认为对于您的忽略别名,您想要类似的东西

[alias]
ignore = !sh -c 'echo "$1" >> .gitignore' -

注意:您不需要触摸文件>>即可工作,并且在您的原始别名中,您可能打算使用;而不是将触摸的输出通过管道传输到 echo 中。

于 2014-02-19T15:10:56.623 回答