authorgravatar for julius0216@outlook.comJulius Marminge <julius0216@outlook.com> 2026-02-11 15:36:11-08:00
committergravatar for julius0216@outlook.comJulius Marminge <julius0216@outlook.com> 2026-02-11 15:36:11-08:00
logd6ecf1580510cd7f156dbe831c68f357fe162210
treec6c1e9f3569cd1fa7bd9e2ecc91dd3d2b7d55b4b
parent5b7af7b9f641b8e8f1bdffabcdd423d3f09e9d80

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 <noreply@anthropic.com>

1 files changed, 7 insertions(+), 6 deletions(-)

apps/web/src/components/BranchToolbar.tsx+7-6
...@@ -128,13 +128,14 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B...@@ -128,13 +128,14 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
128 return;128 return;
129 }129 }
130130
131 // If the branch already lives in a secondary worktree, point the thread131 // If the branch already lives in a worktree, redirect there instead of
132 // there instead of trying to checkout (which git would reject).132 // trying to checkout (which git would reject with "already used by worktree").
133 // Ignore the main worktree (project cwd) — that's just a normal local checkout.133 if (branch.worktreePath) {
134 const isSecondaryWorktree = branch.worktreePath && branch.worktreePath !== activeProject?.cwd;134 const isMainWorktree = branch.worktreePath === activeProject?.cwd;
135 if (isSecondaryWorktree) {
136 setThreadError(null);135 setThreadError(null);
137 setThreadBranch(branch.name, branch.worktreePath);136 // Main worktree → switch back to local (project cwd, worktreePath=null).
137 // Secondary worktree → point the thread at that worktree path.
138 setThreadBranch(branch.name, isMainWorktree ? null : branch.worktreePath);
138 setIsBranchMenuOpen(false);139 setIsBranchMenuOpen(false);
139 return;140 return;
140 }141 }