mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Refactor git subcommands to accept a git instance
This commit is contained in:
23 files changed
+581
-413
No files matched your search
@@ -1,19 +1,30 @@
|
||||
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";
|
||||
import type { GitSubcommand, GitSubcommandOptions } from "../git";
|
||||
import type { Git } from "../../git";
|
||||
|
||||
export class GitDiffCommand implements GitSubcommand {
|
||||
name = 'diff';
|
||||
description = 'Show changes between commits, commit and working tree, etc';
|
||||
usage = 'git diff [<commit>] [<commit>] [-- <path>...]';
|
||||
|
||||
async execute(args: string[], cwd: string, fs: JSRuntimeFS): Promise<ShellCommandResult> {
|
||||
private git: Git;
|
||||
private fs: JSRuntimeFS;
|
||||
private pwd: string;
|
||||
|
||||
constructor(options: GitSubcommandOptions) {
|
||||
this.git = options.git;
|
||||
this.fs = options.fs;
|
||||
this.pwd = options.pwd;
|
||||
}
|
||||
|
||||
async execute(args: string[]): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await fs.stat(`${cwd}/.git`);
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -22,13 +33,13 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
|
||||
if (commits.length === 0) {
|
||||
// Show working directory changes vs index
|
||||
return await this.showWorkingDirectoryDiff(fs, cwd, paths);
|
||||
return await this.showWorkingDirectoryDiff(paths);
|
||||
} else if (commits.length === 1) {
|
||||
// Show changes between commit and working directory
|
||||
return await this.showCommitDiff(fs, cwd, commits[0], paths);
|
||||
return await this.showCommitDiff(commits[0], paths);
|
||||
} else if (commits.length === 2) {
|
||||
// Show changes between two commits
|
||||
return await this.showCommitRangeDiff(fs, cwd, commits[0], commits[1], paths);
|
||||
return await this.showCommitRangeDiff(commits[0], commits[1], paths);
|
||||
} else {
|
||||
return createErrorResult('usage: git diff [<commit>] [<commit>] [-- <path>...]');
|
||||
}
|
||||
@@ -68,12 +79,12 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
return { commits, paths, options };
|
||||
}
|
||||
|
||||
private async showWorkingDirectoryDiff(fs: JSRuntimeFS, cwd: string, paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showWorkingDirectoryDiff(paths: string[]): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Get status to find changed files
|
||||
const statusMatrix = await git.statusMatrix({
|
||||
fs,
|
||||
dir: cwd,
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
|
||||
dir: this.pwd,
|
||||
});
|
||||
|
||||
// Filter for modified files in working directory
|
||||
@@ -96,9 +107,9 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
// Get file content from HEAD
|
||||
let headContent = '';
|
||||
try {
|
||||
const headBlob = await git.readBlob({
|
||||
fs,
|
||||
dir: cwd,
|
||||
const headBlob = await this.git.readBlob({
|
||||
|
||||
dir: this.pwd,
|
||||
oid: 'HEAD',
|
||||
filepath,
|
||||
});
|
||||
@@ -110,7 +121,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
// Get file content from working directory
|
||||
let workdirContent = '';
|
||||
try {
|
||||
workdirContent = await fs.readFile(`${cwd}/${filepath}`, 'utf8');
|
||||
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||
} catch {
|
||||
// File might be deleted
|
||||
}
|
||||
@@ -154,12 +165,12 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async showCommitDiff(fs: JSRuntimeFS, cwd: string, commit: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showCommitDiff(commit: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// For simplicity, show the commit message and changed files
|
||||
const commits = await git.log({
|
||||
fs,
|
||||
dir: cwd,
|
||||
const commits = await this.git.log({
|
||||
|
||||
dir: this.pwd,
|
||||
depth: 1,
|
||||
ref: commit,
|
||||
});
|
||||
@@ -188,7 +199,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async showCommitRangeDiff(_fs: JSRuntimeFS, _cwd: string, _commit1: string, _commit2: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showCommitRangeDiff(_commit1: string, _commit2: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
// This would require complex implementation to compare two commits
|
||||
return createErrorResult('Commit range diff is not implemented in this git command');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user