Check out the default branch when the clone has no origin/HEAD - #158
Merged
Conversation
A repo could load as a completely empty city and stay that way. PostHog hit it: 13,114 branches and all of master's history on disk, working tree holding nothing but .git/, scan walking 0 files, every reload identical. refs/remotes/origin/HEAD is written by git clone and never by git fetch, so a clone that never got one has no local record of the default branch. _resolve_default_branch read that missing ref as "the remote has no commits" and skipped the checkout entirely, which is only true for a genuinely empty remote. Skipping isn't an error either, so the self-heal re-clone never fired. Re-derive the default instead: ask the remote what HEAD points at, else pick a conventional name from the branches already fetched, else the lone branch. Only conclude the remote is empty when nothing was fetched at all. Record the answer back to origin/HEAD so the repair sticks. git clone also exits 0 when it can't resolve the remote's HEAD, checking nothing out, so guard the fresh-clone path too. Restoring the files alone left a second failure: reset --hard moves whatever HEAD already points at rather than repointing it, so HEAD stayed on the dangling refs/heads/.invalid. Files landed on disk while every history command failed, and _run_git returns "" on failure, so the scan silently read zero commits — a city with no ages, no trees and no timeline. Put HEAD on a real branch before resetting. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Recently-modified files were washing out toward white at 50. 40 keeps them bright without losing the color. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
thalida
commented
Aug 10, 2026
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Loading https://github.com/PostHog/posthog rendered a completely empty city, and stayed that way on every reload.
What was wrong
The clone on disk had everything except a checkout:
refs/remotes/origin/HEADis written bygit cloneand never bygit fetch, so a clone that never got one has no local record of which branch is default._resolve_default_branchread that missing ref as "the remote has no commits" and skipped the checkout — an inference that only holds for a genuinely empty remote. A repo with 13,114 branches took the same path as a brand-new empty one.Skipping the reset isn't an error either, so the self-heal (
update failed → discard clone → re-clone) never fired. Every reload re-fetched, re-decided "no commits", re-scanned 0 files.Fixing that exposed a second bug.
reset --hardmoves whatever HEAD already points at; it does not repoint a broken HEAD. Files landed on disk while HEAD stayed on the danglingrefs/heads/.invalid:_run_gitreturns""on failure, so the scan silently read zero commits — no dates, no ages, no trees, no timeline, while the city otherwise looked fine.The fix
origin/HEADis absent, re-derive the default: ask the remote what HEAD points at, else a conventional name among the branches already fetched, else the lone branch. ReturnNoneonly when nothing was fetched at all.refs/remotes/origin/HEADso the repair is permanent._reset_to()puts HEAD on a real branch before resetting when it isn't on a usable one.git cloneexits 0 with nothing checked out, so the first load of such a repo would otherwise still be empty.Verification
The regression test reproduces the exact on-disk shape (populated clone,
origin/HEADdeleted as a symref, HEAD parked onrefs/heads/.invalid, tree emptied). I disabled each half of the fix in turn to confirm both assertions bite — the checkout assertion and thegit logassertion each go red on their own.Against the real repo after the fix:
branch: master, rootcreated_min: 2020-01-23(was the checkout timestamp), 42,969 files with 0 missing creation dates, only 38 falling back to mtime, spanning 2020–2026. The manifest grew 21.9 MB → 34.7 MB, all recovered history.403 passed, coverage 90%.ruffclean. Frontend2,992 passed.Also here
One unrelated commit: the building lightness ramp's top end drops 50 → 40, so recently-modified files stop washing out toward white. Happy to split it out if you'd rather.
🤖 Generated with Claude Code