@@ -1272,20 +1272,12 @@ export async function tryAnswerGitCommand(
12721272 if ( ! isGitFastPathEnabled ( ) || ! isGitFastPathEnabled ( input . env ?? { } ) ) return null ;
12731273 if ( hasGitEnvOverride ( process . env ) || hasGitEnvOverride ( input . env ) ) return null ;
12741274 if ( movesGitOrItsConfig ( input . env ) ) return null ;
1275- // No budget at all: nothing may be answered, however fast the reads turn out.
1276- if ( typeof input . timeoutMs === "number" && input . timeoutMs <= 0 ) return null ;
12771275 const maxOutputBytes = input . maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES ;
1278- let timer : NodeJS . Timeout | undefined ;
12791276 try {
1280- // Reads that take this long mean a stuck disk or share; git gets the question instead.
1281- const timedOut = new Promise < null > ( ( resolve ) => {
1282- timer = setTimeout ( resolve , Math . min ( ANSWER_TIMEOUT_MS , input . timeoutMs ?? Infinity ) , null ) ;
1283- } ) ;
12841277 // Asking for the outer config first proves git runs at all, so a missing git is not papered over.
1285- const answering = getOuterConfig ( ) . then ( ( ) => answer ( input ) ) ;
1286- // When the timer wins, the abandoned attempt still settles; its decline is not an error.
1287- answering . catch ( ( ) => undefined ) ;
1288- const result = await Promise . race ( [ answering , timedOut ] ) ;
1278+ const result = await withinReadBudget ( input . timeoutMs , ( ) =>
1279+ getOuterConfig ( ) . then ( ( ) => answer ( input ) ) ,
1280+ ) ;
12891281 return result !== null &&
12901282 Math . max ( Buffer . byteLength ( result . stdout ) , Buffer . byteLength ( result . stderr ) ) > maxOutputBytes
12911283 ? null
@@ -1294,6 +1286,30 @@ export async function tryAnswerGitCommand(
12941286 return error instanceof NotARepository && ! gitMessagesMayBeTranslated ( input . env )
12951287 ? { exitCode : 128 , stdout : "" , stderr : error . stderr }
12961288 : null ;
1289+ }
1290+ }
1291+
1292+ /**
1293+ * Settles with `work`'s result, or with `null` once the caller's budget or
1294+ * ANSWER_TIMEOUT_MS runs out, whichever comes first. A rejection of `work`
1295+ * before then is passed on.
1296+ */
1297+ async function withinReadBudget < T > (
1298+ timeoutMs : number | null | undefined ,
1299+ work : ( ) => Promise < T > ,
1300+ ) : Promise < T | null > {
1301+ // No budget at all: nothing may be answered, however fast the reads turn out.
1302+ if ( typeof timeoutMs === "number" && timeoutMs <= 0 ) return null ;
1303+ let timer : NodeJS . Timeout | undefined ;
1304+ // Reads that take this long mean a stuck disk or share; git gets the question instead.
1305+ const timedOut = new Promise < null > ( ( resolve ) => {
1306+ timer = setTimeout ( resolve , Math . min ( ANSWER_TIMEOUT_MS , timeoutMs ?? Infinity ) , null ) ;
1307+ } ) ;
1308+ const working = work ( ) ;
1309+ // When the timer wins, the abandoned attempt still settles; its decline is not an error.
1310+ working . catch ( ( ) => undefined ) ;
1311+ try {
1312+ return await Promise . race ( [ working , timedOut ] ) ;
12971313 } finally {
12981314 clearTimeout ( timer ) ;
12991315 }
@@ -1311,9 +1327,12 @@ export async function gitAnswerMemoKey(input: GitFastPathInput): Promise<string
13111327 const [ command , ...rest ] = input . args ;
13121328 if ( command !== "rev-list" ) return null ;
13131329 try {
1314- const { repo } = await discoverRepository ( input . cwd ) ;
1315- await requireGitAgrees ( repo , false ) ;
1316- return ( await revListQuery ( repo , rest ) ) . key ;
1330+ // Without a key git still runs, so a stuck disk must not hold up the spawn.
1331+ return await withinReadBudget ( input . timeoutMs , async ( ) => {
1332+ const { repo } = await discoverRepository ( input . cwd ) ;
1333+ await requireGitAgrees ( repo , false ) ;
1334+ return ( await revListQuery ( repo , rest ) ) . key ;
1335+ } ) ;
13171336 } catch {
13181337 return null ;
13191338 }
0 commit comments