perf: classify relay bulk discard paths safely (#3770)
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in New Issue
Block a user