From 4ad315f9643c4c4c796e74dc74698fbbfc214524 Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 20:51:53 -0500 Subject: [PATCH] feat: Auto-switch to new branch when no uncommitted changes Modified BranchManager to automatically switch to newly created branch if there are no uncommitted changes. If there are uncommitted changes, the branch is created but the user stays on the current branch with a helpful message to commit changes first. Changes: - Added useGitStatus hook to check for uncommitted changes - Updated handleCreateBranch to auto-checkout new branch when safe - Added conditional toast messages based on uncommitted changes state --- src/components/git/BranchManager.tsx | 38 ++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/components/git/BranchManager.tsx b/src/components/git/BranchManager.tsx index 42e8b42e..8050047f 100644 --- a/src/components/git/BranchManager.tsx +++ b/src/components/git/BranchManager.tsx @@ -30,6 +30,7 @@ import { ArrowUpRight, } from 'lucide-react'; import { useGit } from '@/hooks/useGit'; +import { useGitStatus } from '@/hooks/useGitStatus'; import { useToast } from '@/hooks/useToast'; import { cn } from '@/lib/utils'; @@ -60,6 +61,7 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran const { git } = useGit(); const { toast } = useToast(); + const { data: gitStatus } = useGitStatus(projectId); const projectPath = `/projects/${projectId}`; const loadBranches = useCallback(async () => { @@ -179,16 +181,42 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran }); // Create the new branch + const branchName = newBranchName.trim(); await git.branch({ dir: projectPath, - ref: newBranchName.trim(), + ref: branchName, object: currentRef, }); - toast({ - title: 'Branch created', - description: `Created branch "${newBranchName}"`, - }); + // Check if we can automatically switch to the new branch + const hasUncommittedChanges = gitStatus?.hasUncommittedChanges ?? false; + + if (!hasUncommittedChanges) { + // No uncommitted changes, automatically switch to the new branch + try { + await git.checkout({ + dir: projectPath, + ref: branchName, + }); + + toast({ + title: 'Branch created and switched', + description: `Created and switched to branch "${branchName}"`, + }); + } catch (checkoutError) { + // Branch created but checkout failed + toast({ + title: 'Branch created', + description: `Created branch "${branchName}" but failed to switch to it`, + }); + } + } else { + // Has uncommitted changes, don't switch automatically + toast({ + title: 'Branch created', + description: `Created branch "${branchName}". Commit your changes to switch to it.`, + }); + } setNewBranchName(''); setIsCreateDialogOpen(false);