@@ -355,6 +355,11 @@ export const make = Effect.gen(function* () {
355355 return yield * failure ( operation , `GitCafe's stack operation ${ outcome . state } .` ) ;
356356 } ) ;
357357
358+ // GitCafe's pulls carry no line totals, and linked pull requests are summarized every minute,
359+ // so hunk totals are held per compared pair of commits. The file list that names the pair is
360+ // cheap; a retarget or a landed parent layer moves the base without moving the head.
361+ const lineTotals = new Map < string , { readonly additions : number ; readonly deletions : number } > ( ) ;
362+
358363 const readLineStats = Effect . fn ( "GitCafePullRequestProvider.readLineStats" ) ( function * (
359364 input : PullRef ,
360365 version : number ,
@@ -382,45 +387,34 @@ export const make = Effect.gen(function* () {
382387 after = batch . next ;
383388 if ( after === null || after === previous || batch . items . length === 0 ) break ;
384389 }
385- const counted =
386- files . some ( ( file ) => file . additions !== undefined ) ||
387- files . length > LINE_STATS_MAX_FILES ||
388- revision === undefined
389- ? files
390- : ( yield * Effect . forEach (
390+ if ( revision === undefined ) return { changedFiles : 0 , additions : 0 , deletions : 0 } ;
391+ const key = `${ input . host } ${ input . repository . toLowerCase ( ) } ${ input . number } ${ revision . baseOid } ${ revision . headOid } ` ;
392+ const held = lineTotals . get ( key ) ;
393+ if ( held !== undefined ) return { changedFiles : files . length , ...held } ;
394+ const details =
395+ files . some ( ( file ) => file . additions !== undefined ) || files . length > LINE_STATS_MAX_FILES
396+ ? Option . some ( files )
397+ : yield * Effect . forEach (
391398 chunk ( detailablePaths ( files ) , DIFF_FILES_BATCH ) ,
392399 ( paths ) => readDiffFiles ( input , revision , paths ) ,
393400 { concurrency : 4 } ,
394- ) ) . flat ( ) ;
395- return {
396- changedFiles : files . length ,
401+ ) . pipe (
402+ Effect . map ( ( batches ) => batches . flat ( ) ) ,
403+ Effect . option ,
404+ ) ;
405+ // Unread hunks leave the lines unknown rather than zero; the file count still stands.
406+ if ( Option . isNone ( details ) ) return { changedFiles : files . length } ;
407+ const counted = details . value ;
408+ const totals = {
397409 additions : counted . reduce ( ( total , file ) => total + ( file . additions ?? 0 ) , 0 ) ,
398410 deletions : counted . reduce ( ( total , file ) => total + ( file . deletions ?? 0 ) , 0 ) ,
399411 } ;
412+ if ( lineTotals . size >= LINE_STATS_CACHE_SIZE )
413+ lineTotals . delete ( lineTotals . keys ( ) . next ( ) . value ! ) ;
414+ lineTotals . set ( key , totals ) ;
415+ return { changedFiles : files . length , ...totals } ;
400416 } ) ;
401417
402- // GitCafe's pulls carry no line totals, and linked pull requests are summarized every minute,
403- // so totals are held per head commit rather than recounted from hunks on every read. The
404- // pull's version also moves on comments and reviews, which change no lines.
405- const lineStatsByHead = new Map < string , Effect . Success < ReturnType < typeof readLineStats > > > ( ) ;
406- const lineStats = (
407- input : PullRef ,
408- pull : { readonly version : number ; readonly headOid : string } ,
409- ) => {
410- const key = `${ input . host } ${ input . repository . toLowerCase ( ) } ${ input . number } ${ pull . headOid } ` ;
411- const held = lineStatsByHead . get ( key ) ;
412- if ( held !== undefined ) return Effect . succeed ( held ) ;
413- return readLineStats ( input , pull . version ) . pipe (
414- Effect . tap ( ( stats ) =>
415- Effect . sync ( ( ) => {
416- if ( lineStatsByHead . size >= LINE_STATS_CACHE_SIZE )
417- lineStatsByHead . delete ( lineStatsByHead . keys ( ) . next ( ) . value ! ) ;
418- lineStatsByHead . set ( key , stats ) ;
419- } ) ,
420- ) ,
421- ) ;
422- } ;
423-
424418 const merge = Effect . fn ( "GitCafePullRequestProvider.merge" ) ( function * (
425419 input : Parameters < PullRequestProviderApi [ "runAction" ] > [ 0 ] ,
426420 ) {
@@ -554,9 +548,7 @@ export const make = Effect.gen(function* () {
554548 const changes =
555549 pull . headOid === null
556550 ? null
557- : yield * lineStats ( input , { version : pull . version , headOid : pull . headOid } ) . pipe (
558- Effect . orElseSucceed ( ( ) => null ) ,
559- ) ;
551+ : yield * readLineStats ( input , pull . version ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
560552 // A count that could not be read is absent rather than zero.
561553 const {
562554 additions : _additions ,
@@ -588,9 +580,7 @@ export const make = Effect.gen(function* () {
588580 ) ,
589581 pull . headOid === null
590582 ? Effect . succeed ( null )
591- : lineStats ( input , { version : pull . version , headOid : pull . headOid } ) . pipe (
592- Effect . orElseSucceed ( ( ) => null ) ,
593- ) ,
583+ : readLineStats ( input , pull . version ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ,
594584 ] ,
595585 { concurrency : 3 } ,
596586 ) ;
0 commit comments