mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
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:
41 files changed
+4384
-796
No files matched your search
+184
-32
@@ -4,10 +4,12 @@ import { createSuccessResult, createErrorResult } from "../ShellCommand";
|
||||
import type { GitSubcommand, GitSubcommandOptions } from "../git";
|
||||
import type { Git } from "../../git";
|
||||
|
||||
type ConfigAction = 'list' | 'get' | 'set' | 'unset' | 'get-all' | 'add';
|
||||
|
||||
export class GitConfigCommand implements GitSubcommand {
|
||||
name = 'config';
|
||||
description = 'Get and set repository or global options';
|
||||
usage = 'git config [--global] <name> [<value>] | git config [--global] --list';
|
||||
usage = 'git config [--global | --local] [--list] [--get | --get-all | --unset | --add] <name> [<value>]';
|
||||
|
||||
private git: Git;
|
||||
private fs: JSRuntimeFS;
|
||||
@@ -35,10 +37,16 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
return await this.listConfig(options.global, cwd);
|
||||
case 'get':
|
||||
return await this.getConfig(key!, options.global, cwd);
|
||||
case 'get-all':
|
||||
return await this.getConfigAll(key!, options.global, cwd);
|
||||
case 'set':
|
||||
return await this.setConfig(key!, value!, options.global, cwd);
|
||||
case 'add':
|
||||
return await this.addConfig(key!, value!, options.global, cwd);
|
||||
case 'unset':
|
||||
return await this.unsetConfig(key!, options.global, cwd);
|
||||
default:
|
||||
return createErrorResult('usage: git config [--global] <name> [<value>] | git config [--global] --list');
|
||||
return createErrorResult('usage: git config [--global | --local] [--list] [--get | --get-all | --unset | --add] <name> [<value>]');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -47,30 +55,69 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
}
|
||||
|
||||
private parseArgs(args: string[]): {
|
||||
action: 'list' | 'get' | 'set';
|
||||
action: ConfigAction;
|
||||
key?: string;
|
||||
value?: string;
|
||||
options: { global: boolean }
|
||||
options: { global: boolean; local: boolean };
|
||||
} {
|
||||
const options = { global: false };
|
||||
let action: 'list' | 'get' | 'set' = 'get';
|
||||
const options = { global: false, local: false };
|
||||
let action: ConfigAction = 'get';
|
||||
let key: string | undefined;
|
||||
let value: string | undefined;
|
||||
let explicitAction = false;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
|
||||
if (arg === '--global') {
|
||||
options.global = true;
|
||||
} else if (arg === '--local') {
|
||||
options.local = true;
|
||||
} else if (arg === '--system') {
|
||||
// Not supported; treat as global
|
||||
options.global = true;
|
||||
} else if (arg === '--list' || arg === '-l') {
|
||||
action = 'list';
|
||||
explicitAction = true;
|
||||
} else if (arg === '--get') {
|
||||
action = 'get';
|
||||
explicitAction = true;
|
||||
if (i + 1 < args.length) {
|
||||
key = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
} else if (arg === '--get-all') {
|
||||
action = 'get-all';
|
||||
explicitAction = true;
|
||||
if (i + 1 < args.length) {
|
||||
key = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
} else if (arg === '--add') {
|
||||
action = 'add';
|
||||
explicitAction = true;
|
||||
if (i + 1 < args.length) {
|
||||
key = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
if (i + 1 < args.length) {
|
||||
value = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
} else if (arg === '--unset') {
|
||||
action = 'unset';
|
||||
explicitAction = true;
|
||||
if (i + 1 < args.length) {
|
||||
key = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
} else if (!arg.startsWith('-')) {
|
||||
if (!key) {
|
||||
key = arg;
|
||||
action = 'get'; // Default to get if only key is provided
|
||||
if (!explicitAction) action = 'get';
|
||||
} else if (!value) {
|
||||
value = arg;
|
||||
action = 'set'; // Set if both key and value are provided
|
||||
if (!explicitAction) action = 'set';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,31 +125,87 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
return { action, key, value, options };
|
||||
}
|
||||
|
||||
private async listConfig(global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
/**
|
||||
* Read the .git/config file directly to list all keys.
|
||||
*/
|
||||
private async readConfigFile(global: boolean, cwd: string): Promise<Map<string, string[]>> {
|
||||
const result = new Map<string, string[]>();
|
||||
const configPath = global ? '/config/git.json' : `${cwd}/.git/config`;
|
||||
try {
|
||||
// For simplicity, we'll only show the most common config values
|
||||
// isomorphic-git doesn't have a direct way to list all config
|
||||
const commonKeys = [
|
||||
'user.name',
|
||||
'user.email',
|
||||
'core.bare',
|
||||
'remote.origin.url',
|
||||
'remote.origin.fetch'
|
||||
];
|
||||
const content = await this.fs.readFile(configPath, 'utf8');
|
||||
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const key of commonKeys) {
|
||||
if (global) {
|
||||
// JSON-based global config
|
||||
try {
|
||||
const value = await this.git.getConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
if (value !== undefined) {
|
||||
lines.push(`${key}=${value}`);
|
||||
}
|
||||
const parsed = JSON.parse(content);
|
||||
const flatten = (obj: Record<string, unknown>, prefix: string = ''): void => {
|
||||
for (const [k, v] of Object.entries(obj)) {
|
||||
const fullKey = prefix ? `${prefix}.${k}` : k;
|
||||
if (v !== null && typeof v === 'object' && !Array.isArray(v)) {
|
||||
flatten(v as Record<string, unknown>, fullKey);
|
||||
} else if (Array.isArray(v)) {
|
||||
result.set(fullKey, v.map(String));
|
||||
} else {
|
||||
result.set(fullKey, [String(v)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
flatten(parsed);
|
||||
} catch {
|
||||
// Skip if config doesn't exist
|
||||
// Not JSON
|
||||
}
|
||||
} else {
|
||||
// INI-format .git/config
|
||||
let section = '';
|
||||
for (const rawLine of content.split('\n')) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith('#') || line.startsWith(';')) continue;
|
||||
const sectionMatch = line.match(/^\[([^\]]+)\]$/);
|
||||
if (sectionMatch) {
|
||||
section = sectionMatch[1].trim().replace(/\s+"/, '.').replace(/"$/, '').replace(/\s+/g, '.');
|
||||
continue;
|
||||
}
|
||||
const kvMatch = line.match(/^([^=]+?)\s*=\s*(.*)$/);
|
||||
if (kvMatch && section) {
|
||||
const fullKey = `${section}.${kvMatch[1].trim()}`.toLowerCase();
|
||||
const val = kvMatch[2].replace(/^["']|["']$/g, '');
|
||||
const existing = result.get(fullKey) || [];
|
||||
existing.push(val);
|
||||
result.set(fullKey, existing);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Config file doesn't exist or couldn't be read
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private async listConfig(global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const configMap = await this.readConfigFile(global, cwd);
|
||||
const lines: string[] = [];
|
||||
for (const [key, values] of configMap) {
|
||||
for (const value of values) {
|
||||
lines.push(`${key}=${value}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Also include common keys that might only be accessible via getConfig
|
||||
const commonKeys = ['user.name', 'user.email', 'core.bare', 'remote.origin.url', 'remote.origin.fetch'];
|
||||
for (const key of commonKeys) {
|
||||
if (!configMap.has(key.toLowerCase())) {
|
||||
try {
|
||||
const val = await this.git.getConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
if (val !== undefined && val !== null) {
|
||||
lines.push(`${key}=${val}`);
|
||||
}
|
||||
} catch {
|
||||
// Skip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,14 +216,14 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async getConfig(key: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
private async getConfig(key: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const value = await this.git.getConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
|
||||
if (value === undefined) {
|
||||
if (value === undefined || value === null) {
|
||||
return createErrorResult(`error: key does not exist: ${key}`);
|
||||
}
|
||||
|
||||
@@ -131,7 +234,25 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async setConfig(key: string, value: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
private async getConfigAll(key: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const values = await this.git.getConfigAll({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
|
||||
if (!values || values.length === 0) {
|
||||
return createErrorResult(`error: key does not exist: ${key}`);
|
||||
}
|
||||
|
||||
return createSuccessResult(values.join('\n') + '\n');
|
||||
|
||||
} catch (error) {
|
||||
return createErrorResult(`Failed to get config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async setConfig(key: string, value: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
await this.git.setConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
@@ -145,4 +266,35 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to set config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async addConfig(key: string, value: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
await this.git.setConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
value: value,
|
||||
append: true,
|
||||
} as Parameters<typeof this.git.setConfig>[0]);
|
||||
|
||||
return createSuccessResult('');
|
||||
|
||||
} catch (error) {
|
||||
return createErrorResult(`Failed to add config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async unsetConfig(key: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
await this.git.setConfig({
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
value: undefined,
|
||||
} as Parameters<typeof this.git.setConfig>[0]);
|
||||
|
||||
return createSuccessResult('');
|
||||
|
||||
} catch (error) {
|
||||
return createErrorResult(`Failed to unset config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user