Skip to content

The agent that grows with you

fix(update): say how many commits the diverged reset drops and where they went

brooklyn!db1ff59937e4
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index d16623930ee..2e8db85a470 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -776,7 +776,7 @@ def _repair_current_checkout(
def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha) -> None:
"""Fast-forward failed: merge on a custom branch (local commits survive) or reset --hard on the
- same branch (rescue ref first when histories share no ancestor). ``sys.exit(1)`` on failure."""
+ same branch after parking the old HEAD behind a rescue ref. ``sys.exit(1)`` on failure."""
# A custom branch (local commits atop origin/<branch>) also can't ff, and reset --hard
# would discard that work: merge instead, stop on conflict.
_cur_branch = (_git_run(git_cmd, ["branch", "--show-current"]).stdout or "").strip()
@@ -796,7 +796,7 @@ def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha) -> None:
# Same branch: the reset below is right either way, but the two causes of divergence here
# are indistinguishable from the checkout alone. An upstream force-push/rebase loses
# nothing; local commits on this branch lose everything, and the reflog is the only way
- # back — a 90-day expiry the user has to know to reach for, in a directory Hermes updates
+ # back — an expiring log the user has to know to reach for, in a directory Hermes updates
# unattended. So park pre_pull_sha behind a rescue ref for BOTH, orphan divergence (no
# common ancestor: corrupted HEAD, re-init) included.
merge_base_result = _git_run(git_cmd, ["merge-base", "HEAD", f"origin/{branch}"])
@@ -818,7 +818,10 @@ def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha) -> None:
f"{head}backed up current HEAD to {rescue_ref} before resetting. "
f"This backup expires after {_ORPHAN_RESCUE_REF_MAX_AGE_DAYS} days.")
if has_common_ancestor:
- print(f" Recover local commits with: git log {rescue_ref}")
+ dropped = (_git_run(
+ git_cmd, ["rev-list", "--count", f"origin/{branch}..{pre_pull_sha}"]).stdout or "").strip()
+ print(f" {dropped or 'Some'} commit(s) not on origin/{branch} leave the branch; "
+ f"list them with: git log origin/{branch}..{rescue_ref}")
else:
# update-ref failure is intentionally non-fatal, but never claim a backup exists.
print(
@@ -869,7 +872,7 @@ def _pull_updates(
git_cmd, branch, auto_stash_ref, *, prompt_for_restore, gw_input_fn, discard_local_changes,
keep_stash):
"""Fast-forward onto ``origin/<branch>`` and settle the autostash. Divergence by shape:
- custom branch -> merge, same branch -> reset, orphan history -> rescue ref first; a
+ custom branch -> merge, same branch -> rescue ref then reset; a
post-pull syntax error in a critical file rolls back. Exits on failure; returns pre-pull SHA."""
update_succeeded = False
# Pre-pull SHA for auto-rollback (stray conflict markers once bricked every updater).
diff --git a/hermes_cli/update_cmd_git.py b/hermes_cli/update_cmd_git.py
index 1417195fdbb..fa1f0f9e194 100644
--- a/hermes_cli/update_cmd_git.py
+++ b/hermes_cli/update_cmd_git.py
@@ -64,7 +64,7 @@ def _prune_orphan_rescue_refs(
list_result = _git_run(
git_cmd, ["for-each-ref", "--format=%(refname)", "--sort=refname", f"{prefix}*"], cwd)
if list_result.returncode != 0:
- return
+ continue
refs = [line.strip() for line in list_result.stdout.splitlines() if line.strip()]
stale |= set(refs[:-keep] if keep > 0 else refs)
if max_age_days > 0:
diff --git a/tests/hermes_cli/test_update_diverged_rescue_ref.py b/tests/hermes_cli/test_update_diverged_rescue_ref.py
index 470ea77206e..2611d00e522 100644
--- a/tests/hermes_cli/test_update_diverged_rescue_ref.py
+++ b/tests/hermes_cli/test_update_diverged_rescue_ref.py
@@ -1,11 +1,12 @@
-"""`hermes update` must not drop local commits without leaving a way back.
+"""`hermes update` must not drop local commits without leaving a named way back.
When the checkout sits on the update's target branch and its history has
diverged, the update resets hard to ``origin/<branch>``. Divergence there has
two indistinguishable causes: an upstream force-push (nothing local is lost)
-and local commits on that branch (everything is). The rescue ref that makes
-the second case recoverable was written only for orphan divergence — a
-disjoint history — which is the rarer of the two.
+and local commits on that branch (everything is). The update must park the old
+HEAD under ``refs/hermes-update-backups/`` first and tell the user the ref name.
+The installer update paths are covered in
+``tests/scripts/install/test_install_diverged_rescue_ref.py``.
"""
from __future__ import annotations
@@ -34,7 +35,7 @@ def _commit(repo, name, text):
@pytest.fixture()
-def diverged_checkout(tmp_path, monkeypatch):
+def diverged_checkout(tmp_path):
"""A checkout on ``main`` carrying a local commit its ``origin/main`` does not have."""
upstream = tmp_path / "upstream"
upstream.mkdir()
@@ -46,8 +47,6 @@ def diverged_checkout(tmp_path, monkeypatch):
_git(tmp_path, "clone", "-q", str(upstream), str(checkout))
_git(checkout, "reset", "-q", "--hard", "HEAD~1") # back to the shared commit
local_sha = _commit(checkout, "local-fix.txt", "local\n") # diverges from origin/main
-
- monkeypatch.setattr(update_cmd._m(), "PROJECT_ROOT", checkout)
return checkout, local_sha
@@ -57,29 +56,29 @@ def _rescue_refs(checkout):
return dict(line.split() for line in out.splitlines() if line.strip())
-def test_reset_leaves_a_rescue_ref_for_the_discarded_commits(diverged_checkout):
- """The reset is fine; losing the only pointer to the local work is not."""
- checkout, local_sha = diverged_checkout
-
- update_cmd._reconcile_diverged_checkout(GIT, "main", local_sha)
-
+def _assert_reset_kept_local_commit(checkout, local_sha, output):
head = _git(checkout, "rev-parse", "HEAD").stdout.strip()
origin = _git(checkout, "rev-parse", "origin/main").stdout.strip()
assert head == origin, "the reset itself must still happen"
-
- refs = _rescue_refs(checkout)
- assert local_sha in refs.values(), (
- "the discarded commit must stay reachable through a rescue ref; without one "
- f"it is recoverable only from the reflog. refs found: {refs}")
+ refs = [ref for ref, sha in _rescue_refs(checkout).items() if sha == local_sha]
+ assert len(refs) == 1, f"the dropped commit needs exactly one rescue ref: {_rescue_refs(checkout)}"
+ assert refs[0].startswith("refs/hermes-update-backups/diverged-main-")
+ assert refs[0] in output, "the user must be told where the commits went"
+ dropped = _git(checkout, "log", "--format=%H", f"origin/main..{refs[0]}").stdout.split()
+ assert dropped == [local_sha]
-def test_the_rescue_ref_carries_the_whole_discarded_history(diverged_checkout):
- """A ref on the tip is enough: every dropped commit is reachable from it."""
+def test_hermes_update_keeps_local_commit_behind_a_rescue_ref(
+ diverged_checkout, monkeypatch, capsys):
+ """The real apply path: ff-only fails, the reconcile resets, the local commit stays reachable."""
checkout, local_sha = diverged_checkout
+ monkeypatch.setattr(update_cmd._m(), "PROJECT_ROOT", checkout)
+
+ update_cmd._pull_updates(
+ GIT, "main", None, prompt_for_restore=False, gw_input_fn=None,
+ discard_local_changes=False, keep_stash=False)
- update_cmd._reconcile_diverged_checkout(GIT, "main", local_sha)
+ out = capsys.readouterr().out
+ _assert_reset_kept_local_commit(checkout, local_sha, out)
+ assert "1 commit(s) not on origin/main" in out
- ref = next(r for r, sha in _rescue_refs(checkout).items() if sha == local_sha)
- listed = _git(checkout, "log", "--format=%H", ref).stdout.split()
- assert local_sha in listed
- assert len(listed) >= 2, "the shared base must remain reachable from the rescue ref too"