为什么将命令作为git alias提供不同的结果? [英] Why does running command as git alias gives different results?

查看:113
本文介绍了为什么将命令作为git alias提供不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下1行代码,用于查看谁可能是一个很好的候选人,以帮助您完成一段代码:

I have the following 1-liner that I use to see who might be a good candidate for help with a peice of code:

git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr

按提交顺序列出了作者,这很粗糙,但它工作正常。

which lists authors in order of commits, it's crude but it works OK.

然而,当我将它添加到我的git配置中时,像这样:

When I add it to my git config however, like this:

[alias]
    guru=!git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr

运行

running

git guru

给出不同的结果来从命令行运行它。 / p>

Gives different results to running it from the command line.

stuart@beavis(rp):~/git/apps$ git log --pretty=short . | grep ^Auth | sort | uniq -c | sort -nr
710 Author: dave <dave@b2368a2b-315f-46b9-a0b0-05934f827f41>
415 Author: pete <pete@b2368a2b-315f-46b9-a0b0-05934f827f41>
402 Author: craig <craig@b2368a2b-315f-46b9-a0b0-05934f827f41>

相比于:

Compared to:

stuart@beavis(rp):~/git/apps$ git guru
859 Author: craig <craig@b2368a2b-315f-46b9-a0b0-05934f827f41>
813 Author: paul <paul@b2368a2b-315f-46b9-a0b0-05934f827f41>
798 Author: dave <dave@b2368a2b-315f-46b9-a0b0-05934f827f41>

正如StefanNäwe在下面指出的,别名在你的仓库的根目录下运行,有没有什么办法可以运行在目录我的命令,或指定?

As Stefan Näwe notes below, aliases run in the root of your repository, is there any way to run the command on the directory I'm in, or specify?

推荐答案

看看你在做什么,你可能想使用 git shortlog -sn 相反,因为这个任务已经有问题了。

Looking at what you are doing you probably want to use git shortlog -sn instead as this does the task in question already.

git别名并不全部运行在顶层。如 git config手册中所述请注意,shell命令将从存储库的顶级目录,可能不一定是当前目录。所以只有shell命令可以在顶层运行。如果你只是别名一个git子命令,它将运行在当前位置。以下会话记录演示了这一点:

git aliases do not all run at the toplevel. As described in the git config manual "Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory." So only shell commands get run at the top level. If you simply alias a git subcommand it will run at the current location. The following session log demonstrates this:

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$ git rev-parse  --show-cdup
../../

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$ git config alias.cdup 'rev-parse --show-cdup'

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$ git cdup
../../

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$ git config alias.cdup2 '!git rev-parse --show-cdup'

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$ git cdup2

pat@FROG /c/src/WiRE.git/wdf/src (pt/wdf)
$

您的示例使用以'git'开头的别名命令。如果我尝试设置'git rev-parse --show-cdup'作为别名,它会返回一个错误,指出'git不是git命令',所以我怀疑你实际上也有一个感叹号,因此将它作为shell命令运行,因此从顶层运行。

Your example uses an alias command that starts with 'git'. If I try setting 'git rev-parse --show-cdup' as the alias it returns an error stating 'git is not a git command' so I suspect you actually had a exclamation mark too hence running it as a shell command, thus running from the toplevel.

这篇关于为什么将命令作为git alias提供不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆