--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 中,最长的哈希前缀必须多长才能防止任何重叠?
发布评论