@@ -1337,31 +1337,109 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
13371337 return null ;
13381338 }
13391339
1340- const listPaths = ( args : ReadonlyArray < string > ) =>
1340+ const lines = ( args : ReadonlyArray < string > , stdin ?: string ) =>
1341+ execute ( {
1342+ operation,
1343+ cwd : input . cwd ,
1344+ args,
1345+ ...( stdin === undefined ? { } : { stdin } ) ,
1346+ maxOutputBytes : VcsDriver . CHECKPOINT_DIFF_MAX_OUTPUT_BYTES ,
1347+ outputMode : "error" ,
1348+ } ) . pipe ( Effect . map ( ( result ) => result . stdout . split ( "\n" ) . filter ( ( line ) => line !== "" ) ) ) ;
1349+ const listPaths = ( args : ReadonlyArray < string > , stdin ?: string ) =>
13411350 execute ( {
13421351 operation,
13431352 cwd : input . cwd ,
13441353 args : [ ...args , "--name-only" , "-z" , "--no-renames" , "--no-ext-diff" ] ,
1354+ ...( stdin === undefined ? { } : { stdin } ) ,
13451355 maxOutputBytes : VcsDriver . CHECKPOINT_DIFF_MAX_OUTPUT_BYTES ,
13461356 outputMode : "error" ,
13471357 } ) . pipe ( Effect . map ( ( result ) => result . stdout . split ( "\0" ) ) ) ;
1358+
1359+ // A commit is an import when a remote-tracking reflog shows a fetch delivering it after the
1360+ // previous checkpoint. Only refs with a tip that recent can hold one, which keeps the walk
1361+ // cheap in repositories with thousands of remote refs.
1362+ const listFetchedCommits = Effect . fn ( "GitVcsDriver.checkpoints.listFetchedCommits" ) (
1363+ function * ( ) {
1364+ const since = Number ( capturedAt ) ;
1365+ const recentRemoteRefs = ( yield * lines ( [
1366+ "for-each-ref" ,
1367+ "--format=%(committerdate:unix) %(refname)" ,
1368+ "refs/remotes" ,
1369+ ] ) ) . flatMap ( ( line ) => {
1370+ const [ committedAt = "" , ref = "" ] = line . split ( " " ) ;
1371+ return Number ( committedAt ) >= since ? [ ref ] : [ ] ;
1372+ } ) ;
1373+ if ( recentRemoteRefs . length === 0 ) return new Set < string > ( ) ;
1374+
1375+ const entries = ( yield * lines (
1376+ [ "log" , "--walk-reflogs" , "--stdin" , "--date=unix" , "--format=%H %gD %gs" ] ,
1377+ `${ [ ...recentRemoteRefs , "HEAD" ] . join ( "\n" ) } \n` ,
1378+ ) ) . map ( ( line ) => {
1379+ const [ commit = "" , selector = "" , ...subject ] = line . split ( " " ) ;
1380+ const at = selector . lastIndexOf ( "@{" ) ;
1381+ return {
1382+ commit,
1383+ ref : selector . slice ( 0 , at ) ,
1384+ updatedAt : Number ( selector . slice ( at + 2 , - 1 ) ) ,
1385+ subject : subject . join ( " " ) ,
1386+ } ;
1387+ } ) ;
1388+ // Fetching another branch can rediscover a commit made locally during this turn.
1389+ const localCommits = new Set (
1390+ entries
1391+ . filter (
1392+ ( entry ) =>
1393+ entry . ref === "HEAD" &&
1394+ entry . updatedAt >= since &&
1395+ / ^ c o m m i t (?: \( [ ^ ) ] * \) ) ? : / . test ( entry . subject ) ,
1396+ )
1397+ . map ( ( entry ) => entry . commit ) ,
1398+ ) ;
1399+ const previousByRef = new Map < string , string > ( ) ;
1400+ const fetchRanges = entries . toReversed ( ) . flatMap ( ( entry ) => {
1401+ const previous = previousByRef . get ( entry . ref ) ;
1402+ previousByRef . set ( entry . ref , entry . commit ) ;
1403+ if ( entry . ref === "HEAD" ) return [ ] ;
1404+ if ( entry . updatedAt < since || entry . subject === "update by push" ) return [ ] ;
1405+ return [ entry . commit , ...( previous === undefined ? [ ] : [ `^${ previous } ` ] ) ] ;
1406+ } ) ;
1407+ if ( fetchRanges . length === 0 ) return new Set < string > ( ) ;
1408+ return new Set (
1409+ ( yield * lines (
1410+ [ "rev-list" , "--stdin" ] ,
1411+ `${ [ ...fetchRanges , `^${ startHead } ` ] . join ( "\n" ) } \n` ,
1412+ ) ) . filter ( ( commit ) => ! localCommits . has ( commit ) ) ,
1413+ ) ;
1414+ } ,
1415+ ) ;
1416+
13481417 // Remerge diffs retain merge conflict fixes while excluding clean upstream changes.
1349- const pathLists = yield * Effect . all (
1418+ const [ pathLists , recentCommits ] = yield * Effect . all (
13501419 [
1351- listPaths ( [ "diff" , startHead , `${ input . fromCheckpointRef } ^{commit}` ] ) ,
1352- listPaths ( [ "diff" , endHead , `${ input . toCheckpointRef } ^{commit}` ] ) ,
1353- listPaths ( [ "log" , "--format=" , "--diff-merges=remerge" , `${ endHead } ..${ startHead } ` ] ) ,
1354- listPaths ( [
1355- "log" ,
1356- "--format=" ,
1357- "--diff-merges=remerge" ,
1358- `--since=@${ capturedAt } ` ,
1359- `${ startHead } ..${ endHead } ` ,
1360- ] ) ,
1420+ Effect . all (
1421+ [
1422+ listPaths ( [ "diff" , startHead , `${ input . fromCheckpointRef } ^{commit}` ] ) ,
1423+ listPaths ( [ "diff" , endHead , `${ input . toCheckpointRef } ^{commit}` ] ) ,
1424+ listPaths ( [ "log" , "--format=" , "--diff-merges=remerge" , `${ endHead } ..${ startHead } ` ] ) ,
1425+ ] ,
1426+ { concurrency : "unbounded" } ,
1427+ ) ,
1428+ lines ( [ "rev-list" , `--since=@${ capturedAt } ` , `${ startHead } ..${ endHead } ` ] ) ,
13611429 ] ,
13621430 { concurrency : "unbounded" } ,
13631431 ) ;
1364- return new Set ( pathLists . flat ( ) . filter ( ( path ) => path . length > 0 ) ) ;
1432+ const fetchedCommits =
1433+ recentCommits . length === 0 ? new Set < string > ( ) : yield * listFetchedCommits ( ) ;
1434+ const ownCommits = recentCommits . filter ( ( commit ) => ! fetchedCommits . has ( commit ) ) ;
1435+ const ownCommitPaths =
1436+ ownCommits . length === 0
1437+ ? [ ]
1438+ : yield * listPaths (
1439+ [ "log" , "--no-walk=unsorted" , "--stdin" , "--format=" , "--diff-merges=remerge" ] ,
1440+ `${ ownCommits . join ( "\n" ) } \n` ,
1441+ ) ;
1442+ return new Set ( [ ...pathLists . flat ( ) , ...ownCommitPaths ] . filter ( ( path ) => path . length > 0 ) ) ;
13651443 } ) ,
13661444
13671445 deleteCheckpointRefs : Effect . fn ( "GitVcsDriver.checkpoints.deleteCheckpointRefs" ) (
0 commit comments