From d5c28c3097e865385653bd2c70f761e7f53d47f6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 30 Sep 2025 13:17:58 -0500 Subject: [PATCH] Fix "git diff" --- src/lib/commands/git/diff.test.ts | 32 +++++++++++++++++++++++++------ src/lib/commands/git/diff.ts | 22 ++++++++------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/lib/commands/git/diff.test.ts b/src/lib/commands/git/diff.test.ts index cba63962..0b721ab6 100644 --- a/src/lib/commands/git/diff.test.ts +++ b/src/lib/commands/git/diff.test.ts @@ -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, diff --git a/src/lib/commands/git/diff.ts b/src/lib/commands/git/diff.ts index cf205106..0e7bd61e 100644 --- a/src/lib/commands/git/diff.ts +++ b/src/lib/commands/git/diff.ts @@ -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)}`);