From ecb74cd31ec46f085f6d64c3fe3308b5c6952a07 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 24 Sep 2025 11:13:01 -0500 Subject: [PATCH] Allow rollback for current change if there are uncommitted changes Fixes https://gitlab.com/soapbox-pub/act2/-/issues/42 --- src/components/ai/GitHistoryDialog.tsx | 177 +++++++++++++++++++++++-- 1 file changed, 168 insertions(+), 9 deletions(-) diff --git a/src/components/ai/GitHistoryDialog.tsx b/src/components/ai/GitHistoryDialog.tsx index e29e0d39..fe87aba2 100644 --- a/src/components/ai/GitHistoryDialog.tsx +++ b/src/components/ai/GitHistoryDialog.tsx @@ -8,6 +8,7 @@ import { History, GitCommit, RotateCcw, AlertTriangle, ChevronDown, ChevronUp } import { useGit } from '@/hooks/useGit'; import { useFS } from '@/hooks/useFS'; import { useToast } from '@/hooks/useToast'; +import { useGitStatus } from '@/hooks/useGitStatus'; interface GitCommit { oid: string; @@ -47,6 +48,7 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange const { git } = useGit(); const { toast } = useToast(); const navigate = useNavigate(); + const { data: gitStatus } = useGitStatus(projectId); const loadGitHistory = useCallback(async () => { setIsLoading(true); @@ -231,6 +233,105 @@ ${commitsToRevert.map(c => { } }, [git, fs, projectId, toast, navigate, setIsOpen]); + const resetToHead = useCallback(async () => { + if (!fs) return; + + setIsRollingBack('HEAD'); + + try { + const projectPath = `/projects/${projectId}`; + + // Get the current HEAD commit + const headCommit = await git.resolveRef({ + dir: projectPath, + ref: 'HEAD', + }); + + // Get all files from HEAD + const headFiles = await git.listFiles({ + dir: projectPath, + ref: 'HEAD', + }); + + // Get current files in working directory + const currentFiles = await git.listFiles({ + dir: projectPath, + }); + + // Remove files that don't exist in HEAD (equivalent to git clean -fd for tracked files) + for (const filepath of currentFiles) { + if (!headFiles.includes(filepath)) { + try { + await fs.unlink(`${projectPath}/${filepath}`); + } catch (err) { + console.warn(`Could not remove ${filepath}:`, err); + } + } + } + + // Reset all files to HEAD state (equivalent to git reset --hard HEAD) + for (const filepath of headFiles) { + try { + const { blob } = await git.readBlob({ + dir: projectPath, + oid: headCommit, + filepath, + }); + + // Ensure directory exists + const dirPath = filepath.split('/').slice(0, -1).join('/'); + if (dirPath) { + await fs.mkdir(`${projectPath}/${dirPath}`, { recursive: true }); + } + + // Write the file + await fs.writeFile(`${projectPath}/${filepath}`, blob); + } catch (err) { + console.error(`Failed to reset ${filepath}:`, err); + } + } + + // Remove untracked files and directories (equivalent to git clean -fd) + // Get git status to find untracked files + const status = await git.statusMatrix({ + dir: projectPath, + }); + + const untrackedFiles = status + .filter(([, headStatus, workdirStatus, stageStatus]) => + headStatus === 0 && workdirStatus === 1 && stageStatus === 0 + ) + .map(([filepath]) => filepath); + + for (const filepath of untrackedFiles) { + try { + await fs.unlink(`${projectPath}/${filepath}`); + } catch (err) { + console.warn(`Could not remove untracked file ${filepath}:`, err); + } + } + + toast({ + title: "Reset successful", + description: "All changes have been discarded and working directory reset to HEAD", + }); + + // Close the dialog and navigate to project page with build query parameter + setIsOpen(false); + navigate(`/project/${projectId}?build`); + + } catch (err) { + console.error('Failed to reset:', err); + toast({ + title: "Reset failed", + description: err instanceof Error ? err.message : 'Failed to reset to HEAD', + variant: "destructive", + }); + } finally { + setIsRollingBack(null); + } + }, [git, fs, projectId, toast, navigate, setIsOpen]); + useEffect(() => { if (isOpen) { loadGitHistory(); @@ -375,15 +476,73 @@ ${commitsToRevert.map(c => {
{index === 0 ? ( - // Show "Current" for the most recent commit - + // Show "Current" or "Rollback" for the most recent commit + gitStatus?.hasUncommittedChanges ? ( + + + + + + + + + Discard All Changes + + +

+ This will discard all unstaged changes and reset your working directory to the current commit: +

+
+
{firstLine}
+
+ {commit.oid.substring(0, 7)} • {formatRelativeTime(commit.commit.author.timestamp)} +
+
+

+ This action cannot be undone. +

+
+
+ + Cancel + + {isRollingBack === 'HEAD' ? ( + <> +
+ Discarding... + + ) : ( + <> + + Discard Changes + + )} + + + + + ) : ( + + ) ) : ( // Show rollback button for older commits