The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): count new commits from where HEAD was when checkout -B moves it
kshitijk4poor80d6e395db2d
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.pyindex 6445a5e1d27..535b5a220a9 100644--- a/hermes_cli/update_cmd.py+++ b/hermes_cli/update_cmd.py@@ -638,9 +638,9 @@ def _is_shallow_checkout(git_cmd) -> bool:return _git_run(git_cmd, ["rev-parse", "--is-shallow-repository"]).stdout.strip() == "true"-def _tip_shas(git_cmd, target_ref: str) -> tuple[str, str]:- """``(HEAD sha, <target_ref> sha)`` as printed by rev-parse ("" when unresolvable)."""- return tuple(_git_run(git_cmd, ["rev-parse", ref]).stdout.strip() for ref in ("HEAD", target_ref))+def _tip_shas(git_cmd, target_ref: str, base: str = "HEAD") -> tuple[str, str]:+ """``(<base> sha, <target_ref> sha)`` as printed by rev-parse ("" when unresolvable)."""+ return tuple(_git_run(git_cmd, ["rev-parse", ref]).stdout.strip() for ref in (base, target_ref))def _print_update_check_result(behind: int | None, compare_branch: str) -> None:@@ -948,9 +948,13 @@ def _prepare_checkout_for_update(# Before the stash: its refs/stash would contain HEAD until it is dropped._park_detached_head(git_cmd, _m().PROJECT_ROOT, branch)auto_stash_ref = _m()._stash_local_changes_if_needed(git_cmd, _m().PROJECT_ROOT)+ moved_from_sha = Noneif (not release_tag and not in_place_update and current_branch != branchand _git_run(git_cmd, ["checkout", branch]).returncode != 0):+ # `checkout -B` lands ON the target, so HEAD..target would count 0 and the update would+ # finish as "Already up to date!" with nothing synced (#125112): count from here instead.+ moved_from_sha = _capture_head_sha(git_cmd, _m().PROJECT_ROOT)track_result = _git_run(git_cmd, ["checkout", "-B", branch, f"origin/{branch}"])if track_result.returncode != 0:# Restore the stash before bailing so the user isn't stranded.@@ -978,13 +982,14 @@ def _prepare_checkout_for_update(# On shallow checkouts `rev-list --count` can report the entire remote ancestry. The# zero/nonzero gate is still sound; treat the shallow NUMBER as unknown and recover it# via the GitHub compare API when possible.- result = _git_run(git_cmd, ["rev-list", f"HEAD..{target_ref}", "--count"], check=True)+ base = moved_from_sha or "HEAD"+ result = _git_run(git_cmd, ["rev-list", f"{base}..{target_ref}", "--count"], check=True)commit_count = int(result.stdout.strip())apply_is_shallow = _is_shallow_checkout(git_cmd)if commit_count > 0 and apply_is_shallow:from hermes_cli.source_check import _github_compare_behind- counted = _github_compare_behind(*_tip_shas(git_cmd, target_ref))+ counted = _github_compare_behind(*_tip_shas(git_cmd, target_ref, base))# counted == 0 means local-ahead: falls through to the up-to-date path.commit_count = counted if counted is not None else -1@@ -998,7 +1003,6 @@ def _prepare_checkout_for_update(# "Already up to date!" and verified nothing). Non-fork checkouts have no upstream question: origin IS# the official repo, so "Already up to date!" is fully verified there.upstream_checked = True- moved_from_sha = Noneif commit_count == 0 and is_fork and branch == "main" and not release_tag:pre_sync_sha = _capture_head_sha(git_cmd, _m().PROJECT_ROOT)upstream_checked = _m()._sync_with_upstream_if_needed(diff --git a/tests/hermes_cli/test_update_narrow_clone_compare_ref.py b/tests/hermes_cli/test_update_narrow_clone_compare_ref.pyindex a6afe3cb413..c3373674116 100644--- a/tests/hermes_cli/test_update_narrow_clone_compare_ref.py+++ b/tests/hermes_cli/test_update_narrow_clone_compare_ref.py@@ -83,3 +83,38 @@ def test_forced_refspec_updates_shallow_clone_to_new_tip(narrow_clone: Path) ->assert compare_branch == "origin/main"landed = _run(narrow_clone, "rev-parse", "--verify", "--quiet", "origin/main").stdout.strip()assert landed == new_tip+++def test_detached_narrow_clone_counts_from_the_pre_checkout_head(narrow_clone: Path, monkeypatch) -> None:+ """A narrow clone is detached with no local branch, so the updater runs ``checkout -B main+ origin/main``, which lands ON the target. Counting ``HEAD..origin/main`` afterwards read 0 and+ the update finished as "Already up to date!" with dependencies and migrations skipped."""+ from hermes_cli import update_cmd++ monkeypatch.setattr(update_cmd._m(), "PROJECT_ROOT", narrow_clone)+ monkeypatch.setattr("hermes_cli.source_check._github_compare_behind", lambda *a, **k: None)+ pinned = _run(narrow_clone, "rev-parse", "HEAD").stdout.strip()++ def plan():+ fetch_result, _ = update_cmd_check.fetch_compare_branch(git_cmd, narrow_clone, "main", ["--depth", "1"])+ assert fetch_result.returncode == 0, fetch_result.stderr+ return update_cmd._prepare_checkout_for_update(+ git_cmd, "main", update_cmd._current_branch_name(git_cmd, check=True), is_fork=False,+ assume_yes=True, gateway_mode=False, gw_input_fn=None, switch_branch=False,+ _windows_gateway_resume=None)++ # Pinned tag == origin/main: genuinely up to date.+ assert plan().commit_count == 0++ # Back to the narrow shape, then advance origin/main past the pinned tag.+ assert _run(narrow_clone, "checkout", "-q", "--detach", "v1").returncode == 0+ assert _run(narrow_clone, "branch", "-q", "-D", "main").returncode == 0+ adv = narrow_clone.parent / "advance"+ assert _run(narrow_clone.parent, "clone", "-q", f"file://{narrow_clone.parent / 'origin.git'}", str(adv)).returncode == 0+ assert _run(adv, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "--allow-empty", "-m", "b").returncode == 0+ assert _run(adv, "push", "-q", "origin", "main").returncode == 0++ behind = plan()+ assert behind.commit_count != 0 # -1 here: shallow, and the compare API is stubbed out+ # _pull_updates' did-HEAD-move guard compares against this, not the already-moved HEAD.+ assert behind.pre_sync_sha == pinned