Audit and overhaul virtual shell's git command

Fix critical bugs, add extensive missing flags, and add 8 new subcommands
to bring the browser-side git command closer to parity with real git.

Critical bug fixes:
- git commit: file-count classification used impossible stageStatus=1
  comparisons, always reporting 0 files changed
- git status --porcelain: untracked files could clobber staged indicators
- git add: directory walker skipped all dotfiles (including .gitignore
  itself), ignoring real git's .gitignore-based filtering
- git pull: untracked files incorrectly blocked pulls
- git reset: target regex rejected valid refs like branch names
- git diff: path filter used substring matching; --cached read working
  directory instead of index; <commit> and <commit>..<commit> forms were
  stubbed/unimplemented; used fake JS hashes instead of git.hashBlob

Implemented git reset --soft (was returning "not implemented").

New flags added across existing subcommands:
- commit: --signoff, --no-verify, -F/--file, --author, -C/--reuse-message,
  multiple -m support
- add: -u/--update, -f/--force, -n/--dry-run, absolute paths inside repo
- log: --stat, -p/--patch, --name-only, --name-status, --author, --grep,
  --since, --until, --no-merges, --reverse, --follow, custom
  --pretty=format:, proper timezone in output
- push: -u/--set-upstream (now wired to config), --tags, --delete,
  --dry-run, refspec (local:remote)
- fetch: --all, --tags, --prune now actually passed through
- branch: -a/--all, -r/--remotes, -m/-M (rename), --show-current, -v
- checkout: -B, -f/--force, commit-hash support (detached HEAD)
- config: --unset, --get-all, --add, --local, proper --list that reads
  the whole config file
- tag: real annotated tags via git.annotatedTag, -m, -f, -n, glob patterns
- clone: -b/--branch, -o/--origin, --bare
- init: -b/--initial-branch
- show: --stat, --name-only, --name-status
- diff: --stat, --name-only, --name-status
- remote: set-url, get-url, rename, show subcommands

New subcommands:
- git rm: remove files from index (and optionally worktree)
- git mv: rename/move files with index updates
- git restore: modern replacement for checkout of files
- git switch: modern replacement for branch checkout
- git merge: wrapper over git.merge (including --abort, --ff-only)
- git revert: create an inverse commit
- git rev-parse: resolve refs, --short, --abbrev-ref, --show-toplevel,
  --git-dir, HEAD~N / HEAD^N
- git ls-files: list tracked/modified/deleted/untracked files

All existing tests updated where behavior intentionally changed; new
subcommands have dedicated test suites. Final suite: 1392 passing.
This commit is contained in:
Alex Gleason committed 2026-04-27 00:42:10 -05:00
1 parent 376a982449
commit b3fddf6cb3
41 files changed
+4384 -796

No files matched your search

+68 -12
View File
@@ -27,7 +27,7 @@ export class GitResetCommand implements GitSubcommand {
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
}
const { mode, target, files, isFileReset } = this.parseArgs(args);
const { mode, target, files, isFileReset } = await this.parseArgs(args, cwd);
if (isFileReset) {
// Reset specific files or all files (unstage)
@@ -42,18 +42,20 @@ export class GitResetCommand implements GitSubcommand {
}
}
private parseArgs(args: string[]): {
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';
@@ -65,12 +67,17 @@ export class GitResetCommand implements GitSubcommand {
mode = 'hard';
hasMode = true;
} else if (!arg.startsWith('-')) {
if (!foundTarget && (arg === 'HEAD' || arg.match(/^[a-f0-9]{7,40}$/) || arg.match(/^HEAD[~^]\d*$/))) {
target = arg;
foundTarget = true;
} else {
files.push(arg);
}
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);
}
}
@@ -82,11 +89,30 @@ export class GitResetCommand implements GitSubcommand {
// git reset --mode <commit> = reset to commit with mode
const isFileReset = (args.length === 0) ||
(target === 'HEAD' && !hasMode && files.length === 0) ||
(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<boolean> {
// 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<ShellCommandResult> {
try {
const resetFiles: string[] = [];
@@ -190,8 +216,38 @@ export class GitResetCommand implements GitSubcommand {
switch (mode) {
case 'soft':
// Move HEAD only (keep staging area and working directory)
// This is complex to implement with isomorphic-git
return createErrorResult('--soft reset is not implemented in this git command');
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)