Skip to content

The agent that grows with you

fix(worktree): hermes -w branches from the remote tip on narrow clones

kshitijk4poor71b1a6fa97b4
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index ee1e85933de..aa34f9b6805 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -216,6 +216,7 @@ def _resolve_worktree_base(repo_root: str, fetch_timeout: float = 5,
remote-tracking ref is used (the pre-push stale-base gate backstops genuine staleness).
"""
from hermes_cli._subprocess_compat import noninteractive_git_env
+ from hermes_cli.update_cmd_check import tracking_refspec
def _run(args, timeout: float = 20):
return _git(args, repo_root, timeout=timeout, stdin=subprocess.DEVNULL, env=noninteractive_git_env())
@@ -244,7 +245,7 @@ def _resolve_worktree_base(repo_root: str, fetch_timeout: float = 5,
if age is not None and age < freshness_window and _ref_exists(ref):
return ref, f"{ref} (fetched {int(age)}s ago)"
try:
- fetched = _run(["fetch", remote, branch], timeout=fetch_timeout)
+ fetched = _run(["fetch", remote, tracking_refspec(remote, branch)], timeout=fetch_timeout)
if fetched.returncode == 0:
return ref, f"{ref} (fetched)"
reason = "fetch failed"
diff --git a/tests/hermes_cli/test_worktree_sync_base.py b/tests/hermes_cli/test_worktree_sync_base.py
index 64505b52c99..dd525b36d15 100644
--- a/tests/hermes_cli/test_worktree_sync_base.py
+++ b/tests/hermes_cli/test_worktree_sync_base.py
@@ -87,6 +87,21 @@ class TestResolveWorktreeBase:
assert resolved == remote_head
assert resolved != stale_local_head
+ def test_resolves_remote_tip_on_tag_pinned_narrow_clone(self, remote_and_clone, tmp_path):
+ """`--single-branch --branch <tag>` maps remote.origin.fetch to the tag only, so a
+ by-name fetch writes FETCH_HEAD and never creates origin/main (#125686)."""
+ clone, remote_head, _ = remote_and_clone
+ _run(["git", "tag", "v0"], clone) # the clone's stale HEAD; origin/main is past it
+ _run(["git", "push", "origin", "v0"], clone)
+ remote = _run(["git", "remote", "get-url", "origin"], clone).stdout.strip()
+ narrow = tmp_path / "narrow"
+ _run(["git", "clone", "-q", "--single-branch", "--branch", "v0", remote, str(narrow)], tmp_path)
+
+ base_ref, label = worktree_ops._resolve_worktree_base(str(narrow))
+
+ assert (base_ref, label) == ("origin/main", "origin/main (fetched)")
+ assert _run(["git", "rev-parse", base_ref], narrow).stdout.strip() == remote_head
+
def test_falls_back_to_head_without_remote(self, tmp_path):
repo = tmp_path / "no-remote"
repo.mkdir()