From 8c3585f367a7d09095a22565a44fa3b47f5b690c Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Mon, 4 Aug 2025 12:08:13 -0700 Subject: [PATCH] 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 Reviewed-by: Scott Lee Tested-by: Gavin Mak --- project.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/project.py b/project.py index 4c699d4b8..2badfb025 100644 --- a/project.py +++ b/project.py @@ -3841,8 +3841,29 @@ class Project: 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 = []