From 1844a992e8c385366f6f78997ded2a561789fe5c Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Sat, 30 May 2026 01:25:41 -0700 Subject: [PATCH] fix: pr-bug-scan validated finding from #2947 (#2965) * fix: address pr-bug-scan validated finding from #2947 Prepended `:(literal)` pathspec magic to each path passed to `git clean -ffdx --` in both `cleanUntrackedPaths` call sites, so untracked filenames containing pathspec glob magic (`*`, `?`, `[`, `]`) a * fix: literalize discard git pathspecs --------- Co-authored-by: orca-bug-scan-bot Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com> --- src/main/git/status-discard-symlink.test.ts | 47 +++++++++++++++++++++ src/main/git/status.test.ts | 20 ++++++--- src/main/git/status.ts | 38 ++++++++++++----- src/relay/git-handler.test.ts | 31 ++++++++++++++ src/relay/git-handler.ts | 36 +++++++++++++--- 5 files changed, 150 insertions(+), 22 deletions(-) diff --git a/src/main/git/status-discard-symlink.test.ts b/src/main/git/status-discard-symlink.test.ts index 0f0bfaad8..88a22d060 100644 --- a/src/main/git/status-discard-symlink.test.ts +++ b/src/main/git/status-discard-symlink.test.ts @@ -91,6 +91,53 @@ describe('discardChanges symlink safety', () => { await expect(access(path.join(repo, 'ignored'))).rejects.toThrow() }) + it('treats untracked discard paths with Git glob characters as literal paths', async () => { + const { repo } = await createRepoWithOutsideDirectory() + await writeFile(path.join(repo, '.gitignore'), 'ignored.log\n') + execFileSync('git', ['add', '.gitignore'], { cwd: repo }) + execFileSync('git', ['commit', '-q', '-m', 'ignore log fixture'], { cwd: repo }) + await writeFile(path.join(repo, '*.log'), 'selected') + await writeFile(path.join(repo, 'keep.log'), 'unrelated') + await writeFile(path.join(repo, 'ignored.log'), 'ignored') + + await discardChanges(repo, '*.log') + + await expect(access(path.join(repo, '*.log'))).rejects.toThrow() + await expect(access(path.join(repo, 'keep.log'))).resolves.toBeUndefined() + await expect(access(path.join(repo, 'ignored.log'))).resolves.toBeUndefined() + }) + + it('treats tracked discard paths with Git glob characters as literal paths', async () => { + const { repo } = await createRepoWithOutsideDirectory() + await writeFile(path.join(repo, '*.log'), 'selected') + await writeFile(path.join(repo, 'keep.log'), 'keep') + execFileSync('git', ['add', '*.log', 'keep.log'], { cwd: repo }) + execFileSync('git', ['commit', '-q', '-m', 'track log fixtures'], { cwd: repo }) + await writeFile(path.join(repo, '*.log'), 'selected modified') + await writeFile(path.join(repo, 'keep.log'), 'keep modified') + + await discardChanges(repo, '*.log') + + await expect(readFile(path.join(repo, '*.log'), 'utf8')).resolves.toBe('selected') + await expect(readFile(path.join(repo, 'keep.log'), 'utf8')).resolves.toBe('keep modified') + }) + + it('treats bulk untracked discard paths with Git glob characters as literal paths', async () => { + const { repo } = await createRepoWithOutsideDirectory() + await writeFile(path.join(repo, '.gitignore'), 'ignored.log\n') + execFileSync('git', ['add', '.gitignore'], { cwd: repo }) + execFileSync('git', ['commit', '-q', '-m', 'ignore log fixture'], { cwd: repo }) + await writeFile(path.join(repo, '*.log'), 'selected') + await writeFile(path.join(repo, 'keep.log'), 'unrelated') + await writeFile(path.join(repo, 'ignored.log'), 'ignored') + + await bulkDiscardChanges(repo, ['*.log']) + + await expect(access(path.join(repo, '*.log'))).rejects.toThrow() + await expect(access(path.join(repo, 'keep.log'))).resolves.toBeUndefined() + await expect(access(path.join(repo, 'ignored.log'))).resolves.toBeUndefined() + }) + it('removes untracked nested git repos selected for discard', async () => { const { repo } = await createRepoWithOutsideDirectory() const nestedRepo = path.join(repo, 'nested') diff --git a/src/main/git/status.test.ts b/src/main/git/status.test.ts index 2547f60b7..252b23ff6 100644 --- a/src/main/git/status.test.ts +++ b/src/main/git/status.test.ts @@ -75,14 +75,14 @@ describe('discardChanges', () => { expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 1, - ['ls-files', '--error-unmatch', '--', 'src/file.ts'], + ['ls-files', '--error-unmatch', '--', ':(literal)src/file.ts'], { cwd: '/repo' } ) expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 2, - ['restore', '--worktree', '--source=HEAD', '--', 'src/file.ts'], + ['restore', '--worktree', '--source=HEAD', '--', ':(literal)src/file.ts'], { cwd: '/repo' } @@ -98,7 +98,7 @@ describe('discardChanges', () => { expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(2) expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 2, - ['clean', '-ffdx', '--', 'src/new-file.ts'], + ['clean', '-ffdx', '--', ':(literal)src/new-file.ts'], { cwd: '/repo' } @@ -178,7 +178,15 @@ describe('bulk git helpers', () => { expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 1, - ['ls-files', '-z', '--', 'src/file.ts', 'src/new-file.ts', 'docs', 'scratch'], + [ + 'ls-files', + '-z', + '--', + ':(literal)src/file.ts', + ':(literal)src/new-file.ts', + ':(literal)docs', + ':(literal)scratch' + ], { cwd: '/repo' } @@ -187,14 +195,14 @@ describe('bulk git helpers', () => { // tracked descendant, which keeps directory pathspecs on the restore path. expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 2, - ['restore', '--worktree', '--source=HEAD', '--', 'src/file.ts', 'docs'], + ['restore', '--worktree', '--source=HEAD', '--', ':(literal)src/file.ts', ':(literal)docs'], { cwd: '/repo' } ) expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( 3, - ['clean', '-ffdx', '--', 'src/new-file.ts', 'scratch'], + ['clean', '-ffdx', '--', ':(literal)src/new-file.ts', ':(literal)scratch'], { cwd: '/repo' } diff --git a/src/main/git/status.ts b/src/main/git/status.ts index ed34cd56b..5ab2c757d 100644 --- a/src/main/git/status.ts +++ b/src/main/git/status.ts @@ -1105,7 +1105,7 @@ export async function discardChanges(worktreePath: string, filePath: string): Pr let tracked = false try { - await gitExecFileAsync(['ls-files', '--error-unmatch', '--', filePath], { + await gitExecFileAsync(['ls-files', '--error-unmatch', '--', literalPathspec(filePath)], { cwd: worktreePath }) tracked = true @@ -1114,9 +1114,12 @@ export async function discardChanges(worktreePath: string, filePath: string): Pr } if (tracked) { - await gitExecFileAsync(['restore', '--worktree', '--source=HEAD', '--', filePath], { - cwd: worktreePath - }) + await gitExecFileAsync( + ['restore', '--worktree', '--source=HEAD', '--', literalPathspec(filePath)], + { + cwd: worktreePath + } + ) return } @@ -1129,6 +1132,11 @@ function normalizeGitPathForCompare(filePath: string): string { return filePath.replace(/\\/g, '/').replace(/\/+$/, '') } +function literalPathspec(filePath: string): string { + // Why: source-control selections are concrete paths, not user-authored Git globs. + return `:(literal)${filePath}` +} + function isTrackedPathSpec(filePath: string, trackedPaths: readonly string[]): boolean { const normalized = normalizeGitPathForCompare(filePath) return trackedPaths.some((trackedPath) => { @@ -1144,9 +1152,12 @@ async function listTrackedPathSpecs( const trackedPaths: string[] = [] for (let i = 0; i < filePaths.length; i += BULK_CHUNK_SIZE) { const chunk = filePaths.slice(i, i + BULK_CHUNK_SIZE) - const { stdout } = await gitExecFileAsync(['ls-files', '-z', '--', ...chunk], { - cwd: worktreePath - }) + const { stdout } = await gitExecFileAsync( + ['ls-files', '-z', '--', ...chunk.map(literalPathspec)], + { + cwd: worktreePath + } + ) trackedPaths.push(...stdout.split('\0').filter(Boolean)) } return trackedPaths @@ -1160,7 +1171,9 @@ async function cleanUntrackedPaths( const chunk = filePaths.slice(i, i + BULK_CHUNK_SIZE) if (chunk.length > 0) { // Why: Git pathspec cleanup avoids raw recursive deletion through symlinked parents. - await gitExecFileAsync(['clean', '-ffdx', '--', ...chunk], { cwd: worktreePath }) + await gitExecFileAsync(['clean', '-ffdx', '--', ...chunk.map(literalPathspec)], { + cwd: worktreePath + }) } } } @@ -1193,9 +1206,12 @@ export async function bulkDiscardChanges(worktreePath: string, filePaths: string async () => { for (let i = 0; i < trackedPaths.length; i += BULK_CHUNK_SIZE) { const chunk = trackedPaths.slice(i, i + BULK_CHUNK_SIZE) - await gitExecFileAsync(['restore', '--worktree', '--source=HEAD', '--', ...chunk], { - cwd: worktreePath - }) + await gitExecFileAsync( + ['restore', '--worktree', '--source=HEAD', '--', ...chunk.map(literalPathspec)], + { + cwd: worktreePath + } + ) } } ) diff --git a/src/relay/git-handler.test.ts b/src/relay/git-handler.test.ts index f99c8acc5..326e42059 100644 --- a/src/relay/git-handler.test.ts +++ b/src/relay/git-handler.test.ts @@ -448,6 +448,37 @@ describe('GitHandler', () => { await expect(fs.access(path.join(tmpDir, 'new.txt'))).rejects.toThrow() }) + it('treats untracked discard paths with Git glob characters as literal paths', async () => { + gitInit(tmpDir) + writeFileSync(path.join(tmpDir, '.gitignore'), 'ignored.log\n') + gitCommit(tmpDir, 'initial') + writeFileSync(path.join(tmpDir, '*.log'), 'selected') + writeFileSync(path.join(tmpDir, 'keep.log'), 'unrelated') + writeFileSync(path.join(tmpDir, 'ignored.log'), 'ignored') + + await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: '*.log' }) + + await expect(fs.access(path.join(tmpDir, '*.log'))).rejects.toThrow() + await expect(fs.access(path.join(tmpDir, 'keep.log'))).resolves.toBeUndefined() + await expect(fs.access(path.join(tmpDir, 'ignored.log'))).resolves.toBeUndefined() + }) + + it('treats tracked discard paths with Git glob characters as literal paths', async () => { + gitInit(tmpDir) + writeFileSync(path.join(tmpDir, '*.log'), 'selected') + writeFileSync(path.join(tmpDir, 'keep.log'), 'keep') + gitCommit(tmpDir, 'track log fixtures') + writeFileSync(path.join(tmpDir, '*.log'), 'selected modified') + writeFileSync(path.join(tmpDir, 'keep.log'), 'keep modified') + + await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: '*.log' }) + + await expect(fs.readFile(path.join(tmpDir, '*.log'), 'utf-8')).resolves.toBe('selected') + await expect(fs.readFile(path.join(tmpDir, 'keep.log'), 'utf-8')).resolves.toBe( + 'keep modified' + ) + }) + it('bulk discards tracked and untracked files', async () => { gitInit(tmpDir) writeFileSync(path.join(tmpDir, 'a.txt'), 'a') diff --git a/src/relay/git-handler.ts b/src/relay/git-handler.ts index 9956a4e76..b532ab070 100644 --- a/src/relay/git-handler.ts +++ b/src/relay/git-handler.ts @@ -236,14 +236,20 @@ export class GitHandler { let tracked = false try { - await this.git(['ls-files', '--error-unmatch', '--', filePath], worktreePath) + await this.git( + ['ls-files', '--error-unmatch', '--', this.literalPathspec(filePath)], + worktreePath + ) tracked = true } catch { // untracked } if (tracked) { - await this.git(['restore', '--worktree', '--source=HEAD', '--', filePath], worktreePath) + await this.git( + ['restore', '--worktree', '--source=HEAD', '--', this.literalPathspec(filePath)], + worktreePath + ) return } @@ -266,7 +272,10 @@ export class GitHandler { const trackedPathSpecs: string[] = [] for (let i = 0; i < filePaths.length; i += BULK_CHUNK_SIZE) { const chunk = filePaths.slice(i, i + BULK_CHUNK_SIZE) - const { stdout } = await this.git(['ls-files', '-z', '--', ...chunk], worktreePath) + const { stdout } = await this.git( + ['ls-files', '-z', '--', ...chunk.map((p) => this.literalPathspec(p))], + worktreePath + ) trackedPathSpecs.push(...stdout.split('\0').filter(Boolean)) } @@ -283,18 +292,35 @@ export class GitHandler { async () => { for (let i = 0; i < trackedPaths.length; i += BULK_CHUNK_SIZE) { const chunk = trackedPaths.slice(i, i + BULK_CHUNK_SIZE) - await this.git(['restore', '--worktree', '--source=HEAD', '--', ...chunk], worktreePath) + await this.git( + [ + 'restore', + '--worktree', + '--source=HEAD', + '--', + ...chunk.map((p) => this.literalPathspec(p)) + ], + worktreePath + ) } } ) } + private literalPathspec(filePath: string): string { + // Why: source-control selections are concrete paths, not user-authored Git globs. + return `:(literal)${filePath}` + } + private async cleanUntrackedPaths(worktreePath: string, filePaths: readonly string[]) { for (let i = 0; i < filePaths.length; i += BULK_CHUNK_SIZE) { const chunk = filePaths.slice(i, i + BULK_CHUNK_SIZE) if (chunk.length > 0) { // Why: Git pathspec cleanup avoids raw recursive deletion through symlinked parents. - await this.git(['clean', '-ffdx', '--', ...chunk], worktreePath) + await this.git( + ['clean', '-ffdx', '--', ...chunk.map((p) => this.literalPathspec(p))], + worktreePath + ) } } }