@@ -1123,6 +1123,287 @@ describe("pullCurrentBranch", () => {
11231123 } ;
11241124 } ) ;
11251125
1126+ it . effect ( "resolves a status waiter when Pull claims its queued fetch" , ( ) =>
1127+ Effect . scoped (
1128+ Effect . gen ( function * ( ) {
1129+ const { cwd, remoteHead } = yield * cloneBehindRemote ;
1130+ const blockers : string [ ] = [ ] ;
1131+ for ( let index = 0 ; index < 4 ; index ++ ) {
1132+ const blocker = yield * cloneBehindRemote ;
1133+ blockers . push ( NodeFS . realpathSync . native ( blocker . cwd ) ) ;
1134+ }
1135+ const targetPath = NodeFS . realpathSync . native ( cwd ) ;
1136+ const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
1137+ const release = yield * Deferred . make < void > ( ) ;
1138+ const blockersStarted = yield * Deferred . make < void > ( ) ;
1139+ const targetEntered = yield * Deferred . make < void > ( ) ;
1140+ let running = 0 ;
1141+ let targetFetches = 0 ;
1142+ let waiterSucceeded = false ;
1143+ const spawner = ChildProcessSpawner . make ( ( command ) =>
1144+ Effect . gen ( function * ( ) {
1145+ const handle = yield * delegate . spawn ( command ) ;
1146+ if ( ! ChildProcess . isStandardCommand ( command ) || ! command . args . includes ( "fetch" ) )
1147+ return handle ;
1148+ const commandCwd = String ( command . options . cwd ) ;
1149+ if ( commandCwd === targetPath ) targetFetches ++ ;
1150+ if ( blockers . includes ( commandCwd ) ) {
1151+ running ++ ;
1152+ if ( running === 4 ) yield * Deferred . succeed ( blockersStarted , undefined ) ;
1153+ yield * Deferred . await ( release ) ;
1154+ }
1155+ return handle ;
1156+ } ) ,
1157+ ) ;
1158+ const driver = yield * makeGitVcsDriverCore ( ) . pipe (
1159+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
1160+ ) ;
1161+ const tracer = Tracer . make ( {
1162+ span ( options ) {
1163+ const span = new Tracer . NativeSpan ( options ) ;
1164+ if ( options . name === "GitVcsDriver.fetchRemoteForStatus.wait" ) {
1165+ const attribute = span . attribute . bind ( span ) ;
1166+ span . attribute = ( key , value ) => {
1167+ attribute ( key , value ) ;
1168+ if ( key === "statusFetch.joining" ) Deferred . doneUnsafe ( targetEntered , Effect . void ) ;
1169+ } ;
1170+ const end = span . end . bind ( span ) ;
1171+ span . end = ( endTime , exit ) => {
1172+ waiterSucceeded = Exit . isSuccess ( exit ) ;
1173+ end ( endTime , exit ) ;
1174+ } ;
1175+ }
1176+ return span ;
1177+ } ,
1178+ } ) ;
1179+ yield * Effect . gen ( function * ( ) {
1180+ for ( const blocker of blockers ) {
1181+ yield * driver . statusDetailsRemote ( blocker ) . pipe ( Effect . forkChild ) ;
1182+ }
1183+ yield * Deferred . await ( blockersStarted ) ;
1184+ const status = yield * driver
1185+ . statusDetailsRemote ( cwd )
1186+ . pipe ( Effect . withTracer ( tracer ) , Effect . forkChild ( { startImmediately : true } ) ) ;
1187+ yield * Deferred . await ( targetEntered ) ;
1188+ const pull = yield * driver . pullCurrentBranch ( cwd ) ;
1189+ const refreshed = yield * Fiber . join ( status ) ;
1190+ assert . equal ( pull . status , "pulled" ) ;
1191+ assert . equal ( yield * git ( cwd , [ "rev-parse" , "HEAD" ] ) , remoteHead ) ;
1192+ assert . isTrue ( waiterSucceeded ) ;
1193+ assert . equal ( refreshed . behindCount , 0 ) ;
1194+ assert . equal ( targetFetches , 0 ) ;
1195+ } ) . pipe ( Effect . ensuring ( Deferred . succeed ( release , undefined ) ) ) ;
1196+ } ) ,
1197+ ) . pipe ( Effect . provide ( layerTest ) ) ,
1198+ ) ;
1199+
1200+ const claimedFetchFallbacks = [ "diverged" , "interrupted" , "upstream-only" ] as const ;
1201+
1202+ it . effect . each ( claimedFetchFallbacks ) (
1203+ "falls back to the claimed status fetch when Pull is %s" ,
1204+ ( failure ) =>
1205+ Effect . scoped (
1206+ Effect . gen ( function * ( ) {
1207+ const { cwd, updater, remoteHead } = yield * cloneBehindRemote ;
1208+ if ( failure === "upstream-only" ) {
1209+ yield * git ( updater , [ "push" , "origin" , "HEAD:refs/heads/other" ] ) ;
1210+ yield * writeTextFile ( cwd , ".git/refs/remotes/origin/other.lock" , "" ) ;
1211+ }
1212+ if ( failure === "diverged" ) {
1213+ yield * writeTextFile ( cwd , "local.txt" , "local\n" ) ;
1214+ yield * git ( cwd , [ "add" , "local.txt" ] ) ;
1215+ yield * git ( cwd , [ "commit" , "-m" , "local commit" ] ) ;
1216+ }
1217+ const blockers : string [ ] = [ ] ;
1218+ for ( let index = 0 ; index < 4 ; index ++ ) {
1219+ const blocker = yield * cloneBehindRemote ;
1220+ blockers . push ( NodeFS . realpathSync . native ( blocker . cwd ) ) ;
1221+ }
1222+ const targetPath = NodeFS . realpathSync . native ( cwd ) ;
1223+ const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
1224+ const release = yield * Deferred . make < void > ( ) ;
1225+ const blockersStarted = yield * Deferred . make < void > ( ) ;
1226+ const targetEntered = yield * Deferred . make < void > ( ) ;
1227+ const targetFetchStarted = yield * Deferred . make < void > ( ) ;
1228+ const pullBeforeFetch = yield * Deferred . make < void > ( ) ;
1229+ const releasePull = yield * Deferred . make < void > ( ) ;
1230+ let running = 0 ;
1231+ let targetFetches = 0 ;
1232+ let waiterSucceeded = false ;
1233+ let targetFetchExitCode : number | undefined ;
1234+ const spawner = ChildProcessSpawner . make ( ( command ) =>
1235+ Effect . gen ( function * ( ) {
1236+ if ( ChildProcess . isStandardCommand ( command ) ) {
1237+ const commandCwd = NodeFS . realpathSync . native ( String ( command . options . cwd ) ) ;
1238+ if (
1239+ commandCwd === targetPath &&
1240+ failure === "interrupted" &&
1241+ command . args [ 0 ] === "rev-parse" &&
1242+ command . args [ 1 ] === "HEAD" &&
1243+ command . args . length === 2
1244+ ) {
1245+ yield * Deferred . succeed ( pullBeforeFetch , undefined ) ;
1246+ yield * Deferred . await ( releasePull ) ;
1247+ }
1248+ }
1249+ const handle = yield * delegate . spawn ( command ) ;
1250+ if ( ! ChildProcess . isStandardCommand ( command ) || ! command . args . includes ( "fetch" ) )
1251+ return handle ;
1252+ const commandCwd = String ( command . options . cwd ) ;
1253+ if ( commandCwd === targetPath ) {
1254+ targetFetches ++ ;
1255+ yield * Deferred . succeed ( targetFetchStarted , undefined ) ;
1256+ return ChildProcessSpawner . makeHandle ( {
1257+ ...handle ,
1258+ exitCode : handle . exitCode . pipe (
1259+ Effect . tap ( ( exitCode ) =>
1260+ Effect . sync ( ( ) => {
1261+ targetFetchExitCode = exitCode ;
1262+ } ) ,
1263+ ) ,
1264+ ) ,
1265+ } ) ;
1266+ }
1267+ if ( blockers . includes ( commandCwd ) ) {
1268+ running ++ ;
1269+ if ( running === 4 ) yield * Deferred . succeed ( blockersStarted , undefined ) ;
1270+ yield * Deferred . await ( release ) ;
1271+ }
1272+ return handle ;
1273+ } ) ,
1274+ ) ;
1275+ const driver = yield * makeGitVcsDriverCore ( ) . pipe (
1276+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
1277+ ) ;
1278+ const tracer = Tracer . make ( {
1279+ span ( options ) {
1280+ const span = new Tracer . NativeSpan ( options ) ;
1281+ if ( options . name === "GitVcsDriver.fetchRemoteForStatus.wait" ) {
1282+ const attribute = span . attribute . bind ( span ) ;
1283+ span . attribute = ( key , value ) => {
1284+ attribute ( key , value ) ;
1285+ if ( key === "statusFetch.joining" )
1286+ Deferred . doneUnsafe ( targetEntered , Effect . void ) ;
1287+ } ;
1288+ const end = span . end . bind ( span ) ;
1289+ span . end = ( endTime , exit ) => {
1290+ waiterSucceeded = Exit . isSuccess ( exit ) ;
1291+ end ( endTime , exit ) ;
1292+ } ;
1293+ }
1294+ return span ;
1295+ } ,
1296+ } ) ;
1297+ yield * Effect . gen ( function * ( ) {
1298+ for ( const blocker of blockers ) {
1299+ yield * driver . statusDetailsRemote ( blocker ) . pipe ( Effect . forkChild ) ;
1300+ }
1301+ yield * Deferred . await ( blockersStarted ) ;
1302+ const status = yield * driver
1303+ . statusDetailsRemote ( cwd )
1304+ . pipe ( Effect . withTracer ( tracer ) , Effect . forkChild ( { startImmediately : true } ) ) ;
1305+ yield * Deferred . await ( targetEntered ) ;
1306+ if ( failure === "interrupted" ) {
1307+ const pull = yield * driver . pullCurrentBranch ( cwd ) . pipe ( Effect . forkChild ) ;
1308+ yield * Deferred . await ( pullBeforeFetch ) ;
1309+ yield * Fiber . interrupt ( pull ) ;
1310+ const exit = yield * Fiber . await ( pull ) ;
1311+ assert . isTrue ( Exit . isFailure ( exit ) && Cause . hasInterruptsOnly ( exit . cause ) ) ;
1312+ } else if ( failure === "upstream-only" ) {
1313+ assert . equal ( ( yield * driver . pullCurrentBranch ( cwd ) ) . status , "pulled" ) ;
1314+ yield * ( yield * FileSystem . FileSystem ) . remove (
1315+ `${ cwd } /.git/refs/remotes/origin/other.lock` ,
1316+ ) ;
1317+ } else {
1318+ const error = yield * Effect . flip ( driver . pullCurrentBranch ( cwd ) ) ;
1319+ assert . equal ( error . operation , "GitVcsDriver.pullCurrentBranch.pull" ) ;
1320+ }
1321+ assert . equal ( targetFetches , 0 ) ;
1322+ yield * Deferred . succeed ( release , undefined ) ;
1323+ yield * Deferred . await ( targetFetchStarted ) ;
1324+ assert . isTrue ( ( yield * Fiber . join ( status ) ) . isRepo ) ;
1325+ assert . isTrue ( waiterSucceeded ) ;
1326+ assert . equal ( targetFetches , 1 ) ;
1327+ assert . equal ( targetFetchExitCode , 0 ) ;
1328+ if ( failure === "upstream-only" ) {
1329+ assert . equal ( yield * git ( cwd , [ "rev-parse" , "refs/remotes/origin/other" ] ) , remoteHead ) ;
1330+ }
1331+ } ) . pipe (
1332+ Effect . ensuring (
1333+ Effect . all ( [
1334+ Deferred . succeed ( release , undefined ) ,
1335+ Deferred . succeed ( releasePull , undefined ) ,
1336+ ] ) ,
1337+ ) ,
1338+ ) ;
1339+ } ) ,
1340+ ) . pipe ( Effect . provide ( layerTest ) ) ,
1341+ ) ;
1342+
1343+ it . effect ( "waits for a running status fetch before starting Pull" , ( ) =>
1344+ Effect . scoped (
1345+ Effect . gen ( function * ( ) {
1346+ const { cwd, remoteHead } = yield * cloneBehindRemote ;
1347+ const targetPath = NodeFS . realpathSync . native ( cwd ) ;
1348+ const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
1349+ const fetchStarted = yield * Deferred . make < void > ( ) ;
1350+ const releaseFetch = yield * Deferred . make < void > ( ) ;
1351+ const pullResolvedCommonDir = yield * Deferred . make < void > ( ) ;
1352+ const pullStarted = yield * Deferred . make < void > ( ) ;
1353+ const events : string [ ] = [ ] ;
1354+ const spawner = ChildProcessSpawner . make ( ( command ) =>
1355+ Effect . gen ( function * ( ) {
1356+ if ( ! ChildProcess . isStandardCommand ( command ) ) return yield * delegate . spawn ( command ) ;
1357+ const target = NodeFS . realpathSync . native ( String ( command . options . cwd ) ) === targetPath ;
1358+ if ( target && command . args . includes ( "pull" ) ) {
1359+ events . push ( "pull-spawn" ) ;
1360+ yield * Deferred . succeed ( pullStarted , undefined ) ;
1361+ }
1362+ const handle = yield * delegate . spawn ( command ) ;
1363+ if ( target && command . args . includes ( "fetch" ) ) {
1364+ yield * Deferred . succeed ( fetchStarted , undefined ) ;
1365+ yield * Deferred . await ( releaseFetch ) ;
1366+ yield * handle . exitCode ;
1367+ events . push ( "fetch-exit" ) ;
1368+ }
1369+ return handle ;
1370+ } ) ,
1371+ ) ;
1372+ const driver = yield * makeGitVcsDriverCore ( ) . pipe (
1373+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
1374+ ) ;
1375+ const tracer = Tracer . make ( {
1376+ span ( options ) {
1377+ const span = new Tracer . NativeSpan ( options ) ;
1378+ if ( options . name === "resolveGitCommonDir" ) {
1379+ const end = span . end . bind ( span ) ;
1380+ span . end = ( endTime , exit ) => {
1381+ end ( endTime , exit ) ;
1382+ Deferred . doneUnsafe ( pullResolvedCommonDir , Effect . void ) ;
1383+ } ;
1384+ }
1385+ return span ;
1386+ } ,
1387+ } ) ;
1388+ yield * Effect . gen ( function * ( ) {
1389+ yield * driver . statusDetailsRemote ( cwd ) . pipe ( Effect . forkChild ) ;
1390+ yield * Deferred . await ( fetchStarted ) ;
1391+ const pull = yield * driver
1392+ . pullCurrentBranch ( cwd )
1393+ . pipe ( Effect . withTracer ( tracer ) , Effect . forkChild ( { startImmediately : true } ) ) ;
1394+ yield * Deferred . await ( pullResolvedCommonDir ) ;
1395+ assert . isFalse ( yield * Deferred . isDone ( pullStarted ) ) ;
1396+ assert . isUndefined ( pull . pollUnsafe ( ) ) ;
1397+ yield * Deferred . succeed ( releaseFetch , undefined ) ;
1398+ const result = yield * Fiber . join ( pull ) ;
1399+ assert . equal ( result . status , "pulled" ) ;
1400+ assert . equal ( yield * git ( cwd , [ "rev-parse" , "HEAD" ] ) , remoteHead ) ;
1401+ assert . deepEqual ( events , [ "fetch-exit" , "pull-spawn" ] ) ;
1402+ } ) . pipe ( Effect . ensuring ( Deferred . succeed ( releaseFetch , undefined ) ) ) ;
1403+ } ) ,
1404+ ) . pipe ( Effect . provide ( layerTest ) ) ,
1405+ ) ;
1406+
11261407 it . effect ( "still fetches other branches on the first pull" , ( ) =>
11271408 Effect . gen ( function * ( ) {
11281409 const driver = yield * GitVcsDriver . GitVcsDriver ;
0 commit comments