perf: classify relay bulk discard paths safely (#3770)

This commit is contained in:
Neil
2026-05-30 09:37:17 -07:00
committed by GitHub
parent fc644450c9
commit 760f5c34ec
2 changed files with 35 additions and 3 deletions
+28 -2
View File
@@ -20,14 +20,14 @@ import {
describe('GitHandler', () => {
let dispatcher: MockDispatcher
let handler: GitHandler
let tmpDir: string
beforeEach(() => {
tmpDir = mkdtempSync(path.join(tmpdir(), 'relay-git-'))
dispatcher = createMockDispatcher()
const ctx = new RelayContext()
// eslint-disable-next-line no-new
new GitHandler(dispatcher as unknown as RelayDispatcher, ctx)
handler = new GitHandler(dispatcher as unknown as RelayDispatcher, ctx)
})
afterEach(async () => {
@@ -498,6 +498,32 @@ describe('GitHandler', () => {
await expect(fs.access(path.join(tmpDir, 'new.txt'))).rejects.toThrow()
})
it('handles large tracked path lists during bulk discard classification', async () => {
const trackedStdout = Array.from({ length: 150_000 }, (_, index) => `docs/file-${index}.ts`)
.join('\0')
.concat('\0')
const gitMock = vi
.spyOn(
handler as unknown as {
git: (args: string[], cwd: string) => Promise<{ stdout: string; stderr: string }>
},
'git'
)
.mockResolvedValueOnce({ stdout: trackedStdout, stderr: '' })
.mockResolvedValueOnce({ stdout: '', stderr: '' })
await dispatcher.callRequest('git.bulkDiscard', {
worktreePath: tmpDir,
filePaths: ['docs']
})
expect(gitMock).toHaveBeenNthCalledWith(
2,
['restore', '--worktree', '--source=HEAD', '--', ':(literal)docs'],
tmpDir
)
})
it('rejects path traversal', async () => {
gitInit(tmpDir)
await expect(
+7 -1
View File
@@ -276,7 +276,13 @@ export class GitHandler {
['ls-files', '-z', '--', ...chunk.map((p) => this.literalPathspec(p))],
worktreePath
)
trackedPathSpecs.push(...stdout.split('\0').filter(Boolean))
// Why: selecting a tracked directory can make `ls-files -z` return
// enough descendants for push(...split) to exceed the argument limit.
for (const trackedPathSpec of stdout.split('\0')) {
if (trackedPathSpec) {
trackedPathSpecs.push(trackedPathSpec)
}
}
}
const trackedPaths = filePaths.filter((filePath) =>