在我的 repo 中,最长的哈希前缀必须多长才能防止任何重叠?

编程入门 行业动态 更新时间:2024-10-17 19:23:10
本文介绍了在我的 repo 中,最长的哈希前缀必须多长才能防止任何重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

--abbrev-commit 标志可以与 git log 和 git rev-list 结合使用以显示部分前缀而不是提交对象的完整 40 个字符的 SHA-1 哈希值.根据 Pro Git book,

The --abbrev-commit flag can be used in conjunction with git log and git rev-list in order to show partial prefixes instead of the full 40-character SHA-1 hashes of commit objects. According to the Pro Git book,

它默认使用七个字符,但如果需要保持 SHA-1 明确 [...]

it defaults to using seven characters but makes them longer if necessary to keep the SHA-1 unambiguous [...]

此外,短 SHA 的长度至少为 4 个字符.仍然根据 Pro Git 的书,

Additionally, short SHAs are at least 4-character long. Still according to the Pro Git book,

通常,八到十个字符足以在一个项目中成为唯一的.

Generally, eight to ten characters are more than enough to be unique within a project.

举个例子,Linux 内核是一个相当大的项目,拥有超过 45 万次提交和 360 万个对象,没有两个对象的 SHA-1 重叠超过前 11 个字符.

As an example, the Linux kernel, which is a pretty large project with over 450k commits and 3.6 million objects, has no two objects whose SHA-1s overlap more than the first 11 characters.

由于防止提交对象的所有前缀哈希之间的任何重叠所需的最长前缀的长度(在 Linux 内核的情况下为 11)是存储库大小的粗略指标,因此我想以编程方式确定我自己本地存储库中的相应数量.我该怎么做?

Since the length of the longest prefix required to prevent any overlap among all prefix hashes of commit objects (11, in the case of the Linux kernel) is a crude indicator of a repo's size, I'd like to programmatically determine the corresponding quantity in my own local repository. How can I do that?

推荐答案

以下 shell 脚本,当在本地存储库中运行时,会打印所需的最长前缀的长度,以防止该提交对象的所有前缀哈希之间出现任何重叠存储库.

The following shell script, when run in a local repo, prints the length of the longest prefix required to prevent any overlap among all prefix hashes of commit objects of that repository.

MAX_LENGTH=4; git rev-list --abbrev=4 --abbrev-commit --all | ( while read -r line; do if [ ${#line} -gt $MAX_LENGTH ]; then MAX_LENGTH=${#line}; fi done && printf %s\n "$MAX_LENGTH" )

我上次编辑此答案时,已打印脚本

The last time I edited this answer, the script printed

  • "9" 在 Git 项目存储库 的克隆中运行时,
  • 在 OpenStack 存储库 的克隆中运行时为9",
  • 在 Linux 内核存储库 的克隆中运行时为11".
  • "9" when run in a clone of the Git-project repo,
  • "9" when run in a clone of the OpenStack repo,
  • "11" when run in a clone of the Linux-kernel repo.

更多推荐

在我的 repo 中,最长的哈希前缀必须多长才能防止任何重叠?

本文发布于:2023-10-26 18:36:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前缀   最长   repo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!