Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Handle branch selection across main and secondary worktrees
Julius Marminge9bb9023009b0
diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.tsindex 492f080be..e940fe2c3 100644--- a/apps/web/src/components/BranchToolbar.logic.test.ts+++ b/apps/web/src/components/BranchToolbar.logic.test.ts@@ -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,+ });+ });+});diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.tsindex 3db270eeb..79acb487c 100644--- a/apps/web/src/components/BranchToolbar.logic.ts+++ b/apps/web/src/components/BranchToolbar.logic.ts@@ -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,+ };+}diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsxindex 3869ec234..766b28481 100644--- a/apps/web/src/components/BranchToolbarBranchSelector.tsx+++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx@@ -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);});};