feat: commit push/pull/fetch and others

This commit is contained in:
2026-02-16 12:24:36 +01:00
parent 9f3b5d0867
commit 56931f81ba
11 changed files with 429 additions and 2 deletions

View File

@@ -98,6 +98,11 @@ export interface GitLfsPruneResult {
error?: string;
}
export interface GitActionResult {
success: boolean;
error?: string;
}
let gitEngineInstance: GitEngine | null = null;
export function getGitEngine(): GitEngine {
@@ -274,6 +279,67 @@ export class GitEngine {
}));
}
async fetch(projectPath: string): Promise<GitActionResult> {
const git = simpleGit(projectPath);
try {
await git.fetch(['--prune']);
return { success: true };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch remote updates.',
};
}
}
async pull(projectPath: string): Promise<GitActionResult> {
const git = simpleGit(projectPath);
try {
await git.pull();
return { success: true };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to pull remote changes.',
};
}
}
async push(projectPath: string): Promise<GitActionResult> {
const git = simpleGit(projectPath);
try {
await git.push();
return { success: true };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to push local commits.',
};
}
}
async commitAll(projectPath: string, message: string): Promise<GitActionResult> {
const normalizedMessage = message.trim();
if (!normalizedMessage) {
return {
success: false,
error: 'Commit message is required.',
};
}
const git = simpleGit(projectPath);
try {
await git.add(['-A']);
await git.commit(normalizedMessage);
return { success: true };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to create commit.',
};
}
}
async ensureGitignore(projectPath: string): Promise<GitIgnoreEnsureResult> {
const gitignorePath = path.join(projectPath, '.gitignore');

View File

@@ -63,6 +63,26 @@ export function registerIpcHandlers(): void {
return engine.getHistory(projectPath, limit);
});
safeHandle('git:fetch', async (_, projectPath: string) => {
const engine = getGitEngine();
return engine.fetch(projectPath);
});
safeHandle('git:pull', async (_, projectPath: string) => {
const engine = getGitEngine();
return engine.pull(projectPath);
});
safeHandle('git:push', async (_, projectPath: string) => {
const engine = getGitEngine();
return engine.push(projectPath);
});
safeHandle('git:commitAll', async (_, projectPath: string, message: string) => {
const engine = getGitEngine();
return engine.commitAll(projectPath, message);
});
safeHandle('git:init', async (event, projectPath: string, remoteUrl?: string) => {
const engine = getGitEngine();
return engine.initializeRepo(projectPath, remoteUrl, (progress) => {

View File

@@ -13,6 +13,10 @@ export const electronAPI: ElectronAPI = {
getDiff: (projectPath: string, filePath: string) => ipcRenderer.invoke('git:diff', projectPath, filePath),
getDiffContent: (projectPath: string, filePath: string) => ipcRenderer.invoke('git:diffContent', projectPath, filePath),
getHistory: (projectPath: string, limit?: number) => ipcRenderer.invoke('git:history', projectPath, limit),
fetch: (projectPath: string) => ipcRenderer.invoke('git:fetch', projectPath),
pull: (projectPath: string) => ipcRenderer.invoke('git:pull', projectPath),
push: (projectPath: string) => ipcRenderer.invoke('git:push', projectPath),
commitAll: (projectPath: string, message: string) => ipcRenderer.invoke('git:commitAll', projectPath, message),
ensureGitignore: (projectPath: string) => ipcRenderer.invoke('git:ensureGitignore', projectPath),
pruneLfs: (projectPath: string, options?: { dryRun?: boolean; verifyRemote?: boolean }) => ipcRenderer.invoke('git:pruneLfs', projectPath, options),
init: (projectPath: string, remoteUrl?: string) => {

View File

@@ -293,6 +293,11 @@ export interface GitLfsPruneResult {
error?: string;
}
export interface GitActionResult {
success: boolean;
error?: string;
}
// Post-Media Link types
export interface MediaLinkData {
id: string;
@@ -370,6 +375,10 @@ export interface ElectronAPI {
getDiff: (projectPath: string, filePath: string) => Promise<GitDiffDto>;
getDiffContent: (projectPath: string, filePath: string) => Promise<GitDiffContentDto>;
getHistory: (projectPath: string, limit?: number) => Promise<GitHistoryEntry[]>;
fetch: (projectPath: string) => Promise<GitActionResult>;
pull: (projectPath: string) => Promise<GitActionResult>;
push: (projectPath: string) => Promise<GitActionResult>;
commitAll: (projectPath: string, message: string) => Promise<GitActionResult>;
ensureGitignore: (projectPath: string) => Promise<GitIgnoreEnsureResult>;
pruneLfs: (projectPath: string, options?: { dryRun?: boolean; verifyRemote?: boolean }) => Promise<GitLfsPruneResult>;
init: (projectPath: string, remoteUrl?: string) => Promise<GitInitResult>;

View File

@@ -35,6 +35,11 @@ function detectLanguage(filePath: string): string {
}
}
function toModelPath(filePath: string, side: 'original' | 'modified'): string {
const normalized = filePath.replace(/^\/+/, '');
return `inmemory://model/git-diff/${side}/${normalized}`;
}
export const GitDiffView: React.FC<GitDiffViewProps> = ({ filePath }) => {
const { activeProject, gitDiffPreferences } = useAppStore();
const [loading, setLoading] = useState(true);
@@ -100,6 +105,10 @@ export const GitDiffView: React.FC<GitDiffViewProps> = ({ filePath }) => {
<DiffEditor
original={original}
modified={modified}
originalModelPath={toModelPath(filePath, 'original')}
modifiedModelPath={toModelPath(filePath, 'modified')}
keepCurrentOriginalModel
keepCurrentModifiedModel
language={detectLanguage(filePath)}
theme="vs-dark"
height="100%"

View File

@@ -30,6 +30,12 @@
padding: 8px 0 0;
}
.git-sidebar-actions {
display: flex;
gap: 6px;
padding: 0 12px 8px;
}
.git-sidebar-section {
display: flex;
flex-direction: column;
@@ -54,6 +60,17 @@
overflow: auto;
}
.git-sidebar-commit-row {
display: flex;
align-items: center;
gap: 6px;
padding: 0 12px 8px;
}
.git-sidebar-commit-row .git-sidebar-input {
margin: 0;
}
.git-sidebar-file-item {
width: 100%;
border: none;

View File

@@ -5,21 +5,24 @@ import './GitSidebar.css';
import '../Sidebar/Sidebar.css';
export const GitSidebar: React.FC = () => {
const { activeProject, openTab } = useAppStore();
const { activeProject, openTab, tabs, closeTab } = useAppStore();
const [projectPath, setProjectPath] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [initializing, setInitializing] = useState(false);
const [statusLoading, setStatusLoading] = useState(false);
const [actionLoading, setActionLoading] = useState<'fetch' | 'pull' | 'push' | 'commit' | null>(null);
const [error, setError] = useState<string | null>(null);
const [isRepo, setIsRepo] = useState(false);
const [currentBranch, setCurrentBranch] = useState<string | null>(null);
const [statusFiles, setStatusFiles] = useState<Array<{ path: string; status: string }>>([]);
const [commitMessage, setCommitMessage] = useState('');
const [historyLoading, setHistoryLoading] = useState(false);
const [historyEntries, setHistoryEntries] = useState<GitHistoryEntry[]>([]);
const [initProgress, setInitProgress] = useState<GitInitProgress | null>(null);
const [initTranscript, setInitTranscript] = useState<GitInitProgress[]>([]);
const [isTranscriptExpanded, setIsTranscriptExpanded] = useState(false);
const remoteUrlInputRef = useRef<HTMLInputElement | null>(null);
const commitMessageInputRef = useRef<HTMLInputElement | null>(null);
const getDiffTabId = (filePath: string): string => `git-diff:${filePath}`;
@@ -149,6 +152,78 @@ export const GitSidebar: React.FC = () => {
}
};
const handleRepoAction = async (action: 'fetch' | 'pull' | 'push') => {
if (actionLoading) {
return;
}
const effectiveProjectPath = projectPath ?? (await resolveProjectPath());
if (!effectiveProjectPath) {
setError('No active project selected.');
return;
}
if (!projectPath) {
setProjectPath(effectiveProjectPath);
}
setActionLoading(action);
setError(null);
try {
const result =
action === 'fetch'
? await window.electronAPI.git.fetch(effectiveProjectPath)
: action === 'pull'
? await window.electronAPI.git.pull(effectiveProjectPath)
: await window.electronAPI.git.push(effectiveProjectPath);
if (!result.success) {
setError(result.error || `Failed to ${action}.`);
return;
}
await loadRepoState();
} catch {
setError(`Failed to ${action}.`);
} finally {
setActionLoading(null);
}
};
const handleCommit = async () => {
if (actionLoading) {
return;
}
const effectiveProjectPath = projectPath ?? (await resolveProjectPath());
if (!effectiveProjectPath) {
setError('No active project selected.');
return;
}
if (!projectPath) {
setProjectPath(effectiveProjectPath);
}
setActionLoading('commit');
setError(null);
try {
const messageToCommit = commitMessageInputRef.current?.value ?? commitMessage;
const result = await window.electronAPI.git.commitAll(effectiveProjectPath, messageToCommit);
if (!result.success) {
setError(result.error || 'Failed to commit changes.');
return;
}
tabs
.filter((tab) => tab.type === 'git-diff')
.forEach((tab) => closeTab(tab.id));
setCommitMessage('');
await loadRepoState();
} catch {
setError('Failed to commit changes.');
} finally {
setActionLoading(null);
}
};
if (loading) {
return (
<div className="git-sidebar">
@@ -186,8 +261,56 @@ export const GitSidebar: React.FC = () => {
<div className="git-sidebar">
<div className="git-sidebar-header">SOURCE CONTROL</div>
<div className="git-sidebar-content">
<div className="git-sidebar-actions" role="group" aria-label="Repository actions">
<button
type="button"
className="git-sidebar-button"
onClick={() => handleRepoAction('fetch')}
disabled={actionLoading !== null}
>
Fetch
</button>
<button
type="button"
className="git-sidebar-button"
onClick={() => handleRepoAction('pull')}
disabled={actionLoading !== null}
>
Pull
</button>
<button
type="button"
className="git-sidebar-button"
onClick={() => handleRepoAction('push')}
disabled={actionLoading !== null}
>
Push
</button>
</div>
<div className="git-sidebar-section">
<div className="sidebar-section-title">Open Changes ({statusFiles.length})</div>
<div className="git-sidebar-commit-row">
<input
ref={commitMessageInputRef}
className="git-sidebar-input"
type="text"
placeholder="Commit message"
value={commitMessage}
onChange={(event) => setCommitMessage(event.target.value)}
disabled={actionLoading !== null}
/>
<button
type="button"
className="git-sidebar-button"
onClick={handleCommit}
disabled={actionLoading !== null}
>
Commit
</button>
</div>
{statusLoading ? (
<div className="git-sidebar-empty-state">Loading changes...</div>
) : statusFiles.length === 0 ? (
@@ -233,6 +356,7 @@ export const GitSidebar: React.FC = () => {
)}
{currentBranch && <div className="git-sidebar-empty-state">Branch: {currentBranch}</div>}
</div>
{error && <div className="git-sidebar-empty-state git-sidebar-error">{error}</div>}
{transcriptSection}
</div>
</div>