Skip to content

The agent that grows with you

fix(desktop): no-op branch switch on non-repo project lanes

Inspired-atmdb1d8983e500
diff --git a/apps/desktop/electron/git-worktree-ops.test.ts b/apps/desktop/electron/git-worktree-ops.test.ts
index 43af70e83dc..d3cf6a19e9a 100644
--- a/apps/desktop/electron/git-worktree-ops.test.ts
+++ b/apps/desktop/electron/git-worktree-ops.test.ts
@@ -435,3 +435,38 @@ test('addWorktree: a remote default branch gets its own worktree, not a home swi
fs.rmSync(cloneDir, { recursive: true, force: true })
}
})
+
+test('switchBranch: non-repo dir short-circuits instead of throwing', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-sw-'))
+
+ try {
+ // A plain folder pinned as a project (no .git): its lane label is the
+ // folder basename, not a branch — switching must no-op, not error, so
+ // callers like "+" new session can proceed with a plain session.
+ const result = await switchBranch(dir, '国创大赛', 'git')
+
+ assert.deepEqual(result, { branch: null })
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+})
+
+test('switchBranch: repo dir still validates the branch name and switches', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-sw-'))
+
+ try {
+ execFileSync('git', ['init', '-b', 'main'], { cwd: dir })
+ execFileSync('git', ['config', 'user.email', '[email protected]'], { cwd: dir })
+ execFileSync('git', ['config', 'user.name', 'test'], { cwd: dir })
+ execFileSync('git', ['commit', '--allow-empty', '-m', 'root'], { cwd: dir })
+
+ // Existing behaviour preserved: an illegal branch name still errors.
+ await assert.rejects(() => switchBranch(dir, '///', 'git'), /Branch name is required/)
+
+ // And switching to a real branch still works.
+ const result = await switchBranch(dir, 'main', 'git')
+ assert.deepEqual(result, { branch: 'main' })
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/git-worktree-ops.ts b/apps/desktop/electron/git-worktree-ops.ts
index 2ff43862c32..384e8c3fbe3 100644
--- a/apps/desktop/electron/git-worktree-ops.ts
+++ b/apps/desktop/electron/git-worktree-ops.ts
@@ -437,6 +437,25 @@ async function listBranches(repoPath, gitBin) {
async function switchBranch(repoPath, branch, gitBin) {
const resolved = resolveRequestedPathForIpc(repoPath, { purpose: 'Branch switch' })
+
+ // Sidebar lanes exist for plain folders too (non-repo explicit projects),
+ // and their lane label is the folder basename — not a branch. `git switch`
+ // there is meaningless, and sanitizing that label would throw a misleading
+ // "Branch name is required." — so short-circuit for non-repo roots and let
+ // callers (e.g. "+" new session on the project lane) proceed with a plain
+ // session instead of aborting.
+ let inside = 'false'
+
+ try {
+ inside = (await runGit(gitBin, ['rev-parse', '--is-inside-work-tree'], resolved)).trim()
+ } catch {
+ // Not a git repo (or git unavailable): fall through to the short-circuit.
+ }
+
+ if (inside !== 'true') {
+ return { branch: null }
+ }
+
const target = sanitizeBranch(branch)
if (!target) {