Skip to content

The agent that grows with you

fix(update): keep never-pushed and unmerged branches pinned in source check

Austin Pickettf6df13bac690
diff --git a/hermes_cli/source_check.py b/hermes_cli/source_check.py
index 3397ce7af96..1c6e08d33fc 100644
--- a/hermes_cli/source_check.py
+++ b/hermes_cli/source_check.py
@@ -306,6 +306,29 @@ def _heal_deleted_branch(branch_config_path: Path, desktop_config: dict) -> None
atomic_json_write(branch_config_path, {**desktop_config, "branch": "main"})
+def _unhealable_reason(co: _Checkout, branch: str) -> Optional[str]:
+ """Why a branch the remote does not advertise must keep its pin; None when healing loses nothing.
+
+ An empty ref advertisement cannot tell "merged and deleted upstream" from "never pushed":
+ only a branch that was once published (remote-tracking ref or configured upstream, which
+ survives ``fetch --prune``) and whose commits are all in main may be re-pinned to main.
+ """
+ if co.embedded:
+ return None
+ local = f"refs/heads/{branch}"
+ if not _git_ok(["rev-parse", "--verify", "--quiet", local], cwd=co.root, git=co.git):
+ return None # Nothing in this checkout to abandon.
+ if not (_git_ok(["rev-parse", "--verify", "--quiet", f"refs/remotes/origin/{branch}"], cwd=co.root, git=co.git)
+ or _git_ok(["config", "--get", f"branch.{branch}.merge"], cwd=co.root, git=co.git)):
+ return "never-pushed"
+ for base in ("refs/remotes/origin/main", "refs/heads/main"):
+ # `git cherry` also treats rebase-merged commits (same patch, new SHA) as merged.
+ cherry = _git_run(["cherry", base, local], cwd=co.root, git=co.git, timeout=10)
+ if cherry is not None and cherry.returncode == 0:
+ return "unmerged" if any(line.startswith("+") for line in cherry.stdout.splitlines()) else None
+ return "unmerged" # Unknown merge state keeps the branch.
+
+
def _behind_count(co: _Checkout, target: str) -> tuple[int, list[dict]]:
"""``(behind, commits)`` for ``target``: local ancestry first, then the GitHub compare API."""
if co.head == target or (not co.embedded and _git_ok(
@@ -325,6 +348,13 @@ def _check_branch(result: dict, co: _Checkout, selected_branch: str, *,
result["branch"] = selected_branch
remote = _branch_remote(co, selected_branch)
target, missing, failure = _branch_tip(co.repository, selected_branch, co.root, co.git, remote)
+ reason = _unhealable_reason(co, selected_branch) if missing and selected_branch != "main" else None
+ if reason:
+ detail = ("has never been pushed" if reason == "never-pushed"
+ else "is gone from the remote but has commits that are not in main")
+ result.update(error="branch-local-only", localOnly=True,
+ message=f"Branch '{selected_branch}' {detail}; keeping it instead of switching to main.")
+ return
if missing and selected_branch != "main":
result["branch"] = "main"
if heal:
diff --git a/tests/hermes_cli/test_source_check.py b/tests/hermes_cli/test_source_check.py
index f0de49d872f..b1f9589424c 100644
--- a/tests/hermes_cli/test_source_check.py
+++ b/tests/hermes_cli/test_source_check.py
@@ -338,6 +338,84 @@ def test_deleted_desktop_branch_is_persisted_only_after_definitive_probe(install
assert not branch_file.read_bytes().startswith(b"\xef\xbb\xbf")
+def _bare_origin(installation):
+ """Point the checkout at a real bare remote holding main; no GitHub repository is involved."""
+ root, linked, home, base, head, responses, requests, git = installation
+ remote = home / "remote.git"
+ git("init", "--bare", "-b", "main", str(remote))
+ git("remote", "set-url", "origin", str(remote))
+ git("push", "-q", "origin", "main")
+ git("fetch", "-q", "origin")
+
+
+def _commit_on(git, branch, message):
+ git("checkout", "-q", branch)
+ git("commit", "-q", "--allow-empty", "-m", message)
+ sha = git("rev-parse", "HEAD")
+ git("checkout", "-q", "main")
+ return sha
+
+
[email protected]("pinned_by", ["desktop", "current"])
+def test_never_pushed_branch_keeps_its_pin(installation, pinned_by):
+ """#105042: an empty advertisement for a branch that was never pushed is not a deletion."""
+ from hermes_cli.source_check import check_for_updates
+ root, linked, home, base, head, responses, requests, git = installation
+ _bare_origin(installation)
+ git("branch", "local-work")
+ _commit_on(git, "local-work", "unpushed work")
+ branch_file = home / "desktop-update.json"
+ if pinned_by == "desktop":
+ branch_file.write_text(json.dumps({"branch": "local-work"}))
+ else:
+ git("checkout", "-q", "local-work")
+ status = check_for_updates(install_root=root, home=home, branch_config_path=branch_file)
+ assert status["branch"] == "local-work", status
+ assert status["error"] == "branch-local-only"
+ assert status["localOnly"] is True
+ assert "never been pushed" in status["message"]
+ assert "targetSha" not in status
+ if pinned_by == "desktop":
+ assert json.loads(branch_file.read_text()) == {"branch": "local-work"}
+ else:
+ assert not branch_file.exists()
+ assert requests == [MAIN_CHANNEL]
+
+
[email protected]("merge", ["fast-forward", "rebase", "unmerged"])
+def test_deleted_remote_branch_heals_only_when_its_commits_are_in_main(installation, merge):
+ from hermes_cli.source_check import check_for_updates
+ root, linked, home, base, head, responses, requests, git = installation
+ _bare_origin(installation)
+ git("branch", "pushed")
+ work = _commit_on(git, "pushed", "published work")
+ git("push", "-q", "-u", "origin", "pushed")
+ if merge == "fast-forward":
+ git("merge", "-q", "--ff-only", "pushed")
+ elif merge == "rebase":
+ git("commit", "-q", "--allow-empty", "-m", "upstream moved on")
+ git("cherry-pick", "--allow-empty", work)
+ if merge != "unmerged":
+ git("push", "-q", "origin", "main")
+ # Upstream deletes the branch; the prune removes the tracking ref but keeps the upstream config.
+ git("push", "-q", "origin", "--delete", "pushed")
+ git("fetch", "-q", "--prune", "origin")
+ assert git("for-each-ref", "refs/remotes/origin/pushed") == ""
+ branch_file = home / "desktop-update.json"
+ branch_file.write_text(json.dumps({"branch": "pushed"}))
+ status = check_for_updates(install_root=root, home=home, branch_config_path=branch_file)
+ if merge == "unmerged":
+ assert status["branch"] == "pushed", status
+ assert status["error"] == "branch-local-only"
+ assert "not in main" in status["message"]
+ assert json.loads(branch_file.read_text())["branch"] == "pushed"
+ else:
+ assert "error" not in status, status
+ assert status["branch"] == "main"
+ assert status["targetSha"] == git("rev-parse", "origin/main")
+ assert json.loads(branch_file.read_text())["branch"] == "main"
+
+
def test_inherited_git_target_cannot_redirect_an_explicit_install(installation, monkeypatch):
from hermes_cli.source_check import check_for_updates
root, linked, home, base, head, responses, requests, git = installation