import type { JSRuntimeFS } from "../../JSRuntime"; import type { ShellCommandResult } from "../ShellCommand"; import { createSuccessResult, createErrorResult } from "../ShellCommand"; import type { GitSubcommand, GitSubcommandOptions } from "../git"; import type { Git } from "../../git"; export class GitResetCommand implements GitSubcommand { name = 'reset'; description = 'Reset current HEAD to the specified state'; usage = 'git reset [--soft | --mixed | --hard] [] | git reset HEAD [...]'; private git: Git; private fs: JSRuntimeFS; constructor(options: GitSubcommandOptions) { this.git = options.git; this.fs = options.fs; } async execute(args: string[], cwd: string): Promise { try { // Check if we're in a git repository try { await this.fs.stat(`${cwd}/.git`); } catch { return createErrorResult('fatal: not a git repository (or any of the parent directories): .git'); } const { mode, target, files, isFileReset } = await this.parseArgs(args, cwd); if (isFileReset) { // Reset specific files or all files (unstage) return await this.resetFiles(files, cwd); } else { // Reset to commit return await this.resetToCommit(target, mode, cwd); } } catch (error) { return createErrorResult(`git reset: ${error instanceof Error ? error.message : 'Unknown error'}`); } } private async parseArgs(args: string[], cwd: string): Promise<{ mode: 'soft' | 'mixed' | 'hard'; target: string; files: string[]; isFileReset: boolean; }> { let mode: 'soft' | 'mixed' | 'hard' = 'mixed'; // Default mode let target = 'HEAD'; // Default target const files: string[] = []; let foundTarget = false; let hasMode = false; // First pass: identify mode flags const nonFlagArgs: string[] = []; for (const arg of args) { if (arg === '--soft') { mode = 'soft'; hasMode = true; } else if (arg === '--mixed') { mode = 'mixed'; hasMode = true; } else if (arg === '--hard') { mode = 'hard'; hasMode = true; } else if (!arg.startsWith('-')) { nonFlagArgs.push(arg); } } // Second pass: first non-flag arg may be a ref. Check if it resolves. for (const arg of nonFlagArgs) { if (!foundTarget && await this.isLikelyRef(arg, cwd)) { target = arg; foundTarget = true; } else { files.push(arg); } } // Determine if this is a file reset operation // git reset without arguments = unstage all files // git reset HEAD = unstage all files // git reset HEAD = unstage specific files // git reset = reset to commit // git reset --mode = reset to commit with mode const isFileReset = (args.length === 0) || (target === 'HEAD' && !hasMode && files.length === 0) || (target === 'HEAD' && !hasMode && files.length > 0) || (!foundTarget && files.length > 0); return { mode, target, files, isFileReset }; } /** * Check if a string looks like (and resolves as) a git ref. */ private async isLikelyRef(arg: string, cwd: string): Promise { // Common ref patterns if (arg === 'HEAD') return true; if (/^HEAD[~^]\d*$/.test(arg)) return true; if (/^[a-f0-9]{7,40}$/.test(arg)) return true; // Try to resolve as a ref try { await this.git.resolveRef({ dir: cwd, ref: arg }); return true; } catch { return false; } } private async resetFiles(files: string[], cwd: string): Promise { try { const resetFiles: string[] = []; const errors: string[] = []; if (files.length === 0) { // Reset all staged files (git reset or git reset HEAD) try { // Get the status matrix to find all staged files const statusMatrix = await this.git.statusMatrix({ dir: cwd, }); // Find files that are staged (differ from HEAD) const stagedFiles = statusMatrix .filter(([, headStatus, , stageStatus]) => { return headStatus !== stageStatus; }) .map(([filepath]) => filepath); if (stagedFiles.length === 0) { return createSuccessResult(''); } // Reset each staged file for (const file of stagedFiles) { try { await this.git.resetIndex({ dir: cwd, filepath: file, }); resetFiles.push(file); } catch (error) { errors.push(`error: failed to reset '${file}': ${error instanceof Error ? error.message : 'Unknown error'}`); } } } catch (error) { return createErrorResult(`Failed to get repository status: ${error instanceof Error ? error.message : 'Unknown error'}`); } } else { // Reset specific files for (const file of files) { try { // Remove from staging area (reset to HEAD) await this.git.resetIndex({ dir: cwd, filepath: file, }); resetFiles.push(file); } catch { errors.push(`error: pathspec '${file}' did not match any file(s) known to git`); } } } if (errors.length > 0) { return createErrorResult(errors.join('\n')); } if (resetFiles.length === 0) { return createSuccessResult(''); } return createSuccessResult(`Unstaged changes after reset:\n${resetFiles.map(f => `M\t${f}`).join('\n')}\n`); } catch (error) { return createErrorResult(`Failed to reset files: ${error instanceof Error ? error.message : 'Unknown error'}`); } } private async resetToCommit(target: string, mode: 'soft' | 'mixed' | 'hard', cwd: string): Promise { try { // Resolve the target commit let targetOid: string; try { targetOid = await this.git.resolveRef({ dir: cwd, ref: target, }); } catch { return createErrorResult(`fatal: ambiguous argument '${target}': unknown revision or path not in the working tree.`); } // Get current HEAD for comparison let currentOid: string; try { currentOid = await this.git.resolveRef({ dir: cwd, ref: 'HEAD', }); } catch { return createErrorResult('fatal: Failed to resolve HEAD'); } // Note: Even if currentOid === targetOid, we still need to reset the index // and working directory for --mixed and --hard modes // Note: isomorphic-git doesn't have a direct reset command // We'll implement basic functionality based on the mode switch (mode) { case 'soft': // Move HEAD only (keep staging area and working directory) try { let currentBranch: string | null = null; try { currentBranch = await this.git.currentBranch({ dir: cwd, }) || null; } catch { // Detached HEAD state } if (currentOid !== targetOid) { if (currentBranch) { await this.git.writeRef({ dir: cwd, ref: `refs/heads/${currentBranch}`, value: targetOid, force: true, }); } else { await this.git.writeRef({ dir: cwd, ref: 'HEAD', value: targetOid, force: true, }); } } return createSuccessResult(`HEAD is now at ${targetOid.substring(0, 7)}\n`); } catch (error) { return createErrorResult(`Failed to soft reset: ${error instanceof Error ? error.message : 'Unknown error'}`); } case 'mixed': // Move HEAD and reset staging area (default) // Reset the index to match the target commit try { // Get the current branch name if we're on one let currentBranch: string | null = null; try { currentBranch = await this.git.currentBranch({ dir: cwd, }) || null; } catch { // Detached HEAD state } // Update the branch ref (if on a branch) or HEAD (if detached) only if different if (currentOid !== targetOid) { if (currentBranch) { await this.git.writeRef({ dir: cwd, ref: `refs/heads/${currentBranch}`, value: targetOid, force: true, }); } else { await this.git.writeRef({ dir: cwd, ref: 'HEAD', value: targetOid, force: true, }); } } // Reset the index only (not the working directory) // Using noCheckout: true to only update the index, not the working directory await this.git.checkout({ dir: cwd, ref: targetOid, noCheckout: true, noUpdateHead: true, }); return createSuccessResult(`HEAD is now at ${targetOid.substring(0, 7)}\n`); } catch (error) { return createErrorResult(`Failed to reset index: ${error instanceof Error ? error.message : 'Unknown error'}`); } case 'hard': // Move HEAD, reset staging area, and reset working directory try { // For a hard reset, we need to: // 1. Update HEAD to point to the target commit (if different) // 2. Reset the index (staging area) and working directory to match the target // Get the commit object to ensure it exists await this.git.readCommit({ dir: cwd, oid: targetOid, }); // Get the current branch name if we're on one let currentBranch: string | null = null; try { currentBranch = await this.git.currentBranch({ dir: cwd, }) || null; } catch { // Detached HEAD state } // Update the branch ref (if on a branch) or HEAD (if detached) FIRST // This is important because checkout uses HEAD to determine what to checkout if (currentOid !== targetOid) { if (currentBranch) { await this.git.writeRef({ dir: cwd, ref: `refs/heads/${currentBranch}`, value: targetOid, force: true, }); } else { await this.git.writeRef({ dir: cwd, ref: 'HEAD', value: targetOid, force: true, }); } } // Now use checkout to reset both the index and working directory // Using noUpdateHead: true because we already updated HEAD above // Using force: true to overwrite any local changes await this.git.checkout({ dir: cwd, ref: targetOid, force: true, noUpdateHead: true, }); return createSuccessResult(`HEAD is now at ${targetOid.substring(0, 7)}\n`); } catch (error) { return createErrorResult(`Failed to reset working directory: ${error instanceof Error ? error.message : 'Unknown error'}`); } default: return createErrorResult(`Unknown reset mode: ${mode}`); } } catch (error) { return createErrorResult(`Failed to reset: ${error instanceof Error ? error.message : 'Unknown error'}`); } } }