@@ -10,37 +10,30 @@ import { execSync } from 'child_process'
1010export const withTestRepo = async < T > (
1111 repoConfig : {
1212 repoUrl : string
13- commitSha : string
13+ // The sha of the commit to checkout. If you have a commit with changes to replicate, you would check out the parent commit.
14+ parentSha : string
1415 initCommand ?: string
15- checkoutPrevious ?: boolean
1616 } ,
1717 fn : ( cwd : string ) => Promise < T > ,
1818) : Promise < T > => {
19- const { repoUrl, commitSha , initCommand, checkoutPrevious } = repoConfig
19+ const { repoUrl, parentSha , initCommand } = repoConfig
2020
2121 // Create a temporary directory for the test repo
2222 const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'codebuff-eval-' ) )
2323 const repoDir = path . join ( tempDir , 'repo' )
2424
2525 try {
26- // Clone the repository
27- console . log ( `Cloning repository ${ repoUrl } to ${ repoDir } ...` )
28- execSync ( `git clone ${ repoUrl } ${ repoDir } ` , { stdio : 'ignore' } )
26+ console . log (
27+ `Cloning repository ${ repoUrl } at commit ${ parentSha } to ${ repoDir } (shallow)...` ,
28+ )
29+ execSync ( `git clone --depth 1 ${ repoUrl } ${ repoDir } ` , { stdio : 'ignore' } )
2930
30- // Checkout the specific commit or the previous commit
31- if ( checkoutPrevious ) {
32- const previousCommitSha = getPreviousCommitSha ( commitSha , repoDir )
33- console . log ( `Checking out previous commit ${ previousCommitSha } ...` )
34- execSync ( `git checkout ${ previousCommitSha } ` , {
35- cwd : repoDir ,
36- stdio : 'ignore' ,
37- } )
38- } else {
39- console . log ( `Checking out commit ${ commitSha } ...` )
40- execSync ( `git checkout ${ commitSha } ` , { cwd : repoDir , stdio : 'ignore' } )
41- }
31+ execSync ( `git fetch --depth 1 origin ${ parentSha } ` , {
32+ cwd : repoDir ,
33+ stdio : 'ignore' ,
34+ } )
35+ execSync ( `git checkout ${ parentSha } ` , { cwd : repoDir , stdio : 'ignore' } )
4236
43- // Run initialization command if provided
4437 if ( initCommand ) {
4538 console . log ( `Running init command: ${ initCommand } ...` )
4639 execSync ( initCommand , { cwd : repoDir , stdio : 'ignore' } )
@@ -50,22 +43,10 @@ export const withTestRepo = async <T>(
5043 return await fn ( repoDir )
5144 } finally {
5245 // Clean up the temporary directory
53- console . log ( `Cleaning up temporary directory ${ tempDir } ...` )
5446 try {
5547 fs . rmSync ( tempDir , { recursive : true , force : true } )
5648 } catch ( error ) {
5749 console . warn ( `Failed to clean up temporary directory: ${ error } ` )
5850 }
5951 }
6052}
61-
62- /**
63- * Gets the previous commit SHA (parent) of a given commit
64- */
65- const getPreviousCommitSha = ( commitSha : string , repoDir : string ) : string => {
66- const previousSha = execSync ( `git rev-parse ${ commitSha } ^` , {
67- cwd : repoDir ,
68- encoding : 'utf-8' ,
69- } ) . trim ( )
70- return previousSha
71- }
0 commit comments