write-tree turns the staging area into a tree object, the bridge from index to commit. Today you handle the flat case, an index with no subdirectories, and its id matches the tree you built by hand earlier.
Turn a flat index into one tree object.
write-tree is the operation that snapshots the staging area into a real object.
For a flat index (no subdirectories) it is almost trivial: every entry becomes a
tree entry with the same mode, name, and id, and you hand them to the WriteTree
you already built, which sorts and hashes them. The payoff is the point: an index
staged from hello.txt and README.md produces the identical tree id you got by
constructing those entries directly, 3aa9b583....
This closes the loop between the index and the object store: staged files become a
content-addressed tree, ready to be committed. The next lesson handles the harder
case, an index whose paths reach into subdirectories, which is where write-tree
has to build nested trees.
// each index entry with no slash in its path is one file entryfunc (r *Repo) WriteTreeFromIndex(ix *Index) (string, error) {var entries []Entryfor _, p := range ix.Paths() {e := ix.entries[p]entries = append(entries, Entry{Mode: e.Mode, Name: p, Id: e.Id})}return r.WriteTree(entries)}