mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Fix git status
This commit is contained in:
2 files changed
+247
-24
No files matched your search
@@ -0,0 +1,218 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { GitStatusCommand } from './status';
|
||||
import type { JSRuntimeFS } from '../../JSRuntime';
|
||||
import type { Git } from '../../git';
|
||||
|
||||
// Mock filesystem
|
||||
const createMockFS = (): JSRuntimeFS => ({
|
||||
readFile: vi.fn().mockResolvedValue('mock content'),
|
||||
writeFile: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
stat: vi.fn().mockImplementation((path: string) => {
|
||||
if (path.endsWith('/.git')) {
|
||||
return Promise.resolve({
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
size: 0,
|
||||
mtimeMs: Date.now(),
|
||||
});
|
||||
}
|
||||
throw new Error('ENOENT: no such file or directory');
|
||||
}),
|
||||
lstat: vi.fn().mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
size: 0,
|
||||
mtimeMs: Date.now(),
|
||||
}),
|
||||
unlink: vi.fn().mockResolvedValue(undefined),
|
||||
rmdir: vi.fn().mockResolvedValue(undefined),
|
||||
rename: vi.fn().mockResolvedValue(undefined),
|
||||
readlink: vi.fn().mockResolvedValue('mock link'),
|
||||
symlink: vi.fn().mockResolvedValue(undefined),
|
||||
});
|
||||
|
||||
// Mock Git
|
||||
const createMockGit = (): Git => ({
|
||||
statusMatrix: vi.fn(),
|
||||
currentBranch: vi.fn(),
|
||||
stat: vi.fn(),
|
||||
} as unknown as Git);
|
||||
|
||||
describe('GitStatusCommand', () => {
|
||||
let statusCommand: GitStatusCommand;
|
||||
let mockFS: JSRuntimeFS;
|
||||
let mockGit: Git;
|
||||
const testCwd = '/test';
|
||||
|
||||
beforeEach(() => {
|
||||
mockFS = createMockFS();
|
||||
mockGit = createMockGit();
|
||||
statusCommand = new GitStatusCommand({
|
||||
git: mockGit,
|
||||
fs: mockFS,
|
||||
pwd: testCwd,
|
||||
});
|
||||
});
|
||||
|
||||
it('should have correct command properties', () => {
|
||||
expect(statusCommand.name).toBe('status');
|
||||
expect(statusCommand.description).toBe('Show the working tree status');
|
||||
expect(statusCommand.usage).toBe('git status [--porcelain] [--short]');
|
||||
});
|
||||
|
||||
it('should return error when not in a git repository', async () => {
|
||||
// Mock stat to throw error for .git directory
|
||||
vi.mocked(mockFS.stat).mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('fatal: not a git repository');
|
||||
});
|
||||
|
||||
it('should show clean status when no changes', async () => {
|
||||
// Mock git status matrix with no changes
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
expect(result.stdout).toContain('nothing to commit, working tree clean');
|
||||
});
|
||||
|
||||
it('should show untracked files correctly', async () => {
|
||||
// Mock git status matrix with untracked file
|
||||
// [filepath, headStatus, workdirStatus, stageStatus]
|
||||
// Untracked: headStatus=0, stageStatus=0, workdirStatus=2
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['newfile.txt', 0, 2, 0]
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
expect(result.stdout).toContain('Untracked files:');
|
||||
expect(result.stdout).toContain('newfile.txt');
|
||||
});
|
||||
|
||||
it('should show staged files correctly', async () => {
|
||||
// Mock git status matrix with staged file
|
||||
// Staged new file: headStatus=0, stageStatus=2, workdirStatus=2
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['newfile.txt', 0, 2, 2]
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
expect(result.stdout).toContain('Changes to be committed:');
|
||||
expect(result.stdout).toContain('new file: newfile.txt');
|
||||
});
|
||||
|
||||
it('should show modified files correctly', async () => {
|
||||
// Mock git status matrix with modified file
|
||||
// Modified file: headStatus=1, stageStatus=1, workdirStatus=2
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['existing.txt', 1, 2, 1]
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
expect(result.stdout).toContain('Changes not staged for commit:');
|
||||
expect(result.stdout).toContain('modified: existing.txt');
|
||||
});
|
||||
|
||||
it('should show deleted files correctly', async () => {
|
||||
// Mock git status matrix with deleted file
|
||||
// Deleted file: headStatus=1, stageStatus=1, workdirStatus=0
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['deleted.txt', 1, 0, 1]
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch main');
|
||||
expect(result.stdout).toContain('Changes not staged for commit:');
|
||||
expect(result.stdout).toContain('deleted: deleted.txt');
|
||||
});
|
||||
|
||||
it('should format porcelain output correctly', async () => {
|
||||
// Mock git status matrix with various changes
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['untracked.txt', 0, 2, 0], // Untracked
|
||||
['staged.txt', 0, 2, 2], // Staged new file
|
||||
['modified.txt', 1, 2, 1], // Modified
|
||||
['deleted.txt', 1, 0, 1], // Deleted
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute(['--porcelain']);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('?? untracked.txt');
|
||||
expect(result.stdout).toContain('A staged.txt');
|
||||
expect(result.stdout).toContain(' M modified.txt');
|
||||
expect(result.stdout).toContain(' D deleted.txt');
|
||||
});
|
||||
|
||||
it('should handle short format', async () => {
|
||||
// Mock git status matrix with changes
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['untracked.txt', 0, 2, 0], // Untracked
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('main');
|
||||
|
||||
const result = await statusCommand.execute(['--short']);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('?? untracked.txt');
|
||||
});
|
||||
|
||||
it('should handle complex status matrix correctly', async () => {
|
||||
// Mock git status matrix with a complex scenario
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
|
||||
['file1.txt', 1, 2, 2], // Modified and staged
|
||||
['file2.txt', 0, 2, 0], // Untracked
|
||||
['file3.txt', 1, 0, 0], // Deleted (staged)
|
||||
['file4.txt', 1, 2, 1], // Modified (not staged)
|
||||
]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue('feature-branch');
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('On branch feature-branch');
|
||||
expect(result.stdout).toContain('Changes to be committed:');
|
||||
expect(result.stdout).toContain('modified: file1.txt');
|
||||
expect(result.stdout).toContain('deleted: file3.txt');
|
||||
expect(result.stdout).toContain('Untracked files:');
|
||||
expect(result.stdout).toContain('file2.txt');
|
||||
expect(result.stdout).toContain('Changes not staged for commit:');
|
||||
expect(result.stdout).toContain('modified: file4.txt');
|
||||
});
|
||||
|
||||
it('should handle detached HEAD state', async () => {
|
||||
// Mock git status matrix with no current branch
|
||||
vi.mocked(mockGit.statusMatrix).mockResolvedValue([]);
|
||||
vi.mocked(mockGit.currentBranch).mockResolvedValue(undefined);
|
||||
|
||||
const result = await statusCommand.execute([]);
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toContain('HEAD detached at unknown');
|
||||
expect(result.stdout).toContain('nothing to commit, working tree clean');
|
||||
});
|
||||
});
|
||||
@@ -39,7 +39,6 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
let currentBranch: string | null = null;
|
||||
try {
|
||||
currentBranch = await this.git.currentBranch({
|
||||
|
||||
dir: this.pwd,
|
||||
}) || null;
|
||||
} catch {
|
||||
@@ -78,22 +77,26 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
let indexStatus = ' ';
|
||||
let workingStatus = ' ';
|
||||
|
||||
// Determine index status (staged changes)
|
||||
if (headStatus === 0 && stageStatus === 1) {
|
||||
indexStatus = 'A'; // Added
|
||||
// Determine index status (staged changes - comparing HEAD to stage)
|
||||
if (headStatus === 0 && stageStatus === 2) {
|
||||
indexStatus = 'A'; // Added to stage
|
||||
} else if (headStatus === 1 && stageStatus === 0) {
|
||||
indexStatus = 'D'; // Deleted
|
||||
} else if (headStatus === 1 && stageStatus === 1 && headStatus !== stageStatus) {
|
||||
indexStatus = 'M'; // Modified
|
||||
indexStatus = 'D'; // Deleted from stage
|
||||
} else if (headStatus === 1 && stageStatus === 2) {
|
||||
indexStatus = 'M'; // Modified in stage
|
||||
}
|
||||
|
||||
// Determine working directory status
|
||||
if (headStatus === 0 && stageStatus === 0 && workdirStatus === 1) {
|
||||
workingStatus = '?'; // Untracked
|
||||
// Determine working directory status (comparing stage to working dir)
|
||||
if (headStatus === 0 && stageStatus === 0 && workdirStatus === 2) {
|
||||
// Untracked files show as ?? in porcelain format
|
||||
indexStatus = '?';
|
||||
workingStatus = '?';
|
||||
} else if (stageStatus === 2 && workdirStatus === 0) {
|
||||
workingStatus = 'D'; // Deleted in working dir
|
||||
} else if (stageStatus === 1 && workdirStatus === 0) {
|
||||
workingStatus = 'D'; // Deleted
|
||||
} else if (stageStatus !== workdirStatus) {
|
||||
workingStatus = 'M'; // Modified
|
||||
workingStatus = 'D'; // Deleted in working dir (was same as HEAD)
|
||||
} else if (stageStatus !== workdirStatus && workdirStatus === 2) {
|
||||
workingStatus = 'M'; // Modified in working dir
|
||||
}
|
||||
|
||||
// Only show files with changes
|
||||
@@ -112,7 +115,7 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
if (currentBranch) {
|
||||
lines.push(`On branch ${currentBranch}`);
|
||||
} else {
|
||||
lines.push('On branch main');
|
||||
lines.push('HEAD detached at unknown');
|
||||
}
|
||||
|
||||
// Categorize changes
|
||||
@@ -122,24 +125,26 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
const deleted: string[] = [];
|
||||
|
||||
for (const [filepath, headStatus, workdirStatus, stageStatus] of statusMatrix) {
|
||||
// Staged changes (index differs from HEAD)
|
||||
if (headStatus === 0 && stageStatus === 1) {
|
||||
// Staged changes (stage differs from HEAD)
|
||||
if (headStatus === 0 && stageStatus === 2) {
|
||||
staged.push(`\tnew file: ${filepath}`);
|
||||
} else if (headStatus === 1 && stageStatus === 0) {
|
||||
staged.push(`\tdeleted: ${filepath}`);
|
||||
} else if (headStatus !== stageStatus && headStatus === 1 && stageStatus === 1) {
|
||||
} else if (headStatus === 1 && stageStatus === 2) {
|
||||
staged.push(`\tmodified: ${filepath}`);
|
||||
}
|
||||
|
||||
// Working directory changes (working dir differs from index)
|
||||
if (stageStatus === 1 && workdirStatus === 0) {
|
||||
// Working directory changes (working dir differs from stage)
|
||||
if (stageStatus === 2 && workdirStatus === 0) {
|
||||
deleted.push(`\tdeleted: ${filepath}`);
|
||||
} else if (stageStatus !== workdirStatus && stageStatus === 1 && workdirStatus === 1) {
|
||||
} else if (stageStatus === 1 && workdirStatus === 0) {
|
||||
deleted.push(`\tdeleted: ${filepath}`);
|
||||
} else if (stageStatus !== workdirStatus && workdirStatus === 2) {
|
||||
modified.push(`\tmodified: ${filepath}`);
|
||||
}
|
||||
|
||||
// Untracked files
|
||||
if (headStatus === 0 && stageStatus === 0 && workdirStatus === 1) {
|
||||
// Untracked files (not in HEAD, not in stage, but in working dir)
|
||||
if (headStatus === 0 && stageStatus === 0 && workdirStatus === 2) {
|
||||
untracked.push(`\t${filepath}`);
|
||||
}
|
||||
}
|
||||
@@ -148,7 +153,7 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
if (staged.length > 0) {
|
||||
lines.push('');
|
||||
lines.push('Changes to be committed:');
|
||||
lines.push(' (use "git reset HEAD <file>..." to unstage)');
|
||||
lines.push(' (use "git restore --staged <file>..." to unstage)');
|
||||
lines.push('');
|
||||
lines.push(...staged);
|
||||
}
|
||||
@@ -158,7 +163,7 @@ export class GitStatusCommand implements GitSubcommand {
|
||||
lines.push('');
|
||||
lines.push('Changes not staged for commit:');
|
||||
lines.push(' (use "git add <file>..." to update what will be committed)');
|
||||
lines.push(' (use "git checkout -- <file>..." to discard changes in working directory)');
|
||||
lines.push(' (use "git restore <file>..." to discard changes in working directory)');
|
||||
lines.push('');
|
||||
lines.push(...modified);
|
||||
lines.push(...deleted);
|
||||
|
||||
Reference in new issue
Block a user