Skip to content

The agent that grows with you

fix(worktree): convert an origin/ branch on a tag-pinned narrow clone

Konstantin Khlopkovd31d9b3724b6
diff --git a/hermes_cli/web_git.py b/hermes_cli/web_git.py
index 61dc9b4a660..ad64d244578 100644
--- a/hermes_cli/web_git.py
+++ b/hermes_cli/web_git.py
@@ -546,6 +546,27 @@ 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).
+ """
+ _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
+
+
def _slugify(name: str) -> str:
slug = re.sub(r"[^a-z0-9]+", "-", str(name or "").strip().lower())
slug = re.sub(r"^-+|-+$", "", slug)[:40].rstrip("-")
@@ -598,21 +619,42 @@ def _worktree_for_existing(root: str, raw_name: str) -> dict:
if not requested:
raise RuntimeError("Branch name is required.")
# "origin/feature" is a remote-tracking ref, not a branch git can check out — `git worktree add <dir>
- # origin/feature` detaches HEAD. Create a local branch with the same short name that tracks the remote
+ # origin/feature` detaches HEAD. Create a local branch of the same short name that tracks the remote
# ref, like `git switch feature` does for a branch on exactly one remote. (Parity with the Electron op;
# a remote gateway serves this mirror, so the desktop's convert-a-branch flow must behave identically.
# #81724)
remote = _remote_of_ref(root, requested)
+ 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.
+ 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
existing = requested.split("/", 1)[1] if remote else requested
if not remote and existing == _default_branch(root):
_git_ok(root, ["switch", existing])
return {"path": root, "branch": existing, "repoRoot": root}
target = _unique_dir(os.path.join(root, ".worktrees", _slugify(existing)))
if remote:
- # Best-effort freshness; on failure (offline, branch gone) the last known ref is still
- # there to branch from.
- _git(root, ["fetch", remote, existing])
- _git_ok(root, ["worktree", "add", "--track", "-b", existing, target, requested])
+ 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
+ ):
+ _git_ok(root, ["worktree", "add", "-b", existing, target, ref])
+ _git(root, ["branch", f"--set-upstream-to={ref}", existing])
else:
_git_ok(root, ["worktree", "add", target, existing])
return {"path": target, "branch": existing, "repoRoot": root}
diff --git a/tests/hermes_cli/test_web_server_git.py b/tests/hermes_cli/test_web_server_git.py
index 9ff7343ff65..043df758ec2 100644
--- a/tests/hermes_cli/test_web_server_git.py
+++ b/tests/hermes_cli/test_web_server_git.py
@@ -313,3 +313,82 @@ def test_worktree_add_from_origin_base_on_tag_pinned_clone(tmp_path):
cwd=clone, capture_output=True, text=True,
)
assert upstream.returncode != 0
+
+
+def _narrow_clone(tmp_path, *, seed_tracking_ref=False):
+ """A tag-pinned narrow clone (bare origin + --single-branch --branch <tag>) whose
+ remote.origin.fetch maps only the tag — the shape older installers made."""
+ origin = tmp_path / "origin.git"
+ origin.mkdir()
+
+ def og(*args):
+ subprocess.run(["git", *args], cwd=origin, check=True, capture_output=True, text=True)
+
+ og("init", "-q", "-b", "main", "--bare")
+ work = tmp_path / "worktree-src"
+ work.mkdir()
+
+ def wg(*args):
+ subprocess.run(["git", *args], cwd=work, check=True, capture_output=True, text=True)
+
+ wg("init", "-q", "-b", "main")
+ wg("-c", "[email protected]", "-c", "user.name=Test", "commit", "-q", "--allow-empty", "-m", "c1")
+ wg("tag", "v0")
+ wg("branch", "feature")
+ wg("-c", "[email protected]", "-c", "user.name=Test", "commit", "-q", "--allow-empty", "-m", "c2")
+ tip = subprocess.run(["git", "rev-parse", "feature"], cwd=work, check=True,
+ capture_output=True, text=True).stdout.strip()
+ wg("push", "-q", str(origin), "main", "feature", "v0")
+
+ clone = tmp_path / "clone"
+ subprocess.run(["git", "clone", "-q", "--single-branch", "--branch", "v0", str(origin), str(clone)],
+ check=True, capture_output=True, text=True)
+ if seed_tracking_ref:
+ subprocess.run(["git", "fetch", "-q", "origin",
+ "+refs/heads/feature:refs/remotes/origin/feature"],
+ cwd=clone, check=True, capture_output=True, text=True)
+ return clone, tip
+
+
+def test_worktree_add_existing_branch_materializes_missing_tracking_ref(client, tmp_path):
+ """On a tag-pinned narrow clone the tracking ref never exists, so `origin/feature` used to
+ be read as a local branch name and worktree creation died with `invalid reference`."""
+ clone, tip = _narrow_clone(tmp_path)
+
+ added = client.post(
+ "/api/git/worktree/add",
+ json={"path": str(clone), "existingBranch": "origin/feature"},
+ ).json()
+
+ assert added["branch"] == "feature"
+ head = subprocess.run(
+ ["git", "rev-parse", "HEAD"], cwd=added["path"], check=True, capture_output=True, text=True,
+ ).stdout.strip()
+ assert head == tip # branched from the remote feature, not the tag
+ upstream = subprocess.run(
+ ["git", "rev-parse", "--abbrev-ref", "feature@{upstream}"],
+ cwd=added["path"], capture_output=True, text=True,
+ ).stdout.strip()
+ assert upstream == "origin/feature"
+
+
+def test_worktree_add_existing_branch_tracks_when_refspec_cannot_reverse_map(client, tmp_path):
+ """With the tracking ref present, `worktree add --track` still cannot wire upstream on a
+ narrow clone (remote.origin.fetch maps only the tag); the mirror must set it explicitly."""
+ clone, tip = _narrow_clone(tmp_path, seed_tracking_ref=True)
+
+ added = client.post(
+ "/api/git/worktree/add",
+ json={"path": str(clone), "existingBranch": "origin/feature"},
+ ).json()
+
+ assert added["branch"] == "feature"
+ head = subprocess.run(
+ ["git", "rev-parse", "HEAD"], cwd=added["path"], check=True, capture_output=True, text=True,
+ ).stdout.strip()
+ assert head == tip
+ upstream = subprocess.run(
+ ["git", "rev-parse", "--abbrev-ref", "feature@{upstream}"],
+ cwd=added["path"], capture_output=True, text=True,
+ ).stdout.strip()
+ assert upstream == "origin/feature"