perf: avoid blocking base-ref resolution (#8182)
* perf: avoid blocking base-ref resolution * fix(git): bound async default-base probes * fix(git): bound default-base probes during creation * test(git): cover bounded Windows and WSL probes
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { gitExecFileAsyncMock } = vi.hoisted(() => ({
|
||||
gitExecFileAsyncMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('./runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock,
|
||||
gitExecFileSync: vi.fn()
|
||||
}))
|
||||
|
||||
import { getBaseRefDefault } from './repo'
|
||||
|
||||
describe('getBaseRefDefault async subprocess bounds', () => {
|
||||
beforeEach(() => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
})
|
||||
|
||||
it('bounds every local probe and degrades a timeout to no default', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValue(new Error('git timed out.'))
|
||||
|
||||
await expect(getBaseRefDefault('/repo')).resolves.toBeNull()
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalled()
|
||||
for (const [, options] of gitExecFileAsyncMock.mock.calls) {
|
||||
expect(options).toEqual({ cwd: '/repo', timeout: 15_000 })
|
||||
}
|
||||
})
|
||||
|
||||
it('preserves the same timeout when routing probes through WSL', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValue(new Error('git timed out.'))
|
||||
|
||||
await expect(
|
||||
getBaseRefDefault('\\\\wsl.localhost\\Ubuntu\\repo', { wslDistro: 'Ubuntu' })
|
||||
).resolves.toBeNull()
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalled()
|
||||
for (const [, options] of gitExecFileAsyncMock.mock.calls) {
|
||||
expect(options).toEqual({
|
||||
cwd: '\\\\wsl.localhost\\Ubuntu\\repo',
|
||||
timeout: 15_000,
|
||||
wslDistro: 'Ubuntu'
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
+20
-3
@@ -15,6 +15,13 @@ type LocalGitExecOptions = {
|
||||
wslDistro?: string
|
||||
}
|
||||
|
||||
type LocalDefaultBaseRefGitOptions = {
|
||||
cwd: string
|
||||
wslDistro?: string
|
||||
}
|
||||
|
||||
const DEFAULT_BASE_REF_PROBE_TIMEOUT_MS = 15_000
|
||||
|
||||
type GitRepoProbeResult = 'repo' | 'not-repo' | 'indeterminate'
|
||||
type GitMarkerScanResult = { status: 'valid'; rootPath: string } | { status: 'absent' | 'invalid' }
|
||||
|
||||
@@ -651,13 +658,23 @@ export async function resolveDefaultBaseRefViaExec(exec: GitExec): Promise<strin
|
||||
return resolveDefaultBaseRefFromProbes((ref) => hasGitRefViaExec(exec, ref))
|
||||
}
|
||||
|
||||
export function resolveDefaultBaseRefWithLocalGit(
|
||||
options: LocalDefaultBaseRefGitOptions
|
||||
): Promise<string | null> {
|
||||
return resolveDefaultBaseRefViaExec((argv) =>
|
||||
gitExecFileAsync(argv, {
|
||||
...options,
|
||||
// Why: async avoids main-thread stalls, but dead local/WSL filesystems still need a bound.
|
||||
timeout: DEFAULT_BASE_REF_PROBE_TIMEOUT_MS
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
async function getDefaultBaseRefAsync(
|
||||
path: string,
|
||||
options: LocalGitExecOptions = {}
|
||||
): Promise<string | null> {
|
||||
return resolveDefaultBaseRefViaExec((argv) =>
|
||||
gitExecFileAsync(argv, gitExecOptions(path, options))
|
||||
)
|
||||
return resolveDefaultBaseRefWithLocalGit(gitExecOptions(path, options))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user