import { useState, useEffect, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, DialogFooter, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent } from '@/components/ui/card'; import { GitMerge, Loader2, AlertCircle, CheckCircle, GitCommit, AlertTriangle, } from 'lucide-react'; import { useGit } from '@/hooks/useGit'; import { useFSPaths } from '@/hooks/useFSPaths'; import { useToast } from '@/hooks/useToast'; import { useSearchParams } from 'react-router-dom'; interface MergeDialogProps { projectId: string; currentBranch: string | null; sourceBranch?: string; targetBranch?: string; open?: boolean; onOpenChange?: (open: boolean) => void; onMergeComplete?: () => void; } interface MergePreview { sourceBranch: string; targetBranch: string; commitsToMerge: Array<{ oid: string; message: string; author: string; timestamp: number; }>; filesChanged: string[]; canFastForward: boolean; hasConflicts: boolean; conflictingFiles: string[]; } export function MergeDialog({ projectId, currentBranch, sourceBranch: initialSourceBranch, targetBranch: _initialTargetBranch, open, onOpenChange, onMergeComplete, }: MergeDialogProps) { const [isOpen, setIsOpen] = useState(open ?? false); const [branches, setBranches] = useState([]); const [sourceBranch, setSourceBranch] = useState(initialSourceBranch || ''); const [isLoadingBranches, setIsLoadingBranches] = useState(false); const [isLoadingPreview, setIsLoadingPreview] = useState(false); const [isMerging, setIsMerging] = useState(false); const [mergePreview, setMergePreview] = useState(null); const [error, setError] = useState(null); const [mergeComplete, setMergeComplete] = useState(false); const { git } = useGit(); const { projectsPath } = useFSPaths(); const { toast } = useToast(); const [searchParams, setSearchParams] = useSearchParams(); const projectPath = `${projectsPath}/${projectId}`; useEffect(() => { if (open !== undefined) { setIsOpen(open); } }, [open]); const loadBranches = useCallback(async () => { setIsLoadingBranches(true); try { const localBranches = await git.listBranches({ dir: projectPath }); // Filter out current branch from source options const availableBranches = localBranches.filter(b => b !== currentBranch); setBranches(availableBranches); // Set default source branch if (availableBranches.length > 0) { setSourceBranch(availableBranches[0]); } } catch (err) { console.error('Failed to load branches:', err); toast({ title: 'Failed to load branches', description: err instanceof Error ? err.message : 'Unknown error', variant: 'destructive', }); } finally { setIsLoadingBranches(false); } }, [git, projectPath, currentBranch, toast]); const loadMergePreview = useCallback(async () => { if (!sourceBranch || !currentBranch) return; setIsLoadingPreview(true); setError(null); try { // Get commits that will be merged const sourceCommits = await git.log({ dir: projectPath, ref: sourceBranch, depth: 50 }); const targetCommits = await git.log({ dir: projectPath, ref: currentBranch, depth: 50 }); const targetCommitIds = new Set(targetCommits.map(c => c.oid)); const commitsToMerge = sourceCommits.filter(c => !targetCommitIds.has(c.oid)); // Get changed files const sourceFiles = await git.listFiles({ dir: projectPath, ref: sourceBranch }); const targetFiles = await git.listFiles({ dir: projectPath, ref: currentBranch }); const changedFiles = new Set(); for (const file of [...sourceFiles, ...targetFiles]) { try { const sourceBlob = await git.readBlob({ dir: projectPath, oid: sourceBranch, filepath: file, }).catch(() => null); const targetBlob = await git.readBlob({ dir: projectPath, oid: currentBranch, filepath: file, }).catch(() => null); if (!sourceBlob || !targetBlob || Buffer.from(sourceBlob.blob).toString() !== Buffer.from(targetBlob.blob).toString()) { changedFiles.add(file); } } catch { changedFiles.add(file); } } // Check if can fast-forward const sourceOid = await git.resolveRef({ dir: projectPath, ref: sourceBranch }); const targetOid = await git.resolveRef({ dir: projectPath, ref: currentBranch }); const canFastForward = await git.isDescendent({ dir: projectPath, oid: sourceOid, ancestor: targetOid, }).catch(() => false); // Try to detect conflicts (simplified - real detection happens during merge) const conflictingFiles: string[] = []; // For now, we'll set this during actual merge setMergePreview({ sourceBranch, targetBranch: currentBranch, commitsToMerge: commitsToMerge.map(c => ({ oid: c.oid, message: c.commit.message, author: c.commit.author.name, timestamp: c.commit.author.timestamp, })), filesChanged: Array.from(changedFiles), canFastForward, hasConflicts: false, conflictingFiles, }); } catch (err) { console.error('Failed to load merge preview:', err); setError(err instanceof Error ? err.message : 'Unknown error'); } finally { setIsLoadingPreview(false); } }, [sourceBranch, currentBranch, git, projectPath]); useEffect(() => { if (isOpen) { loadBranches(); } }, [isOpen, loadBranches]); useEffect(() => { if (sourceBranch && currentBranch && sourceBranch !== currentBranch) { loadMergePreview(); } else { setMergePreview(null); } }, [sourceBranch, currentBranch, loadMergePreview]); const performMerge = async () => { if (!sourceBranch || !currentBranch) return; setIsMerging(true); setError(null); try { // Check if can fast-forward const sourceOid = await git.resolveRef({ dir: projectPath, ref: sourceBranch }); const targetOid = await git.resolveRef({ dir: projectPath, ref: currentBranch }); const canFastForward = await git.isDescendent({ dir: projectPath, oid: sourceOid, ancestor: targetOid, }).catch(() => false); if (canFastForward) { // Fast-forward merge: update the current branch to point to source branch commit await git.writeRef({ dir: projectPath, ref: `refs/heads/${currentBranch}`, value: sourceOid, force: true, }); // Update working directory to match the new HEAD await git.checkout({ dir: projectPath, ref: currentBranch, force: true, }); toast({ title: 'Fast-forward merge successful', description: `Successfully fast-forwarded ${currentBranch} to ${sourceBranch}`, }); } else { // Three-way merge const result = await git.merge({ dir: projectPath, ours: currentBranch, theirs: sourceBranch, author: { name: 'shakespeare.diy', email: 'assistant@shakespeare.diy', }, message: `Merge branch '${sourceBranch}' into ${currentBranch}`, }); if (result.alreadyMerged) { toast({ title: 'Already merged', description: 'The branches are already merged', }); setMergeComplete(true); onMergeComplete?.(); return; } // The merge result contains the new tree OID // We need to create a commit with this tree const mergeCommitOid = await git.commit({ dir: projectPath, message: `Merge branch '${sourceBranch}' into ${currentBranch}`, parent: [targetOid, sourceOid], // Two parents for merge commit tree: result.tree, }); console.log('Created merge commit:', mergeCommitOid); // Update working directory to match the merge await git.checkout({ dir: projectPath, ref: currentBranch, force: true, }); toast({ title: 'Merge successful', description: `Successfully merged ${sourceBranch} into ${currentBranch}`, }); } setMergeComplete(true); // Trigger rebuild setTimeout(() => { const newSearchParams = new URLSearchParams(searchParams); newSearchParams.set('build', ''); setSearchParams(newSearchParams); }, 500); onMergeComplete?.(); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error'; if (errorMessage.includes('conflict') || errorMessage.includes('CONFLICT')) { setError('Merge conflicts detected. Please resolve conflicts manually using the terminal.'); } else { setError(errorMessage); } toast({ title: 'Merge failed', description: errorMessage, variant: 'destructive', duration: Infinity, }); } finally { setIsMerging(false); } }; const formatDate = (timestamp: number) => { const date = new Date(timestamp * 1000); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); if (diffDays === 0) { return 'today'; } else if (diffDays === 1) { return 'yesterday'; } else if (diffDays < 7) { return `${diffDays} days ago`; } else { return date.toLocaleDateString(); } }; const handleOpenChange = (newOpen: boolean) => { setIsOpen(newOpen); onOpenChange?.(newOpen); if (!newOpen) { setSourceBranch(''); setMergePreview(null); setError(null); setMergeComplete(false); } }; if (!currentBranch) { return null; } return ( Merge Branch Merge changes from another branch into {currentBranch} {mergeComplete ? ( // Success state

Merge Complete!

Successfully merged {sourceBranch} into {currentBranch}

) : (
{/* Source branch selection */}

The branch to merge into {currentBranch}

{/* Merge direction visualization */} {sourceBranch && (
{sourceBranch} {currentBranch}
)} {/* Error alert */} {error && ( {error} )} {/* Loading preview */} {isLoadingPreview && (
Loading merge preview...
)} {/* Merge preview */} {mergePreview && !isLoadingPreview && (
{/* Stats */}
{mergePreview.commitsToMerge.length}
Commits to Merge
{mergePreview.filesChanged.length}
Files Changed
{/* Fast-forward badge */} {mergePreview.canFastForward && ( This merge can be fast-forwarded (no merge commit needed) )} {/* Commits to merge */} {mergePreview.commitsToMerge.length > 0 && (

Commits to be merged:

{mergePreview.commitsToMerge.slice(0, 10).map((commit) => (

{commit.message.split('\n')[0]}

{commit.author} • {formatDate(commit.timestamp)}
))} {mergePreview.commitsToMerge.length > 10 && (

+ {mergePreview.commitsToMerge.length - 10} more commits

)}
)} {/* No changes */} {mergePreview.commitsToMerge.length === 0 && ( No new commits to merge. The branches are already in sync. )} {/* Warning about conflicts */} {mergePreview.hasConflicts && ( Conflicts detected in {mergePreview.conflictingFiles.length} file(s). You may need to resolve conflicts manually. )}
)}
)} {!mergeComplete && ( )}
); }