summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Mak <gavinmak@google.com>2026-03-18 21:15:16 +0000
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2026-04-02 13:57:05 -0700
commit3f3c681a02ed27663e06c35328b494b9245881b5 (patch)
tree5bea9eb94219f631122d85000d2f8a4720d55be9
parent242e97d9ddf1a62fa3d9e92e21bd8b98b00e4455 (diff)
downloadgit-repo-3f3c681a02ed27663e06c35328b494b9245881b5.tar.gz
project: Refactor GetHead to use symbolic-ref first
Simplify branch resolution and optimize unborn branch detection by prioritizing symbolic-ref over rev-parse. Change-Id: Ic62dcb87cd051dafb00d520b1157be2e32abc2ab Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/563222 Reviewed-by: Nasser Grainawi <nasser.grainawi@oss.qualcomm.com> Reviewed-by: Mike Frysinger <vapier@google.com> Tested-by: Gavin Mak <gavinmak@google.com> Commit-Queue: Gavin Mak <gavinmak@google.com>
-rw-r--r--project.py30
1 files changed, 7 insertions, 23 deletions
diff --git a/project.py b/project.py
index 67c00bdbb..577847f8c 100644
--- a/project.py
+++ b/project.py
@@ -3964,30 +3964,14 @@ class Project:
3964 def GetHead(self): 3964 def GetHead(self):
3965 """Return the ref that HEAD points to.""" 3965 """Return the ref that HEAD points to."""
3966 try: 3966 try:
3967 symbolic_head = self.rev_parse("--symbolic-full-name", HEAD) 3967 return self.symbolic_ref("-q", HEAD, log_as_error=False)
3968 if symbolic_head == HEAD: 3968 except GitError:
3969 # Detached HEAD. Return the commit SHA instead. 3969 pass
3970 return self.rev_parse(HEAD)
3971 return symbolic_head
3972 except GitError as e:
3973 # `git rev-parse --symbolic-full-name HEAD` will fail for unborn
3974 # branches, so try symbolic-ref before falling back to raw file
3975 # parsing.
3976 try:
3977 p = GitCommand(
3978 self._project,
3979 ["symbolic-ref", "-q", HEAD],
3980 bare=True,
3981 gitdir=self._gitdir,
3982 capture_stdout=True,
3983 capture_stderr=True,
3984 log_as_error=False,
3985 )
3986 if p.Wait() == 0:
3987 return p.stdout.rstrip("\n")
3988 except GitError:
3989 pass
3990 3970
3971 try:
3972 # If symbolic-ref fails, try to treat as detached HEAD.
3973 return self.rev_parse(HEAD)
3974 except GitError as e:
3991 logger.warning( 3975 logger.warning(
3992 "project %s: unparseable HEAD; trying to recover.\n" 3976 "project %s: unparseable HEAD; trying to recover.\n"
3993 "Check that HEAD ref in .git/HEAD is valid. The error " 3977 "Check that HEAD ref in .git/HEAD is valid. The error "