Add "Force Push" functionality

This commit is contained in:
Alex Gleason committed 2025-12-24 17:44:13 -06:00
1 parent dcdee5b875
commit 46affcbdcc
3 files changed
+131 -10

No files matched your search

@@ -0,0 +1,71 @@
import { AlertTriangle } from 'lucide-react';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
interface ForcePushDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onConfirm: () => void;
remoteName?: string;
}
export function ForcePushDialog({
open,
onOpenChange,
onConfirm,
remoteName = 'the remote'
}: ForcePushDialogProps) {
const handleConfirm = () => {
onOpenChange(false);
onConfirm();
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<AlertTriangle className="h-5 w-5 text-destructive" />
Force Push - Overwrite Remote History?
</DialogTitle>
<DialogDescription className="space-y-3">
<p>
This will <strong className="text-destructive">completely overwrite the remote repository</strong> on <strong>{remoteName}</strong> with your local version, permanently deleting any commits and changes on {remoteName} that don't exist locally.
</p>
<div className="rounded-md bg-destructive/10 p-3 text-sm border border-destructive/20">
<p className="font-medium text-destructive mb-1">What will be deleted from {remoteName}:</p>
<ul className="list-disc list-inside space-y-1 text-muted-foreground">
<li>All commits on {remoteName} that aren't in your local repository</li>
<li>Any work by other contributors not in your local copy</li>
<li>The entire commit history will be replaced with your local history</li>
</ul>
</div>
<p className="text-sm text-muted-foreground">
<strong>Warning:</strong> This operation cannot be undone and may affect other collaborators. Only use this if you're certain your local version is correct.
</p>
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button
onClick={handleConfirm}
variant="destructive"
className="gap-2"
>
<AlertTriangle className="h-4 w-4" />
Force Push
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
+19 -1
View File
@@ -12,6 +12,7 @@ import {
import { ExternalFavicon } from '@/components/ExternalFavicon';
import { SyncStepError } from './SyncStepError';
import { ForcePullDialog } from './ForcePullDialog';
import { ForcePushDialog } from './ForcePushDialog';
import { useGitStatus } from '@/hooks/useGitStatus';
import { useGitSettings } from '@/hooks/useGitSettings';
import { useGit } from '@/hooks/useGit';
@@ -23,7 +24,7 @@ import { NostrURI } from '@/lib/NostrURI';
import { ngitWebUrl } from '@/lib/ngitWebUrl';
import { useQueryClient } from '@tanstack/react-query';
type GitOperation = 'sync' | 'pull' | 'push' | 'force-pull';
type GitOperation = 'sync' | 'pull' | 'push' | 'force-pull' | 'force-push';
export function SyncStep({ projectId }: StepProps) {
const queryClient = useQueryClient();
@@ -35,6 +36,7 @@ export function SyncStep({ projectId }: StepProps) {
const [syncSuccess, setSyncSuccess] = useState(false);
const [dropdownOpen, setDropdownOpen] = useState(false);
const [showForcePullDialog, setShowForcePullDialog] = useState(false);
const [showForcePushDialog, setShowForcePushDialog] = useState(false);
const { data: gitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
@@ -135,6 +137,12 @@ export function SyncStep({ projectId }: StepProps) {
});
};
const handleForcePush = async () => {
await executeGitOperation('force-push', async () => {
await git.push({ dir, remote: 'origin', ref, force: true });
});
};
// Temporary feedback state helper
const showTemporaryFeedback = useCallback((
setter: (value: boolean) => void,
@@ -244,6 +252,8 @@ export function SyncStep({ projectId }: StepProps) {
return 'Push';
case 'force-pull':
return 'Force Pull';
case 'force-push':
return 'Force Push';
case 'sync':
return 'Sync';
default:
@@ -315,6 +325,7 @@ export function SyncStep({ projectId }: StepProps) {
error={error}
onDismiss={() => setError(null)}
onForcePull={() => setShowForcePullDialog(true)}
onForcePush={() => setShowForcePushDialog(true)}
onPull={handlePull}
/>
)}
@@ -373,6 +384,13 @@ export function SyncStep({ projectId }: StepProps) {
onConfirm={handleForcePull}
remoteName={providerInfo?.name}
/>
<ForcePushDialog
open={showForcePushDialog}
onOpenChange={setShowForcePushDialog}
onConfirm={handleForcePush}
remoteName={providerInfo?.name}
/>
</div>
);
}
@@ -1,5 +1,5 @@
import { Errors as GitErrors } from 'isomorphic-git';
import { AlertTriangle, X, ArrowDown } from 'lucide-react';
import { AlertTriangle, X, ArrowDown, ArrowUp } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Alert, AlertDescription } from '@/components/ui/alert';
@@ -7,10 +7,11 @@ interface SyncStepErrorProps {
error: Error;
onDismiss: () => void;
onForcePull: () => void;
onForcePush: () => void;
onPull: () => void;
}
export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncStepErrorProps) {
export function SyncStepError({ error, onDismiss, onForcePull, onForcePush, onPull }: SyncStepErrorProps) {
const renderForcePullButton = () => (
<Button
variant="outline"
@@ -26,6 +27,21 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
</Button>
);
const renderForcePushButton = () => (
<Button
variant="outline"
size="sm"
onClick={() => {
onDismiss();
onForcePush();
}}
className="w-full hover:bg-destructive-foreground/20 border-destructive-foreground/20"
>
<AlertTriangle className="h-4 w-4" />
Force Push
</Button>
);
const renderDismissButton = () => (
<Button
variant="ghost"
@@ -37,7 +53,7 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
</Button>
);
// MergeNotSupportedError - offer force pull
// MergeNotSupportedError - offer force pull or force push
if (error instanceof GitErrors.MergeNotSupportedError) {
return (
<Alert variant="destructive">
@@ -48,13 +64,16 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
</AlertDescription>
{renderDismissButton()}
</div>
{renderForcePullButton()}
<div className="flex gap-2">
{renderForcePullButton()}
{renderForcePushButton()}
</div>
</div>
</Alert>
);
}
// FastForwardError - offer force pull
// FastForwardError - offer force pull or force push
if (error instanceof GitErrors.FastForwardError) {
return (
<Alert variant="destructive">
@@ -66,7 +85,10 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
</AlertDescription>
{renderDismissButton()}
</div>
{renderForcePullButton()}
<div className="flex gap-2">
{renderForcePullButton()}
{renderForcePushButton()}
</div>
</div>
</Alert>
);
@@ -84,7 +106,7 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
</AlertDescription>
{renderDismissButton()}
</div>
<div className="flex gap-2">
<div className="grid grid-cols-2 gap-2">
<Button
variant="outline"
size="sm"
@@ -92,12 +114,22 @@ export function SyncStepError({ error, onDismiss, onForcePull, onPull }: SyncSte
onDismiss();
onPull();
}}
className="flex-1"
>
<ArrowDown className="h-4 w-4" />
Pull First
</Button>
{renderForcePullButton()}
<Button
variant="outline"
size="sm"
onClick={() => {
onDismiss();
onForcePush();
}}
className="hover:bg-destructive-foreground/20 border-destructive-foreground/20"
>
<ArrowUp className="h-4 w-4" />
Force Push
</Button>
</div>
</div>
</Alert>