From 6e0d35480c38eb7c8e386e7250f08a9f91f98cdb Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 04:26:41 -0700 Subject: [PATCH] fix: use nul numstat for compare counts (#4104) --- src/main/git/status.test.ts | 97 +++++++++++++++++++++++++++++++++++++ src/main/git/status.ts | 5 +- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/main/git/status.test.ts b/src/main/git/status.test.ts index 8cfb0a58d..cc2ec0169 100644 --- a/src/main/git/status.test.ts +++ b/src/main/git/status.test.ts @@ -49,6 +49,7 @@ import { detectConflictOperation, discardChanges, getBranchCompare, + getCommitCompare, getDiff, getStagedCommitContext, getStatus, @@ -947,4 +948,100 @@ describe('getBranchCompare', () => { { path: 'docs/日本語/sample.md', status: 'modified', added: 2, removed: 1 } ]) }) + + it('attaches counts for branch compare paths containing rename markers', async () => { + gitExecFileAsyncMock.mockImplementation((args: string[]) => { + if (args[0] === 'branch') { + return Promise.resolve({ stdout: 'main\n' }) + } + if (args[0] === 'rev-parse' && args.includes('HEAD')) { + return Promise.resolve({ stdout: 'head-oid\n' }) + } + if (args[0] === 'rev-parse') { + return Promise.resolve({ stdout: 'base-oid\n' }) + } + if (args[0] === 'merge-base') { + return Promise.resolve({ stdout: 'merge-base-oid\n' }) + } + if (args.includes('--name-status')) { + return Promise.resolve({ stdout: 'M\tdocs/a => b.txt\n' }) + } + if (args.includes('--numstat')) { + return Promise.resolve({ + stdout: args.includes('-z') ? '1\t0\tdocs/a => b.txt\0' : '1\t0\tdocs/a => b.txt\n' + }) + } + if (args[0] === 'rev-list') { + return Promise.resolve({ stdout: '1\n' }) + } + throw new Error(`unexpected git args: ${args.join(' ')}`) + }) + + const result = await getBranchCompare('/repo', 'origin/main') + + expect(gitExecFileAsyncMock).toHaveBeenCalledWith( + [ + '-c', + 'core.quotePath=false', + 'diff', + '-z', + '--numstat', + '-M', + '-C', + 'merge-base-oid', + 'head-oid' + ], + expect.objectContaining({ cwd: '/repo' }) + ) + expect(result.entries).toEqual([ + { path: 'docs/a => b.txt', status: 'modified', added: 1, removed: 0 } + ]) + }) +}) + +describe('getCommitCompare', () => { + beforeEach(() => { + gitExecFileAsyncMock.mockReset() + gitExecFileAsyncBufferMock.mockReset() + }) + + it('attaches counts for commit compare paths containing rename markers', async () => { + gitExecFileAsyncMock.mockImplementation((args: string[]) => { + if (args[0] === 'rev-parse') { + return Promise.resolve({ stdout: 'commit-oid\n' }) + } + if (args[0] === 'rev-list') { + return Promise.resolve({ stdout: 'commit-oid parent-oid\n' }) + } + if (args.includes('--name-status')) { + return Promise.resolve({ stdout: 'M\tdocs/a => b.txt\n' }) + } + if (args.includes('--numstat')) { + return Promise.resolve({ + stdout: args.includes('-z') ? '1\t0\tdocs/a => b.txt\0' : '1\t0\tdocs/a => b.txt\n' + }) + } + throw new Error(`unexpected git args: ${args.join(' ')}`) + }) + + const result = await getCommitCompare('/repo', 'commit-oid') + + expect(gitExecFileAsyncMock).toHaveBeenCalledWith( + [ + '-c', + 'core.quotePath=false', + 'diff', + '-z', + '--numstat', + '-M', + '-C', + 'parent-oid', + 'commit-oid' + ], + expect.objectContaining({ cwd: '/repo' }) + ) + expect(result.entries).toEqual([ + { path: 'docs/a => b.txt', status: 'modified', added: 1, removed: 0 } + ]) + }) }) diff --git a/src/main/git/status.ts b/src/main/git/status.ts index d516ff7ff..3883c9407 100644 --- a/src/main/git/status.ts +++ b/src/main/git/status.ts @@ -754,7 +754,7 @@ async function loadBranchChanges( gitOptions ), gitExecFileAsync( - ['-c', 'core.quotePath=false', 'diff', '--numstat', '-M', '-C', mergeBase, headOid], + ['-c', 'core.quotePath=false', 'diff', '-z', '--numstat', '-M', '-C', mergeBase, headOid], gitOptions ) ]) @@ -797,11 +797,12 @@ async function loadCommitChanges( commitOid ] const numstatArgs = parentOid - ? ['-c', 'core.quotePath=false', 'diff', '--numstat', '-M', '-C', parentOid, commitOid] + ? ['-c', 'core.quotePath=false', 'diff', '-z', '--numstat', '-M', '-C', parentOid, commitOid] : [ '-c', 'core.quotePath=false', 'diff-tree', + '-z', '--root', '--no-commit-id', '--numstat',