在我的回购中,最长的哈希前缀需要多长时间才能防止重叠? [英] In my repo, how long must the longest hash prefix be to prevent any overlap?

查看:180
本文介绍了在我的回购中,最长的哈希前缀需要多长时间才能防止重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


它默认使用七个字符,但如果需要使它们更长以保持SHA-1清晰[...]


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


一般而言,8到10个字符足以在项目中独一无二。



举个例子,Linux内核是一个超过450k提交和360万个对象的大型项目,没有两个对象的SHA-1重叠超过前11个字符。

由于为防止所有提交对象的前缀哈希值之间的重叠所需的最长前缀的长度(11,在Linux内核)是回购仓库规模的粗略指标,我想以编程方式在我自己的本地仓库中确定相应的数量。我怎么做到这一点?

解决方案

以下shell脚本在本地仓库中运行时,输出最长前缀的长度需要防止该存储库的提交对象的所有前缀哈希值之间的重叠。


$ b

  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
完成&& printf%s \\$ MAX_LENGTH

上一次我编辑这个答案时,脚本在


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,

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

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.

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.

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?

解决方案

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

这篇关于在我的回购中,最长的哈希前缀需要多长时间才能防止重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆