【问题标题】:How to disambiguate an ambiguous abbreviated sha1 in git如何在 git 中消除歧义的缩写 sha1
【发布时间】:2015-02-10 06:24:17
【问题描述】:

我对@9​​87654321@很感兴趣。

假设有人在明确的时候写下了一个缩写的提交 ID 8b82547e33。但是从那时起,其他对象已经创建了相同的前缀,所以现在 git 告诉你(出于某种原因两次):

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

现在,作为一个人类,如果 git 只会向我显示模棱两可的对象,我可能会知道我指的是哪个对象。我怎样才能实现以下目标?

$ git objects-starting-with 8b82547e33
8b82547e33e: commit: l2tp: Restore socket refcount when sendmsg succeeds
8b82547e338: tree [2 files, 26 subtrees]

(注意:以上示例使用的是http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git的相对最新克隆。)

【问题讨论】:

  • 嗯。阅读该页面,只要 Git 可以保证它打印的缩写哈希在创建时是唯一的,也许您总是可以使用最旧的,假设较新的哈希会提供更具体的缩写?

标签: git


【解决方案1】:

使用 Git 2.11+(2016 年第四季度),您甚至不必输入 git rev-parse --disambiguate=...。

Git 会为你列出可能的候选人!

请参阅commit 5b33cb1(2016 年 9 月 27 日)和commit 1ffa26c、commit fad6b9e、commit 16ddcd4、commit 0c99171、commit 59e4e34、commit 0016043、commit 5d5def2、commit 7243ffd、@987 commit 259942f(2016 年 9 月 26 日)Jeff King (peff)。
(由 Junio C Hamano -- gitster -- 合并于 commit 66c22ba,2016 年 10 月 6 日)

get_short_sha1: 出错时列出不明确的对象

当用户给我们一个模棱两可的短 sha1 时,我们会打印一个错误并拒绝解决它。
在某些情况下,下一步是让他们为我们提供更多字符(例如,如果他们正在重新输入或从完整的 sha1 中剪切和粘贴)。但在其他情况下,这可能就是他们所拥有的一切。

例如,旧的提交消息可能使用了 7 个字符的十六进制 在当时是独一无二的,但现在是模棱两可的。
Git 没有提供关于它找到的模棱两可对象的任何信息,因此用户很难找出他们可能指的是哪一个。

这个补丁教 get_short_sha1() 列出它找到的对象的 sha1s,以及一些可以帮助用户确定他们所指的对象的信息。
这是git.git 上的样子:

  $ git rev-parse b2e1
  error: short SHA1 b2e1 is ambiguous
  hint: The candidates are:
  hint:   b2e1196 tag v2.8.0-rc1
  hint:   b2e11d1 tree
  hint:   b2e1632 commit 2007-11-14 - Merge branch 'bs/maint-commit-options'
  hint:   b2e1759 blob
  hint:   b2e18954 blob
  hint:   b2e1895c blob
  fatal: ambiguous argument 'b2e1': unknown revision or path not in the working tree.
  Use '--' to separate paths from revisions, like this:
  'git <command> [<revision>...] -- [<file>...]'

我们显示标签的标签名,以及提交的日期和主题。
对于树木和斑点,理论上我们可以挖掘历史以找到它们存在的路径。但这非常昂贵(内核大约需要 30 秒),而且不太可能有那么大的帮助。
大多数简短的引用都指向提交,因此有用的信息通常是所讨论的对象不是提交。所以花大量CPU抢先挖路径是愚蠢的;如果真的需要,用户可以自己做。

当然,我们在消歧提示中缩写 sha1 有点讽刺。
但是完整的 sha1 会导致提交行的换行令人讨厌,并且可能用户会立即使用更正后的 sha1 重新发出他们的命令。

我们还将列表限制为匹配任何消歧提示的列表。例如:

  $ git rev-parse b2e1:foo
  error: short SHA1 b2e1 is ambiguous
  hint: The candidates are:
  hint:   b2e1196 tag v2.8.0-rc1
  hint:   b2e11d1 tree
  hint:   b2e1632 commit 2007-11-14 - Merge branch 'bs/maint-commit-options'
  fatal: Invalid object name 'b2e1'.

不会费心报告 blob,因为它们不能像树一样工作。


2017 年 11 月更新(一年后),使用 Git 2.16(2018 年第一季度)可以更快地消除歧义:
见commit 0e87b85Derrick Stolee,最初是discussed here。

sha1_name:在消歧过程中最小化 OID 比较

在消除包文件 OID 歧义期间最小化 OID 比较。

教 git 使用带有完整 OID 的二进制搜索来查找对象的 包索引中的位置(或插入位置,如果不存在)。 之前和之后的对象(或插入处的对象 position) 给出最大公共前缀。没有后续的线性搜索 是必需的。

注意检查哪两个,以防对象 id 存在于 包文件。

如果find_unique_abbrev_r() 的输入是部分前缀,则 用于二进制搜索的 OID 用零填充,因此对象将 回购中不存在(很有可能)和相同的逻辑 适用。

本次提交完成了对 OID 缩写的一系列三项更改 代码,并且可以使用标准命令查看整体变化 大型回购。下面我们报告性能测试 4211.6 的性能统计信息 来自p4211-line-log.sh,使用三个 Linux 存储库副本:

| Packs | Loose  | HEAD~3   | HEAD     | Rel%  |
|-------|--------|----------|----------|-------|
|  1    |      0 |  41.27 s |  38.93 s | -4.8% |
| 24    |      0 |  98.04 s |  91.35 s | -5.7% |
| 23    | 323952 | 117.78 s | 112.18 s | -4.8% |

2018 年 3 月更新,sha1 消歧更加强大,因为(在 21.7 之前)在查找唯一对象名称缩写时,代码可能会意外读取到包中对象名称数组的末尾之外。

参见 Derrick Stolee (derrickstolee) 的 commit 21abed5(2018 年 2 月 27 日)。
(由 Junio C Hamano -- gitster -- 合并于 commit 7519a60,2018 年 3 月 8 日)

它也更快:

sha1_name:在消歧过程中最小化 OID 比较

在消除包文件 OID 歧义期间最小化 OID 比较。

教git 使用带有完整 OID 的二进制搜索来查找对象的 pack-index 中的位置(或插入位置,如果不存在)。
之前和之后的对象(或插入处的对象 position) 给出最大公共前缀。没有后续的线性搜索 是必需的。

本次提交完成了对 OID 缩写的一系列三项更改 代码,并且可以使用标准命令查看整体变化 大型回购。
下面我们报告性能测试 4211.6 的性能统计信息 来自p4211-line-log.sh,使用三个 Linux 存储库副本:

| Packs | Loose  | HEAD~3   | HEAD     | Rel%  |
|-------|--------|----------|----------|-------|
|  1    |      0 |  41.27 s |  38.93 s | -4.8% |
| 24    |      0 |  98.04 s |  91.35 s | -5.7% |
| 23    | 323952 | 117.78 s | 112.18 s | -4.8% |

Git 2.18(2018 年第二季度)改进了候选列表:当使用简短的十六进制字符串命名对象但有多个对象共享该字符串作为其名称的前缀时,代码会在帮助中列出这些模棱两可的候选消息。
这些对象名称现在根据它们的类型进行排序,以便于观察。

请参阅commit 5cc044e、commit a885c93、commit 89f32a9、commit 7248672、commit a264f22(2018 年 5 月 10 日)Ævar Arnfjörð Bjarmason (avar)。
帮助者:Derrick Stolee (derrickstolee).
(由 Junio C Hamano -- gitster -- 合并于 commit ab48bc0,2018 年 5 月 30 日)

get_short_oid: 按类型对不明确的对象排序,然后按 SHA-1

更改遇到不明确对象时发出的输出,以便 我们首先显示标签,然后提交,然后是树,最后 斑点。
在每种类型中,我们以hashcmp() 的顺序显示对象。
在此更改之前,对象仅按hashcmp() 排序。

这样做的原因是输出看起来更好, 例如在“git show e8f2”上进行此更改之前的 v2.17.0 标签将 显示:

hint: The candidates are:
hint:   e8f2093055 tree
hint:   e8f21ca commit 2013-06-24 - bash prompt: print unique detached HEAD abbreviated object name
hint:   e8f21d02f7 blob
hint:   e8f21d577c blob
hint:   e8f25a3a50 tree
hint:   e8f2625 commit 2017-02-03 - Merge pull request #996 from jeffhostetler/jeffhostetler/register_rename_src
hint:   e8f2650052 tag v2.17.0
hint:   e8f2867228 blob
hint:   e8f28d537c tree
hint:   e8f2a35526 blob
hint:   e8f2bc0 commit 2015-05-10 - Documentation: note behavior for multiple remote.url entries
hint:   e8f2cf6ec0 tree

现在我们将改为显示:

hint:   e8f2650052 tag v2.17.0
hint:   e8f21ca commit 2013-06-24 - bash prompt: print unique detached HEAD abbreviated object name
hint:   e8f2625 commit 2017-02-03 - Merge pull request #996 from jeffhostetler/jeffhostetler/register_rename_src
hint:   e8f2bc0 commit 2015-05-10 - Documentation: note behavior for multiple remote.url entries
hint:   e8f2093055 tree
hint:   e8f25a3a50 tree
hint:   e8f28d537c tree
hint:   e8f2cf6ec0 tree
hint:   e8f21d02f7 blob
hint:   e8f21d577c blob
hint:   e8f2867228 blob
hint:   e8f2a35526 blob

【讨论】:

    【解决方案2】:

    您可以使用git rev-parse,前提是您至少有一个完整哈希的 4 位前缀。

    git rev-parse --disambiguate=8b82547e33
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      相关资源
      最近更新 更多