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