Skip to content

The agent that grows with you

fix(worktree): register a remote branch only after the fetch finds it

kshitijk4poorbe1181c53347
diff --git a/hermes_cli/web_git.py b/hermes_cli/web_git.py
index ad64d244578..b5c4ddf8a41 100644
--- a/hermes_cli/web_git.py
+++ b/hermes_cli/web_git.py
@@ -546,25 +546,15 @@ def _sanitize_branch(name: str) -> str:
return value
-def _materialize_tracking_ref(root: str, remote: str, branch: str) -> bool:
- """Ensure ``refs/remotes/<remote>/<branch>`` exists and ``--track`` can wire it.
-
- A tag-pinned narrow clone maps only the tag in ``remote.<remote>.fetch``, so the tracking
- refs of every other branch are missing and a by-name fetch writes FETCH_HEAD without
- creating them. An explicit refspec fetch creates the ref, but ``worktree add --track``
- and ``branch --set-upstream-to`` still need the *configured* refspec to reverse-map the
- ref back to a remote branch — so register the branch on the remote additively
- (``set-branches --add`` keeps any existing refspecs) and fetch through it. The ``+``
- matches what `hermes update` uses because on a depth-1 clone the new tip need not
- descend from the old one. Returns whether the tracking ref is available afterwards
- (fetch failures fall back to the last known ref, or none).
+def _fetch_tracking_ref(root: str, remote: str, branch: str) -> bool:
+ """Fetch ``<remote>/<branch>`` by explicit refspec; True when the remote has the branch.
+
+ A tag-pinned narrow clone maps only the tag in ``remote.<remote>.fetch``, so a by-name
+ fetch writes FETCH_HEAD without creating the tracking ref (#125686).
"""
- _git(root, ["remote", "set-branches", "--add", remote, branch])
- _git(
- root,
- ["fetch", remote, f"+refs/heads/{branch}:refs/remotes/{remote}/{branch}"],
- )
- return _git(root, ["show-ref", "--verify", "--quiet", f"refs/remotes/{remote}/{branch}"])[0] == 0
+ from hermes_cli.update_cmd_check import tracking_refspec
+
+ return _git(root, ["fetch", remote, tracking_refspec(remote, branch)])[0] == 0
def _slugify(name: str) -> str:
@@ -624,16 +614,16 @@ def _worktree_for_existing(root: str, raw_name: str) -> dict:
# a remote gateway serves this mirror, so the desktop's convert-a-branch flow must behave identically.
# #81724)
remote = _remote_of_ref(root, requested)
+ fetched = False
if not remote and "/" in requested:
- # A tag-pinned narrow clone maps only the tag in remote.<r>.fetch, so the tracking ref of
- # every other branch is missing and the ref-based reading above misreads "origin/feature"
- # as a local branch. Register the branch on the remote (additive — the existing refspec
- # stays) and fetch it; when the remote does not carry the branch either, keep the
- # local-branch reading so the error stays the familiar one.
+ # A tag-pinned narrow clone has no tracking ref for any branch, so the ref-based reading
+ # above misreads "origin/feature" as a local branch. When the remote carries the branch,
+ # fetching it creates the ref; otherwise keep the local-branch reading and its error.
maybe_remote, maybe_branch = requested.split("/", 1)
- if _git_line(root, ["remote", "get-url", maybe_remote]):
- if _materialize_tracking_ref(root, maybe_remote, maybe_branch):
- remote = maybe_remote
+ if _git_line(root, ["remote", "get-url", maybe_remote]) and _fetch_tracking_ref(
+ root, maybe_remote, maybe_branch
+ ):
+ remote, fetched = maybe_remote, True
existing = requested.split("/", 1)[1] if remote else requested
if not remote and existing == _default_branch(root):
_git_ok(root, ["switch", existing])
@@ -641,20 +631,18 @@ def _worktree_for_existing(root: str, raw_name: str) -> dict:
target = _unique_dir(os.path.join(root, ".worktrees", _slugify(existing)))
if remote:
ref = f"{remote}/{existing}"
- # `worktree add --track` (and `branch --set-upstream-to`) derive upstream from the
- # configured remote refspecs; on a tag-pinned narrow clone they map only the tag, so the
- # wiring dies with "starting point is not a branch" even with a fresh tracking ref. Make
- # sure the branch is registered on the remote (additive; no-op on a normal clone where the
- # wildcard refspec already covers it), refresh the ref (best-effort: on failure the last
- # known one is still there to branch from), then wire upstream explicitly — `--track`
- # silently skips the wiring instead of failing when the reverse-map is impossible.
- _materialize_tracking_ref(root, remote, existing)
- if (
- _git(root, ["worktree", "add", "--track", "-b", existing, target, ref])[0]
- != 0
- ):
+ # Best-effort freshness: on failure (offline, branch gone) the last known ref is still
+ # there to branch from.
+ fetched = fetched or _fetch_tracking_ref(root, remote, existing)
+ if _git(root, ["worktree", "add", "--track", "-b", existing, target, ref])[0] != 0:
+ # `--track` needs remote.<remote>.fetch to map the ref back to a remote branch; a
+ # narrow clone maps only its tag. Branch untracked, then register the branch and
+ # wire upstream, but only for a branch the fetch just proved exists: a configured
+ # refspec whose source is gone makes every later plain `git fetch` fail.
_git_ok(root, ["worktree", "add", "-b", existing, target, ref])
- _git(root, ["branch", f"--set-upstream-to={ref}", existing])
+ if fetched:
+ _git(root, ["remote", "set-branches", "--add", remote, existing])
+ _git(root, ["branch", f"--set-upstream-to={ref}", existing])
else:
_git_ok(root, ["worktree", "add", target, existing])
return {"path": target, "branch": existing, "repoRoot": root}
@@ -677,11 +665,11 @@ def worktree_add(cwd: str, options: dict) -> dict:
# (offline / no remote) are ignored — git uses the local ref or raises a clear error
# below if it is entirely missing.
if base.startswith("origin/"):
- # A by-name fetch writes only FETCH_HEAD when remote.origin.fetch maps a
- # tag (#125686). The updater's tracking refspec still creates origin/<branch>.
- from hermes_cli.update_cmd_check import tracking_refspec
-
- _git(root, ["fetch", "origin", tracking_refspec("origin", base[len("origin/"):])])
+ remote_branch = base[len("origin/"):]
+ # `base` comes straight from the API, and inside a refspec a glob such as
+ # "origin/*" would fetch every branch: only fetch names the sanitizer leaves alone.
+ if remote_branch == _sanitize_branch(remote_branch):
+ _fetch_tracking_ref(root, "origin", remote_branch)
# Branching off a remote-tracking ref auto-wires upstream tracking; the user wants
# a standalone local branch (Electron-op parity).
args.append("--no-track")
diff --git a/tests/hermes_cli/test_web_server_git.py b/tests/hermes_cli/test_web_server_git.py
index 043df758ec2..58df45f6013 100644
--- a/tests/hermes_cli/test_web_server_git.py
+++ b/tests/hermes_cli/test_web_server_git.py
@@ -392,3 +392,60 @@ def test_worktree_add_existing_branch_tracks_when_refspec_cannot_reverse_map(cli
cwd=added["path"], capture_output=True, text=True,
).stdout.strip()
assert upstream == "origin/feature"
+
+
+def _fetch_config(clone):
+ return subprocess.run(
+ ["git", "config", "--get-all", "remote.origin.fetch"],
+ cwd=clone, check=True, capture_output=True, text=True,
+ ).stdout
+
+
+def _normal_clone(tmp_path):
+ """The narrow fixture's origin, cloned the normal way (wildcard fetch refspec)."""
+ _narrow_clone(tmp_path)
+ clone = tmp_path / "normal"
+ subprocess.run(["git", "clone", "-q", str(tmp_path / "origin.git"), str(clone)],
+ check=True, capture_output=True, text=True)
+ return clone
+
+
[email protected]("shape", ["narrow", "normal"])
+def test_worktree_add_missing_remote_branch_leaves_fetch_config_alone(client, tmp_path, shape):
+ """A configured refspec whose source is missing makes every later plain `git fetch`
+ fail, so a typo'd "origin/<branch>" must never be registered on the remote."""
+ clone = _narrow_clone(tmp_path)[0] if shape == "narrow" else _normal_clone(tmp_path)
+ before = _fetch_config(clone)
+
+ resp = client.post(
+ "/api/git/worktree/add", json={"path": str(clone), "existingBranch": "origin/typo"},
+ )
+
+ assert resp.status_code != 200
+ assert _fetch_config(clone) == before
+ subprocess.run(["git", "fetch", "-q"], cwd=clone, check=True, capture_output=True)
+
+
+def test_worktree_add_existing_branch_on_normal_clone_adds_no_fetch_refspec(client, tmp_path):
+ clone = _normal_clone(tmp_path)
+ before = _fetch_config(clone)
+
+ added = client.post(
+ "/api/git/worktree/add", json={"path": str(clone), "existingBranch": "origin/feature"},
+ ).json()
+
+ assert added["branch"] == "feature"
+ assert _fetch_config(clone) == before
+
+
+def test_worktree_add_glob_base_is_not_fetched(tmp_path):
+ """`base` comes from the API; inside a refspec "origin/*" would fetch every branch."""
+ clone, _ = _narrow_clone(tmp_path)
+ from hermes_cli.web_git import worktree_add
+
+ with pytest.raises(RuntimeError):
+ worktree_add(str(clone), {"base": "origin/*", "name": "glob"})
+
+ refs = subprocess.run(["git", "for-each-ref", "refs/remotes"], cwd=clone,
+ check=True, capture_output=True, text=True).stdout
+ assert refs == ""