fix(protocol): heal dangling HEAD on load and after push
Repositories initialized with HEAD -> refs/heads/main (the project
default) but populated by pushing a repo with a different default
branch (golang/go uses master) end up with HEAD pointing at a
nonexistent branch. Git clients cannot check out a worktree and
warn: "remote HEAD refers to nonexistent ref, unable to checkout".
Fix: repoint HEAD at an existing branch when its symbolic target is
missing. Prefer refs/heads/main, then master, then trunk; fall back
to the lexicographically smallest branch. Heal is idempotent — a
detached HEAD, already-valid HEAD, or branch-less repo is left
untouched.
Heal on every repository load (fixes repos already in the bucket with
no re-push) and after successful receive-pack (fixes new pushes
immediately). Both call sites are now guarded by ensureHEAD(), which
is no-op when HEAD is already valid.
Fixes: #99 (clone checkout abort with dangling HEAD warning)
Assisted-by: Claude Opus 4.8 via claude.ai/code