Handle clone situations where HEAD is invalid
Created by: flying-robot
In some situations, it appears that empty repos may wind up with a truncated HEAD
file on disk for reasons unknown. That breaks the repositories view (among other things), and is not easily recoverable within the Sourcegraph application (fixing the repository or deleting it and re-cloning are the current suggestions).
To begin addressing that, this PR:
- Introduces an
ensureHead
function that will create aHEAD
file with a default if the file is absent or blank. - Introduces a
testRepoCorrupter
that tests can use to inject repo-level faults. - Adds a number of tests to verify the overall clone behavior with and without protections.
I wasn't able to trick git into cloning a repo that was truly busted, so I opted instead for fault injection. The repo is checked out to a temporary location first, and alongside the bad ref cleanup it will check HEAD presence and try to take a compensating action if necessary.