Handle branch selection across main and secondary worktrees

- add `resolveBranchSelectionTarget` to unify checkout cwd/worktree decisions
- reuse existing worktrees when available, including switching back to main repo
- update branch selector to use shared logic and add targeted unit tests
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-03-09 22:50:45 -07:00
1 parent f70cb5de45
commit 9bb9023009
3 files changed
+111 -5

No files matched your search

@@ -4,6 +4,7 @@ import {
filterBranchPickerItems,
dedupeRemoteBranchesWithLocalMatches,
deriveLocalBranchNameFromRemoteRef,
resolveBranchSelectionTarget,
resolveDraftEnvModeAfterBranchChange,
resolveBranchToolbarValue,
} from "./BranchToolbar.logic";
@@ -223,3 +224,73 @@ describe("filterBranchPickerItems", () => {
).toEqual(["feature/488", "__create_new_branch__:48"]);
});
});
describe("resolveBranchSelectionTarget", () => {
it("reuses an existing secondary worktree for the selected branch", () => {
expect(
resolveBranchSelectionTarget({
activeProjectCwd: "/repo",
activeWorktreePath: "/repo/.t3/worktrees/feature-a",
branch: {
isDefault: false,
worktreePath: "/repo/.t3/worktrees/feature-b",
},
}),
).toEqual({
checkoutCwd: "/repo/.t3/worktrees/feature-b",
nextWorktreePath: "/repo/.t3/worktrees/feature-b",
reuseExistingWorktree: true,
});
});
it("switches back to the main repo when the branch already lives there", () => {
expect(
resolveBranchSelectionTarget({
activeProjectCwd: "/repo",
activeWorktreePath: "/repo/.t3/worktrees/feature-a",
branch: {
isDefault: true,
worktreePath: "/repo",
},
}),
).toEqual({
checkoutCwd: "/repo",
nextWorktreePath: null,
reuseExistingWorktree: true,
});
});
it("checks out the default branch in the main repo when leaving a secondary worktree", () => {
expect(
resolveBranchSelectionTarget({
activeProjectCwd: "/repo",
activeWorktreePath: "/repo/.t3/worktrees/feature-a",
branch: {
isDefault: true,
worktreePath: null,
},
}),
).toEqual({
checkoutCwd: "/repo",
nextWorktreePath: null,
reuseExistingWorktree: false,
});
});
it("keeps checkout in the current worktree for non-default branches", () => {
expect(
resolveBranchSelectionTarget({
activeProjectCwd: "/repo",
activeWorktreePath: "/repo/.t3/worktrees/feature-a",
branch: {
isDefault: false,
worktreePath: null,
},
}),
).toEqual({
checkoutCwd: "/repo/.t3/worktrees/feature-a",
nextWorktreePath: "/repo/.t3/worktrees/feature-a",
reuseExistingWorktree: false,
});
});
});
@@ -112,3 +112,32 @@ export function filterBranchPickerItems(input: {
return itemValue.toLowerCase().includes(normalizedQuery);
});
}
export function resolveBranchSelectionTarget(input: {
activeProjectCwd: string;
activeWorktreePath: string | null;
branch: Pick<GitBranch, "isDefault" | "worktreePath">;
}): {
checkoutCwd: string;
nextWorktreePath: string | null;
reuseExistingWorktree: boolean;
} {
const { activeProjectCwd, activeWorktreePath, branch } = input;
if (branch.worktreePath) {
return {
checkoutCwd: branch.worktreePath,
nextWorktreePath: branch.worktreePath === activeProjectCwd ? null : branch.worktreePath,
reuseExistingWorktree: true,
};
}
const nextWorktreePath =
activeWorktreePath !== null && branch.isDefault ? null : activeWorktreePath;
return {
checkoutCwd: nextWorktreePath ?? activeProjectCwd,
nextWorktreePath,
reuseExistingWorktree: false,
};
}
@@ -27,6 +27,7 @@ import {
deriveLocalBranchNameFromRemoteRef,
EnvMode,
filterBranchPickerItems,
resolveBranchSelectionTarget,
resolveBranchToolbarValue,
} from "./BranchToolbar.logic";
import { Button } from "./ui/button";
@@ -175,10 +176,15 @@ export function BranchToolbarBranchSelector({
return;
}
const selectionTarget = resolveBranchSelectionTarget({
activeProjectCwd,
activeWorktreePath,
branch,
});
// If the branch already lives in a worktree, point the thread there.
if (branch.worktreePath) {
const isMainWorktree = branch.worktreePath === activeProjectCwd;
onSetThreadBranch(branch.name, isMainWorktree ? null : branch.worktreePath);
if (selectionTarget.reuseExistingWorktree) {
onSetThreadBranch(branch.name, selectionTarget.nextWorktreePath);
setIsBranchMenuOpen(false);
onComposerFocusRequest?.();
return;
@@ -194,7 +200,7 @@ export function BranchToolbarBranchSelector({
runBranchAction(async () => {
setOptimisticBranch(selectedBranchName);
try {
await api.git.checkout({ cwd: branchCwd, branch: branch.name });
await api.git.checkout({ cwd: selectionTarget.checkoutCwd, branch: branch.name });
await invalidateGitQueries(queryClient);
} catch (error) {
toastManager.add({
@@ -214,7 +220,7 @@ export function BranchToolbarBranchSelector({
}
setOptimisticBranch(nextBranchName);
onSetThreadBranch(nextBranchName, activeWorktreePath);
onSetThreadBranch(nextBranchName, selectionTarget.nextWorktreePath);
});
};