I'm trying to understand the exact format for how git stores tree objects. How is the hash of a tree object calculated?
-
1Regarding the hash, http://stackoverflow.com/questions/5290444/why-does-git-hash-object-return-a-different-hash-than-openssl-sha1 and http://stackoverflow.com/questions/552659/assigning-git-sha1s-without-git can help – VonC Sep 04 '12 at 04:04
-
Possible duplicate of [What is the internal format of a git tree object?](http://stackoverflow.com/questions/14790681/what-is-the-internal-format-of-a-git-tree-object) – Ciro Santilli Путлер Капут 六四事 May 17 '16 at 09:27
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
- 19,090
- 9
- 57
- 80
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.
- 124,516
- 15
- 228
- 298
-
-
@mernen 's edit was correct; I didn't get to see it before it was rejected. mernen, please repeat the edit and I will approve it. – user4815162342 Apr 16 '13 at 14:46