From bde60700d01b14158f2a7e21954ce22a6e632333 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 19 Jul 2026 13:00:24 -0600 Subject: [PATCH] upgrade: don't treat a detached HEAD as a branch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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) --- wrolpi/test/test_upgrade.py | 12 ++++++++++++ wrolpi/upgrade.py | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/wrolpi/test/test_upgrade.py b/wrolpi/test/test_upgrade.py index 49405710..2215e388 100644 --- a/wrolpi/test/test_upgrade.py +++ b/wrolpi/test/test_upgrade.py @@ -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() diff --git a/wrolpi/upgrade.py b/wrolpi/upgrade.py index a8ef4741..40138de8 100644 --- a/wrolpi/upgrade.py +++ b/wrolpi/upgrade.py @@ -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)