I'm trying to understand the exact format for how git stores tree objects. How is the hash of a tree object calculated?
tree object
'tree' ' ' size_decimal '\0' tree_content
for each entry in tree_content
mode ' ' filename '\0' hash_20_bin
mode: 100644 for a regular file, 100755 executable; 040000: tree; 120000: symlink; 160000: gitlink
table http://linquize.blogspot.hk/2011/10/supplemental-information-for-git.html
A tree object is internally stored as a binary object (of type "tree", which distinguishes it from actual files) that contains a list of entries. An entry can describe a file or another tree (directory). Each line contains the entry name, its SHA1 hash, and its mode. A more detailed description can be found here.
Commands like git ls-tree
and git cat-file -p
will output a textual representation of this object. This textual form is a pretty straightforward conversion: the SHA-1 is shown before the entry name in hex form, with an additional column describing the kind of object it points to to ("blob", "tree") just for clarity.
Its hash is calculated simply as the hash of that content. Since it contains the names and hashes of its constituents, the tree hash is guaranteed to change whenever a hash of any of the subtrees changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With