首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果一个速记提交ID可以引用两个不同的提交,Git会警告我吗?

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

Stack Overflow用户
提问于 2014-05-06 18:39:03
回答 2查看 4.9K关注 0票数 130

如果cee157可以引用两个不同的提交If,例如

cee157eb799af829a9a0c42c0915f55cd29818d4cee1577fecf6fc5369a80bd6e926ac5f864a754b

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

我认为它应该,尽管我找不到任何权威来源说它会。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-06 18:46:18

它应该会给你类似这样的东西:

$ 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

我认为可以肯定地说,如果你的版本是Git1.8.1,>=会警告你有重复的地方。(它将拒绝使用重复项进行操作。)我猜更老的版本也是这样的。

更新

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

票数 168
EN

Stack Overflow用户

发布于 2014-05-07 03:30:27

最初的海报上写着:

我认为它应该,尽管我找不到任何权威来源说它会。

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

引用this

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

this

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;

此外,还存在tests来确保该功能按预期工作。

票数 63
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23492381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档