Skip to content

The agent that grows with you

fix(worktree): fetch origin bases with a tracking refspec

686f6c616941423f4557
diff --git a/apps/desktop/electron/git-worktree-ops.test.ts b/apps/desktop/electron/git-worktree-ops.test.ts
index d3cf6a19e9a..20af7c7755b 100644
--- a/apps/desktop/electron/git-worktree-ops.test.ts
+++ b/apps/desktop/electron/git-worktree-ops.test.ts
@@ -322,6 +322,48 @@ test('addWorktree: base origin/main does not set up upstream tracking', async ()
}
})
+test('addWorktree: base origin/main resolves on a tag-pinned narrow clone', async () => {
+ // `--single-branch --branch <tag>` maps remote.origin.fetch to the tag only.
+ // A by-name `git fetch origin main` writes FETCH_HEAD and never origin/main.
+ const remoteDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-narrow-remote-'))
+ const cloneDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-narrow-clone-'))
+ const ident = ['-c', 'user.email=hermes@localhost', '-c', 'user.name=Hermes']
+
+ try {
+ execFileSync('git', ['init', '-q', '-b', 'main', remoteDir])
+ execFileSync('git', ['-C', remoteDir, ...ident, 'commit', '-q', '--allow-empty', '-m', 'init'])
+ execFileSync('git', ['-C', remoteDir, 'tag', 'v0'])
+ execFileSync('git', ['-C', remoteDir, ...ident, 'commit', '-q', '--allow-empty', '-m', 'tip'])
+ const tip = execFileSync('git', ['-C', remoteDir, 'rev-parse', 'HEAD']).toString().trim()
+
+ execFileSync('git', ['clone', '-q', '--single-branch', '--branch', 'v0', remoteDir, cloneDir])
+
+ const result = await addWorktree(
+ cloneDir,
+ { base: 'origin/main', branch: 'from-main', name: 'from-main' },
+ 'git'
+ )
+
+ const head = execFileSync('git', ['-C', result.path, 'rev-parse', 'HEAD']).toString().trim()
+
+ assert.equal(head, tip)
+ assert.equal(result.branch, 'from-main')
+
+ let hasUpstream = true
+
+ try {
+ execFileSync('git', ['-C', cloneDir, 'rev-parse', '--abbrev-ref', '--symbolic-full-name', 'from-main@{u}'])
+ } catch {
+ hasUpstream = false
+ }
+
+ assert.equal(hasUpstream, false)
+ } finally {
+ fs.rmSync(remoteDir, { recursive: true, force: true })
+ fs.rmSync(cloneDir, { recursive: true, force: true })
+ }
+})
+
// A pair of repos: a bare "remote" with `main` and the extra branches in
// `branches`, plus a clone of it. Returns both paths. The caller must remove
// them.
diff --git a/apps/desktop/electron/git-worktree-ops.ts b/apps/desktop/electron/git-worktree-ops.ts
index 4e51d5068f8..87825cd2a6b 100644
--- a/apps/desktop/electron/git-worktree-ops.ts
+++ b/apps/desktop/electron/git-worktree-ops.ts
@@ -307,7 +307,13 @@ async function addWorktree(repoPath, options, gitBin) {
const remoteBranch = base.slice('origin/'.length)
try {
- await runGit(gitBin, ['fetch', 'origin', remoteBranch], root)
+ // A by-name fetch writes only FETCH_HEAD on a tag-pinned narrow clone,
+ // so origin/<branch> never appears (#125686). Same refspec as hermes update.
+ await runGit(
+ gitBin,
+ ['fetch', 'origin', `+refs/heads/${remoteBranch}:refs/remotes/origin/${remoteBranch}`],
+ root
+ )
} catch {
// The fetch isn't mandatory, but it would be nice to do if possible.
// If it's not possible, just use the local ref of the remote branch.
diff --git a/hermes_cli/web_git.py b/hermes_cli/web_git.py
index 42365b201e0..61dc9b4a660 100644
--- a/hermes_cli/web_git.py
+++ b/hermes_cli/web_git.py
@@ -635,7 +635,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/"):
- _git(root, ["fetch", "origin", base[len("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/"):])])
# 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 5449733654a..9ff7343ff65 100644
--- a/tests/hermes_cli/test_web_server_git.py
+++ b/tests/hermes_cli/test_web_server_git.py
@@ -267,3 +267,49 @@ def test_worktree_add_from_origin_base_does_not_track(client, repo_with_remote):
cwd=repo_with_remote, capture_output=True, text=True,
)
assert probe.returncode != 0
+
+
+def test_worktree_add_from_origin_base_on_tag_pinned_clone(tmp_path):
+ """A tag-only fetch refspec must still materialize origin/main (#125686).
+
+ ``git clone --single-branch --branch <tag>`` maps ``remote.origin.fetch`` to
+ the tag. ``git fetch origin main`` then writes FETCH_HEAD and leaves
+ ``origin/main`` missing, so ``worktree add`` dies with invalid reference.
+ """
+ origin = tmp_path / "origin"
+ origin.mkdir()
+
+ def git(*args, cwd):
+ subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True)
+
+ git("init", "-q", "-b", "main", cwd=origin)
+ git("-c", "[email protected]", "-c", "user.name=Test", "commit", "-q", "--allow-empty", "-m", "init", cwd=origin)
+ git("tag", "v0", cwd=origin)
+ git("-c", "[email protected]", "-c", "user.name=Test", "commit", "-q", "--allow-empty", "-m", "tip", cwd=origin)
+ tip = subprocess.run(
+ ["git", "rev-parse", "HEAD"], cwd=origin, check=True, capture_output=True, text=True,
+ ).stdout.strip()
+
+ clone = tmp_path / "clone"
+ subprocess.run(
+ ["git", "clone", "-q", "--single-branch", "--branch", "v0", str(origin), str(clone)],
+ check=True, capture_output=True, text=True,
+ )
+ missing = subprocess.run(
+ ["git", "show-ref", "--verify", "--quiet", "refs/remotes/origin/main"],
+ cwd=clone, capture_output=True,
+ )
+ assert missing.returncode != 0
+
+ from hermes_cli.web_git import worktree_add
+
+ added = worktree_add(str(clone), {"base": "origin/main", "name": "x"})
+ 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", f"{added['branch']}@{{upstream}}"],
+ cwd=clone, capture_output=True, text=True,
+ )
+ assert upstream.returncode != 0