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

nop*_*ole 130 git

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

cee157eb799af829a9a0c42c0915f55cd29818d4cee1577fecf6fc5369a80bd6e926ac5f864a754b

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

我认为它应该,但我找不到任何权威的来源说它会.

Sto*_*ica 167

它应该给你这样的东西:

$ 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>...]'
Run Code Online (Sandbox Code Playgroud)

我刚刚在一个真正的Git存储库上测试了这个,通过查找具有重复前缀的提交,如下所示:

git rev-list master | cut -c-4 | sort | uniq -c | sort -nr | head
Run Code Online (Sandbox Code Playgroud)

这将获取修订列表master,删除前4个字符并丢弃其余字符,计算重复项并按数字排序.在我相对较小的~1500次提交的存储库中,我发现了一些带有常见4位前缀的修订版.我选择了一个4位数的前缀,因为这似乎是Git支持的最短法定长度.(不使用3位数或更少,即使不含糊也是如此.)

顺便说一下,这不是一个错字,我不知道为什么有关模糊SHA1的错误消息出现两次,无论重复SHA1的数量(尝试使用2和3):

error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.
Run Code Online (Sandbox Code Playgroud)

(两者都打开stderr.实际上整个输出都打开了stderr,没有任何内容stdout.)

在Windows中测试:

$ git --version
git version 1.8.1.msysgit.1
Run Code Online (Sandbox Code Playgroud)

我认为可以肯定地说,如果你的版本是> = 1.8.1,Git 警告你重复.(它会拒绝重复操作.)我猜想很多旧版本也会这样工作.

UPDATE

在测试时,您需要至少4位数的SHA1,因为int minimum_abbrev = 4environment.c中.(感谢@devnull指出这一点!)

  • 即使有两个以上具有匹配前缀的提交,错误是否会出现两次? (5认同)
  • @Nit是的,即使有3个重复,消息也会出现两次.更新了我的答案以澄清这一点. (4认同)
  • 是否在stdout和stderr上报告了错误? (2认同)

dev*_*ull 63

原始海报说明:

我认为它应该,但我找不到任何权威的来源说它会.

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

引用这个:

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

:

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;
Run Code Online (Sandbox Code Playgroud)

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