Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Git store tree objects?

Tags:

git

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

like image 321
Jake Avatar asked Sep 04 '12 02:09

Jake


2 Answers

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

like image 126
linquize Avatar answered Sep 28 '22 15:09

linquize


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.

like image 40
user4815162342 Avatar answered Sep 28 '22 14:09

user4815162342