Reimplement the entire git command in the virtual shell, lmao

This commit is contained in:
Alex Gleason committed 2025-09-13 13:51:37 -05:00
1 parent 874bca5d61
commit 0119972cc9
21 files changed
+2892 -3

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import git from 'isomorphic-git';
import type { JSRuntimeFS } from "../../JSRuntime";
import type { ShellCommandResult } from "../ShellCommand";
import { createSuccessResult, createErrorResult } from "../ShellCommand";
import type { GitSubcommand } from "../git";
export class GitInitCommand implements GitSubcommand {
name = 'init';
description = 'Create an empty Git repository or reinitialize an existing one';
usage = 'git init [--bare] [<directory>]';
async execute(args: string[], cwd: string, fs: JSRuntimeFS): Promise<ShellCommandResult> {
try {
// Parse arguments
const { options, directory } = this.parseArgs(args);
const targetDir = directory || cwd;
// Check if directory exists, create if needed
if (directory) {
try {
await fs.stat(targetDir);
} catch {
// Directory doesn't exist, create it
await fs.mkdir(targetDir, { recursive: true });
}
}
// Check if already a git repository
try {
await fs.stat(`${targetDir}/.git`);
return createSuccessResult(`Reinitialized existing Git repository in ${targetDir}/.git/\n`);
} catch {
// Not a git repository, proceed with initialization
}
// Initialize the repository
await git.init({
fs,
dir: targetDir,
bare: options.bare,
defaultBranch: 'main'
});
// Set up basic configuration
await git.setConfig({
fs,
dir: targetDir,
path: 'user.name',
value: 'shakespeare.diy'
});
await git.setConfig({
fs,
dir: targetDir,
path: 'user.email',
value: '[email protected]'
});
const message = options.bare
? `Initialized empty Git repository in ${targetDir}/\n`
: `Initialized empty Git repository in ${targetDir}/.git/\n`;
return createSuccessResult(message);
} catch (error) {
return createErrorResult(`git init: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
private parseArgs(args: string[]): { options: { bare: boolean }; directory?: string } {
const options = { bare: false };
let directory: string | undefined;
for (const arg of args) {
if (arg === '--bare') {
options.bare = true;
} else if (!arg.startsWith('-')) {
directory = arg;
}
}
return { options, directory };
}
}