Files
shakespeare/src/lib/commands/git/ls-files.ts
T
Alex Gleason b3fddf6cb3 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.
2026-04-27 00:42:10 -05:00

127 lines
4.1 KiB
TypeScript

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 GitLsFilesCommand implements GitSubcommand {
name = 'ls-files';
description = 'Show information about files in the index and the working tree';
usage = 'git ls-files [-m|--modified] [-o|--others] [-d|--deleted] [-s|--stage] [--] [<path>...]';
private git: Git;
private fs: JSRuntimeFS;
constructor(options: GitSubcommandOptions) {
this.git = options.git;
this.fs = options.fs;
}
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
try {
try {
await this.fs.stat(`${cwd}/.git`);
} catch {
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
}
const { paths, options } = this.parseArgs(args);
const statusMatrix = await this.git.statusMatrix({ dir: cwd });
const matchesPaths = (filepath: string): boolean => {
if (paths.length === 0) return true;
return paths.some(p => p === '.' || filepath === p || filepath.startsWith(p + '/'));
};
const lines: string[] = [];
const anyFilter = options.modified || options.others || options.deleted;
for (const [filepath, headStatus, workdirStatus, stageStatus] of statusMatrix) {
if (!matchesPaths(filepath)) continue;
const isTracked = headStatus === 1 || stageStatus !== 0;
const isModified = (headStatus === 1 && (workdirStatus !== 1 || stageStatus !== 1));
const isDeleted = isTracked && workdirStatus === 0;
const isUntracked = headStatus === 0 && stageStatus === 0 && workdirStatus === 2;
if (anyFilter) {
if (options.modified && isModified && !isDeleted) {
lines.push(filepath);
}
if (options.deleted && isDeleted) {
lines.push(filepath);
}
if (options.others && isUntracked) {
lines.push(filepath);
}
} else {
// Default: show tracked files (cached)
if (isTracked) {
if (options.stage) {
try {
const head = await this.git.resolveRef({ dir: cwd, ref: 'HEAD' });
const blob = await this.git.readBlob({ dir: cwd, oid: head, filepath });
lines.push(`100644 ${blob.oid} 0\t${filepath}`);
} catch {
lines.push(`100644 0000000000000000000000000000000000000000 0\t${filepath}`);
}
} else {
lines.push(filepath);
}
}
}
}
return createSuccessResult(lines.join('\n') + (lines.length > 0 ? '\n' : ''));
} catch (error) {
return createErrorResult(`git ls-files: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
private parseArgs(args: string[]): {
paths: string[];
options: {
modified: boolean;
others: boolean;
deleted: boolean;
stage: boolean;
cached: boolean;
};
} {
const options = {
modified: false,
others: false,
deleted: false,
stage: false,
cached: false,
};
const paths: string[] = [];
let foundDoubleDash = false;
for (const arg of args) {
if (arg === '--') {
foundDoubleDash = true;
continue;
}
if (foundDoubleDash) {
paths.push(arg);
} else if (arg === '-m' || arg === '--modified') {
options.modified = true;
} else if (arg === '-o' || arg === '--others') {
options.others = true;
} else if (arg === '-d' || arg === '--deleted') {
options.deleted = true;
} else if (arg === '-s' || arg === '--stage') {
options.stage = true;
} else if (arg === '-c' || arg === '--cached') {
options.cached = true;
} else if (!arg.startsWith('-')) {
paths.push(arg);
}
}
return { paths, options };
}
}