From ba453a463d818ea201188a06c4dc40ffd21ecb12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leynier=20Guti=C3=A9rrez=20Gonz=C3=A1lez?= Date: Tue, 19 May 2026 00:46:24 -0600 Subject: [PATCH] fix: normalize windows paths in git blob readers (#2290) --- src/main/git/status.test.ts | 12 +++++++++++ src/main/git/status.ts | 8 +++++-- src/relay/git-handler-blob-readers.test.ts | 25 ++++++++++++++++++++++ src/relay/git-handler-ops.ts | 8 +++++-- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/relay/git-handler-blob-readers.test.ts diff --git a/src/main/git/status.test.ts b/src/main/git/status.test.ts index b96098e99..4c6e815c2 100644 --- a/src/main/git/status.test.ts +++ b/src/main/git/status.test.ts @@ -213,6 +213,18 @@ describe('getDiff', () => { }) }) + it('normalizes Windows separators before reading git blobs', async () => { + gitExecFileAsyncBufferMock.mockResolvedValueOnce({ stdout: Buffer.from('index-content\n') }) + readFileMock.mockResolvedValue(Buffer.from('working-tree-content')) + + await getDiff('/repo', 'src\\file.ts', false) + + expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(['show', ':src/file.ts'], { + cwd: '/repo', + maxBuffer: 10 * 1024 * 1024 + }) + }) + it('falls back to HEAD for unstaged diffs when the file is not in the index', async () => { gitExecFileAsyncBufferMock .mockRejectedValueOnce(new Error('missing index')) diff --git a/src/main/git/status.ts b/src/main/git/status.ts index 40ff61d96..28ba7aabe 100644 --- a/src/main/git/status.ts +++ b/src/main/git/status.ts @@ -811,8 +811,10 @@ async function readGitBlobAtIndexPath( worktreePath: string, filePath: string ): Promise { + // Why: Git's `:` syntax expects forward slashes even on Windows. + const gitPath = filePath.replace(/\\/g, '/') try { - const { stdout } = await gitExecFileAsyncBuffer(['show', `:${filePath}`], { + const { stdout } = await gitExecFileAsyncBuffer(['show', `:${gitPath}`], { cwd: worktreePath, maxBuffer: MAX_GIT_SHOW_BYTES }) @@ -828,9 +830,11 @@ async function readGitBlobAtOidPath( oid: string, filePath: string ): Promise { + // Why: Git's `:` syntax expects forward slashes even on Windows. + const gitPath = filePath.replace(/\\/g, '/') try { const { stdout } = await gitExecFileAsyncBuffer( - ['show', '--end-of-options', `${oid}:${filePath}`], + ['show', '--end-of-options', `${oid}:${gitPath}`], { cwd: worktreePath, maxBuffer: MAX_GIT_SHOW_BYTES diff --git a/src/relay/git-handler-blob-readers.test.ts b/src/relay/git-handler-blob-readers.test.ts new file mode 100644 index 000000000..8eea74a68 --- /dev/null +++ b/src/relay/git-handler-blob-readers.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it, vi } from 'vitest' +import { readBlobAtIndex, readBlobAtOid, type GitBufferExec } from './git-handler-ops' + +describe('git blob readers', () => { + it('normalizes Windows separators before reading OID blobs', async () => { + const gitBuffer = vi.fn().mockResolvedValue(Buffer.from('head-content')) + + const result = await readBlobAtOid(gitBuffer, '/repo', 'HEAD', 'src\\file.ts') + + expect(gitBuffer).toHaveBeenCalledWith( + ['show', '--end-of-options', 'HEAD:src/file.ts'], + '/repo' + ) + expect(result.content).toBe('head-content') + }) + + it('normalizes Windows separators before reading index blobs', async () => { + const gitBuffer = vi.fn().mockResolvedValue(Buffer.from('index-content')) + + const result = await readBlobAtIndex(gitBuffer, '/repo', 'src\\file.ts') + + expect(gitBuffer).toHaveBeenCalledWith(['show', '--end-of-options', ':src/file.ts'], '/repo') + expect(result.content).toBe('index-content') + }) +}) diff --git a/src/relay/git-handler-ops.ts b/src/relay/git-handler-ops.ts index a24ff2745..9b7d28aab 100644 --- a/src/relay/git-handler-ops.ts +++ b/src/relay/git-handler-ops.ts @@ -23,8 +23,10 @@ export async function readBlobAtOid( oid: string, filePath: string ): Promise<{ content: string; isBinary: boolean }> { + // Why: Git's `:` syntax expects forward slashes even on Windows. + const gitPath = filePath.replace(/\\/g, '/') try { - const buf = await gitBuffer(['show', `${oid}:${filePath}`], cwd) + const buf = await gitBuffer(['show', '--end-of-options', `${oid}:${gitPath}`], cwd) return bufferToBlob(buf, filePath) } catch { return { content: '', isBinary: false } @@ -36,8 +38,10 @@ export async function readBlobAtIndex( cwd: string, filePath: string ): Promise<{ content: string; isBinary: boolean }> { + // Why: Git's `:` syntax expects forward slashes even on Windows. + const gitPath = filePath.replace(/\\/g, '/') try { - const buf = await gitBuffer(['show', `:${filePath}`], cwd) + const buf = await gitBuffer(['show', '--end-of-options', `:${gitPath}`], cwd) return bufferToBlob(buf, filePath) } catch { return { content: '', isBinary: false }