Fix worktree→local branch switch causing 'already used by worktree' error

When in a secondary worktree and selecting a branch checked out in the
main worktree (e.g. main), the code skipped the worktree redirect
(because worktreePath === project.cwd) and tried git checkout, which
git rejected.

Now any branch with a worktreePath is handled as a redirect — main
worktree redirects to local (worktreePath=null), secondary worktrees
redirect to their path.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 4.6 committed 2026-02-11 15:36:11 -08:00
1 parent 5b7af7b9f6
commit d6ecf15805
1 file changed
+7 -6
+7 -6
View File
@@ -128,13 +128,14 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
return;
}
// If the branch already lives in a secondary worktree, point the thread
// there instead of trying to checkout (which git would reject).
// Ignore the main worktree (project cwd) — that's just a normal local checkout.
const isSecondaryWorktree = branch.worktreePath && branch.worktreePath !== activeProject?.cwd;
if (isSecondaryWorktree) {
// If the branch already lives in a worktree, redirect there instead of
// trying to checkout (which git would reject with "already used by worktree").
if (branch.worktreePath) {
const isMainWorktree = branch.worktreePath === activeProject?.cwd;
setThreadError(null);
setThreadBranch(branch.name, branch.worktreePath);
// Main worktree → switch back to local (project cwd, worktreePath=null).
// Secondary worktree → point the thread at that worktree path.
setThreadBranch(branch.name, isMainWorktree ? null : branch.worktreePath);
setIsBranchMenuOpen(false);
return;
}