git - 在我的 repo 协议(protocol)中,最长的哈希前缀必须有多长才能防止重叠?

标签 git linux-kernel sha1 git-log git-rev-list

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

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

此外,短 SHA 至少有 4 个字符长。仍然根据 Pro Git 书,

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

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.

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

最佳答案

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

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"
  )

我上次编辑这个答案时,打印了脚本

关于git - 在我的 repo 协议(protocol)中,最长的哈希前缀必须有多长才能防止重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405922/

相关文章:

git - git 中的上游/主题/集成之间的 merge 方向是否有重要意义?

git - merge 到 master 时为什么要删除功能分支

linux - Linux 阻塞 I/O 实际上是如何工作的?

c - linux系统打开设备文件失败

php - 我可以在没有 ssh 访问权限的情况下在 PHP 中使用 shell_exec 或反引号吗?

git - 自给定提交以来 git 中的作者列表

linux - 如何在 Linux 64 位机器上找出数据结构实现的内存布局

java - "="位于消息摘要末尾

c# - 检索哈希值后使用 c# HashAlgorithm

python - python加密的基础知识w/hashlib sha1