mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Fix cwd in git commands
This commit is contained in:
29 files changed
+384
-461
No files matched your search
@@ -28,13 +28,12 @@ export interface GitSubcommand {
|
||||
name: string;
|
||||
description: string;
|
||||
usage: string;
|
||||
execute(args: string[]): Promise<ShellCommandResult>;
|
||||
execute(args: string[], cwd: string): Promise<ShellCommandResult>;
|
||||
}
|
||||
|
||||
export interface GitSubcommandOptions {
|
||||
git: Git;
|
||||
fs: JSRuntimeFS;
|
||||
pwd: string;
|
||||
signer?: NostrSigner;
|
||||
}
|
||||
|
||||
@@ -71,7 +70,6 @@ export class GitCommand implements ShellCommand {
|
||||
const subcommandOptions: GitSubcommandOptions = {
|
||||
git: this.git,
|
||||
fs: this.fs,
|
||||
pwd: this.cwd,
|
||||
signer: this.signer,
|
||||
};
|
||||
|
||||
@@ -99,7 +97,7 @@ export class GitCommand implements ShellCommand {
|
||||
this.subcommands.set(subcommand.name, subcommand);
|
||||
}
|
||||
|
||||
async execute(args: string[], _cwd: string, _input?: string): Promise<ShellCommandResult> {
|
||||
async execute(args: string[], cwd: string, _input?: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Handle no arguments or help
|
||||
if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
|
||||
@@ -120,8 +118,9 @@ export class GitCommand implements ShellCommand {
|
||||
return createErrorResult(`git: '${subcommandName}' is not a git command. See 'git --help'.`);
|
||||
}
|
||||
|
||||
// Execute the subcommand
|
||||
return await subcommand.execute(subcommandArgs);
|
||||
// Execute the subcommand, passing the current working directory
|
||||
// This ensures git commands use the correct directory after 'cd' commands
|
||||
return await subcommand.execute(subcommandArgs, cwd);
|
||||
|
||||
} catch (error) {
|
||||
return createErrorResult(`git: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
|
||||
@@ -57,7 +57,6 @@ describe('GitAddCommand', () => {
|
||||
addCommand = new GitAddCommand({
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: testCwd,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -71,14 +70,14 @@ describe('GitAddCommand', () => {
|
||||
// Mock stat to throw error for .git directory
|
||||
vi.mocked(mockFS.stat).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await addCommand.execute(['file.txt']);
|
||||
const result = await addCommand.execute(['file.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should return error when no files specified', async () => {
|
||||
const result = await addCommand.execute([]);
|
||||
const result = await addCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('Nothing specified, nothing added');
|
||||
@@ -95,7 +94,7 @@ describe('GitAddCommand', () => {
|
||||
['staged.txt', 1, 2, 2], // Already staged file
|
||||
]);
|
||||
|
||||
const result = await addCommand.execute(['--all']);
|
||||
const result = await addCommand.execute(['--all'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe(''); // Git add is silent on success
|
||||
@@ -118,7 +117,7 @@ describe('GitAddCommand', () => {
|
||||
['deleted.txt', 1, 0, 1], // Deleted file (not staged)
|
||||
]);
|
||||
|
||||
const result = await addCommand.execute(['--all']);
|
||||
const result = await addCommand.execute(['--all'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -132,7 +131,7 @@ describe('GitAddCommand', () => {
|
||||
});
|
||||
|
||||
it('should add specific file', async () => {
|
||||
const result = await addCommand.execute(['file.txt']);
|
||||
const result = await addCommand.execute(['file.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -151,7 +150,7 @@ describe('GitAddCommand', () => {
|
||||
['deleted.txt', 1, 0, 1], // Deleted file
|
||||
]);
|
||||
|
||||
const result = await addCommand.execute(['.']);
|
||||
const result = await addCommand.execute(['.'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -174,7 +173,7 @@ describe('GitAddCommand', () => {
|
||||
});
|
||||
|
||||
it('should reject absolute paths', async () => {
|
||||
const result = await addCommand.execute(['/absolute/path/file.txt']);
|
||||
const result = await addCommand.execute(['/absolute/path/file.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('absolute paths are not supported');
|
||||
@@ -205,7 +204,7 @@ describe('GitAddCommand', () => {
|
||||
// Mock git.remove to also throw error (file doesn't exist in git either)
|
||||
vi.mocked(mockGit.remove).mockRejectedValueOnce(new Error('File not found'));
|
||||
|
||||
const result = await addCommand.execute(['nonexistent.txt']);
|
||||
const result = await addCommand.execute(['nonexistent.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('pathspec \'nonexistent.txt\' did not match any files');
|
||||
@@ -216,7 +215,7 @@ describe('GitAddCommand', () => {
|
||||
['modified.txt', 1, 2, 1], // Modified file
|
||||
]);
|
||||
|
||||
const result = await addCommand.execute(['-A']);
|
||||
const result = await addCommand.execute(['-A'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -233,7 +232,7 @@ describe('GitAddCommand', () => {
|
||||
['modified.txt', 1, 2, 1], // Modified file
|
||||
]);
|
||||
|
||||
const result = await addCommand.execute(['--all', 'specific.txt']);
|
||||
const result = await addCommand.execute(['--all', 'specific.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
|
||||
@@ -242,7 +241,7 @@ describe('GitAddCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle double dash separator', async () => {
|
||||
const result = await addCommand.execute(['--', 'file.txt']);
|
||||
const result = await addCommand.execute(['--', 'file.txt'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -267,7 +266,7 @@ describe('GitAddCommand', () => {
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
const result = await addCommand.execute(['--all']);
|
||||
const result = await addCommand.execute(['--all'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0); // Should still succeed overall
|
||||
expect(mockGit.add).toHaveBeenCalledTimes(2);
|
||||
|
||||
+21
-31
@@ -12,19 +12,17 @@ export class GitAddCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -33,12 +31,12 @@ export class GitAddCommand implements GitSubcommand {
|
||||
|
||||
if (options.all) {
|
||||
// Add all files (git add -A)
|
||||
return await this.addAllFiles();
|
||||
return await this.addAllFiles(cwd);
|
||||
} else if (paths.length === 0) {
|
||||
return createErrorResult('Nothing specified, nothing added.\nMaybe you wanted to say \'git add .\'?');
|
||||
} else {
|
||||
// Add specific files
|
||||
return await this.addSpecificFiles(paths);
|
||||
return await this.addSpecificFiles(paths, cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -70,11 +68,11 @@ export class GitAddCommand implements GitSubcommand {
|
||||
return { options, paths };
|
||||
}
|
||||
|
||||
private async addAllFiles(): Promise<ShellCommandResult> {
|
||||
private async addAllFiles(cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Get status to find all changed files
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
for (const [filepath, headStatus, workdirStatus, stageStatus] of statusMatrix) {
|
||||
@@ -82,15 +80,13 @@ export class GitAddCommand implements GitSubcommand {
|
||||
if (workdirStatus === 0 && headStatus === 1) {
|
||||
// File was deleted in working directory
|
||||
await this.git.remove({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
} else if (workdirStatus === 2 && stageStatus !== 2) {
|
||||
// File exists and has changes that aren't staged yet
|
||||
await this.git.add({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
}
|
||||
@@ -108,7 +104,7 @@ export class GitAddCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async addSpecificFiles(paths: string[]): Promise<ShellCommandResult> {
|
||||
private async addSpecificFiles(paths: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
const addedFiles: string[] = [];
|
||||
const errors: string[] = [];
|
||||
|
||||
@@ -117,8 +113,7 @@ export class GitAddCommand implements GitSubcommand {
|
||||
if (path === '.') {
|
||||
// Add current directory (all files in it)
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
for (const [filepath, headStatus, workdirStatus, stageStatus] of statusMatrix) {
|
||||
@@ -126,16 +121,14 @@ export class GitAddCommand implements GitSubcommand {
|
||||
if (workdirStatus === 0 && headStatus === 1) {
|
||||
// File was deleted in working directory
|
||||
await this.git.remove({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
addedFiles.push(filepath);
|
||||
} else if (workdirStatus === 2 && stageStatus !== 2) {
|
||||
// File exists and has changes that aren't staged yet
|
||||
await this.git.add({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
addedFiles.push(filepath);
|
||||
@@ -151,7 +144,7 @@ export class GitAddCommand implements GitSubcommand {
|
||||
continue;
|
||||
}
|
||||
|
||||
const absolutePath = join(this.pwd, path);
|
||||
const absolutePath = join(cwd, path);
|
||||
|
||||
// Check if file/directory exists
|
||||
try {
|
||||
@@ -159,12 +152,11 @@ export class GitAddCommand implements GitSubcommand {
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
// Add all files in directory
|
||||
await this.addDirectory(path, addedFiles);
|
||||
await this.addDirectory(path, addedFiles, cwd);
|
||||
} else {
|
||||
// Add single file
|
||||
await this.git.add({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: path,
|
||||
});
|
||||
addedFiles.push(path);
|
||||
@@ -173,8 +165,7 @@ export class GitAddCommand implements GitSubcommand {
|
||||
// Check if it's a deleted file
|
||||
try {
|
||||
await this.git.remove({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: path,
|
||||
});
|
||||
addedFiles.push(path);
|
||||
@@ -196,8 +187,8 @@ export class GitAddCommand implements GitSubcommand {
|
||||
return createSuccessResult('');
|
||||
}
|
||||
|
||||
private async addDirectory(dirPath: string, addedFiles: string[]): Promise<void> {
|
||||
const absolutePath = join(this.pwd, dirPath);
|
||||
private async addDirectory(dirPath: string, addedFiles: string[], cwd: string): Promise<void> {
|
||||
const absolutePath = join(cwd, dirPath);
|
||||
const entries = await this.fs.readdir(absolutePath, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
@@ -208,12 +199,11 @@ export class GitAddCommand implements GitSubcommand {
|
||||
const entryPath = join(dirPath, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await this.addDirectory(entryPath, addedFiles);
|
||||
await this.addDirectory(entryPath, addedFiles, cwd);
|
||||
} else {
|
||||
try {
|
||||
await this.git.add({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: entryPath,
|
||||
});
|
||||
addedFiles.push(entryPath);
|
||||
@@ -223,4 +213,4 @@ export class GitAddCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,17 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -32,13 +30,13 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
|
||||
switch (action) {
|
||||
case 'list':
|
||||
return await this.listBranches();
|
||||
return await this.listBranches(cwd);
|
||||
case 'create':
|
||||
return await this.createBranch(branchName!);
|
||||
return await this.createBranch(branchName!, cwd);
|
||||
case 'delete':
|
||||
return await this.deleteBranch(branchName!, options.force);
|
||||
return await this.deleteBranch(branchName!, options.force, cwd);
|
||||
default:
|
||||
return await this.listBranches();
|
||||
return await this.listBranches(cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -83,10 +81,10 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
return { action, branchName, options };
|
||||
}
|
||||
|
||||
private async listBranches(): Promise<ShellCommandResult> {
|
||||
private async listBranches(cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const branches = await this.git.listBranches({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (branches.length === 0) {
|
||||
@@ -98,7 +96,7 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Might be in detached HEAD state
|
||||
@@ -120,11 +118,11 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async createBranch(branchName: string): Promise<ShellCommandResult> {
|
||||
private async createBranch(branchName: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if branch already exists
|
||||
const branches = await this.git.listBranches({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (branches.includes(branchName)) {
|
||||
@@ -136,7 +134,7 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
try {
|
||||
currentRef = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
});
|
||||
} catch {
|
||||
@@ -145,7 +143,7 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
|
||||
// Create the new branch
|
||||
await this.git.branch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: branchName,
|
||||
object: currentRef,
|
||||
});
|
||||
@@ -157,11 +155,11 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteBranch(branchName: string, force: boolean): Promise<ShellCommandResult> {
|
||||
private async deleteBranch(branchName: string, force: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if branch exists
|
||||
const branches = await this.git.listBranches({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (!branches.includes(branchName)) {
|
||||
@@ -173,19 +171,19 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Continue
|
||||
}
|
||||
|
||||
if (currentBranch === branchName) {
|
||||
return createErrorResult(`error: Cannot delete branch '${branchName}' checked out at '${this.pwd}'`);
|
||||
return createErrorResult(`error: Cannot delete branch '${branchName}' checked out at '${cwd}'`);
|
||||
}
|
||||
|
||||
// Delete the branch
|
||||
await this.git.deleteBranch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: branchName,
|
||||
});
|
||||
|
||||
@@ -198,4 +196,4 @@ export class GitBranchCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to delete branch: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,17 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -32,11 +30,11 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
|
||||
switch (action) {
|
||||
case 'switch':
|
||||
return await this.switchBranch(target!);
|
||||
return await this.switchBranch(target!, cwd);
|
||||
case 'create':
|
||||
return await this.createAndSwitchBranch(target!);
|
||||
return await this.createAndSwitchBranch(target!, cwd);
|
||||
case 'restore':
|
||||
return await this.restoreFiles(target!);
|
||||
return await this.restoreFiles(target!, cwd);
|
||||
default:
|
||||
return createErrorResult('error: pathspec did not match any file(s) known to git');
|
||||
}
|
||||
@@ -82,11 +80,11 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
return { action, target, options };
|
||||
}
|
||||
|
||||
private async switchBranch(branchName: string): Promise<ShellCommandResult> {
|
||||
private async switchBranch(branchName: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if branch exists
|
||||
const branches = await this.git.listBranches({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (!branches.includes(branchName)) {
|
||||
@@ -97,8 +95,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Continue
|
||||
@@ -110,7 +107,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
|
||||
// Check for uncommitted changes
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const hasChanges = statusMatrix.some(([, headStatus, workdirStatus, stageStatus]) => {
|
||||
@@ -123,7 +120,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
|
||||
// Switch to the branch
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: branchName,
|
||||
force: true,
|
||||
});
|
||||
@@ -135,11 +132,11 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async createAndSwitchBranch(branchName: string): Promise<ShellCommandResult> {
|
||||
private async createAndSwitchBranch(branchName: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if branch already exists
|
||||
const branches = await this.git.listBranches({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (branches.includes(branchName)) {
|
||||
@@ -150,8 +147,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
let currentRef: string;
|
||||
try {
|
||||
currentRef = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
});
|
||||
} catch {
|
||||
@@ -160,14 +156,14 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
|
||||
// Create the branch
|
||||
await this.git.branch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: branchName,
|
||||
object: currentRef,
|
||||
});
|
||||
|
||||
// Switch to the new branch
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: branchName,
|
||||
force: true,
|
||||
});
|
||||
@@ -179,7 +175,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async restoreFiles(filePath: string): Promise<ShellCommandResult> {
|
||||
private async restoreFiles(filePath: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// This is a simplified implementation
|
||||
// In a full git implementation, this would restore files from the index or HEAD
|
||||
@@ -187,8 +183,7 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
// Get the file from HEAD
|
||||
try {
|
||||
await this.git.checkout({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
filepaths: [filePath],
|
||||
});
|
||||
@@ -205,4 +200,4 @@ export class GitCheckoutCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to restore file: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,15 +12,13 @@ export class GitCloneCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const { options, repository, directory } = this.parseArgs(args);
|
||||
|
||||
@@ -37,7 +35,7 @@ export class GitCloneCommand implements GitSubcommand {
|
||||
targetDir = repoName.replace(/\.git$/, ''); // Remove .git suffix if present
|
||||
}
|
||||
|
||||
const targetPath = `${this.pwd}/${targetDir}`;
|
||||
const targetPath = `${cwd}/${targetDir}`;
|
||||
|
||||
// Check if target directory already exists
|
||||
try {
|
||||
@@ -117,4 +115,4 @@ export class GitCloneCommand implements GitSubcommand {
|
||||
|
||||
return { options, repository, directory };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,12 +75,11 @@ describe('GitCommitCommand', () => {
|
||||
commitCommand = new GitCommitCommand({
|
||||
git: mockGit as Git,
|
||||
fs: mockFS as JSRuntimeFS,
|
||||
pwd: mockPwd,
|
||||
});
|
||||
});
|
||||
|
||||
it('should commit with a message', async () => {
|
||||
const result = await commitCommand.execute(['-m', 'Test commit']);
|
||||
const result = await commitCommand.execute(['-m', 'Test commit'], mockPwd);
|
||||
|
||||
expect(mockGit.commit).toHaveBeenCalledWith(expect.objectContaining({
|
||||
dir: mockPwd,
|
||||
@@ -92,7 +91,7 @@ describe('GitCommitCommand', () => {
|
||||
});
|
||||
|
||||
it('should fail without a message', async () => {
|
||||
const result = await commitCommand.execute([]);
|
||||
const result = await commitCommand.execute([], mockPwd);
|
||||
|
||||
expect(mockGit.commit).not.toHaveBeenCalled();
|
||||
|
||||
@@ -101,7 +100,7 @@ describe('GitCommitCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle --amend flag', async () => {
|
||||
const result = await commitCommand.execute(['--amend', '-m', 'Amended commit']);
|
||||
const result = await commitCommand.execute(['--amend', '-m', 'Amended commit'], mockPwd);
|
||||
|
||||
expect(mockGit.log).toHaveBeenCalledWith({
|
||||
dir: mockPwd,
|
||||
@@ -119,7 +118,7 @@ describe('GitCommitCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle --amend flag without message', async () => {
|
||||
const result = await commitCommand.execute(['--amend']);
|
||||
const result = await commitCommand.execute(['--amend'], mockPwd);
|
||||
|
||||
expect(mockGit.log).toHaveBeenCalledWith({
|
||||
dir: mockPwd,
|
||||
@@ -135,7 +134,7 @@ describe('GitCommitCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle -a flag to stage all tracked modified files', async () => {
|
||||
const result = await commitCommand.execute(['-a', '-m', 'Commit with auto-stage']);
|
||||
const result = await commitCommand.execute(['-a', '-m', 'Commit with auto-stage'], mockPwd);
|
||||
|
||||
// Should call add for the modified tracked file
|
||||
expect(mockGit.add).toHaveBeenCalledWith({
|
||||
@@ -165,7 +164,7 @@ describe('GitCommitCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle -am flag for combined auto-stage and message', async () => {
|
||||
const result = await commitCommand.execute(['-am', 'Combined flag commit']);
|
||||
const result = await commitCommand.execute(['-am', 'Combined flag commit'], mockPwd);
|
||||
|
||||
// Should call add for the modified tracked file
|
||||
expect(mockGit.add).toHaveBeenCalledWith({
|
||||
@@ -192,7 +191,7 @@ describe('GitCommitCommand', () => {
|
||||
// Mock empty status
|
||||
mockGit.statusMatrix = vi.fn().mockResolvedValue([]);
|
||||
|
||||
const result = await commitCommand.execute(['--allow-empty', '-m', 'Empty commit']);
|
||||
const result = await commitCommand.execute(['--allow-empty', '-m', 'Empty commit'], mockPwd);
|
||||
|
||||
expect(mockGit.commit).toHaveBeenCalledWith(expect.objectContaining({
|
||||
dir: mockPwd,
|
||||
@@ -209,7 +208,7 @@ describe('GitCommitCommand', () => {
|
||||
['file1.txt', 1, 2, 1], // modified in workdir only
|
||||
]);
|
||||
|
||||
const result = await commitCommand.execute(['-m', 'Should fail']);
|
||||
const result = await commitCommand.execute(['-m', 'Should fail'], mockPwd);
|
||||
|
||||
expect(mockGit.commit).not.toHaveBeenCalled();
|
||||
|
||||
@@ -220,7 +219,7 @@ describe('GitCommitCommand', () => {
|
||||
it('should handle not being in a git repository', async () => {
|
||||
mockFS.stat = vi.fn().mockRejectedValue(new Error('Not a directory'));
|
||||
|
||||
const result = await commitCommand.execute(['-m', 'Test commit']);
|
||||
const result = await commitCommand.execute(['-m', 'Test commit'], mockPwd);
|
||||
|
||||
expect(mockGit.commit).not.toHaveBeenCalled();
|
||||
|
||||
|
||||
@@ -11,19 +11,17 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -36,12 +34,12 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
|
||||
// If -a flag is provided, stage all tracked files with modifications
|
||||
if (options.addAll) {
|
||||
await this.stageTrackedChanges();
|
||||
await this.stageTrackedChanges(cwd);
|
||||
}
|
||||
|
||||
// Get current status
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Check for staged changes
|
||||
@@ -58,8 +56,7 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
// Get the last commit message if amending
|
||||
try {
|
||||
const commits = await this.git.log({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
depth: 1,
|
||||
});
|
||||
if (commits.length > 0 && !message) {
|
||||
@@ -77,8 +74,7 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
let currentBranch = 'main';
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || 'main';
|
||||
} catch {
|
||||
// Use default
|
||||
@@ -90,7 +86,7 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
message: string;
|
||||
parent?: string[];
|
||||
} = {
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
message: commitMessage || 'Empty commit',
|
||||
};
|
||||
|
||||
@@ -98,7 +94,7 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
// For amend, we need to reset to the parent of the current commit
|
||||
try {
|
||||
const commits = await this.git.log({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
depth: 2,
|
||||
});
|
||||
if (commits.length > 1) {
|
||||
@@ -151,11 +147,11 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
* Stage all tracked files with modifications
|
||||
* This is used when the -a flag is provided
|
||||
*/
|
||||
private async stageTrackedChanges(): Promise<void> {
|
||||
private async stageTrackedChanges(cwd: string): Promise<void> {
|
||||
try {
|
||||
// Get the status matrix
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Process each file based on its status
|
||||
@@ -165,13 +161,13 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
if (workdirStatus === 0) {
|
||||
// File was deleted, use remove
|
||||
await this.git.remove({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
} else {
|
||||
// File was modified, use add
|
||||
await this.git.add({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
}
|
||||
@@ -218,4 +214,4 @@ export class GitCommitCommand implements GitSubcommand {
|
||||
|
||||
return { options, message };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,22 +11,20 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const { action, key, value, options } = this.parseArgs(args);
|
||||
|
||||
// Check if we're in a git repository (unless --global is used)
|
||||
if (!options.global) {
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -34,11 +32,11 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
|
||||
switch (action) {
|
||||
case 'list':
|
||||
return await this.listConfig(options.global);
|
||||
return await this.listConfig(options.global, cwd);
|
||||
case 'get':
|
||||
return await this.getConfig(key!, options.global);
|
||||
return await this.getConfig(key!, options.global, cwd);
|
||||
case 'set':
|
||||
return await this.setConfig(key!, value!, options.global);
|
||||
return await this.setConfig(key!, value!, options.global, cwd);
|
||||
default:
|
||||
return createErrorResult('usage: git config [--global] <name> [<value>] | git config [--global] --list');
|
||||
}
|
||||
@@ -80,7 +78,7 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
return { action, key, value, options };
|
||||
}
|
||||
|
||||
private async listConfig(global: boolean): Promise<ShellCommandResult> {
|
||||
private async listConfig(global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// For simplicity, we'll only show the most common config values
|
||||
// isomorphic-git doesn't have a direct way to list all config
|
||||
@@ -97,8 +95,7 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
for (const key of commonKeys) {
|
||||
try {
|
||||
const value = await this.git.getConfig({
|
||||
|
||||
dir: global ? undefined : this.pwd,
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
if (value !== undefined) {
|
||||
@@ -116,11 +113,10 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async getConfig(key: string, global: boolean): Promise<ShellCommandResult> {
|
||||
private async getConfig(key: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const value = await this.git.getConfig({
|
||||
|
||||
dir: global ? undefined : this.pwd,
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
});
|
||||
|
||||
@@ -135,10 +131,10 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async setConfig(key: string, value: string, global: boolean): Promise<ShellCommandResult> {
|
||||
private async setConfig(key: string, value: string, global: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
await this.git.setConfig({
|
||||
dir: global ? undefined : this.pwd,
|
||||
dir: global ? undefined : cwd,
|
||||
path: key,
|
||||
value: value,
|
||||
});
|
||||
@@ -149,4 +145,4 @@ export class GitConfigCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to set config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,6 @@ const mockFS = {
|
||||
const mockOptions: GitSubcommandOptions = {
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: '/test/repo',
|
||||
};
|
||||
|
||||
describe('GitDiffCommand', () => {
|
||||
@@ -106,14 +105,14 @@ describe('GitDiffCommand', () => {
|
||||
} as unknown as JSRuntimeFS,
|
||||
});
|
||||
|
||||
const result = await commandWithoutGit.execute([]);
|
||||
const result = await commandWithoutGit.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should show working directory diff for modified files', async () => {
|
||||
const result = await command.execute([]);
|
||||
const result = await command.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('diff --git a/modified.txt b/modified.txt');
|
||||
@@ -125,7 +124,7 @@ describe('GitDiffCommand', () => {
|
||||
});
|
||||
|
||||
it('should NOT show untracked files in diff', async () => {
|
||||
const result = await command.execute([]);
|
||||
const result = await command.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
// Untracked files should not appear in git diff output
|
||||
@@ -135,7 +134,7 @@ describe('GitDiffCommand', () => {
|
||||
});
|
||||
|
||||
it('should show deleted files in diff', async () => {
|
||||
const result = await command.execute([]);
|
||||
const result = await command.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('diff --git a/deleted.txt b/deleted.txt');
|
||||
@@ -146,7 +145,7 @@ describe('GitDiffCommand', () => {
|
||||
});
|
||||
|
||||
it('should show staged diff with --cached option', async () => {
|
||||
const result = await command.execute(['--cached']);
|
||||
const result = await command.execute(['--cached'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('diff --git a/staged.txt b/staged.txt');
|
||||
@@ -166,7 +165,7 @@ describe('GitDiffCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithStagedNew.execute(['--cached']);
|
||||
const result = await commandWithStagedNew.execute(['--cached'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('diff --git a/new-staged.txt b/new-staged.txt');
|
||||
@@ -184,14 +183,14 @@ describe('GitDiffCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithNoChanges.execute([]);
|
||||
const result = await commandWithNoChanges.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
});
|
||||
|
||||
it('should filter by path when specified', async () => {
|
||||
const result = await command.execute(['--', 'modified.txt']);
|
||||
const result = await command.execute(['--', 'modified.txt'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('diff --git a/modified.txt b/modified.txt');
|
||||
|
||||
@@ -12,19 +12,17 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -33,16 +31,16 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
|
||||
if (options.cached || options.staged) {
|
||||
// Show staged changes (index vs HEAD)
|
||||
return await this.showStagedDiff(paths);
|
||||
return await this.showStagedDiff(paths, cwd);
|
||||
} else if (commits.length === 0) {
|
||||
// Show working directory changes vs index
|
||||
return await this.showWorkingDirectoryDiff(paths);
|
||||
return await this.showWorkingDirectoryDiff(paths, cwd);
|
||||
} else if (commits.length === 1) {
|
||||
// Show changes between commit and working directory
|
||||
return await this.showCommitDiff(commits[0], paths);
|
||||
return await this.showCommitDiff(commits[0], paths, cwd);
|
||||
} else if (commits.length === 2) {
|
||||
// Show changes between two commits
|
||||
return await this.showCommitRangeDiff(commits[0], commits[1], paths);
|
||||
return await this.showCommitRangeDiff(commits[0], commits[1], paths, cwd);
|
||||
} else {
|
||||
return createErrorResult('usage: git diff [<commit>] [<commit>] [-- <path>...]');
|
||||
}
|
||||
@@ -82,11 +80,11 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
return { commits, paths, options };
|
||||
}
|
||||
|
||||
private async showWorkingDirectoryDiff(paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showWorkingDirectoryDiff(paths: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Get status to find changed files
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Filter for files with working directory changes
|
||||
@@ -120,7 +118,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
// For files in the index, read from HEAD as the baseline
|
||||
// This represents the last committed version for comparison
|
||||
const headBlob = await this.git.readBlob({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: 'HEAD',
|
||||
filepath,
|
||||
});
|
||||
@@ -135,7 +133,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
let workdirContent = '';
|
||||
if (workdirStatus === 2) {
|
||||
try {
|
||||
workdirContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||
workdirContent = await this.fs.readFile(`${cwd}/${filepath}`, 'utf8');
|
||||
} catch {
|
||||
// File might be deleted
|
||||
workdirContent = '';
|
||||
@@ -156,12 +154,12 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
if (workdirStatus === 0 && (stageStatus === 1 || stageStatus === 2)) {
|
||||
// Deleted file
|
||||
diffLines.push('deleted file mode 100644');
|
||||
diffLines.push(`index ${this.getShortHash(indexContent)}..0000000`);
|
||||
diffLines.push(`index ${this.getShortHash(indexContent, cwd)}..0000000`);
|
||||
diffLines.push(`--- a/${filepath}`);
|
||||
diffLines.push('+++ /dev/null');
|
||||
} else {
|
||||
// Modified file
|
||||
diffLines.push(`index ${this.getShortHash(indexContent)}..${this.getShortHash(workdirContent)} 100644`);
|
||||
diffLines.push(`index ${this.getShortHash(indexContent, cwd)}..${this.getShortHash(workdirContent, cwd)} 100644`);
|
||||
diffLines.push(`--- a/${filepath}`);
|
||||
diffLines.push(`+++ b/${filepath}`);
|
||||
}
|
||||
@@ -211,12 +209,11 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async showCommitDiff(commit: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showCommitDiff(commit: string, _paths: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// For simplicity, show the commit message and changed files
|
||||
const commits = await this.git.log({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
depth: 1,
|
||||
ref: commit,
|
||||
});
|
||||
@@ -245,16 +242,16 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async showCommitRangeDiff(_commit1: string, _commit2: string, _paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showCommitRangeDiff(_commit1: string, _commit2: string, _paths: string[], _cwd: string): Promise<ShellCommandResult> {
|
||||
// This would require complex implementation to compare two commits
|
||||
return createErrorResult('Commit range diff is not implemented in this git command');
|
||||
}
|
||||
|
||||
private async showStagedDiff(paths: string[]): Promise<ShellCommandResult> {
|
||||
private async showStagedDiff(paths: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Get status to find staged files
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Filter for files with staged changes (index differs from HEAD)
|
||||
@@ -282,7 +279,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
if (headStatus === 1) {
|
||||
try {
|
||||
const headBlob = await this.git.readBlob({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: 'HEAD',
|
||||
filepath,
|
||||
});
|
||||
@@ -296,7 +293,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
let stagedContent = '';
|
||||
if (stageStatus === 1 || stageStatus === 2) {
|
||||
try {
|
||||
stagedContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
|
||||
stagedContent = await this.fs.readFile(`${cwd}/${filepath}`, 'utf8');
|
||||
} catch {
|
||||
stagedContent = '';
|
||||
}
|
||||
@@ -308,18 +305,18 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
if (headStatus === 0 && stageStatus === 2) {
|
||||
// New file staged
|
||||
diffLines.push('new file mode 100644');
|
||||
diffLines.push(`index 0000000..${this.getShortHash(stagedContent)}`);
|
||||
diffLines.push(`index 0000000..${this.getShortHash(stagedContent, cwd)}`);
|
||||
diffLines.push('--- /dev/null');
|
||||
diffLines.push(`+++ b/${filepath}`);
|
||||
} else if (headStatus === 1 && stageStatus === 0) {
|
||||
// File deleted from stage
|
||||
diffLines.push('deleted file mode 100644');
|
||||
diffLines.push(`index ${this.getShortHash(headContent)}..0000000`);
|
||||
diffLines.push(`index ${this.getShortHash(headContent, cwd)}..0000000`);
|
||||
diffLines.push(`--- a/${filepath}`);
|
||||
diffLines.push('+++ /dev/null');
|
||||
} else {
|
||||
// Modified file
|
||||
diffLines.push(`index ${this.getShortHash(headContent)}..${this.getShortHash(stagedContent)} 100644`);
|
||||
diffLines.push(`index ${this.getShortHash(headContent, cwd)}..${this.getShortHash(stagedContent, cwd)} 100644`);
|
||||
diffLines.push(`--- a/${filepath}`);
|
||||
diffLines.push(`+++ b/${filepath}`);
|
||||
}
|
||||
@@ -367,7 +364,7 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private getShortHash(content: string): string {
|
||||
private getShortHash(content: string, _cwd: string): string {
|
||||
// Simple hash for demo purposes - not a real git hash
|
||||
let hash = 0;
|
||||
for (let i = 0; i < content.length; i++) {
|
||||
@@ -377,4 +374,4 @@ export class GitDiffCommand implements GitSubcommand {
|
||||
}
|
||||
return Math.abs(hash).toString(16).substring(0, 7).padStart(7, '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,6 @@ const mockFS = {
|
||||
const mockOptions: GitSubcommandOptions = {
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: '/test/repo',
|
||||
};
|
||||
|
||||
describe('GitFetchCommand', () => {
|
||||
@@ -144,21 +143,21 @@ describe('GitFetchCommand', () => {
|
||||
} as unknown as JSRuntimeFS,
|
||||
});
|
||||
|
||||
const result = await commandWithoutGit.execute([]);
|
||||
const result = await commandWithoutGit.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should fail when remote does not exist', async () => {
|
||||
const result = await command.execute(['nonexistent']);
|
||||
const result = await command.execute(['nonexistent'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("fatal: 'nonexistent' does not appear to be a git repository");
|
||||
});
|
||||
|
||||
it('should fetch from default remote successfully', async () => {
|
||||
const result = await command.execute([]);
|
||||
const result = await command.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('From https://github.com/user/repo.git');
|
||||
@@ -166,7 +165,7 @@ describe('GitFetchCommand', () => {
|
||||
});
|
||||
|
||||
it('should fetch from specific remote successfully', async () => {
|
||||
const result = await command.execute(['upstream']);
|
||||
const result = await command.execute(['upstream'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('From https://github.com/upstream/repo.git');
|
||||
@@ -174,7 +173,7 @@ describe('GitFetchCommand', () => {
|
||||
});
|
||||
|
||||
it('should fetch specific refs successfully', async () => {
|
||||
const result = await command.execute(['origin', 'main']);
|
||||
const result = await command.execute(['origin', 'main'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('From https://github.com/user/repo.git');
|
||||
@@ -182,7 +181,7 @@ describe('GitFetchCommand', () => {
|
||||
});
|
||||
|
||||
it('should fetch multiple specific refs successfully', async () => {
|
||||
const result = await command.execute(['origin', 'main', 'develop']);
|
||||
const result = await command.execute(['origin', 'main', 'develop'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('From https://github.com/user/repo.git');
|
||||
@@ -201,7 +200,7 @@ describe('GitFetchCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithAuthError.execute([]);
|
||||
const result = await commandWithAuthError.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: Authentication failed');
|
||||
@@ -218,7 +217,7 @@ describe('GitFetchCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithNetworkError.execute([]);
|
||||
const result = await commandWithNetworkError.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: unable to access remote repository');
|
||||
|
||||
@@ -11,19 +11,17 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -34,7 +32,7 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
let remoteUrl: string;
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const targetRemote = remotes.find(r => r.remote === remote);
|
||||
@@ -52,7 +50,7 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
// Fetch specific refs
|
||||
for (const ref of refs) {
|
||||
await this.git.fetch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: remote,
|
||||
ref: ref,
|
||||
});
|
||||
@@ -60,7 +58,7 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
} else {
|
||||
// Fetch all refs (default behavior)
|
||||
await this.git.fetch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: remote,
|
||||
});
|
||||
}
|
||||
@@ -74,7 +72,7 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
for (const ref of refs) {
|
||||
try {
|
||||
await this.git.resolveRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: `refs/remotes/${remote}/${ref}`,
|
||||
});
|
||||
lines.push(` * branch ${ref} -> ${remote}/${ref}`);
|
||||
@@ -170,4 +168,4 @@ export class GitFetchCommand implements GitSubcommand {
|
||||
|
||||
return { remote, refs, options };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,17 @@ export class GitInitCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Parse arguments
|
||||
const { options, directory } = this.parseArgs(args);
|
||||
const targetDir = directory || this.pwd;
|
||||
const targetDir = directory || cwd;
|
||||
|
||||
// Check if directory exists, create if needed
|
||||
if (directory) {
|
||||
@@ -88,4 +86,4 @@ export class GitInitCommand implements GitSubcommand {
|
||||
|
||||
return { options, directory };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,19 +11,17 @@ export class GitLogCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -32,8 +30,7 @@ export class GitLogCommand implements GitSubcommand {
|
||||
|
||||
try {
|
||||
const commits = await this.git.log({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
depth: limit,
|
||||
});
|
||||
|
||||
@@ -42,9 +39,9 @@ export class GitLogCommand implements GitSubcommand {
|
||||
}
|
||||
|
||||
if (options.oneline) {
|
||||
return this.formatOnelineLog(commits);
|
||||
return this.formatOnelineLog(commits, cwd);
|
||||
} else {
|
||||
return this.formatFullLog(commits, options.graph);
|
||||
return this.formatFullLog(commits, options.graph, cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -103,7 +100,7 @@ export class GitLogCommand implements GitSubcommand {
|
||||
return { options, limit };
|
||||
}
|
||||
|
||||
private formatOnelineLog(commits: Array<{ oid: string; commit: { message: string } }>): ShellCommandResult {
|
||||
private formatOnelineLog(commits: Array<{ oid: string; commit: { message: string } }>, _cwd: string): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const commit of commits) {
|
||||
@@ -115,7 +112,7 @@ export class GitLogCommand implements GitSubcommand {
|
||||
return createSuccessResult(lines.join('\n') + '\n');
|
||||
}
|
||||
|
||||
private formatFullLog(commits: Array<{ oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number } } }>, showGraph: boolean): ShellCommandResult {
|
||||
private formatFullLog(commits: Array<{ oid: string; commit: { message: string; author: { name: string; email: string; timestamp: number } } }>, showGraph: boolean, _cwd: string): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (let i = 0; i < commits.length; i++) {
|
||||
|
||||
@@ -21,7 +21,6 @@ const mockFS = {
|
||||
const mockOptions: GitSubcommandOptions = {
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: '/test/repo',
|
||||
};
|
||||
|
||||
describe('GitPullCommand', () => {
|
||||
@@ -107,10 +106,9 @@ describe('GitPullCommand', () => {
|
||||
const commandWithChanges = new GitPullCommand({
|
||||
git: mockGitWithChanges,
|
||||
fs: mockFSWithGit,
|
||||
pwd: '/test/repo',
|
||||
});
|
||||
|
||||
const result = await commandWithChanges.execute(['origin', 'main']);
|
||||
const result = await commandWithChanges.execute(['origin', 'main'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('error: Your local changes to the following files would be overwritten by merge:');
|
||||
|
||||
@@ -11,19 +11,17 @@ export class GitPullCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -35,8 +33,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
if (!targetBranch) {
|
||||
targetBranch = currentBranch || 'main';
|
||||
@@ -51,8 +48,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
let remoteUrl: string;
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const targetRemote = remotes.find(r => r.remote === remote);
|
||||
@@ -66,7 +62,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
|
||||
// Check for uncommitted changes
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const modifiedFiles = statusMatrix
|
||||
@@ -84,7 +80,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
try {
|
||||
// Fetch from remote
|
||||
await this.git.fetch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: remote,
|
||||
ref: targetBranch,
|
||||
});
|
||||
@@ -94,8 +90,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
let remoteOid: string;
|
||||
try {
|
||||
remoteOid = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: remoteRef,
|
||||
});
|
||||
} catch {
|
||||
@@ -106,15 +101,13 @@ export class GitPullCommand implements GitSubcommand {
|
||||
let currentOid: string;
|
||||
try {
|
||||
currentOid = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
});
|
||||
} catch {
|
||||
// Empty repository, just checkout the remote branch
|
||||
await this.git.checkout({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: targetBranch,
|
||||
});
|
||||
return createSuccessResult(`From ${remoteUrl}\n * branch ${targetBranch} -> FETCH_HEAD\nUpdating empty repository\nFast-forward\n`);
|
||||
@@ -127,8 +120,7 @@ export class GitPullCommand implements GitSubcommand {
|
||||
|
||||
// Perform fast-forward merge (simplified)
|
||||
await this.git.merge({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ours: currentBranch || 'HEAD',
|
||||
theirs: remoteRef,
|
||||
fastForwardOnly: true,
|
||||
@@ -172,4 +164,4 @@ export class GitPullCommand implements GitSubcommand {
|
||||
|
||||
return { remote, branch };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,6 @@ describe('GitPushCommand', () => {
|
||||
pushCommand = new GitPushCommand({
|
||||
git: mockGit as Git,
|
||||
fs: mockFS as JSRuntimeFS,
|
||||
pwd: mockPwd,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -37,17 +36,16 @@ describe('GitPushCommand', () => {
|
||||
const cmd = new GitPushCommand({
|
||||
git: mockGit as Git,
|
||||
fs: mockFSNoRepo as JSRuntimeFS,
|
||||
pwd: '/non-existent',
|
||||
});
|
||||
|
||||
const result = await cmd.execute(['origin', 'main']);
|
||||
const result = await cmd.execute(['origin', 'main'], mockPwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('not a git repository');
|
||||
});
|
||||
|
||||
it('should fail when no remote is configured', async () => {
|
||||
const result = await pushCommand.execute(['origin', 'main']);
|
||||
const result = await pushCommand.execute(['origin', 'main'], mockPwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('does not appear to be a git repository');
|
||||
@@ -59,7 +57,7 @@ describe('GitPushCommand', () => {
|
||||
{ remote: 'origin', url: 'https://example.com/repo.git' },
|
||||
]);
|
||||
|
||||
await pushCommand.execute(['origin', 'main', '--force']);
|
||||
await pushCommand.execute(['origin', 'main', '--force'], mockPwd);
|
||||
|
||||
// Verify that git.push was called with force: true
|
||||
expect(mockGit.push).toHaveBeenCalledWith(
|
||||
@@ -75,7 +73,7 @@ describe('GitPushCommand', () => {
|
||||
{ remote: 'origin', url: 'https://example.com/repo.git' },
|
||||
]);
|
||||
|
||||
await pushCommand.execute(['origin', 'main', '-f']);
|
||||
await pushCommand.execute(['origin', 'main', '-f'], mockPwd);
|
||||
|
||||
// Verify that git.push was called with force: true
|
||||
expect(mockGit.push).toHaveBeenCalledWith(
|
||||
@@ -91,7 +89,7 @@ describe('GitPushCommand', () => {
|
||||
{ remote: 'origin', url: 'https://example.com/repo.git' },
|
||||
]);
|
||||
|
||||
await pushCommand.execute(['origin', 'main']);
|
||||
await pushCommand.execute(['origin', 'main'], mockPwd);
|
||||
|
||||
// Verify that git.push was called with force: false
|
||||
expect(mockGit.push).toHaveBeenCalledWith(
|
||||
|
||||
@@ -14,21 +14,19 @@ export class GitPushCommand implements GitSubcommand {
|
||||
|
||||
private git: Git;
|
||||
private fs: JSRuntimeFS;
|
||||
private pwd: string;
|
||||
private signer?: NostrSigner;
|
||||
|
||||
constructor(options: GitSubcommandOptions) {
|
||||
this.git = options.git;
|
||||
this.fs = options.fs;
|
||||
this.pwd = options.pwd;
|
||||
this.signer = options.signer;
|
||||
}
|
||||
|
||||
async execute(args: string[]): Promise<ShellCommandResult> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -40,8 +38,7 @@ export class GitPushCommand implements GitSubcommand {
|
||||
if (!targetBranch) {
|
||||
try {
|
||||
targetBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || undefined;
|
||||
if (!targetBranch) {
|
||||
return createErrorResult('fatal: You are not currently on a branch.');
|
||||
@@ -55,7 +52,7 @@ export class GitPushCommand implements GitSubcommand {
|
||||
let remoteUrl: string;
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const targetRemote = remotes.find(r => r.remote === remote);
|
||||
@@ -71,7 +68,7 @@ export class GitPushCommand implements GitSubcommand {
|
||||
|
||||
try {
|
||||
await this.git.push({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: remote,
|
||||
ref: targetBranch,
|
||||
remoteRef: targetBranch,
|
||||
@@ -138,4 +135,4 @@ export class GitPushCommand implements GitSubcommand {
|
||||
|
||||
return { remote, branch, options };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,19 +12,17 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -33,13 +31,13 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
|
||||
switch (action) {
|
||||
case 'list':
|
||||
return await this.listRemotes(options.verbose);
|
||||
return await this.listRemotes(options.verbose, cwd);
|
||||
case 'add':
|
||||
return await this.addRemote(name!, url!);
|
||||
return await this.addRemote(name!, url!, cwd);
|
||||
case 'remove':
|
||||
return await this.removeRemote(name!);
|
||||
return await this.removeRemote(name!, cwd);
|
||||
default:
|
||||
return await this.listRemotes(options.verbose);
|
||||
return await this.listRemotes(options.verbose, cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -85,10 +83,10 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
return { action, name, url, options };
|
||||
}
|
||||
|
||||
private async listRemotes(verbose: boolean): Promise<ShellCommandResult> {
|
||||
private async listRemotes(verbose: boolean, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (remotes.length === 0) {
|
||||
@@ -115,7 +113,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async addRemote(name: string, url: string): Promise<ShellCommandResult> {
|
||||
private async addRemote(name: string, url: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
if (!name || !url) {
|
||||
return createErrorResult('usage: git remote add <name> <url>');
|
||||
@@ -125,7 +123,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const existingRemote = remotes.find(r => r.remote === name);
|
||||
@@ -138,7 +136,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
|
||||
// Add the remote
|
||||
await this.git.addRemote({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: name,
|
||||
url: url,
|
||||
});
|
||||
@@ -150,7 +148,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeRemote(name: string): Promise<ShellCommandResult> {
|
||||
private async removeRemote(name: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
if (!name) {
|
||||
return createErrorResult('usage: git remote remove <name>');
|
||||
@@ -160,7 +158,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
try {
|
||||
const remotes = await this.git.listRemotes({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
const existingRemote = remotes.find(r => r.remote === name);
|
||||
@@ -173,7 +171,7 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
|
||||
// Remove the remote
|
||||
await this.git.deleteRemote({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
remote: name,
|
||||
});
|
||||
|
||||
@@ -183,4 +181,4 @@ export class GitRemoteCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to remove remote: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,6 @@ const mockFS = {
|
||||
const mockOptions: GitSubcommandOptions = {
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: '/test/repo',
|
||||
};
|
||||
|
||||
describe('GitResetCommand', () => {
|
||||
@@ -146,14 +145,14 @@ describe('GitResetCommand', () => {
|
||||
} as unknown as JSRuntimeFS,
|
||||
});
|
||||
|
||||
const result = await commandWithoutGit.execute([]);
|
||||
const result = await commandWithoutGit.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should unstage all files when called without arguments', async () => {
|
||||
const result = await command.execute([]);
|
||||
const result = await command.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Unstaged changes after reset:');
|
||||
@@ -165,7 +164,7 @@ describe('GitResetCommand', () => {
|
||||
});
|
||||
|
||||
it('should unstage all files when called with HEAD', async () => {
|
||||
const result = await command.execute(['HEAD']);
|
||||
const result = await command.execute(['HEAD'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Unstaged changes after reset:');
|
||||
@@ -174,7 +173,7 @@ describe('GitResetCommand', () => {
|
||||
});
|
||||
|
||||
it('should unstage specific files when called with HEAD and file paths', async () => {
|
||||
const result = await command.execute(['HEAD', 'staged.txt']);
|
||||
const result = await command.execute(['HEAD', 'staged.txt'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Unstaged changes after reset:');
|
||||
@@ -194,7 +193,7 @@ describe('GitResetCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithNoStaged.execute([]);
|
||||
const result = await commandWithNoStaged.execute([], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -214,35 +213,35 @@ describe('GitResetCommand', () => {
|
||||
} as unknown as Git,
|
||||
});
|
||||
|
||||
const result = await commandWithError.execute(['HEAD', 'nonexistent.txt']);
|
||||
const result = await commandWithError.execute(['HEAD', 'nonexistent.txt'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("error: pathspec 'nonexistent.txt' did not match any file(s) known to git");
|
||||
});
|
||||
|
||||
it('should handle commit reset with hard mode', async () => {
|
||||
const result = await command.execute(['--hard', 'HEAD~1']);
|
||||
const result = await command.execute(['--hard', 'HEAD~1'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('HEAD is now at def456a');
|
||||
});
|
||||
|
||||
it('should handle commit reset with mixed mode', async () => {
|
||||
const result = await command.execute(['--mixed', 'HEAD~1']);
|
||||
const result = await command.execute(['--mixed', 'HEAD~1'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('HEAD is now at def456a');
|
||||
});
|
||||
|
||||
it('should return error for soft reset (not implemented)', async () => {
|
||||
const result = await command.execute(['--soft', 'HEAD~1']);
|
||||
const result = await command.execute(['--soft', 'HEAD~1'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('--soft reset is not implemented');
|
||||
});
|
||||
|
||||
it('should handle invalid commit reference', async () => {
|
||||
const result = await command.execute(['abcdef12345']);
|
||||
const result = await command.execute(['abcdef12345'], '/test/repo');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain("fatal: ambiguous argument 'abcdef12345'");
|
||||
|
||||
@@ -12,19 +12,17 @@ export class GitResetCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -33,10 +31,10 @@ export class GitResetCommand implements GitSubcommand {
|
||||
|
||||
if (isFileReset) {
|
||||
// Reset specific files or all files (unstage)
|
||||
return await this.resetFiles(files);
|
||||
return await this.resetFiles(files, cwd);
|
||||
} else {
|
||||
// Reset to commit
|
||||
return await this.resetToCommit(target, mode);
|
||||
return await this.resetToCommit(target, mode, cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -89,7 +87,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
return { mode, target, files, isFileReset };
|
||||
}
|
||||
|
||||
private async resetFiles(files: string[]): Promise<ShellCommandResult> {
|
||||
private async resetFiles(files: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const resetFiles: string[] = [];
|
||||
const errors: string[] = [];
|
||||
@@ -99,7 +97,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
try {
|
||||
// Get the status matrix to find all staged files
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Find files that are staged (differ from HEAD)
|
||||
@@ -117,7 +115,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
for (const file of stagedFiles) {
|
||||
try {
|
||||
await this.git.resetIndex({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: file,
|
||||
});
|
||||
resetFiles.push(file);
|
||||
@@ -134,7 +132,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
try {
|
||||
// Remove from staging area (reset to HEAD)
|
||||
await this.git.resetIndex({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: file,
|
||||
});
|
||||
resetFiles.push(file);
|
||||
@@ -159,14 +157,13 @@ export class GitResetCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async resetToCommit(target: string, mode: 'soft' | 'mixed' | 'hard'): Promise<ShellCommandResult> {
|
||||
private async resetToCommit(target: string, mode: 'soft' | 'mixed' | 'hard', cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Resolve the target commit
|
||||
let targetOid: string;
|
||||
try {
|
||||
targetOid = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: target,
|
||||
});
|
||||
} catch {
|
||||
@@ -177,8 +174,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
let currentOid: string;
|
||||
try {
|
||||
currentOid = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
});
|
||||
} catch {
|
||||
@@ -205,7 +201,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Detached HEAD state
|
||||
@@ -215,14 +211,14 @@ export class GitResetCommand implements GitSubcommand {
|
||||
if (currentOid !== targetOid) {
|
||||
if (currentBranch) {
|
||||
await this.git.writeRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: `refs/heads/${currentBranch}`,
|
||||
value: targetOid,
|
||||
force: true,
|
||||
});
|
||||
} else {
|
||||
await this.git.writeRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
value: targetOid,
|
||||
force: true,
|
||||
@@ -233,7 +229,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
// Reset the index only (not the working directory)
|
||||
// Using noCheckout: true to only update the index, not the working directory
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: targetOid,
|
||||
noCheckout: true,
|
||||
noUpdateHead: true,
|
||||
@@ -253,7 +249,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
|
||||
// Get the commit object to ensure it exists
|
||||
await this.git.readCommit({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: targetOid,
|
||||
});
|
||||
|
||||
@@ -261,7 +257,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Detached HEAD state
|
||||
@@ -272,14 +268,14 @@ export class GitResetCommand implements GitSubcommand {
|
||||
if (currentOid !== targetOid) {
|
||||
if (currentBranch) {
|
||||
await this.git.writeRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: `refs/heads/${currentBranch}`,
|
||||
value: targetOid,
|
||||
force: true,
|
||||
});
|
||||
} else {
|
||||
await this.git.writeRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: 'HEAD',
|
||||
value: targetOid,
|
||||
force: true,
|
||||
@@ -291,7 +287,7 @@ export class GitResetCommand implements GitSubcommand {
|
||||
// Using noUpdateHead: true because we already updated HEAD above
|
||||
// Using force: true to overwrite any local changes
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: targetOid,
|
||||
force: true,
|
||||
noUpdateHead: true,
|
||||
@@ -310,4 +306,4 @@ export class GitResetCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to reset: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -319,12 +319,11 @@ describe('GitShowCommand', () => {
|
||||
showCommand = new GitShowCommand({
|
||||
git: mockGit as Git,
|
||||
fs: mockFS as JSRuntimeFS,
|
||||
pwd: mockPwd,
|
||||
});
|
||||
});
|
||||
|
||||
it('should show basic commit information', async () => {
|
||||
const result = await showCommand.execute(['simple-commit']);
|
||||
const result = await showCommand.execute(['simple-commit'], mockPwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('commit simple-commit');
|
||||
@@ -333,7 +332,7 @@ describe('GitShowCommand', () => {
|
||||
});
|
||||
|
||||
it('should handle initial commits properly', async () => {
|
||||
const result = await showCommand.execute(['initial-commit']);
|
||||
const result = await showCommand.execute(['initial-commit'], mockPwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Initial commit');
|
||||
@@ -344,7 +343,7 @@ describe('GitShowCommand', () => {
|
||||
|
||||
it('should resolve HEAD~1 correctly', async () => {
|
||||
// First, check that the implementation correctly resolves HEAD~1
|
||||
const result = await showCommand.execute(['HEAD~1']);
|
||||
const result = await showCommand.execute(['HEAD~1'], mockPwd);
|
||||
|
||||
// Verify that resolveRef was called with HEAD
|
||||
expect(mockGit.resolveRef).toHaveBeenCalledWith({
|
||||
@@ -363,7 +362,7 @@ describe('GitShowCommand', () => {
|
||||
});
|
||||
|
||||
it('should resolve HEAD^2 correctly', async () => {
|
||||
const result = await showCommand.execute(['HEAD^2']);
|
||||
const result = await showCommand.execute(['HEAD^2'], mockPwd);
|
||||
|
||||
// Verify that resolveRef was called with HEAD
|
||||
expect(mockGit.resolveRef).toHaveBeenCalledWith({
|
||||
@@ -411,7 +410,7 @@ describe('GitShowCommand', () => {
|
||||
});
|
||||
|
||||
// Execute the command
|
||||
const result = await showCommand.execute(['simple-commit']);
|
||||
const result = await showCommand.execute(['simple-commit'], mockPwd);
|
||||
|
||||
// Check that the command output contains the commit message
|
||||
expect(result.stdout).toContain('Simple changes');
|
||||
@@ -569,7 +568,7 @@ describe('GitShowCommand', () => {
|
||||
});
|
||||
|
||||
// Execute the command
|
||||
const result = await showCommand.execute(['diff-test-commit']);
|
||||
const result = await showCommand.execute(['diff-test-commit'], mockPwd);
|
||||
|
||||
// Debug output
|
||||
console.log('Test output:', result.stdout);
|
||||
@@ -596,7 +595,7 @@ describe('GitShowCommand', () => {
|
||||
});
|
||||
|
||||
it('should return an error for invalid references', async () => {
|
||||
const result = await showCommand.execute(['non-existent-ref']);
|
||||
const result = await showCommand.execute(['non-existent-ref'], mockPwd);
|
||||
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
expect(result.stderr).toContain('bad revision');
|
||||
@@ -605,7 +604,7 @@ describe('GitShowCommand', () => {
|
||||
it('should return an error when not in a git repository', async () => {
|
||||
mockFS.stat = vi.fn().mockRejectedValue(new Error('Not a directory'));
|
||||
|
||||
const result = await showCommand.execute([]);
|
||||
const result = await showCommand.execute([], mockPwd);
|
||||
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
expect(result.stderr).toContain('not a git repository');
|
||||
|
||||
@@ -11,19 +11,17 @@ export class GitShowCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -31,12 +29,12 @@ export class GitShowCommand implements GitSubcommand {
|
||||
const { commit } = this.parseArgs(args);
|
||||
|
||||
// Resolve the commit reference (handling relative refs like HEAD~1)
|
||||
const resolvedCommit = await this.resolveCommitRef(commit);
|
||||
const resolvedCommit = await this.resolveCommitRef(commit, cwd);
|
||||
if (!resolvedCommit) {
|
||||
return createErrorResult(`fatal: bad revision '${commit}'`);
|
||||
}
|
||||
|
||||
return await this.showCommit(resolvedCommit);
|
||||
return await this.showCommit(resolvedCommit, cwd);
|
||||
|
||||
} catch (error) {
|
||||
return createErrorResult(`git show: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
@@ -59,7 +57,7 @@ export class GitShowCommand implements GitSubcommand {
|
||||
/**
|
||||
* Resolve a commit reference, handling special syntax like HEAD~N and HEAD^N
|
||||
*/
|
||||
private async resolveCommitRef(ref: string): Promise<string | null> {
|
||||
private async resolveCommitRef(ref: string, cwd: string): Promise<string | null> {
|
||||
try {
|
||||
// Handle HEAD~N syntax (ancestor)
|
||||
const tildeMatch = ref.match(/^(.+)~(\d+)$/);
|
||||
@@ -69,17 +67,17 @@ export class GitShowCommand implements GitSubcommand {
|
||||
|
||||
if (numSteps === 0) {
|
||||
// ~0 is the commit itself
|
||||
return this.resolveCommitRef(baseRef);
|
||||
return this.resolveCommitRef(baseRef, cwd);
|
||||
}
|
||||
|
||||
// First resolve the base reference
|
||||
const baseCommit = await this.resolveCommitRef(baseRef);
|
||||
const baseCommit = await this.resolveCommitRef(baseRef, cwd);
|
||||
if (!baseCommit) {
|
||||
throw new Error(`Could not resolve base reference: ${baseRef}`);
|
||||
}
|
||||
|
||||
// Walk back N steps in history
|
||||
return this.walkBackNCommits(baseCommit, numSteps);
|
||||
return this.walkBackNCommits(baseCommit, numSteps, cwd);
|
||||
}
|
||||
|
||||
// Handle HEAD^N syntax (Nth parent)
|
||||
@@ -89,19 +87,19 @@ export class GitShowCommand implements GitSubcommand {
|
||||
const parentIndex = parentNum ? parseInt(parentNum, 10) - 1 : 0; // Default to first parent
|
||||
|
||||
// First resolve the base reference
|
||||
const baseCommit = await this.resolveCommitRef(baseRef);
|
||||
const baseCommit = await this.resolveCommitRef(baseRef, cwd);
|
||||
if (!baseCommit) {
|
||||
throw new Error(`Could not resolve base reference: ${baseRef}`);
|
||||
}
|
||||
|
||||
// Get the specified parent
|
||||
return this.getNthParent(baseCommit, parentIndex);
|
||||
return this.getNthParent(baseCommit, parentIndex, cwd);
|
||||
}
|
||||
|
||||
// Handle simple reference (direct resolution)
|
||||
try {
|
||||
return await this.git.resolveRef({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref,
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -119,14 +117,14 @@ export class GitShowCommand implements GitSubcommand {
|
||||
/**
|
||||
* Walk back N commits from the given commit
|
||||
*/
|
||||
private async walkBackNCommits(startCommit: string, steps: number): Promise<string | null> {
|
||||
private async walkBackNCommits(startCommit: string, steps: number, cwd: string): Promise<string | null> {
|
||||
try {
|
||||
let currentCommit = startCommit;
|
||||
|
||||
for (let i = 0; i < steps; i++) {
|
||||
// Get the commit object
|
||||
const commit = await this.git.readCommit({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: currentCommit,
|
||||
});
|
||||
|
||||
@@ -149,11 +147,11 @@ export class GitShowCommand implements GitSubcommand {
|
||||
/**
|
||||
* Get the Nth parent of the given commit
|
||||
*/
|
||||
private async getNthParent(commit: string, parentIndex: number): Promise<string | null> {
|
||||
private async getNthParent(commit: string, parentIndex: number, cwd: string): Promise<string | null> {
|
||||
try {
|
||||
// Get the commit object
|
||||
const commitObj = await this.git.readCommit({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: commit,
|
||||
});
|
||||
|
||||
@@ -175,11 +173,11 @@ export class GitShowCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async showCommit(commitRef: string): Promise<ShellCommandResult> {
|
||||
private async showCommit(commitRef: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Get the commit
|
||||
const commits = await this.git.log({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
depth: 1,
|
||||
ref: commitRef,
|
||||
});
|
||||
@@ -220,7 +218,7 @@ export class GitShowCommand implements GitSubcommand {
|
||||
|
||||
// Get the tree for this commit
|
||||
const tree = await this.git.readTree({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: commit.oid,
|
||||
});
|
||||
|
||||
@@ -237,12 +235,12 @@ export class GitShowCommand implements GitSubcommand {
|
||||
|
||||
// Get the trees for both commits
|
||||
const parentTree = await this.git.readTree({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: parentCommit,
|
||||
});
|
||||
|
||||
const commitTree = await this.git.readTree({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: commit.oid,
|
||||
});
|
||||
|
||||
@@ -285,7 +283,7 @@ export class GitShowCommand implements GitSubcommand {
|
||||
const result = new Map<string, TreeEntry>();
|
||||
try {
|
||||
const tree = await this.git.readTree({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid,
|
||||
});
|
||||
|
||||
@@ -435,7 +433,7 @@ export class GitShowCommand implements GitSubcommand {
|
||||
let parentContent = '';
|
||||
try {
|
||||
const parentBlob = await this.git.readBlob({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: parentCommit,
|
||||
filepath: change.path,
|
||||
});
|
||||
@@ -449,7 +447,7 @@ export class GitShowCommand implements GitSubcommand {
|
||||
let currentContent = '';
|
||||
try {
|
||||
const currentBlob = await this.git.readBlob({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
oid: commit.oid,
|
||||
filepath: change.path,
|
||||
});
|
||||
@@ -689,4 +687,4 @@ export class GitShowCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to show commit: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,6 @@ describe('GitStashCommand', () => {
|
||||
stashCommand = new GitStashCommand({
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: testCwd,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -78,14 +77,14 @@ describe('GitStashCommand', () => {
|
||||
it('should return error when not in a git repository', async () => {
|
||||
vi.mocked(mockFS.stat).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await stashCommand.execute(['push']);
|
||||
const result = await stashCommand.execute(['push'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should show help with --help flag', async () => {
|
||||
const result = await stashCommand.execute(['--help']);
|
||||
const result = await stashCommand.execute(['--help'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('git stash list');
|
||||
@@ -99,7 +98,7 @@ describe('GitStashCommand', () => {
|
||||
['unchanged.txt', 1, 1, 1], // No changes
|
||||
]);
|
||||
|
||||
const result = await stashCommand.execute(['push']);
|
||||
const result = await stashCommand.execute(['push'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('No local changes to save');
|
||||
@@ -119,7 +118,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['push']);
|
||||
const result = await stashCommand.execute(['push'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Saved working directory and index state');
|
||||
@@ -145,7 +144,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['push', '-m', 'my custom message']);
|
||||
const result = await stashCommand.execute(['push', '-m', 'my custom message'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('my custom message');
|
||||
@@ -165,7 +164,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['push', '-u']);
|
||||
const result = await stashCommand.execute(['push', '-u'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Saved working directory');
|
||||
@@ -183,7 +182,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['push']);
|
||||
const result = await stashCommand.execute(['push'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockGit.checkout).toHaveBeenCalledWith({
|
||||
@@ -207,7 +206,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['push']);
|
||||
const result = await stashCommand.execute(['push'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockGit.resetIndex).toHaveBeenCalledWith({
|
||||
@@ -221,7 +220,7 @@ describe('GitStashCommand', () => {
|
||||
it('should return empty when no stashes', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await stashCommand.execute(['list']);
|
||||
const result = await stashCommand.execute(['list'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
@@ -234,7 +233,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['list']);
|
||||
const result = await stashCommand.execute(['list'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('stash@{0}: first stash');
|
||||
@@ -246,7 +245,7 @@ describe('GitStashCommand', () => {
|
||||
it('should return error when no stashes', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await stashCommand.execute(['show']);
|
||||
const result = await stashCommand.execute(['show'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('No stash entries found');
|
||||
@@ -267,7 +266,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['show']);
|
||||
const result = await stashCommand.execute(['show'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('M\tmodified.txt');
|
||||
@@ -293,7 +292,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['show', 'stash@{1}']);
|
||||
const result = await stashCommand.execute(['show', 'stash@{1}'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('file1.txt');
|
||||
@@ -305,7 +304,7 @@ describe('GitStashCommand', () => {
|
||||
it('should return error when no stashes', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await stashCommand.execute(['apply']);
|
||||
const result = await stashCommand.execute(['apply'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('No stash entries found');
|
||||
@@ -325,7 +324,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['apply']);
|
||||
const result = await stashCommand.execute(['apply'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('test stash');
|
||||
@@ -358,7 +357,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['apply', 'stash@{1}']);
|
||||
const result = await stashCommand.execute(['apply', 'stash@{1}'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
@@ -379,7 +378,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['apply']);
|
||||
const result = await stashCommand.execute(['apply'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockFS.unlink).toHaveBeenCalledWith(expect.stringContaining('deleted.txt'));
|
||||
@@ -399,7 +398,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['pop']);
|
||||
const result = await stashCommand.execute(['pop'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
@@ -418,7 +417,7 @@ describe('GitStashCommand', () => {
|
||||
it('should return error when no stashes', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await stashCommand.execute(['drop']);
|
||||
const result = await stashCommand.execute(['drop'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('No stash entries found');
|
||||
@@ -431,7 +430,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['drop']);
|
||||
const result = await stashCommand.execute(['drop'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Dropped stash@{0}');
|
||||
@@ -449,7 +448,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['drop', 'stash@{1}']);
|
||||
const result = await stashCommand.execute(['drop', 'stash@{1}'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Dropped stash@{1}');
|
||||
@@ -461,7 +460,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['drop', 'stash@{5}']);
|
||||
const result = await stashCommand.execute(['drop', 'stash@{5}'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('is not a valid reference');
|
||||
@@ -476,7 +475,7 @@ describe('GitStashCommand', () => {
|
||||
];
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(mockStashes));
|
||||
|
||||
const result = await stashCommand.execute(['clear']);
|
||||
const result = await stashCommand.execute(['clear'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
@@ -501,7 +500,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute(['save']);
|
||||
const result = await stashCommand.execute(['save'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Saved working directory');
|
||||
@@ -521,7 +520,7 @@ describe('GitStashCommand', () => {
|
||||
return Promise.resolve('');
|
||||
});
|
||||
|
||||
const result = await stashCommand.execute([]);
|
||||
const result = await stashCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('Saved working directory');
|
||||
|
||||
@@ -24,19 +24,17 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -47,26 +45,26 @@ export class GitStashCommand implements GitSubcommand {
|
||||
switch (subcommand) {
|
||||
case 'push':
|
||||
case 'save':
|
||||
return await this.stashPush(subcommandArgs);
|
||||
return await this.stashPush(subcommandArgs, cwd);
|
||||
case 'list':
|
||||
return await this.stashList();
|
||||
return await this.stashList(cwd);
|
||||
case 'apply':
|
||||
return await this.stashApply(subcommandArgs);
|
||||
return await this.stashApply(subcommandArgs, cwd);
|
||||
case 'pop':
|
||||
return await this.stashPop(subcommandArgs);
|
||||
return await this.stashPop(subcommandArgs, cwd);
|
||||
case 'drop':
|
||||
return await this.stashDrop(subcommandArgs);
|
||||
return await this.stashDrop(subcommandArgs, cwd);
|
||||
case 'clear':
|
||||
return await this.stashClear();
|
||||
return await this.stashClear(cwd);
|
||||
case 'show':
|
||||
return await this.stashShow(subcommandArgs);
|
||||
return await this.stashShow(subcommandArgs, cwd);
|
||||
case '--help':
|
||||
case '-h':
|
||||
return this.showHelp();
|
||||
return this.showHelp(cwd);
|
||||
default:
|
||||
// If first arg doesn't look like a subcommand, treat it as 'push'
|
||||
if (subcommand.startsWith('-')) {
|
||||
return await this.stashPush(args);
|
||||
return await this.stashPush(args, cwd);
|
||||
}
|
||||
return createErrorResult(`git stash: '${subcommand}' is not a stash command. See 'git stash --help'.`);
|
||||
}
|
||||
@@ -76,7 +74,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashPush(args: string[]): Promise<ShellCommandResult> {
|
||||
private async stashPush(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Parse arguments
|
||||
let message = 'WIP on';
|
||||
@@ -96,13 +94,13 @@ export class GitStashCommand implements GitSubcommand {
|
||||
// Get current branch
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({ dir: this.pwd }) || null;
|
||||
currentBranch = await this.git.currentBranch({ dir: cwd }) || null;
|
||||
} catch {
|
||||
currentBranch = 'HEAD';
|
||||
}
|
||||
|
||||
// Get status to find changed files
|
||||
const statusMatrix = await this.git.statusMatrix({ dir: this.pwd });
|
||||
const statusMatrix = await this.git.statusMatrix({ dir: cwd });
|
||||
|
||||
const filesToStash: StashEntry['files'] = [];
|
||||
const stagedFiles: string[] = [];
|
||||
@@ -124,7 +122,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
// Untracked files (only with -u flag)
|
||||
if (includeUntracked && headStatus === 0 && stageStatus === 0 && workdirStatus === 2) {
|
||||
const content = await this.fs.readFile(join(this.pwd, filepath), 'utf8');
|
||||
const content = await this.fs.readFile(join(cwd, filepath), 'utf8');
|
||||
filesToStash.push({
|
||||
filepath,
|
||||
content,
|
||||
@@ -141,7 +139,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
// Staged new files or modified files (staged changes)
|
||||
else if (hasStagedChanges && workdirStatus === 2) {
|
||||
const content = await this.fs.readFile(join(this.pwd, filepath), 'utf8');
|
||||
const content = await this.fs.readFile(join(cwd, filepath), 'utf8');
|
||||
const status = headStatus === 0 ? 'added' : 'modified';
|
||||
filesToStash.push({
|
||||
filepath,
|
||||
@@ -151,7 +149,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
// Unstaged modifications in working directory
|
||||
else if (hasWorkdirChanges && workdirStatus === 2) {
|
||||
const content = await this.fs.readFile(join(this.pwd, filepath), 'utf8');
|
||||
const content = await this.fs.readFile(join(cwd, filepath), 'utf8');
|
||||
filesToStash.push({
|
||||
filepath,
|
||||
content,
|
||||
@@ -177,7 +175,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
};
|
||||
|
||||
// Save stash
|
||||
await this.saveStash(stashEntry);
|
||||
await this.saveStash(stashEntry, cwd);
|
||||
|
||||
// Revert changes in working directory
|
||||
for (const file of filesToStash) {
|
||||
@@ -185,21 +183,21 @@ export class GitStashCommand implements GitSubcommand {
|
||||
if (file.status === 'deleted') {
|
||||
// Restore deleted file from HEAD
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepaths: [file.filepath],
|
||||
force: true,
|
||||
});
|
||||
} else if (file.status === 'modified') {
|
||||
// Restore file from HEAD using checkout
|
||||
await this.git.checkout({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepaths: [file.filepath],
|
||||
force: true,
|
||||
});
|
||||
} else if (file.status === 'added') {
|
||||
// Delete new file (it doesn't exist in HEAD)
|
||||
try {
|
||||
await this.fs.unlink(join(this.pwd, file.filepath));
|
||||
await this.fs.unlink(join(cwd, file.filepath));
|
||||
} catch {
|
||||
// File might already be gone
|
||||
}
|
||||
@@ -213,7 +211,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
for (const file of stagedFiles) {
|
||||
try {
|
||||
await this.git.resetIndex({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath: file,
|
||||
});
|
||||
} catch {
|
||||
@@ -228,9 +226,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashList(): Promise<ShellCommandResult> {
|
||||
private async stashList(cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const stashes = await this.loadStashes();
|
||||
const stashes = await this.loadStashes(cwd);
|
||||
|
||||
if (stashes.length === 0) {
|
||||
return createSuccessResult('');
|
||||
@@ -247,9 +245,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashApply(args: string[]): Promise<ShellCommandResult> {
|
||||
private async stashApply(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const stashes = await this.loadStashes();
|
||||
const stashes = await this.loadStashes(cwd);
|
||||
|
||||
if (stashes.length === 0) {
|
||||
return createErrorResult('No stash entries found.');
|
||||
@@ -257,7 +255,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
// Parse stash reference (e.g., stash@{0} or just 0)
|
||||
const stashRef = args[0] || 'stash@{0}';
|
||||
const index = this.parseStashRef(stashRef);
|
||||
const index = this.parseStashRef(stashRef, cwd);
|
||||
|
||||
if (index < 0 || index >= stashes.length) {
|
||||
return createErrorResult(`stash@{${index}} is not a valid reference`);
|
||||
@@ -271,19 +269,19 @@ export class GitStashCommand implements GitSubcommand {
|
||||
if (file.status === 'deleted') {
|
||||
// Delete the file
|
||||
try {
|
||||
await this.fs.unlink(join(this.pwd, file.filepath));
|
||||
await this.fs.unlink(join(cwd, file.filepath));
|
||||
} catch {
|
||||
// File might not exist
|
||||
}
|
||||
} else {
|
||||
// Create file with stashed content
|
||||
const dir = join(this.pwd, file.filepath, '..');
|
||||
const dir = join(cwd, file.filepath, '..');
|
||||
try {
|
||||
await this.fs.mkdir(dir, { recursive: true });
|
||||
} catch {
|
||||
// Directory might exist
|
||||
}
|
||||
await this.fs.writeFile(join(this.pwd, file.filepath), file.content);
|
||||
await this.fs.writeFile(join(cwd, file.filepath), file.content);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to apply ${file.filepath}:`, error);
|
||||
@@ -294,7 +292,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
for (const filepath of stash.stagedFiles) {
|
||||
try {
|
||||
await this.git.add({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
filepath,
|
||||
});
|
||||
} catch {
|
||||
@@ -309,17 +307,17 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashPop(args: string[]): Promise<ShellCommandResult> {
|
||||
private async stashPop(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Apply the stash first
|
||||
const result = await this.stashApply(args);
|
||||
const result = await this.stashApply(args, cwd);
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// If apply succeeded, drop the stash
|
||||
const dropResult = await this.stashDrop(args);
|
||||
const dropResult = await this.stashDrop(args, cwd);
|
||||
|
||||
if (dropResult.exitCode !== 0) {
|
||||
return createErrorResult(`Applied stash successfully but failed to drop: ${dropResult.stderr}`);
|
||||
@@ -332,9 +330,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashDrop(args: string[]): Promise<ShellCommandResult> {
|
||||
private async stashDrop(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const stashes = await this.loadStashes();
|
||||
const stashes = await this.loadStashes(cwd);
|
||||
|
||||
if (stashes.length === 0) {
|
||||
return createErrorResult('No stash entries found.');
|
||||
@@ -342,7 +340,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
// Parse stash reference
|
||||
const stashRef = args[0] || 'stash@{0}';
|
||||
const index = this.parseStashRef(stashRef);
|
||||
const index = this.parseStashRef(stashRef, cwd);
|
||||
|
||||
if (index < 0 || index >= stashes.length) {
|
||||
return createErrorResult(`stash@{${index}} is not a valid reference`);
|
||||
@@ -350,7 +348,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
// Remove the stash
|
||||
stashes.splice(index, 1);
|
||||
await this.saveStashes(stashes);
|
||||
await this.saveStashes(stashes, cwd);
|
||||
|
||||
return createSuccessResult(`Dropped stash@{${index}}\n`);
|
||||
|
||||
@@ -359,9 +357,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashClear(): Promise<ShellCommandResult> {
|
||||
private async stashClear(cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
await this.saveStashes([]);
|
||||
await this.saveStashes([], cwd);
|
||||
return createSuccessResult('');
|
||||
|
||||
} catch (error) {
|
||||
@@ -369,9 +367,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async stashShow(args: string[]): Promise<ShellCommandResult> {
|
||||
private async stashShow(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const stashes = await this.loadStashes();
|
||||
const stashes = await this.loadStashes(cwd);
|
||||
|
||||
if (stashes.length === 0) {
|
||||
return createErrorResult('No stash entries found.');
|
||||
@@ -379,7 +377,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
// Parse stash reference
|
||||
const stashRef = args[0] || 'stash@{0}';
|
||||
const index = this.parseStashRef(stashRef);
|
||||
const index = this.parseStashRef(stashRef, cwd);
|
||||
|
||||
if (index < 0 || index >= stashes.length) {
|
||||
return createErrorResult(`stash@{${index}} is not a valid reference`);
|
||||
@@ -405,7 +403,7 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private parseStashRef(ref: string): number {
|
||||
private parseStashRef(ref: string, _cwd: string): number {
|
||||
// Parse stash@{N} or just N
|
||||
const match = ref.match(/^(?:stash@\{)?(\d+)\}?$/);
|
||||
if (match) {
|
||||
@@ -414,9 +412,9 @@ export class GitStashCommand implements GitSubcommand {
|
||||
return 0; // Default to stash@{0}
|
||||
}
|
||||
|
||||
private async loadStashes(): Promise<StashEntry[]> {
|
||||
private async loadStashes(cwd: string): Promise<StashEntry[]> {
|
||||
try {
|
||||
const stashPath = join(this.pwd, '.git', 'shakespeare_stash.json');
|
||||
const stashPath = join(cwd, '.git', 'shakespeare_stash.json');
|
||||
const data = await this.fs.readFile(stashPath, 'utf8');
|
||||
return JSON.parse(data) as StashEntry[];
|
||||
} catch {
|
||||
@@ -424,18 +422,18 @@ export class GitStashCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async saveStash(stash: StashEntry): Promise<void> {
|
||||
const stashes = await this.loadStashes();
|
||||
private async saveStash(stash: StashEntry, cwd: string): Promise<void> {
|
||||
const stashes = await this.loadStashes(cwd);
|
||||
stashes.unshift(stash); // Add to beginning of array
|
||||
await this.saveStashes(stashes);
|
||||
await this.saveStashes(stashes, cwd);
|
||||
}
|
||||
|
||||
private async saveStashes(stashes: StashEntry[]): Promise<void> {
|
||||
const stashPath = join(this.pwd, '.git', 'shakespeare_stash.json');
|
||||
private async saveStashes(stashes: StashEntry[], cwd: string): Promise<void> {
|
||||
const stashPath = join(cwd, '.git', 'shakespeare_stash.json');
|
||||
await this.fs.writeFile(stashPath, JSON.stringify(stashes, null, 2));
|
||||
}
|
||||
|
||||
private showHelp(): ShellCommandResult {
|
||||
private showHelp(_cwd: string): ShellCommandResult {
|
||||
const helpText = `usage: git stash list [<options>]
|
||||
or: git stash show [<stash>]
|
||||
or: git stash drop [<stash>]
|
||||
@@ -446,4 +444,4 @@ export class GitStashCommand implements GitSubcommand {
|
||||
|
||||
return createSuccessResult(helpText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,6 @@ describe('GitStatusCommand', () => {
|
||||
statusCommand = new GitStatusCommand({
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: testCwd,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,7 +65,7 @@ describe('GitStatusCommand', () => {
|
||||
// Mock stat to throw error for .git directory
|
||||
vi.mocked(mockFS.stat).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
@@ -77,7 +76,7 @@ describe('GitStatusCommand', () => {
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
@@ -93,7 +92,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
@@ -109,7 +108,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
@@ -125,7 +124,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
@@ -141,7 +140,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
@@ -159,7 +158,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute(['--porcelain']);
|
||||
const result = await statusCommand.execute(['--porcelain'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('?? untracked.txt');
|
||||
@@ -175,7 +174,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute(['--short']);
|
||||
const result = await statusCommand.execute(['--short'], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('?? untracked.txt');
|
||||
@@ -191,7 +190,7 @@ describe('GitStatusCommand', () => {
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('feature-branch');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch feature-branch');
|
||||
@@ -209,7 +208,7 @@ describe('GitStatusCommand', () => {
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue(undefined);
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
const result = await statusCommand.execute([], testCwd);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('HEAD detached at unknown');
|
||||
|
||||
@@ -11,19 +11,18 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -32,23 +31,23 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
|
||||
// Get the status matrix
|
||||
const statusMatrix = await this.git.statusMatrix({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
// Get current branch
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
// Might be an empty repository
|
||||
}
|
||||
|
||||
if (options.porcelain || options.short) {
|
||||
return this.formatPorcelainStatus(statusMatrix);
|
||||
return this.formatPorcelainStatus(statusMatrix, cwd);
|
||||
} else {
|
||||
return this.formatHumanStatus(statusMatrix, currentBranch);
|
||||
return this.formatHumanStatus(statusMatrix, currentBranch, cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -70,7 +69,7 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
return options;
|
||||
}
|
||||
|
||||
private formatPorcelainStatus(statusMatrix: Array<[string, number, number, number]>): ShellCommandResult {
|
||||
private formatPorcelainStatus(statusMatrix: Array<[string, number, number, number]>, _cwd: string): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const [filepath, headStatus, workdirStatus, stageStatus] of statusMatrix) {
|
||||
@@ -108,7 +107,7 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
return createSuccessResult(lines.join('\n') + (lines.length > 0 ? '\n' : ''));
|
||||
}
|
||||
|
||||
private formatHumanStatus(statusMatrix: Array<[string, number, number, number]>, currentBranch: string | null): ShellCommandResult {
|
||||
private formatHumanStatus(statusMatrix: Array<[string, number, number, number]>, currentBranch: string | null, _cwd: string): ShellCommandResult {
|
||||
const lines: string[] = [];
|
||||
|
||||
// Show current branch
|
||||
@@ -186,4 +185,4 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
lines.push('');
|
||||
return createSuccessResult(lines.join('\n'));
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
-21
@@ -12,19 +12,17 @@ export class GitTagCommand implements GitSubcommand {
|
||||
|
||||
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> {
|
||||
async execute(args: string[], cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
// Check if we're in a git repository
|
||||
try {
|
||||
await this.fs.stat(`${this.pwd}/.git`);
|
||||
await this.fs.stat(`${cwd}/.git`);
|
||||
} catch {
|
||||
return createErrorResult('fatal: not a git repository (or any of the parent directories): .git');
|
||||
}
|
||||
@@ -33,13 +31,13 @@ export class GitTagCommand implements GitSubcommand {
|
||||
|
||||
switch (action) {
|
||||
case 'list':
|
||||
return await this.listTags();
|
||||
return await this.listTags(cwd);
|
||||
case 'create':
|
||||
return await this.createTag(tagName!, commit);
|
||||
return await this.createTag(tagName!, cwd, commit);
|
||||
case 'delete':
|
||||
return await this.deleteTag(tagName!);
|
||||
return await this.deleteTag(tagName!, cwd);
|
||||
default:
|
||||
return await this.listTags();
|
||||
return await this.listTags(cwd);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -84,10 +82,10 @@ export class GitTagCommand implements GitSubcommand {
|
||||
return { action, tagName, commit, options };
|
||||
}
|
||||
|
||||
private async listTags(): Promise<ShellCommandResult> {
|
||||
private async listTags(cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
const tags = await this.git.listTags({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (tags.length === 0) {
|
||||
@@ -104,7 +102,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async createTag(tagName: string, commit?: string): Promise<ShellCommandResult> {
|
||||
private async createTag(tagName: string, cwd: string, commit?: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
if (!tagName) {
|
||||
return createErrorResult('usage: git tag <tagname> [<commit>]');
|
||||
@@ -113,8 +111,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
// Check if tag already exists
|
||||
try {
|
||||
const tags = await this.git.listTags({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (tags.includes(tagName)) {
|
||||
@@ -128,8 +125,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
let targetOid: string;
|
||||
try {
|
||||
targetOid = await this.git.resolveRef({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: commit || 'HEAD',
|
||||
});
|
||||
} catch {
|
||||
@@ -138,7 +134,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
|
||||
// Create the tag
|
||||
await this.git.tag({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: tagName,
|
||||
object: targetOid,
|
||||
});
|
||||
@@ -150,7 +146,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteTag(tagName: string): Promise<ShellCommandResult> {
|
||||
private async deleteTag(tagName: string, cwd: string): Promise<ShellCommandResult> {
|
||||
try {
|
||||
if (!tagName) {
|
||||
return createErrorResult('usage: git tag -d <tagname>');
|
||||
@@ -159,8 +155,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
// Check if tag exists
|
||||
try {
|
||||
const tags = await this.git.listTags({
|
||||
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
});
|
||||
|
||||
if (!tags.includes(tagName)) {
|
||||
@@ -172,7 +167,7 @@ export class GitTagCommand implements GitSubcommand {
|
||||
|
||||
// Delete the tag
|
||||
await this.git.deleteTag({
|
||||
dir: this.pwd,
|
||||
dir: cwd,
|
||||
ref: tagName,
|
||||
});
|
||||
|
||||
@@ -182,4 +177,4 @@ export class GitTagCommand implements GitSubcommand {
|
||||
return createErrorResult(`Failed to delete tag: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user