@@ -89,6 +89,28 @@ const makeFailedHandle = (stderr: string) =>
8989 stderr : Stream . encodeText ( Stream . make ( stderr ) ) ,
9090 } ) ;
9191
92+ // Reports when matching git commands exit, so a test can act between a write's attempts.
93+ const makeDriverReportingExits = ( matches : ( args : ReadonlyArray < string > ) => boolean ) =>
94+ Effect . gen ( function * ( ) {
95+ const exits = yield * Queue . unbounded < ChildProcessSpawner . ExitCode > ( ) ;
96+ const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
97+ const spawner = ChildProcessSpawner . make ( ( command ) =>
98+ Effect . gen ( function * ( ) {
99+ const handle = yield * delegate . spawn ( command ) ;
100+ if ( ! ChildProcess . isStandardCommand ( command ) || ! matches ( command . args ) ) return handle ;
101+ return ChildProcessSpawner . makeHandle ( {
102+ ...handle ,
103+ exitCode : handle . exitCode . pipe ( Effect . tap ( ( exitCode ) => Queue . offer ( exits , exitCode ) ) ) ,
104+ } ) ;
105+ } ) ,
106+ ) ;
107+ const driver = yield * makeGitVcsDriverCore ( ) . pipe (
108+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
109+ Effect . provide ( layerServerConfig ) ,
110+ ) ;
111+ return { driver, exits } ;
112+ } ) ;
113+
92114describe ( "Git output collection" , ( ) => {
93115 // Git writes in arbitrary chunk sizes; one byte per chunk splits every
94116 // multibyte character across reads.
@@ -3160,7 +3182,8 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31603182 const args = ChildProcess . isStandardCommand ( command ) ? command . args : [ ] ;
31613183 const writesConfig =
31623184 ( args . includes ( "worktree" ) && args . includes ( "add" ) && args . includes ( "-b" ) ) ||
3163- ( args [ 0 ] === "branch" && ( args [ 1 ] === "--" || args [ 1 ] === "-m" ) ) ||
3185+ ( args [ 0 ] === "branch" &&
3186+ ( args [ 1 ] === "-m" || args [ 1 ] ?. startsWith ( "--set-upstream-to" ) ) ) ||
31643187 ( args [ 0 ] === "config" && ! args [ 1 ] ?. startsWith ( "-" ) ) ;
31653188 if ( ! writesConfig ) return yield * delegate . spawn ( command ) ;
31663189 configWriters += 1 ;
@@ -3526,49 +3549,26 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
35263549 } ) ,
35273550 ) ;
35283551
3529- it . effect ( "still creates the worktree when the base-ref config is locked" , ( ) =>
3552+ it . effect ( "still creates the worktree when the base-ref config stays locked" , ( ) =>
35303553 Effect . gen ( function * ( ) {
35313554 const cwd = yield * makeTmpDir ( ) ;
35323555 const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
35333556 const pathService = yield * Path . Path ;
35343557 const fileSystem = yield * FileSystem . FileSystem ;
35353558 const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "locked-config" ) ;
35363559 yield * writeTextFile ( cwd , ".git/config.lock" , "" ) ;
3537- const configWritesFinished = yield * Queue . unbounded < void > ( ) ;
3538- const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
3539- const spawner = ChildProcessSpawner . make ( ( command ) =>
3540- Effect . gen ( function * ( ) {
3541- const handle = yield * delegate . spawn ( command ) ;
3542- const args = ChildProcess . isStandardCommand ( command ) ? command . args : [ ] ;
3543- if ( args [ 0 ] !== "config" || ! args [ 1 ] ?. startsWith ( "branch." ) ) return handle ;
3544- return ChildProcessSpawner . makeHandle ( {
3545- ...handle ,
3546- exitCode : handle . exitCode . pipe (
3547- Effect . tap ( ( ) => Queue . offer ( configWritesFinished , undefined ) ) ,
3548- ) ,
3549- } ) ;
3550- } ) ,
3551- ) ;
3552- const driver = yield * makeGitVcsDriverCore ( ) . pipe (
3553- Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
3554- Effect . provide ( layerServerConfig ) ,
3555- ) ;
3560+ const driver = yield * GitVcsDriver . GitVcsDriver ;
35563561
3557- const creating = yield * driver
3558- . createWorktree ( {
3562+ // The base-ref write retries on the real clock before giving up.
3563+ const created = yield * TestClock . withLive (
3564+ driver . createWorktree ( {
35593565 cwd,
35603566 path : worktreePath ,
35613567 refName : initialBranch ,
35623568 newRefName : "feature/locked-config" ,
35633569 baseRefName : initialBranch ,
3564- } )
3565- . pipe ( Effect . forkChild ) ;
3566- // The base-ref write tries once and retries ten times before giving up.
3567- for ( let attempt = 0 ; attempt < 11 ; attempt ++ ) {
3568- yield * Queue . take ( configWritesFinished ) ;
3569- yield * TestClock . adjust ( "50 millis" ) ;
3570- }
3571- const created = yield * Fiber . join ( creating ) ;
3570+ } ) ,
3571+ ) ;
35723572
35733573 assert . deepEqual ( created . worktree , {
35743574 path : worktreePath ,
@@ -3858,40 +3858,60 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
38583858 const path = yield * Path . Path ;
38593859 const lockPath = path . join ( cwd , ".git" , "config.lock" ) ;
38603860 yield * fileSystem . writeFileString ( lockPath , "" ) ;
3861- const writesFinished = yield * Queue . unbounded < number > ( ) ;
3862- const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
3863- const spawner = ChildProcessSpawner . make ( ( command ) =>
3864- Effect . gen ( function * ( ) {
3865- const handle = yield * delegate . spawn ( command ) ;
3866- if ( ! ChildProcess . isStandardCommand ( command ) || command . args [ 1 ] !== "t3.test" ) {
3867- return handle ;
3868- }
3869- return ChildProcessSpawner . makeHandle ( {
3870- ...handle ,
3871- exitCode : handle . exitCode . pipe (
3872- Effect . tap ( ( exitCode ) => Queue . offer ( writesFinished , exitCode ) ) ,
3873- ) ,
3874- } ) ;
3875- } ) ,
3876- ) ;
3877- const driver = yield * makeGitVcsDriverCore ( ) . pipe (
3878- Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
3879- Effect . provide ( layerServerConfig ) ,
3880- ) ;
3861+ const { driver, exits } = yield * makeDriverReportingExits ( ( args ) => args [ 1 ] === "t3.test" ) ;
38813862
3882- const write = yield * driver
3883- . writeConfigValue ( { cwd, key : "t3.test" , value : "written" } )
3884- . pipe ( Effect . forkChild ) ;
3885- assert . notEqual ( yield * Queue . take ( writesFinished ) , 0 ) ;
3863+ // Retries wait on the real clock while the external lock is released.
3864+ const write = yield * TestClock . withLive (
3865+ driver . writeConfigValue ( { cwd, key : "t3.test" , value : "written" } ) ,
3866+ ) . pipe ( Effect . forkChild ) ;
3867+ assert . notEqual ( yield * Queue . take ( exits ) , 0 ) ;
38863868 yield * fileSystem . remove ( lockPath ) ;
3887- yield * TestClock . adjust ( "50 millis" ) ;
38883869 yield * Fiber . join ( write ) ;
38893870
3890- assert . equal ( yield * Queue . take ( writesFinished ) , 0 ) ;
38913871 assert . equal ( yield * git ( cwd , [ "config" , "t3.test" ] ) , "written" ) ;
38923872 } ) ,
38933873 ) ;
38943874
3875+ it . effect ( "tracks a remote start point once another process releases the config lock" , ( ) =>
3876+ Effect . gen ( function * ( ) {
3877+ const cwd = yield * makeTmpDir ( ) ;
3878+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3879+ const remote = yield * makeTmpDir ( "git-remote-" ) ;
3880+ yield * git ( remote , [ "init" , "--bare" ] ) ;
3881+ yield * git ( cwd , [ "remote" , "add" , "origin" , remote ] ) ;
3882+ yield * git ( cwd , [ "push" , "origin" , initialBranch ] ) ;
3883+ const fileSystem = yield * FileSystem . FileSystem ;
3884+ const path = yield * Path . Path ;
3885+ const lockPath = path . join ( cwd , ".git" , "config.lock" ) ;
3886+ yield * fileSystem . writeFileString ( lockPath , "" ) ;
3887+ const { driver, exits } = yield * makeDriverReportingExits ( ( args ) =>
3888+ args . some ( ( arg ) => arg . startsWith ( "--set-upstream-to" ) ) ,
3889+ ) ;
3890+
3891+ // The upstream write retries on the real clock while the external lock is released.
3892+ const creating = yield * TestClock . withLive (
3893+ driver . createWorktree ( {
3894+ cwd,
3895+ path : path . join ( yield * makeTmpDir ( "git-worktrees-" ) , "tracked" ) ,
3896+ refName : `origin/${ initialBranch } ` ,
3897+ newRefName : "feature/tracked" ,
3898+ } ) ,
3899+ ) . pipe ( Effect . forkChild ) ;
3900+ // Raced with the launch so a branch step that fails under the lock fails the test.
3901+ assert . notEqual (
3902+ yield * Effect . raceFirst ( Queue . take ( exits ) , Fiber . join ( creating ) ) ,
3903+ ChildProcessSpawner . ExitCode ( 0 ) ,
3904+ ) ;
3905+ yield * fileSystem . remove ( lockPath ) ;
3906+ yield * Fiber . join ( creating ) ;
3907+
3908+ assert . equal (
3909+ yield * git ( cwd , [ "rev-parse" , "--abbrev-ref" , "feature/tracked@{upstream}" ] ) ,
3910+ `origin/${ initialBranch } ` ,
3911+ ) ;
3912+ } ) ,
3913+ ) ;
3914+
38953915 // regression: #12485 explain fetch failures without exposing remote output
38963916 it . effect ( "explains a real fetch failure for a missing local remote" , ( ) =>
38973917 Effect . gen ( function * ( ) {
@@ -4405,6 +4425,29 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
44054425 } ) ,
44064426 ) ;
44074427
4428+ it . effect ( "reports a push whose upstream could not be recorded" , ( ) =>
4429+ Effect . gen ( function * ( ) {
4430+ const cwd = yield * makeTmpDir ( ) ;
4431+ const remote = yield * makeTmpDir ( "git-remote-" ) ;
4432+ yield * initRepoWithCommit ( cwd ) ;
4433+ yield * git ( remote , [ "init" , "--bare" ] ) ;
4434+ yield * git ( cwd , [ "remote" , "add" , "origin" , remote ] ) ;
4435+ yield * git ( cwd , [ "checkout" , "-q" , "-b" , "feature/locked" ] ) ;
4436+ yield * writeTextFile ( cwd , ".git/config.lock" , "" ) ;
4437+ const driver = yield * GitVcsDriver . GitVcsDriver ;
4438+
4439+ // Recording the upstream retries on the real clock before giving up.
4440+ const pushed = yield * TestClock . withLive ( driver . pushCurrentBranch ( cwd , null ) ) ;
4441+
4442+ assert . deepInclude ( pushed , { status : "pushed" , setUpstream : false } ) ;
4443+ assert . include (
4444+ yield * git ( cwd , [ "ls-remote" , "origin" , "refs/heads/feature/locked" ] ) ,
4445+ yield * git ( cwd , [ "rev-parse" , "HEAD" ] ) ,
4446+ ) ;
4447+ assert . equal ( yield * driver . readConfigValue ( cwd , "branch.feature/locked.remote" ) , null ) ;
4448+ } ) ,
4449+ ) ;
4450+
44084451 it . effect . each ( [ "feature/push" , "origin/feature/push" ] ) (
44094452 "publishes %s when the configured diff base already contains HEAD" ,
44104453 ( branch ) =>
0 commit comments