如果速记提交ID可以引用2个不同的提交,Git会警告我吗?


130

如果cee157可以引用2个不同的提交ID,例如

cee157eb799af829a9a0c42c0915f55cd29818d4cee1577fecf6fc5369a80bd6e926ac5f864a754b

如果我输入Git会警告我git log cee157吗?(或Git 1.8.5.2(Apple Git-48)允许我输入git log cee1)。

我认为应该,尽管我找不到任何权威人士说可以。


4
请参阅man gitrevisions,这至少意味着将发出警告,因为它指出您可以使用完整的SHA1-1名称或“存储库中唯一的前导子字符串”来命名修订。
chepner,2014年

5
您有17个不同的提交吗?只是尝试git log c...看看。
djechlin 2014年

1
在ELL中,我可能会将其标记为[general-reference]
2014年

3
@djechlin我需要至少4位数字。git log abc说,fatal: ambiguous argument 'abc': unknown revision or path not in the working tree.即使我有一个独特的SHA1开始abc。不能使用1-2-3位数字,似乎最少是4位。在Windows(1.8.1)和Mac(1.9.1)中进行了测试。
janos 2014年

4
@janos这是因为environment.h定义minimum_abbrev为的值4
devnull 2014年

Answers:


168

它应该给你这样的东西:

$ git log cee157
error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.
fatal: ambiguous argument 'cee157': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

我只是在一个真正的Git存储库上对此进行了测试,方法是查找具有重复前缀的提交,如下所示:

git rev-list master | cut -c-4 | sort | uniq -c | sort -nr | head

这将获取中的修订列表master,删去前4个字符,并丢弃其余的字符,计算重复项并按数字排序。在一个相对较小的约1500次提交的存储库中,我发现了很多带有共同的4位数字前缀的修订。我选择了4位数字的前缀,因为这似乎是Git支持的最短合法长度。(即使模棱两可,也不能使用3位数或更少的数字。)

顺便说一句,这不是一个错字,我不知道为什么关于歧义SHA1的错误消息会出现两次,而不管重复SHA1的数量如何(尝试使用2和3):

error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.

(两个都打开stderr。实际上整个输出都打开了stderr,什么都没有打开stdout。)

在Windows中测试:

$ git --version
git version 1.8.1.msysgit.1

我认为可以肯定地说,如果您的版本> = 1.8.1,Git 警告您重复。(它将拒绝使用重复项进行操作。)我想许多较旧的版本也都可以这种方式工作。

更新

测试时,由于int minimum_abbrev = 4environment.c中,您至少需要4位SHA1 。(感谢@devnull指出这一点!)


5
即使有两个以上具有匹配前缀的提交,该错误还会出现两次吗?
尼特

4
@Nit是的,即使有3个重复,该消息也会出现两次。更新了我的答案以澄清这一点。
janos 2014年

1
在给出git源代码的结构的情况下,看起来两个输出之一是警告,而另一个则是错误。虽然不确定。
2014年

1
@MarkHurd都在stderr上。实际上,整个输出在stderr上,而在stdout上则没有。(这很有意义)
janos

63

原始海报指出:

我认为应该,尽管我找不到任何权威人士说可以。

权威源可以在源代码中找到 get_short_sha1()

引用

if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
    return error("short SHA1 %.*s is ambiguous.", len, hex_pfx);

if (!ds->candidate_checked)
    /*
     * If this is the only candidate, there is no point
     * calling the disambiguation hint callback.
     *
     * On the other hand, if the current candidate
     * replaced an earlier candidate that did _not_ pass
     * the disambiguation hint callback, then we do have
     * more than one objects that match the short name
     * given, so we should make sure this one matches;
     * otherwise, if we discovered this one and the one
     * that we previously discarded in the reverse order,
     * we would end up showing different results in the
     * same repository!
     */
    ds->candidate_ok = (!ds->disambiguate_fn_used ||
                        ds->fn(ds->candidate, ds->cb_data));

if (!ds->candidate_ok)
    return SHORT_NAME_AMBIGUOUS;

此外,还存在测试以确保该功能按预期工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.