From 5fe781de6c4d5d09f4955446c7d18a171d4ec7c0 Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 14:50:24 -0500 Subject: [PATCH] feat: Add quick branch switching and creation to Repository dialog Enable users to quickly switch branches and create new branches directly from the main Repository dialog without opening Advanced Git Management. NEW FEATURES: - Branch selector dropdown showing all local branches - Current branch indicator with checkmark - Switch branches with single click - "New" button to create branches quickly - Create branch dialog with validation - Automatic rebuild after branch switch UI COMPONENTS: - Current Branch card at top of Repository dialog - Branch dropdown with all local branches - Quick "New" button for branch creation - Dedicated create branch dialog - Loading states during operations - Success/error toast notifications WORKFLOW: 1. Open Repository dialog 2. See current branch at the top 3. Click dropdown to switch branches 4. Click "New" to create a branch 5. Automatic rebuild after switching BENEFITS: - Faster branch switching (no need for Advanced dialog) - Quick branch creation for common workflows - Better visibility of current branch - Seamless integration with existing features Branch management is now accessible at two levels: - Quick operations: Repository dialog (switch, create) - Full management: Advanced Git Management (switch, create, delete, view remotes) --- src/components/GitDialog.tsx | 236 +++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) diff --git a/src/components/GitDialog.tsx b/src/components/GitDialog.tsx index febaa38b..6461358a 100644 --- a/src/components/GitDialog.tsx +++ b/src/components/GitDialog.tsx @@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, + DialogDescription, DialogHeader, DialogTitle, DialogTrigger, @@ -21,6 +22,13 @@ import { Alert, AlertDescription } from '@/components/ui/alert'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from '@/components/ui/select'; import { GitBranch, Upload, @@ -70,6 +78,14 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog const [searchParams, setSearchParams] = useSearchParams(); const navigate = useNavigate(); + // Branch management state + const [branches, setBranches] = useState([]); + const [isLoadingBranches, setIsLoadingBranches] = useState(false); + const [isSwitchingBranch, setIsSwitchingBranch] = useState(false); + const [isCreatingBranch, setIsCreatingBranch] = useState(false); + const [newBranchName, setNewBranchName] = useState(''); + const [isCreateBranchDialogOpen, setIsCreateBranchDialogOpen] = useState(false); + const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId); const { settings } = useGitSettings(); const { git } = useGit(); @@ -88,6 +104,111 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } }, [gitStatus?.remotes]); + // Load branches when dialog opens + useEffect(() => { + if (open && gitStatus?.isGitRepo) { + loadBranches(); + } + }, [open, gitStatus?.isGitRepo]); + + const loadBranches = async () => { + setIsLoadingBranches(true); + try { + const localBranches = await git.listBranches({ dir: projectPath }); + setBranches(localBranches); + } catch (error) { + console.error('Failed to load branches:', error); + } finally { + setIsLoadingBranches(false); + } + }; + + const handleSwitchBranch = async (branchName: string) => { + setIsSwitchingBranch(true); + try { + await git.checkout({ + dir: projectPath, + ref: branchName, + }); + + toast({ + title: 'Branch switched', + description: `Switched to branch "${branchName}"`, + }); + + await refetchGitStatus(); + + // Trigger rebuild + const newSearchParams = new URLSearchParams(searchParams); + newSearchParams.set('build', ''); + setSearchParams(newSearchParams); + } catch (error) { + toast({ + title: 'Failed to switch branch', + description: error instanceof Error ? error.message : 'Unknown error', + variant: 'destructive', + }); + } finally { + setIsSwitchingBranch(false); + } + }; + + const handleCreateBranch = async () => { + if (!newBranchName.trim()) { + toast({ + title: 'Invalid branch name', + description: 'Please enter a branch name', + variant: 'destructive', + }); + return; + } + + // Validate branch name + const invalidChars = /[~^: \\?*[\]]/; + if (invalidChars.test(newBranchName)) { + toast({ + title: 'Invalid branch name', + description: 'Branch name contains invalid characters', + variant: 'destructive', + }); + return; + } + + setIsCreatingBranch(true); + try { + // Get current HEAD to base the new branch on + const currentRef = await git.resolveRef({ + dir: projectPath, + ref: 'HEAD', + }); + + // Create the new branch + await git.branch({ + dir: projectPath, + ref: newBranchName.trim(), + object: currentRef, + }); + + toast({ + title: 'Branch created', + description: `Created branch "${newBranchName}"`, + }); + + setNewBranchName(''); + setIsCreateBranchDialogOpen(false); + await loadBranches(); + await refetchGitStatus(); + } catch (error) { + toast({ + title: 'Failed to create branch', + description: error instanceof Error ? error.message : 'Unknown error', + variant: 'destructive', + }); + } finally { + setIsCreatingBranch(false); + } + }; + const handleSaveOrigin = async () => { if (!gitStatus?.isGitRepo) { toast({ @@ -703,6 +824,62 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
+ {/* Branch Selector */} + {gitStatus?.isGitRepo && gitStatus.currentBranch && ( + + + Current Branch + + +
+ + +
+
+
+ )} + {/* Sync Status */} {gitStatus?.isGitRepo && ( @@ -853,6 +1030,65 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog open={isGitManagementOpen} onOpenChange={setIsGitManagementOpen} /> + + {/* Create Branch Dialog */} + + + + Create New Branch + + Create a new branch from {gitStatus?.currentBranch || 'current HEAD'} + + +
+
+ + setNewBranchName(e.target.value)} + onKeyDown={(e) => { + if (e.key === 'Enter' && !isCreatingBranch) { + handleCreateBranch(); + } + }} + autoFocus + /> +
+ {gitStatus?.currentBranch && ( +
+ Creating from: {gitStatus.currentBranch} +
+ )} +
+
+ + +
+
+
); } \ No newline at end of file