diff --git a/src/components/git-sync/steps/SyncStep.tsx b/src/components/git-sync/steps/SyncStep.tsx index bee5d5e2..08d58e1b 100644 --- a/src/components/git-sync/steps/SyncStep.tsx +++ b/src/components/git-sync/steps/SyncStep.tsx @@ -1,5 +1,6 @@ +import { Errors as GitErrors } from 'isomorphic-git'; import { useState, useCallback } from 'react'; -import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp, X } from 'lucide-react'; +import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp, X, AlertTriangle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; @@ -10,6 +11,16 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from '@/components/ui/alert-dialog'; import { ExternalFavicon } from '@/components/ExternalFavicon'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useGitSettings } from '@/hooks/useGitSettings'; @@ -22,7 +33,7 @@ import { NostrURI } from '@/lib/NostrURI'; import { ngitWebUrl } from '@/lib/ngitWebUrl'; import { useQueryClient } from '@tanstack/react-query'; -type GitOperation = 'sync' | 'pull' | 'push'; +type GitOperation = 'sync' | 'pull' | 'push' | 'force-pull'; export function SyncStep({ projectId }: StepProps) { const queryClient = useQueryClient(); @@ -33,6 +44,7 @@ export function SyncStep({ projectId }: StepProps) { const [copiedUrl, setCopiedUrl] = useState(false); const [syncSuccess, setSyncSuccess] = useState(false); const [dropdownOpen, setDropdownOpen] = useState(false); + const [showForcePullDialog, setShowForcePullDialog] = useState(false); const { data: gitStatus } = useGitStatus(projectId); const { settings } = useGitSettings(); @@ -99,6 +111,40 @@ export function SyncStep({ projectId }: StepProps) { }); }; + const handleForcePull = async () => { + await executeGitOperation('force-pull', async () => { + // Step 1: Fetch latest changes from remote + await git.fetch({ + dir, + remote: 'origin', + ref, + singleBranch: true, + }); + + // Step 2: Get the remote commit SHA + const remoteRef = `refs/remotes/origin/${ref}`; + const remoteSha = await git.resolveRef({ dir, ref: remoteRef }); + + // Step 3: Force update the current branch to point to remote + await git.writeRef({ + dir, + ref: `refs/heads/${ref}`, + value: remoteSha, + force: true, + }); + + // Step 4: Force checkout all files (overwrite local changes) + await git.checkout({ + dir, + ref, + force: true, + }); + + // Invalidate git status to refresh UI + queryClient.invalidateQueries({ queryKey: ['git-status', projectId] }); + }); + }; + // Temporary feedback state helper const showTemporaryFeedback = useCallback(( setter: (value: boolean) => void, @@ -196,6 +242,196 @@ export function SyncStep({ projectId }: StepProps) { const providerInfo = getProviderInfo(); + // Get the button label based on current operation + const getButtonLabel = (): string => { + if (!isSyncing) return 'Sync'; + + switch (currentOperation) { + case 'pull': + return 'Pull'; + case 'push': + return 'Push'; + case 'force-pull': + return 'Force Pull'; + case 'sync': + return 'Sync'; + default: + return 'Sync'; + } + }; + + const renderForcePullButton = () => ( + + ); + + // Render special error UI based on error type + const renderError = () => { + if (!error) return null; + + // MergeNotSupportedError - offer force pull + if (error instanceof GitErrors.MergeNotSupportedError) { + return ( + +
+
+ + Cannot merge changes automatically. Your local changes conflict with the remote repository. + + +
+ {renderForcePullButton()} +
+
+ ); + } + + // FastForwardError - offer force push or force pull + if (error instanceof GitErrors.FastForwardError) { + return ( + + +
+
+ + Cannot fast-forward. The remote has changes that conflict with your local commits. + + +
+ +
+
+ ); + } + + // PushRejectedError - remote has changes + if (error instanceof GitErrors.PushRejectedError) { + return ( + + +
+
+ + Push rejected. The remote repository has changes you don't have locally. Try pulling first. + + +
+
+ + {renderForcePullButton()} +
+
+
+ ); + } + + // HTTP errors + if (error instanceof GitErrors.HttpError) { + let message: string; + + switch (error.data.statusCode) { + case 401: + message = 'Authentication failed. Please check your credentials.'; + break; + case 403: + message = 'Access forbidden. You do not have permission to access this repository.'; + break; + default: + message = `HTTP Error: ${error.data.statusCode} ${error.data.statusMessage}`; + } + + return ( + + +
+ + {message} + + +
+
+ ); + } + + // Generic error fallback + return ( + + +
+ {error.message} + +
+
+ ); + }; + if (!originRemote) { return null; } @@ -224,7 +460,7 @@ export function SyncStep({ projectId }: StepProps) { - + Disconnect @@ -255,21 +491,7 @@ export function SyncStep({ projectId }: StepProps) { - {error && ( - -
- {error.message} - -
-
- )} + {renderError()}
@@ -282,11 +504,7 @@ export function SyncStep({ projectId }: StepProps) { {syncSuccess ? : } - - {isSyncing && currentOperation === 'pull' ? 'Pull' - : isSyncing && currentOperation === 'push' ? 'Push' - : 'Sync'} - + {getButtonLabel()}
@@ -322,6 +540,47 @@ export function SyncStep({ projectId }: StepProps) {
+ + {/* Force Pull Confirmation Dialog */} + + + + + + Force Pull - Discard Local Changes? + + +

+ This will permanently discard all local changes and reset your repository to match the remote. +

+

+ This action cannot be undone. Any uncommitted work will be lost. +

+
+

What will happen:

+
    +
  • All local changes will be discarded
  • +
  • Repository will reset to remote state
  • +
  • Latest changes will be pulled from remote
  • +
+
+
+
+ + Cancel + { + setShowForcePullDialog(false); + handleForcePull(); + }} + className="bg-destructive hover:bg-destructive/90" + > + + Force Pull + + +
+
); }