project: fallback to reading HEAD when rev-parse fails
git rev-parse fails on invalid HEAD, e.g. after incomplete sync, causing
NoManifestException. Fall back to v2.56's direct file reading when
rev-parse fails.
Bug: 435045466
Change-Id: Ia14560335110c00d80408b2a93595a84446f8a57
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/495181
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Scott Lee <ddoman@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
diff --git a/project.py b/project.py
index 4c699d4..2badfb0 100644
--- a/project.py
+++ b/project.py
@@ -3841,8 +3841,29 @@
return self.rev_parse(HEAD)
return symbolic_head
except GitError as e:
+ logger.warning(
+ "project %s: unparseable HEAD; trying to recover.\n"
+ "Check that HEAD ref in .git/HEAD is valid. The error "
+ "was: %s",
+ self._project.RelPath(local=False),
+ e,
+ )
+
+ # Fallback to direct file reading for compatibility with broken
+ # repos, e.g. if HEAD points to an unborn branch.
path = self.GetDotgitPath(subpath=HEAD)
- raise NoManifestException(path, str(e))
+ try:
+ with open(path) as fd:
+ line = fd.readline()
+ except OSError:
+ raise NoManifestException(path, str(e))
+ try:
+ line = line.decode()
+ except AttributeError:
+ pass
+ if line.startswith("ref: "):
+ return line[5:-1]
+ return line[:-1]
def SetHead(self, ref, message=None):
cmdv = []