git tag 对象的格式是什么以及如何计算它的 SHA? [英] What is the format of a git tag object and how to calculate its SHA?
问题描述
我熟悉 Git 如何为文件(blob)创建 SHA1 哈希,但不熟悉它们是如何为标签创建的对象.我假设它们是,如果我创建一个带注释的标签,但配方是什么?我如何在 Git 之外复制它(例如,在 Perl 或 Python 中)?
I am familiar with how Git creates SHA1 hashes for files (blobs), but not how they are created for tag objects. I assume they are, if I create an annotated tag, but what is the recipe? And how might I replicate it outside of Git (e.g., in Perl or Python)?
推荐答案
模式基本是:
sha1("tag " + datasize + " " + data)
其中data
是git cat-file
的输出.可以通过将输出通过管道传输到 git-hash-object
来产生这一点,如下所示:
Where data
is the output of git cat-file
. One can produce this by piping that output to git-hash-object
like so:
git cat-file tag v0.30 | git hash-object -t tag --stdin
等价的 perl one-liner 是:
And the equivalent a perl one-liner is:
git cat-file tag v0.30 | perl -MDigest::SHA1 -E '$/=undef;$_=<>;say Digest::SHA1->new->add("tag ".length()." ".$_)->hex digest'
似乎可以对任何类型的对象执行相同的操作,只需将 "tag "
替换为正确的对象名称:"blob "
, >树"
,或提交"
.
It seems that one can do this same thing with any of the types objects simply by replacing "tag "
with the proper object name: "blob "
, "tree "
, or "commit "
.
这篇关于git tag 对象的格式是什么以及如何计算它的 SHA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!