--abbrev-commit
标志可以与 git log
和 git 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-project repo 的克隆中运行时为“9” ,
- 在 OpenStack repo 的克隆中运行时为“9” ,
- 在 Linux-kernel repo 的克隆中运行时为“11” .
关于git - 在我的 repo 协议(protocol)中,最长的哈希前缀必须有多长才能防止重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405922/