What is the format of a git tag object and how to calculate its SHA?

前端 未结 4 838
小蘑菇
小蘑菇 2020-12-11 10:45

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

4条回答
  •  自闭症患者
    2020-12-11 10:57

    The pattern is basically:

    sha1("tag " + datasize + "\0" + data)
    

    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
    

    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()."\0".$_)->hex digest'
    

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

提交回复
热议问题