@@ -384,8 +384,9 @@ Options:
384384 -h, --help Show this help` )
385385}
386386
387- async function git ( args : readonly string [ ] ) : Promise < string > {
387+ async function git ( args : readonly string [ ] , cwd ?: string ) : Promise < string > {
388388 const result = await spawn ( 'git' , args as string [ ] , {
389+ cwd,
389390 stdio : 'pipe' ,
390391 stdioString : true ,
391392 } )
@@ -453,6 +454,7 @@ async function runBackend(
453454 promptText : string ,
454455 tempDir : string ,
455456 passLabel : string ,
457+ cwd : string ,
456458) : Promise < { ok : boolean ; output : string ; logPath : string } > {
457459 const desc = BACKENDS [ backend ]
458460 const promptFile = path . join ( tempDir , `${ passLabel } .prompt.txt` )
@@ -464,6 +466,7 @@ async function runBackend(
464466 let stdout = ''
465467 try {
466468 const child = spawn ( desc . bin , argv as string [ ] , {
469+ cwd,
467470 stdio : 'pipe' ,
468471 stdioString : true ,
469472 } )
@@ -573,18 +576,20 @@ async function main(): Promise<void> {
573576 logger . error ( 'Must be run inside a git repository.' )
574577 process . exit ( 1 )
575578 }
576- process . chdir ( repoRoot )
577579
578- const branchRaw = await git ( [ 'branch' , '--show-current' ] )
580+ const branchRaw = await git ( [ 'branch' , '--show-current' ] , repoRoot )
579581 const branch =
580582 branchRaw . length > 0
581583 ? branchRaw
582- : `detached-${ ( await git ( [ 'rev-parse' , '--short' , 'HEAD' ] ) ) } `
583- const baseRef = await resolveBaseRef ( args . baseRef )
584- const mergeBase = await git ( [ 'merge-base' , baseRef , 'HEAD' ] )
584+ : `detached-${ ( await git ( [ 'rev-parse' , '--short' , 'HEAD' ] , repoRoot ) ) } `
585+ const baseRef = await resolveBaseRef ( args . baseRef , repoRoot )
586+ const mergeBase = await git ( [ 'merge-base' , baseRef , 'HEAD' ] , repoRoot )
585587 const range = `${ mergeBase } ..HEAD`
586- const commitList = await git ( [ 'log' , '--oneline' , '--no-decorate' , range ] )
587- const diffStat = await git ( [ 'diff' , '--stat' , range ] )
588+ const commitList = await git (
589+ [ 'log' , '--oneline' , '--no-decorate' , range ] ,
590+ repoRoot ,
591+ )
592+ const diffStat = await git ( [ 'diff' , '--stat' , range ] , repoRoot )
588593
589594 const outputPath =
590595 args . outputPath ??
@@ -631,7 +636,13 @@ async function main(): Promise<void> {
631636 }
632637 logger . info ( `${ passLabel } : running on ${ backend } ` )
633638 const promptText = ROLES [ role ] . buildPrompt ( ctx )
634- const result = await runBackend ( backend , promptText , tempDir , passLabel )
639+ const result = await runBackend (
640+ backend ,
641+ promptText ,
642+ tempDir ,
643+ passLabel ,
644+ repoRoot ,
645+ )
635646 if ( ! result . ok ) {
636647 logger . error ( `${ passLabel } : failed; see ${ result . logPath } ` )
637648 await appendSkipNote (
@@ -674,17 +685,23 @@ async function main(): Promise<void> {
674685 }
675686}
676687
677- async function resolveBaseRef ( provided : string | undefined ) : Promise < string > {
688+ async function resolveBaseRef (
689+ provided : string | undefined ,
690+ cwd : string ,
691+ ) : Promise < string > {
678692 if ( provided ) {
679693 return provided
680694 }
681695 try {
682- const headRef = await git ( [
683- 'symbolic-ref' ,
684- '--quiet' ,
685- '--short' ,
686- 'refs/remotes/origin/HEAD' ,
687- ] )
696+ const headRef = await git (
697+ [
698+ 'symbolic-ref' ,
699+ '--quiet' ,
700+ '--short' ,
701+ 'refs/remotes/origin/HEAD' ,
702+ ] ,
703+ cwd ,
704+ )
688705 if ( headRef . length > 0 ) {
689706 return headRef
690707 }
0 commit comments