mirror of
https://gerrit.googlesource.com/git-repo
synced 2026-05-07 03:28:31 +00:00
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>
This commit is contained in:
+7
-23
@@ -3964,30 +3964,14 @@ class Project:
|
||||
def GetHead(self):
|
||||
"""Return the ref that HEAD points to."""
|
||||
try:
|
||||
symbolic_head = self.rev_parse("--symbolic-full-name", HEAD)
|
||||
if symbolic_head == HEAD:
|
||||
# Detached HEAD. Return the commit SHA instead.
|
||||
return self.rev_parse(HEAD)
|
||||
return symbolic_head
|
||||
except GitError as e:
|
||||
# `git rev-parse --symbolic-full-name HEAD` will fail for unborn
|
||||
# branches, so try symbolic-ref before falling back to raw file
|
||||
# parsing.
|
||||
try:
|
||||
p = GitCommand(
|
||||
self._project,
|
||||
["symbolic-ref", "-q", HEAD],
|
||||
bare=True,
|
||||
gitdir=self._gitdir,
|
||||
capture_stdout=True,
|
||||
capture_stderr=True,
|
||||
log_as_error=False,
|
||||
)
|
||||
if p.Wait() == 0:
|
||||
return p.stdout.rstrip("\n")
|
||||
except GitError:
|
||||
pass
|
||||
return self.symbolic_ref("-q", HEAD, log_as_error=False)
|
||||
except GitError:
|
||||
pass
|
||||
|
||||
try:
|
||||
# If symbolic-ref fails, try to treat as detached HEAD.
|
||||
return self.rev_parse(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 "
|
||||
|
||||
Reference in New Issue
Block a user