Refactor code to use our custom Git class

This commit is contained in:
Alex Gleason committed 2025-09-14 16:26:37 -05:00
1 parent 55120452c7
commit 772a2f424b
9 files changed
+93 -123

No files matched your search

+2 -2
View File
@@ -36,9 +36,9 @@ import { AppSettingsDialog } from '@/components/AppSettingsDialog';
import { useNavigate } from 'react-router-dom';
import { useProjectsManager } from '@/hooks/useProjectsManager';
import { useToast } from '@/hooks/useToast';
import { useGit } from '@/hooks/useGit';
import { useFS } from '@/hooks/useFS';
import JSZip from 'jszip';
import git from 'isomorphic-git';
interface ActionsMenuProps {
projectId: string;
@@ -75,6 +75,7 @@ export function ActionsMenu({
const navigate = useNavigate();
const projectsManager = useProjectsManager();
const { toast } = useToast();
const git = useGit();
const { fs } = useFS();
const isAnyLoading = isLoading || isBuildLoading || isDeployLoading || isDeleting || isExporting;
@@ -156,7 +157,6 @@ export function ActionsMenu({
try {
// Get the current commit hash
const commits = await git.log({
fs,
dir: projectPath,
depth: 1,
});
+2 -11
View File
@@ -37,14 +37,11 @@ import {
} from 'lucide-react';
import { useGitStatus } from '@/hooks/useGitStatus';
import { useGitSettings } from '@/hooks/useGitSettings';
import { useFS } from '@/hooks/useFS';
import { useGit } from '@/hooks/useGit';
import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils';
import { findCredentialsForRepo, getOriginDisplayName } from '@/lib/gitCredentials';
import git from 'isomorphic-git';
import http from 'isomorphic-git/http/web';
interface GitDialogProps {
projectId: string;
children?: React.ReactNode;
@@ -61,7 +58,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
const { fs } = useFS();
const git = useGit();
const { toast } = useToast();
const projectPath = `/projects/${projectId}`;
@@ -118,12 +115,9 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
} : {};
await git.push({
fs,
http,
dir: projectPath,
remote: remote.name,
ref: gitStatus.currentBranch,
corsProxy: 'https://cors.isomorphic-git.org',
...authOptions,
});
@@ -182,12 +176,9 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
} : {};
await git.pull({
fs,
http,
dir: projectPath,
ref: gitStatus.currentBranch,
singleBranch: true,
corsProxy: 'https://cors.isomorphic-git.org',
author: {
name: 'shakespeare.diy',
email: '[email protected]',
+19 -18
View File
@@ -3,21 +3,22 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { TestApp } from '@/test/TestApp';
import { GitHistoryDialog } from './GitHistoryDialog';
// Mock isomorphic-git
vi.mock('isomorphic-git', () => ({
default: {
log: vi.fn(),
resolveRef: vi.fn(),
readCommit: vi.fn(),
listFiles: vi.fn(),
readBlob: vi.fn(),
add: vi.fn(),
remove: vi.fn(),
commit: vi.fn(),
},
}));
// Mock the useGit hook
const mockGit = {
log: vi.fn(),
resolveRef: vi.fn(),
readCommit: vi.fn(),
listFiles: vi.fn(),
readBlob: vi.fn(),
add: vi.fn(),
remove: vi.fn(),
commit: vi.fn(),
findRoot: vi.fn(),
};
// Mock the useFS hook
vi.mock('@/hooks/useGit', () => ({
useGit: () => mockGit,
}));
vi.mock('@/hooks/useFS', () => ({
useFS: () => ({
fs: {
@@ -107,8 +108,8 @@ describe('GitHistoryDialog', () => {
},
];
const git = await import('isomorphic-git');
vi.mocked(git.default.log).mockResolvedValue(mockCommits);
mockGit.findRoot.mockResolvedValue('/projects/test-project/.git');
mockGit.log.mockResolvedValue(mockCommits);
render(
<TestApp>
@@ -144,8 +145,8 @@ describe('GitHistoryDialog', () => {
},
];
const git = await import('isomorphic-git');
vi.mocked(git.default.log).mockResolvedValue(mockCommits);
mockGit.findRoot.mockResolvedValue('/projects/test-project/.git');
mockGit.log.mockResolvedValue(mockCommits);
render(
<TestApp>
+5 -15
View File
@@ -5,9 +5,9 @@ import { ScrollArea } from '@/components/ui/scroll-area';
import { Badge } from '@/components/ui/badge';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog';
import { History, GitCommit, RotateCcw, AlertTriangle, ChevronDown, ChevronUp } from 'lucide-react';
import { useGit } from '@/hooks/useGit';
import { useFS } from '@/hooks/useFS';
import { useToast } from '@/hooks/useToast';
import git from 'isomorphic-git';
interface GitCommit {
oid: string;
@@ -43,12 +43,11 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
const setIsOpen = onOpenChange || setInternalOpen;
const [isRollingBack, setIsRollingBack] = useState<string | null>(null);
const [expandedCommits, setExpandedCommits] = useState<Set<string>>(new Set());
const git = useGit();
const { fs } = useFS();
const { toast } = useToast();
const loadGitHistory = useCallback(async () => {
if (!fs) return;
setIsLoading(true);
setError(null);
@@ -57,7 +56,7 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Check if git repository exists
try {
await fs.stat(`${projectPath}/.git`);
await git.findRoot({ filepath: projectPath });
} catch {
setError('No git repository found in this project');
return;
@@ -65,7 +64,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Get git log
const gitLog = await git.log({
fs,
dir: projectPath,
depth: 50, // Limit to last 50 commits
});
@@ -77,7 +75,7 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
} finally {
setIsLoading(false);
}
}, [fs, projectId]);
}, [git, projectId]);
const rollbackToCommit = useCallback(async (targetCommit: GitCommit) => {
if (!fs) return;
@@ -89,7 +87,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Get the current HEAD commit
const currentCommit = await git.resolveRef({
fs,
dir: projectPath,
ref: 'HEAD',
});
@@ -105,7 +102,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Get all commits between the target commit and HEAD
const allCommits = await git.log({
fs,
dir: projectPath,
depth: 1000,
});
@@ -138,14 +134,12 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Get all files from the target commit
const targetFiles = await git.listFiles({
fs,
dir: projectPath,
ref: targetCommit.oid,
});
// Get current files to know what to remove
const currentFiles = await git.listFiles({
fs,
dir: projectPath,
ref: 'HEAD',
});
@@ -156,7 +150,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
try {
await fs.unlink(`${projectPath}/${filepath}`);
await git.remove({
fs,
dir: projectPath,
filepath,
});
@@ -171,7 +164,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
for (const filepath of targetFiles) {
try {
const { blob } = await git.readBlob({
fs,
dir: projectPath,
oid: targetCommit.oid,
filepath,
@@ -188,7 +180,6 @@ export function GitHistoryDialog({ projectId, open: controlledOpen, onOpenChange
// Stage the file
await git.add({
fs,
dir: projectPath,
filepath,
});
@@ -210,7 +201,6 @@ ${commitsToRevert.map(c => {
}).join('\n')}`;
await git.commit({
fs,
dir: projectPath,
message: revertMessage,
author: {
@@ -237,7 +227,7 @@ ${commitsToRevert.map(c => {
} finally {
setIsRollingBack(null);
}
}, [fs, projectId, toast, loadGitHistory]);
}, [git, fs, projectId, toast, loadGitHistory]);
useEffect(() => {
if (isOpen) {