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

Non-repo explicit projects (plain folders) get a main-checkout lane whose
label is the folder basename, not a branch. Clicking "+" (new session) on
such a lane calls switchBranchInRepo -> switchBranch, which sanitizes the
basename to "" and throws "Branch name is required.", aborting the
session creation. Short-circuit switchBranch for roots that are not git
work trees so callers proceed with a plain session.

Fixes #83028
This commit is contained in:
Inspired-atm
2026-08-10 16:09:27 +08:00
committed by Teknium
parent d40dda7db6
commit db1d8983e5
2 changed files with 54 additions and 0 deletions
@@ -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', 't@example.com'], { 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 })
}
})
+19
View File
@@ -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) {