Skip to content

The agent that grows with you

fix(worktree): refresh any valid branch name used as a base

kshitijk4poor77019b3b7d12
diff --git a/apps/desktop/electron/git-worktree-ops.test.ts b/apps/desktop/electron/git-worktree-ops.test.ts
index 885b381a69c..b86363d87aa 100644
--- a/apps/desktop/electron/git-worktree-ops.test.ts
+++ b/apps/desktop/electron/git-worktree-ops.test.ts
@@ -448,6 +448,25 @@ test('addWorktree: a local slash branch named like a remote branch stays local',
}
})
+test('addWorktree: a valid branch the sanitizer would rewrite is still refreshed as a base', async () => {
+ const { cloneDir, remoteDir } = seedRemoteAndClone('plus-base', ['fix+1'])
+ const ident = ['-c', 'user.email=hermes@localhost', '-c', 'user.name=Hermes']
+
+ try {
+ execFileSync('git', ['-C', remoteDir, 'checkout', '-q', 'fix+1'])
+ execFileSync('git', ['-C', remoteDir, ...ident, 'commit', '-q', '--allow-empty', '-m', 'moved after the clone'])
+ const moved = execFileSync('git', ['-C', remoteDir, 'rev-parse', 'HEAD']).toString().trim()
+
+ const result = await addWorktree(cloneDir, { base: 'origin/fix+1', branch: 'x', name: 'x' }, 'git')
+ const head = execFileSync('git', ['-C', result.path, 'rev-parse', 'HEAD']).toString().trim()
+
+ assert.equal(head, moved)
+ } finally {
+ fs.rmSync(remoteDir, { recursive: true, force: true })
+ fs.rmSync(cloneDir, { recursive: true, force: true })
+ }
+})
+
test('addWorktree: a glob base is not turned into a fetch of every branch', async () => {
const { cloneDir, remoteDir } = seedNarrowClone('narrow-glob')
diff --git a/apps/desktop/electron/git-worktree-ops.ts b/apps/desktop/electron/git-worktree-ops.ts
index 3fa3774a525..8d206e2f4e7 100644
--- a/apps/desktop/electron/git-worktree-ops.ts
+++ b/apps/desktop/electron/git-worktree-ops.ts
@@ -344,10 +344,10 @@ async function addWorktree(repoPath, options, gitBin) {
const remoteBranch = base.slice('origin/'.length)
// `base` comes straight from IPC, and inside a refspec a glob such as
- // "origin/*" would fetch every branch: only fetch names the sanitizer
- // leaves alone. The fetch is best effort; git uses the local ref or
- // raises a clear error below if it is entirely missing.
- if (remoteBranch === sanitizeBranch(remoteBranch)) {
+ // "origin/*" would fetch every branch: only fetch valid branch names.
+ // The fetch is best effort; git uses the local ref or raises a clear
+ // error below if it is entirely missing.
+ if (await gitOk(gitBin, ['check-ref-format', '--branch', remoteBranch], root)) {
await fetchTrackingRef(gitBin, root, 'origin', remoteBranch)
}
diff --git a/hermes_cli/web_git.py b/hermes_cli/web_git.py
index 8dbd9069102..bec611c7563 100644
--- a/hermes_cli/web_git.py
+++ b/hermes_cli/web_git.py
@@ -667,8 +667,8 @@ def worktree_add(cwd: str, options: dict) -> dict:
if base.startswith("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):
+ # "origin/*" would fetch every branch: only fetch valid branch names.
+ if _git(root, ["check-ref-format", "--branch", remote_branch])[0] == 0:
_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).
diff --git a/tests/hermes_cli/test_web_server_git.py b/tests/hermes_cli/test_web_server_git.py
index 3b48c1f56c5..e5ff1819181 100644
--- a/tests/hermes_cli/test_web_server_git.py
+++ b/tests/hermes_cli/test_web_server_git.py
@@ -399,3 +399,21 @@ def test_worktree_add_glob_base_is_not_fetched(tmp_path):
worktree_add(str(clone), {"base": "origin/*", "name": "glob"})
assert _out(clone, "for-each-ref", "refs/remotes") == ""
+
+
+def test_worktree_add_base_refreshes_valid_branch_names_the_sanitizer_would_rewrite(tmp_path):
+ """"fix+1" is a valid branch the sanitizer rewrites; its base must still be fetched."""
+ origin, _ = _seed_origin(tmp_path)
+ seed = tmp_path / "seed"
+ _git(seed, "branch", "fix+1")
+ _git(seed, "push", "-q", str(origin), "fix+1")
+ clone = tmp_path / "normal"
+ _git(tmp_path, "clone", "-q", str(origin), str(clone))
+ _git(seed, "-c", "[email protected]", "-c", "user.name=Test",
+ "commit", "-q", "--allow-empty", "-m", "moved after the clone")
+ _git(seed, "push", "-q", str(origin), "HEAD:fix+1")
+ from hermes_cli.web_git import worktree_add
+
+ added = worktree_add(str(clone), {"base": "origin/fix+1", "name": "x"})
+
+ assert _out(added["path"], "rev-parse", "HEAD") == _out(seed, "rev-parse", "HEAD")