mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
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.
136 lines
4.7 KiB
TypeScript
136 lines
4.7 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";
|
|
|
|
/**
|
|
* `git switch` is the modern replacement for `git checkout <branch>`.
|
|
* Only switches branches; doesn't restore files.
|
|
*/
|
|
export class GitSwitchCommand implements GitSubcommand {
|
|
name = 'switch';
|
|
description = 'Switch branches';
|
|
usage = 'git switch [-c | -C] [--detach] [--force] <branch> | git switch - (switch to previous branch)';
|
|
|
|
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 { target, startPoint, options } = this.parseArgs(args);
|
|
|
|
if (!target) {
|
|
return createErrorResult('fatal: missing branch or commit argument');
|
|
}
|
|
|
|
// Handle "switch to previous branch"
|
|
if (target === '-') {
|
|
return createErrorResult('fatal: switch to previous branch is not supported');
|
|
}
|
|
|
|
const branches = await this.git.listBranches({ dir: cwd });
|
|
const isBranch = branches.includes(target);
|
|
|
|
if (options.create || options.createOrReset) {
|
|
if (isBranch && !options.createOrReset) {
|
|
return createErrorResult(`fatal: A branch named '${target}' already exists.`);
|
|
}
|
|
|
|
if (isBranch && options.createOrReset) {
|
|
try {
|
|
await this.git.deleteBranch({ dir: cwd, ref: target });
|
|
} catch {
|
|
// Continue
|
|
}
|
|
}
|
|
|
|
let startOid: string;
|
|
try {
|
|
startOid = await this.git.resolveRef({ dir: cwd, ref: startPoint || 'HEAD' });
|
|
} catch {
|
|
return createErrorResult(`fatal: invalid reference: ${startPoint || 'HEAD'}`);
|
|
}
|
|
|
|
await this.git.branch({ dir: cwd, ref: target, object: startOid });
|
|
|
|
await this.git.checkout({ dir: cwd, ref: target, force: options.force });
|
|
|
|
return createSuccessResult(`Switched to a new branch '${target}'\n`);
|
|
}
|
|
|
|
if (!isBranch && !options.detach) {
|
|
return createErrorResult(`fatal: invalid reference: ${target}`);
|
|
}
|
|
|
|
// Check for uncommitted changes unless --force
|
|
if (!options.force) {
|
|
const statusMatrix = await this.git.statusMatrix({ dir: cwd });
|
|
const hasChanges = statusMatrix.some(([, headStatus, workdirStatus, stageStatus]) => {
|
|
if (headStatus === 0 && stageStatus === 0) return false;
|
|
return headStatus !== workdirStatus || headStatus !== stageStatus;
|
|
});
|
|
if (hasChanges) {
|
|
return createErrorResult('error: Your local changes to the following files would be overwritten by checkout:\nPlease commit your changes or stash them before you switch branches.');
|
|
}
|
|
}
|
|
|
|
let currentBranch: string | null = null;
|
|
try {
|
|
currentBranch = await this.git.currentBranch({ dir: cwd }) || null;
|
|
} catch { /* empty */ }
|
|
|
|
if (isBranch && currentBranch === target) {
|
|
return createSuccessResult(`Already on '${target}'\n`);
|
|
}
|
|
|
|
await this.git.checkout({ dir: cwd, ref: target, force: options.force });
|
|
|
|
return createSuccessResult(isBranch
|
|
? `Switched to branch '${target}'\n`
|
|
: `HEAD is now at ${target.substring(0, 7)}\n`);
|
|
} catch (error) {
|
|
return createErrorResult(`git switch: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
}
|
|
}
|
|
|
|
private parseArgs(args: string[]): {
|
|
target?: string;
|
|
startPoint?: string;
|
|
options: { create: boolean; createOrReset: boolean; detach: boolean; force: boolean };
|
|
} {
|
|
const options = { create: false, createOrReset: false, detach: false, force: false };
|
|
const positionals: string[] = [];
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
const arg = args[i];
|
|
if (arg === '-c' || arg === '--create') {
|
|
options.create = true;
|
|
} else if (arg === '-C' || arg === '--force-create') {
|
|
options.createOrReset = true;
|
|
} else if (arg === '--detach' || arg === '-d') {
|
|
options.detach = true;
|
|
} else if (arg === '--force' || arg === '-f' || arg === '--discard-changes') {
|
|
options.force = true;
|
|
} else if (!arg.startsWith('-')) {
|
|
positionals.push(arg);
|
|
}
|
|
}
|
|
|
|
const target = positionals[0];
|
|
const startPoint = positionals[1];
|
|
return { target, startPoint, options };
|
|
}
|
|
}
|