@@ -115,6 +115,31 @@ function movesGitOrItsConfig(env: NodeJS.ProcessEnv | undefined): boolean {
115115 return false ;
116116}
117117
118+ /**
119+ * Runs at most `max` of the given tasks at once, the rest in arrival order.
120+ * Exported for tests.
121+ */
122+ export function makeTaskLimiter ( max : number ) {
123+ let running = 0 ;
124+ const waiting : Array < ( ) => void > = [ ] ;
125+ return async < T > ( task : ( ) => Promise < T > ) : Promise < T > => {
126+ // A finishing task hands its slot to the next one, so `running` stays put.
127+ if ( running >= max ) await new Promise < void > ( ( resolve ) => waiting . push ( resolve ) ) ;
128+ else running ++ ;
129+ try {
130+ return await task ( ) ;
131+ } finally {
132+ const next = waiting . shift ( ) ;
133+ if ( next ) next ( ) ;
134+ else running -- ;
135+ }
136+ } ;
137+ }
138+
139+ // The git processes started here run outside the drivers' process permits. A
140+ // sweep over many repositories asks for one verdict each, all at once.
141+ const withOwnGitProcess = makeTaskLimiter ( 4 ) ;
142+
118143const toGitPath = ( value : string ) => ( NodePath . sep === "\\" ? value . replaceAll ( "\\" , "/" ) : value ) ;
119144
120145async function statOrNull ( target : string ) {
@@ -328,14 +353,17 @@ async function fingerprintFiles(files: ReadonlyArray<string>): Promise<string> {
328353}
329354
330355function listOuterConfig ( ) : Promise < string > {
331- return new Promise ( ( resolve , reject ) => {
332- NodeChildProcess . execFile (
333- "git" ,
334- [ "config" , "--list" , "--show-scope" , "--show-origin" , "-z" ] ,
335- { cwd : NodeOS . tmpdir ( ) , windowsHide : true , timeout : 10_000 , maxBuffer : 4 * 1024 * 1024 } ,
336- ( error , stdout ) => ( error ? reject ( error ) : resolve ( stdout ) ) ,
337- ) ;
338- } ) ;
356+ return withOwnGitProcess (
357+ ( ) =>
358+ new Promise ( ( resolve , reject ) => {
359+ NodeChildProcess . execFile (
360+ "git" ,
361+ [ "config" , "--list" , "--show-scope" , "--show-origin" , "-z" ] ,
362+ { cwd : NodeOS . tmpdir ( ) , windowsHide : true , timeout : 10_000 , maxBuffer : 4 * 1024 * 1024 } ,
363+ ( error , stdout ) => ( error ? reject ( error ) : resolve ( stdout ) ) ,
364+ ) ;
365+ } ) ,
366+ ) ;
339367}
340368
341369/** Where git looks for an `include.path` value found in `originFile`. */
@@ -555,23 +583,30 @@ interface GitVerdict {
555583const verdicts = new Map < string , { readonly at : number ; readonly verdict : Promise < GitVerdict > } > ( ) ;
556584
557585function askGit ( args : ReadonlyArray < string > ) : Promise < GitVerdict > {
558- return new Promise ( ( resolve , reject ) => {
559- NodeChildProcess . execFile (
560- "git" ,
561- [ ...args ] ,
562- {
563- cwd : NodeOS . tmpdir ( ) ,
564- env : { ...process . env , LC_ALL : "C" } ,
565- windowsHide : true ,
566- timeout : 10_000 ,
567- maxBuffer : 64 * 1024 ,
568- } ,
569- ( error , stdout , stderr ) =>
570- error && typeof error . code !== "number"
571- ? reject ( error )
572- : resolve ( { exitCode : typeof error ?. code === "number" ? error . code : 0 , stdout, stderr } ) ,
573- ) ;
574- } ) ;
586+ return withOwnGitProcess (
587+ ( ) =>
588+ new Promise ( ( resolve , reject ) => {
589+ NodeChildProcess . execFile (
590+ "git" ,
591+ [ ...args ] ,
592+ {
593+ cwd : NodeOS . tmpdir ( ) ,
594+ env : { ...process . env , LC_ALL : "C" } ,
595+ windowsHide : true ,
596+ timeout : 10_000 ,
597+ maxBuffer : 64 * 1024 ,
598+ } ,
599+ ( error , stdout , stderr ) =>
600+ error && typeof error . code !== "number"
601+ ? reject ( error )
602+ : resolve ( {
603+ exitCode : typeof error ?. code === "number" ? error . code : 0 ,
604+ stdout,
605+ stderr,
606+ } ) ,
607+ ) ;
608+ } ) ,
609+ ) ;
575610}
576611
577612function gitVerdict ( args : ReadonlyArray < string > ) : Promise < GitVerdict > {
@@ -1237,6 +1272,8 @@ export async function tryAnswerGitCommand(
12371272 if ( ! isGitFastPathEnabled ( ) || ! isGitFastPathEnabled ( input . env ?? { } ) ) return null ;
12381273 if ( hasGitEnvOverride ( process . env ) || hasGitEnvOverride ( input . env ) ) return null ;
12391274 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 ;
12401277 const maxOutputBytes = input . maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES ;
12411278 let timer : NodeJS . Timeout | undefined ;
12421279 try {
0 commit comments