# Using ts-node
npx ts-node <path/to/git.ts> write-tree
# Using node
node <path/to/git.js> write-tree
Create a Tree object from the current index and displays the hash for the Tree object.
-
First parse the index and get all the files present in the index. We use the IndexParser class for this.
-
Create a tree and insert all the files that we got from the previous step. The code for this can be found in tree.ts.
The tree follows the structure of Patricia Trie. A given
Tree
object consists ofTreeNode
where each node can have multiple children nodes. The leaf node represents a File in the WorkTree while the internal nodes represent the directories. -
Finally save the tree to the disk and return the hash of the tree created. This is done by calling the
calculateHash()
method on theTree
class instance. This function accepts an optional boolean argumentwriteToDisk
which tells the function to create new objects for the directories. It also accepts another argumentcachedTree
which stores the entries corresponding to directories and is used while updating the index file.
To learn more how Git uses Tree objects, view this : Tree Objects section.