Fix "git diff"

This commit is contained in:
Alex Gleason committed 2025-09-30 13:17:58 -05:00
1 parent 31de1b934d
commit d5c28c3097
2 files changed
+34 -20

No files matched your search

+26 -6
View File
@@ -124,15 +124,14 @@ describe('GitDiffCommand', () => {
expect(result.stdout).toContain('+new line');
});
it('should show untracked files in diff', async () => {
it('should NOT show untracked files in diff', async () => {
const result = await command.execute([]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('diff --git a/new.txt b/new.txt');
expect(result.stdout).toContain('new file mode 100644');
expect(result.stdout).toContain('--- /dev/null');
expect(result.stdout).toContain('+++ b/new.txt');
expect(result.stdout).toContain('+this is a new file');
// Untracked files should not appear in git diff output
expect(result.stdout).not.toContain('diff --git a/new.txt b/new.txt');
expect(result.stdout).not.toContain('new file mode 100644');
expect(result.stdout).not.toContain('+this is a new file');
});
it('should show deleted files in diff', async () => {
@@ -155,6 +154,27 @@ describe('GitDiffCommand', () => {
expect(result.stdout).toContain('+++ b/staged.txt');
});
it('should show newly added files in staged diff', async () => {
// Test with a file that was added to the index (new file staged)
const commandWithStagedNew = new GitDiffCommand({
...mockOptions,
git: {
...mockGit,
statusMatrix: async () => [
['new-staged.txt', 0, 1, 2], // New file added to stage
],
} as unknown as Git,
});
const result = await commandWithStagedNew.execute(['--cached']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('diff --git a/new-staged.txt b/new-staged.txt');
expect(result.stdout).toContain('new file mode 100644');
expect(result.stdout).toContain('--- /dev/null');
expect(result.stdout).toContain('+++ b/new-staged.txt');
});
it('should return empty output when no changes', async () => {
const commandWithNoChanges = new GitDiffCommand({
...mockOptions,
+8 -14
View File
@@ -92,12 +92,12 @@ export class GitDiffCommand implements GitSubcommand {
// Filter for files with working directory changes
// Status matrix: [filepath, headStatus, workdirStatus, stageStatus]
// 0 = absent, 1 = same as previous stage, 2 = different from previous stage
const changedFiles = statusMatrix.filter(([filepath, headStatus, workdirStatus, stageStatus]) => {
const changedFiles = statusMatrix.filter(([filepath, _headStatus, workdirStatus, stageStatus]) => {
// Show files that have working directory changes (differ from index)
const hasWorkingDirChanges = (stageStatus !== workdirStatus && workdirStatus === 2) ||
// Exclude untracked files - git diff only shows changes to tracked files
const hasWorkingDirChanges = (stageStatus !== workdirStatus && workdirStatus === 2 && stageStatus !== 0) ||
(stageStatus === 2 && workdirStatus === 0) ||
(stageStatus === 1 && workdirStatus === 0) ||
(headStatus === 0 && stageStatus === 0 && workdirStatus === 2); // untracked
(stageStatus === 1 && workdirStatus === 0);
if (paths.length > 0) {
return hasWorkingDirChanges && paths.some(path => filepath.includes(path));
@@ -111,7 +111,7 @@ export class GitDiffCommand implements GitSubcommand {
const diffLines: string[] = [];
for (const [filepath, headStatus, workdirStatus, stageStatus] of changedFiles) {
for (const [filepath, _headStatus, workdirStatus, stageStatus] of changedFiles) {
try {
// Get staged/index content
let indexContent = '';
@@ -153,13 +153,7 @@ export class GitDiffCommand implements GitSubcommand {
// Generate diff header
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
if ((headStatus === 0 && stageStatus === 0 && workdirStatus === 2)) {
// New untracked file
diffLines.push('new file mode 100644');
diffLines.push(`index 0000000..${this.getShortHash(workdirContent)}`);
diffLines.push('--- /dev/null');
diffLines.push(`+++ b/${filepath}`);
} else if (workdirStatus === 0 && (stageStatus === 1 || stageStatus === 2)) {
if (workdirStatus === 0 && (stageStatus === 1 || stageStatus === 2)) {
// Deleted file
diffLines.push('deleted file mode 100644');
diffLines.push(`index ${this.getShortHash(indexContent)}..0000000`);
@@ -300,7 +294,7 @@ export class GitDiffCommand implements GitSubcommand {
// Get staged content (approximated by working directory for now)
let stagedContent = '';
if (stageStatus === 1) {
if (stageStatus === 1 || stageStatus === 2) {
try {
stagedContent = await this.fs.readFile(`${this.pwd}/${filepath}`, 'utf8');
} catch {
@@ -311,7 +305,7 @@ export class GitDiffCommand implements GitSubcommand {
// Generate diff header
diffLines.push(`diff --git a/${filepath} b/${filepath}`);
if (headStatus === 0 && stageStatus === 1) {
if (headStatus === 0 && stageStatus === 2) {
// New file staged
diffLines.push('new file mode 100644');
diffLines.push(`index 0000000..${this.getShortHash(stagedContent)}`);