upgrade: don't treat a detached HEAD as a branch

`git rev-parse --abbrev-ref HEAD` prints the literal "HEAD" on a detached
HEAD, which get_current_branch() returned as if it were a branch. check_for_update
then compared origin/HEAD (the remote's default branch, usually master) and
reported a bogus "N commits behind on HEAD" — a false "update available".
Return None for a detached HEAD so no phantom upgrade is offered.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-19 13:00:24 -06:00 • committed by lrnselfreliance
parent ee45b5e19d
commit bde60700d0
2 changed files with 19 additions and 1 deletions

View file

@ -68,6 +68,18 @@ class TestGitFunctions:
with patch('wrolpi.upgrade.PROJECT_DIR', mock_path):
assert get_current_branch() is None
def test_get_current_branch_detached_head(self):
"""A detached HEAD (abbrev-ref prints the literal "HEAD") is not a branch."""
mock_result = MagicMock()
mock_result.returncode = 0
mock_result.stdout = b'HEAD\n'
mock_path = MagicMock()
mock_path.is_dir.return_value = True
with patch('wrolpi.upgrade.PROJECT_DIR', mock_path), \
patch('subprocess.run', return_value=mock_result):
assert get_current_branch() is None
def test_get_local_commit(self):
"""get_local_commit returns the short commit hash."""
mock_result = MagicMock()

View file

@ -62,7 +62,13 @@ def get_current_branch() -> str | None:
timeout=10,
)
if result.returncode == 0:
return result.stdout.decode().strip()
branch = result.stdout.decode().strip()
# A detached HEAD makes `--abbrev-ref HEAD` print the literal
# "HEAD" (not a branch). Comparing origin/HEAD — the remote's
# default branch — against it yields a bogus "commits behind"
# count and a false "update available", so report no branch.
if branch and branch != 'HEAD':
return branch
except Exception as e:
logger.error('Failed to get current branch', exc_info=e)