Skip to content

The agent that grows with you

feat(update): --switch-branch opts an unmerged branch out of the in-place merge

Willian Santos4fad27a10150
diff --git a/hermes_cli/subcommands/update.py b/hermes_cli/subcommands/update.py
index 917cf9ff30c..71e28166dcc 100644
--- a/hermes_cli/subcommands/update.py
+++ b/hermes_cli/subcommands/update.py
@@ -84,6 +84,21 @@ def build_update_parser(subparsers, *, cmd_update: Callable) -> None:
"uncommitted changes)."
),
)
+ update_parser.add_argument(
+ "--switch-branch",
+ action="store_true",
+ default=False,
+ help=(
+ "When the checkout sits on a branch carrying unmerged commits, "
+ "switch to the update target and update THERE instead of merging "
+ "the target into the branch in place. The branch is left exactly "
+ "as it was — no merge commit is written into its history. Use on "
+ "long-lived feature branches where an update-driven merge commit "
+ "would pollute the branch; the default in-place behaviour suits "
+ "branches that track the target with a small patch set. Still "
+ "refuses to touch a dirty tree."
+ ),
+ )
update_parser.add_argument(
"--force",
action="store_true",
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 02f446114be..3b5acaa514b 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -5169,6 +5169,11 @@ def _cmd_update_impl(args, gateway_mode: bool):
# stash. Only applies when an update actually landed; abort/no-op paths
# still restore, since the tree they restore onto is unchanged.
keep_stash = bool(getattr(args, "keep_stash", False))
+ # --switch-branch: on a branch carrying unmerged commits, prefer switching
+ # to the update target over an in-place merge, so the branch's history is
+ # never written to by an update (#89507 review feedback). Only meaningful
+ # when updates.parked_branch_strategy is "update_in_place".
+ switch_branch = bool(getattr(args, "switch_branch", False))
# Whether this update is running without a human at the keyboard.
# Interactive terminal updates always stash-and-ask (unchanged behavior);
@@ -5573,7 +5578,7 @@ def _cmd_update_impl(args, gateway_mode: bool):
logger.debug(
"Could not read updates.parked_branch_strategy: %s", exc
)
- if _in_place_configured:
+ if _in_place_configured and not switch_branch:
# The merge source must exist upstream; --branch typos
# previously surfaced through the checkout failing, which
# does not run on this path.
diff --git a/tests/hermes_cli/test_update_parked_branch_guard.py b/tests/hermes_cli/test_update_parked_branch_guard.py
index fc4a54df4a2..2508606b3a1 100644
--- a/tests/hermes_cli/test_update_parked_branch_guard.py
+++ b/tests/hermes_cli/test_update_parked_branch_guard.py
@@ -395,6 +395,107 @@ def test_update_updates_unmerged_branch_in_place_when_configured(
assert "feature work" in _git(repo_pair, "log", "--oneline").stdout
+def test_switch_branch_flag_overrides_in_place_strategy(
+ repo_pair, monkeypatch, capsys
+):
+ """--switch-branch overrides updates.parked_branch_strategy:
+ update_in_place for one run: the unmerged branch is LEFT ALONE and the
+ update runs on the target instead.
+
+ A long-lived feature branch does not want an update-driven merge commit
+ in its history (#89507 review). The branch tip must be byte-identical
+ afterwards, while the checkout ends up on the updated target.
+ """
+ import hermes_cli.config as hermes_config
+
+ monkeypatch.setattr(
+ hermes_config,
+ "load_config",
+ lambda: {"updates": {"parked_branch_strategy": "update_in_place"}},
+ )
+ (repo_pair / "feature.txt").write_text("unmerged work\n")
+ _git(repo_pair, "add", "feature.txt")
+ _git(repo_pair, "commit", "-qm", "feature work")
+ branch_tip_before = _git(
+ repo_pair, "rev-parse", "old-feature"
+ ).stdout.strip()
+ _patch_update_flow(monkeypatch, repo_pair)
+
+ class _StopFlow(Exception):
+ pass
+
+ monkeypatch.setattr(
+ hermes_main,
+ "_abort_dependency_sync_if_self_locked",
+ lambda *a, **k: (_ for _ in ()).throw(_StopFlow()),
+ )
+ args = SimpleNamespace(
+ branch=None, yes=False, force=False, force_venv=False,
+ switch_branch=True,
+ )
+
+ with pytest.raises(_StopFlow):
+ hermes_main.cmd_update(args)
+
+ out = capsys.readouterr().out
+ assert "1 commit(s) not merged into origin/main" in out
+ assert "updating it in place" not in out
+ assert "CODE UPDATE SKIPPED" not in out
+ # Checkout moved to the target and picked up its code...
+ assert (
+ _git(repo_pair, "rev-parse", "--abbrev-ref", "HEAD").stdout.strip()
+ == "main"
+ )
+ assert (repo_pair / "b.txt").exists()
+ # ...and the feature branch was not written to at all.
+ assert (
+ _git(repo_pair, "rev-parse", "old-feature").stdout.strip()
+ == branch_tip_before
+ )
+
+
+def test_unmerged_branch_still_updates_in_place_without_the_flag(
+ repo_pair, monkeypatch, capsys
+):
+ """--switch-branch is opt-in: with the in-place strategy configured and
+ no flag, the update stays in place."""
+ import hermes_cli.config as hermes_config
+
+ monkeypatch.setattr(
+ hermes_config,
+ "load_config",
+ lambda: {"updates": {"parked_branch_strategy": "update_in_place"}},
+ )
+ (repo_pair / "feature.txt").write_text("unmerged work\n")
+ _git(repo_pair, "add", "feature.txt")
+ _git(repo_pair, "commit", "-qm", "feature work")
+ _patch_update_flow(monkeypatch, repo_pair)
+
+ class _StopFlow(Exception):
+ pass
+
+ monkeypatch.setattr(
+ hermes_main,
+ "_abort_dependency_sync_if_self_locked",
+ lambda *a, **k: (_ for _ in ()).throw(_StopFlow()),
+ )
+ args = SimpleNamespace(
+ branch=None, yes=False, force=False, force_venv=False,
+ switch_branch=False,
+ )
+
+ with pytest.raises(_StopFlow):
+ hermes_main.cmd_update(args)
+
+ out = capsys.readouterr().out
+ assert "updating it in place" in out
+ assert "--switch-branch" not in out
+ assert (
+ _git(repo_pair, "rev-parse", "--abbrev-ref", "HEAD").stdout.strip()
+ == "old-feature"
+ )
+
+
def test_update_auto_switches_clean_merged_parked_branch(
repo_pair, monkeypatch, capsys
):