@@ -5382,6 +5382,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53825382 const setUpLocalCheckoutPullRequest = Effect . fnUntraced ( function * (
53835383 reportedHead : ( pullHead : string ) => string | undefined = ( pullHead ) => pullHead ,
53845384 syncCheckoutWithPullHead = true ,
5385+ gitSpawnGate ?: NonNullable < Parameters < typeof makeManager > [ 0 ] > [ "gitSpawnGate" ] ,
53855386 ) {
53865387 const repoDir = yield * makeTempDir ( "t3code-git-manager-" ) ;
53875388 yield * initRepo ( repoDir ) ;
@@ -5399,6 +5400,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
53995400 yield * runGit ( repoDir , [ "push" , "origin" , "HEAD:refs/pull/91/head" ] ) ;
54005401 yield * runGit ( repoDir , [ "reset" , "--hard" , firstCommit ] ) ;
54015402 const { manager, ghCalls } = yield * makeManager ( {
5403+ ...( gitSpawnGate ? { gitSpawnGate } : { } ) ,
54025404 ghScenario : {
54035405 pullRequest : {
54045406 number : 91 ,
@@ -5415,6 +5417,139 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
54155417 return { repoDir, manager, ghCalls, firstCommit, pullHead } ;
54165418 } ) ;
54175419
5420+ it . effect . each ( [
5421+ [ "switch" , "before-refresh" , false ] ,
5422+ [ "switch" , "during-refresh" , false ] ,
5423+ [ "create-and-switch" , "before-refresh" , false ] ,
5424+ [ "create-and-switch" , "during-refresh" , false ] ,
5425+ [ "switch" , "during-refresh" , true ] ,
5426+ [ "create-and-switch" , "during-refresh" , true ] ,
5427+ [ "feature-branch" , "during-refresh" , true ] ,
5428+ ] as const ) (
5429+ "local PR checkout excludes a concurrent %s from another client (%s, alternate root: %s)" ,
5430+ ( [ operation , phase , alternateRoot ] ) =>
5431+ Effect . gen ( function * ( ) {
5432+ const gate = {
5433+ matches : ( args : ReadonlyArray < string > ) =>
5434+ args [ 0 ] === "rev-parse" &&
5435+ args [ 1 ] === "--verify" &&
5436+ ( phase === "before-refresh"
5437+ ? / ^ [ 0 - 9 a - f ] { 40 } \^ \{ c o m m i t \} $ / . test ( args [ 2 ] ?? "" )
5438+ : args [ 2 ] === "HEAD^{commit}" ) ,
5439+ reached : yield * Deferred . make < void > ( ) ,
5440+ release : yield * Deferred . make < void > ( ) ,
5441+ } ;
5442+ const { repoDir, manager, pullHead } = yield * setUpLocalCheckoutPullRequest (
5443+ ( head ) => head ,
5444+ false ,
5445+ gate ,
5446+ ) ;
5447+ const nestedCwd = NodePath . join ( repoDir , "nested-project" ) ;
5448+ yield * makeDirectory ( nestedCwd ) ;
5449+ const mainCommit = ( yield * runGit ( repoDir , [ "rev-parse" , "main" ] ) ) . stdout . trim ( ) ;
5450+ yield * runGit ( repoDir , [ "checkout" , "main" ] ) ;
5451+ const registry = yield * Layer . build (
5452+ VcsDriverRegistry . layer . pipe (
5453+ Layer . provide ( VcsProjectConfig . layer ) ,
5454+ Layer . provide ( VcsProcess . layer ) ,
5455+ ) ,
5456+ ) ;
5457+ const handle = yield * Context . get ( registry , VcsDriverRegistry . VcsDriverRegistry ) . resolve ( {
5458+ cwd : repoDir ,
5459+ } ) ;
5460+ const resolvedHandle = alternateRoot
5461+ ? {
5462+ ...handle ,
5463+ repository : { ...handle . repository , rootPath : `${ handle . repository . rootPath } /.` } ,
5464+ }
5465+ : handle ;
5466+ const fileSystem = yield * FileSystem . FileSystem ;
5467+ const canonicalRoot = yield * fileSystem . realPath ( resolvedHandle . repository . rootPath ) ;
5468+ const git = yield * GitVcsDriver . GitVcsDriver ;
5469+ const events : string [ ] = [ ] ;
5470+ const workflows = yield * Effect . forEach ( [ 0 , 1 ] , ( ) =>
5471+ GitWorkflowService . make . pipe (
5472+ // Cache the real filesystem result so startImmediately reaches lease acquisition.
5473+ Effect . provideService ( FileSystem . FileSystem , {
5474+ ...fileSystem ,
5475+ realPath : ( root ) =>
5476+ root === resolvedHandle . repository . rootPath
5477+ ? Effect . succeed ( canonicalRoot )
5478+ : fileSystem . realPath ( root ) ,
5479+ } ) ,
5480+ Effect . provideService ( GitManager . GitManager , {
5481+ ...manager ,
5482+ runStackedAction : ( input , options ) =>
5483+ Effect . sync ( ( ) => {
5484+ events . push ( "feature-branch" ) ;
5485+ NodeFS . writeFileSync (
5486+ NodePath . join ( repoDir , "second-client.txt" ) ,
5487+ "second client\n" ,
5488+ ) ;
5489+ } ) . pipe ( Effect . andThen ( manager . runStackedAction ( input , options ) ) ) ,
5490+ } ) ,
5491+ Effect . provideService ( VcsDriverRegistry . VcsDriverRegistry , {
5492+ ...Context . get ( registry , VcsDriverRegistry . VcsDriverRegistry ) ,
5493+ resolve : ( ) => Effect . succeed ( resolvedHandle ) ,
5494+ } ) ,
5495+ Effect . provideService ( GitVcsDriver . GitVcsDriver , {
5496+ ...git ,
5497+ switchRef : ( input ) =>
5498+ Effect . sync ( ( ) => events . push ( "switch" ) ) . pipe ( Effect . andThen ( git . switchRef ( input ) ) ) ,
5499+ createRef : ( input ) =>
5500+ Effect . sync ( ( ) => events . push ( "create-and-switch" ) ) . pipe (
5501+ Effect . andThen ( git . createRef ( input ) ) ,
5502+ ) ,
5503+ } ) ,
5504+ ) ,
5505+ ) ;
5506+ const preparing = yield * Effect . forkChild (
5507+ workflows [ 0 ] ! . preparePullRequestThread ( { cwd : repoDir , reference : "91" , mode : "local" } ) ,
5508+ ) ;
5509+ yield * Deferred . await ( gate . reached ) ;
5510+ const switching = yield * Effect . forkChild (
5511+ operation === "switch"
5512+ ? workflows [ 1 ] ! . switchRef ( { cwd : nestedCwd , refName : "main" } )
5513+ : operation === "create-and-switch"
5514+ ? workflows [ 1 ] ! . createRef ( {
5515+ cwd : nestedCwd ,
5516+ refName : "second-client" ,
5517+ switchRef : true ,
5518+ } )
5519+ : workflows [ 1 ] ! . runStackedAction ( {
5520+ cwd : nestedCwd ,
5521+ actionId : "second-client-feature-branch" ,
5522+ action : "commit" ,
5523+ featureBranch : true ,
5524+ commitMessage : "Second client commit" ,
5525+ } ) ,
5526+ { startImmediately : true } ,
5527+ ) ;
5528+ // Both independent server workflows have started, but the second cannot touch Git yet.
5529+ expect ( events ) . toEqual ( [ ] ) ;
5530+ yield * Deferred . succeed ( gate . release , undefined ) ;
5531+ const prepared = yield * Fiber . join ( preparing ) ;
5532+ const switched = yield * Fiber . join ( switching ) ;
5533+ expect ( events ) . toEqual ( [ operation ] ) ;
5534+ expect ( prepared ) . toMatchObject ( { branch : "feature/pr-safe" , isOnPullRequestHead : true } ) ;
5535+ expect ( ( yield * runGit ( repoDir , [ "rev-parse" , "main" ] ) ) . stdout . trim ( ) ) . toBe ( mainCommit ) ;
5536+ expect ( ( yield * runGit ( repoDir , [ "rev-parse" , "feature/pr-safe" ] ) ) . stdout . trim ( ) ) . toBe (
5537+ pullHead ,
5538+ ) ;
5539+ expect ( ( yield * runGit ( repoDir , [ "branch" , "--show-current" ] ) ) . stdout . trim ( ) ) . toBe (
5540+ operation === "switch"
5541+ ? "main"
5542+ : "branch" in switched
5543+ ? switched . branch . name
5544+ : "second-client" ,
5545+ ) ;
5546+ if ( "commit" in switched ) {
5547+ expect ( switched . commit . status ) . toBe ( "created" ) ;
5548+ expect ( ( yield * runGit ( repoDir , [ "rev-parse" , "HEAD^" ] ) ) . stdout . trim ( ) ) . toBe ( pullHead ) ;
5549+ }
5550+ } ) ,
5551+ ) ;
5552+
54185553 it . effect . each ( [ false , true ] ) (
54195554 "local PR checkout refuses and preserves tracked edits (staged: %s)" ,
54205555 ( staged ) =>
0 commit comments