A commit should remember itself. Today the commit porcelain hooks into HEAD: it takes its parent from the current branch and, after writing the commit, advances that branch to point at it.
Make commit find its parent from HEAD and move the current branch forward.
Until now a commit vanished the moment you made it: nothing pointed at it. This
lesson wires the porcelain into refs so history accumulates. Two halves, both
reading HEAD. First, the commit’s parent is whatever the current branch points
at right now, found via Resolve("HEAD"); for an unborn branch that is empty, so
the first commit has no parent. Second, after writing the commit, we advance the
branch HEAD names to the new id, creating refs/heads/main if it did not exist.
The commit command no longer needs you to pass parents by hand; it finds them from
HEAD. That is the everyday git commit behavior in miniature: commit, and your
current branch moves to include it. The first commit produces the same
f18e98d3... and leaves main pointing at it. With the branch remembering the
tip, the next commit can build on it automatically.
// parent = Resolve("HEAD") (empty when unborn); branch = HEAD's target namefunc (r *Repo) Commit(ix *Index, ident, msg string) (string, error) {parent, _ := r.Resolve("HEAD")var parents []stringif parent != "" { parents = []string{parent} }id, _ := r.CommitTree(tree, parents, ident, msg)r.UpdateRef(headTargetName, id) // move the current branchreturn id, nil}