@@ -490,7 +490,12 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
490490 repository : input . repository ,
491491 } ) . pipe (
492492 Effect . flatMap ( ( ) => {
493- const cloneUrls = scenario . repositoryCloneUrls ?. [ input . repository ] ;
493+ const repository = input . host ? `${ input . host } /${ input . repository } ` : input . repository ;
494+ const cloneUrls =
495+ scenario . repositoryCloneUrls ?. [ repository ] ??
496+ ( input . host === "github.com"
497+ ? scenario . repositoryCloneUrls ?. [ input . repository ]
498+ : undefined ) ;
494499 return cloneUrls
495500 ? Effect . succeed ( { nameWithOwner : input . repository , ...cloneUrls } )
496501 : Effect . fail (
@@ -3906,6 +3911,163 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
39063911 } ) ,
39073912 ) ;
39083913
3914+ it . effect . each ( [
3915+ { mode : "local" , repositoryType : "same repo" } ,
3916+ { mode : "local" , repositoryType : "fork" } ,
3917+ { mode : "worktree" , repositoryType : "same repo" } ,
3918+ { mode : "worktree" , repositoryType : "fork" } ,
3919+ ] as const ) (
3920+ "checks out same-named repositories on the pull request host ($mode, $repositoryType)" ,
3921+ ( { mode, repositoryType } ) =>
3922+ Effect . gen ( function * ( ) {
3923+ const isCrossRepository = repositoryType === "fork" ;
3924+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
3925+ yield * initRepo ( repoDir ) ;
3926+ // The checkout is on github.com, which also has a same-named repository,
3927+ // and the pull request is on another host. Clone lookups that fall back
3928+ // to the checkout's host find the github.com repository.
3929+ const enterpriseRemote = yield * createBareRemote ( ) ;
3930+ const defaultHostRemote = yield * createBareRemote ( ) ;
3931+ const host = "github.enterprise.test" ;
3932+ const repository = isCrossRepository ? "fork/repo" : "owner/base" ;
3933+ const cloneUrl = `https://${ host } /${ repository } .git` ;
3934+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , defaultHostRemote ] ) ;
3935+ yield * runGit ( repoDir , [ "push" , "origin" , "main" , "main:feature/host" ] ) ;
3936+ yield * runGit ( repoDir , [ "push" , enterpriseRemote , "main" ] ) ;
3937+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/host" ] ) ;
3938+ NodeFS . writeFileSync ( NodePath . join ( repoDir , "host.txt" ) , "enterprise head\n" ) ;
3939+ yield * runGit ( repoDir , [ "add" , "host.txt" ] ) ;
3940+ yield * runGit ( repoDir , [ "commit" , "-m" , "Enterprise head" ] ) ;
3941+ const expectedHead = ( yield * runGit ( repoDir , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
3942+ yield * runGit ( repoDir , [ "push" , enterpriseRemote , "feature/host" ] ) ;
3943+ yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
3944+ if ( mode === "worktree" ) {
3945+ yield * runGit ( repoDir , [ "branch" , "-D" , "feature/host" ] ) ;
3946+ }
3947+ yield * configureVisibleRemoteUrlWithLocalRewrite (
3948+ repoDir ,
3949+ "origin" ,
3950+ "https://github.com/owner/base.git" ,
3951+ defaultHostRemote ,
3952+ ) ;
3953+ yield * runGit ( repoDir , [ "config" , `url.${ enterpriseRemote } .insteadOf` , cloneUrl ] ) ;
3954+ const defaultCloneUrl = `https://github.com/${ repository } .git` ;
3955+ yield * runGit ( repoDir , [
3956+ "config" ,
3957+ "--add" ,
3958+ `url.${ defaultHostRemote } .insteadOf` ,
3959+ defaultCloneUrl ,
3960+ ] ) ;
3961+
3962+ const { manager } = yield * makeManager ( {
3963+ providerScenario : {
3964+ pullRequest : {
3965+ number : 42 ,
3966+ title : "Enterprise head" ,
3967+ url : `https://${ host } /owner/base/pull/42` ,
3968+ baseRefName : "main" ,
3969+ headRefName : "feature/host" ,
3970+ state : "open" ,
3971+ isCrossRepository,
3972+ headRepositoryNameWithOwner : repository ,
3973+ headRepositoryOwnerLogin : isCrossRepository ? "fork" : "owner" ,
3974+ } ,
3975+ repositoryCloneUrls : {
3976+ [ repository ] : { url : defaultCloneUrl , sshUrl : defaultCloneUrl } ,
3977+ [ `${ host } /${ repository } ` ] : { url : cloneUrl , sshUrl : cloneUrl } ,
3978+ } ,
3979+ } ,
3980+ } ) ;
3981+ const result = yield * preparePullRequestThread ( manager , {
3982+ cwd : repoDir ,
3983+ reference : "42" ,
3984+ mode,
3985+ } ) ;
3986+ const checkoutDir = result . worktreePath ?? repoDir ;
3987+ const upstreamRemote = ( yield * runGit ( checkoutDir , [
3988+ "config" ,
3989+ `branch.${ result . branch } .remote` ,
3990+ ] ) ) . stdout . trim ( ) ;
3991+ const upstreamUrl = ( yield * runGit ( checkoutDir , [
3992+ "config" ,
3993+ `remote.${ upstreamRemote } .url` ,
3994+ ] ) ) . stdout . trim ( ) ;
3995+ const head = ( yield * runGit ( checkoutDir , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
3996+ expect ( { upstreamUrl, head } ) . toEqual ( { upstreamUrl : cloneUrl , head : expectedHead } ) ;
3997+ expect ( NodeFS . readFileSync ( NodePath . join ( checkoutDir , "host.txt" ) , "utf8" ) ) . toBe (
3998+ "enterprise head\n" ,
3999+ ) ;
4000+ } ) ,
4001+ ) ;
4002+
4003+ it . effect . each ( [
4004+ { mode : "local" , url : "malformed pull request URL" } ,
4005+ { mode : "worktree" , url : "malformed pull request URL" } ,
4006+ { mode : "local" , url : "file:///owner/base/pull/42" } ,
4007+ { mode : "worktree" , url : "file:///owner/base/pull/42" } ,
4008+ ] as const ) (
4009+ "checks out pull requests with hostless URLs using the default host ($mode, $url)" ,
4010+ ( { mode, url } ) =>
4011+ Effect . gen ( function * ( ) {
4012+ const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
4013+ yield * initRepo ( repoDir ) ;
4014+ const originDir = yield * createBareRemote ( ) ;
4015+ const forkDir = yield * createBareRemote ( ) ;
4016+ const cloneUrl = "https://github.com/fork/repo.git" ;
4017+ yield * runGit ( repoDir , [ "remote" , "add" , "origin" , originDir ] ) ;
4018+ yield * runGit ( repoDir , [ "push" , "origin" , "main" ] ) ;
4019+ yield * runGit ( repoDir , [ "checkout" , "-b" , "feature/malformed-url" ] ) ;
4020+ NodeFS . writeFileSync ( NodePath . join ( repoDir , "fork.txt" ) , "fork head\n" ) ;
4021+ yield * runGit ( repoDir , [ "add" , "fork.txt" ] ) ;
4022+ yield * runGit ( repoDir , [ "commit" , "-m" , "Fork head" ] ) ;
4023+ const expectedHead = ( yield * runGit ( repoDir , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
4024+ yield * runGit ( repoDir , [ "push" , forkDir , "feature/malformed-url" ] ) ;
4025+ yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
4026+ if ( mode === "worktree" ) {
4027+ yield * runGit ( repoDir , [ "branch" , "-D" , "feature/malformed-url" ] ) ;
4028+ }
4029+ yield * runGit ( repoDir , [ "config" , `url.${ forkDir } .insteadOf` , cloneUrl ] ) ;
4030+
4031+ const { manager } = yield * makeManager ( {
4032+ providerScenario : {
4033+ pullRequest : {
4034+ number : 42 ,
4035+ title : "Fork head" ,
4036+ url,
4037+ baseRefName : "main" ,
4038+ headRefName : "feature/malformed-url" ,
4039+ state : "open" ,
4040+ isCrossRepository : true ,
4041+ headRepositoryNameWithOwner : "fork/repo" ,
4042+ headRepositoryOwnerLogin : "fork" ,
4043+ } ,
4044+ repositoryCloneUrls : {
4045+ "fork/repo" : { url : cloneUrl , sshUrl : cloneUrl } ,
4046+ } ,
4047+ } ,
4048+ } ) ;
4049+ const result = yield * preparePullRequestThread ( manager , {
4050+ cwd : repoDir ,
4051+ reference : "42" ,
4052+ mode,
4053+ } ) ;
4054+ const checkoutDir = result . worktreePath ?? repoDir ;
4055+ const upstreamRemote = ( yield * runGit ( checkoutDir , [
4056+ "config" ,
4057+ `branch.${ result . branch } .remote` ,
4058+ ] ) ) . stdout . trim ( ) ;
4059+ const upstreamUrl = ( yield * runGit ( checkoutDir , [
4060+ "config" ,
4061+ `remote.${ upstreamRemote } .url` ,
4062+ ] ) ) . stdout . trim ( ) ;
4063+ const head = ( yield * runGit ( checkoutDir , [ "rev-parse" , "HEAD" ] ) ) . stdout . trim ( ) ;
4064+ expect ( { upstreamUrl, head } ) . toEqual ( { upstreamUrl : cloneUrl , head : expectedHead } ) ;
4065+ expect ( NodeFS . readFileSync ( NodePath . join ( checkoutDir , "fork.txt" ) , "utf8" ) ) . toBe (
4066+ "fork head\n" ,
4067+ ) ;
4068+ } ) ,
4069+ ) ;
4070+
39094071 const setUpLocalCheckoutPullRequest = Effect . fnUntraced ( function * (
39104072 reportedHead : ( pullHead : string ) => string | undefined = ( pullHead ) => pullHead ,
39114073 syncCheckoutWithPullHead = true ,
0 commit comments