6

I'm trying to understand the exact format for how git stores tree objects. How is the hash of a tree object calculated?

2

2 Answers 2

10

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

0
6

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.