@@ -28,6 +28,7 @@ import {
2828 type RecordTupleWinner ,
2929} from "./record-tuple.js" ;
3030import { mergeSweepStatusJson } from "./sweep-status-merge.js" ;
31+ import { mergeCommentRouterLedgers } from "./comment-router-ledger-merge.js" ;
3132
3233export type GitRunResult = {
3334 status : number ;
@@ -979,16 +980,24 @@ export function pushCommit(options: {
979980 }
980981 const remoteCommit = runGit ( [ "rev-parse" , `${ remote } /${ branch } ` ] , { quiet : true } ) . trim ( ) ;
981982 const statusMerges = planSweepStatusMerges ( { localCommit, remoteCommit } ) ;
983+ const ledgerMerge = planCommentRouterLedgerMerge ( { localCommit, remoteCommit } ) ;
982984 const rebaseArgs =
983985 rebaseStrategy === "theirs" || rebaseStrategy === "apply-records"
984986 ? [ "rebase" , "-X" , "theirs" , `${ remote } /${ branch } ` ]
985987 : [ "rebase" , `${ remote } /${ branch } ` ] ;
986988 if ( spawnGit ( rebaseArgs ) . status === 0 ) {
987989 applySweepStatusMerges ( { statusMerges, remoteCommit, localCommitMessage } ) ;
990+ applyCommentRouterLedgerMerge ( { ledgerMerge, remoteCommit, localCommitMessage } ) ;
991+ continue ;
992+ }
993+ if ( resolveCommentRouterLedgerConflict ( ledgerMerge ) ) {
994+ applySweepStatusMerges ( { statusMerges, remoteCommit, localCommitMessage } ) ;
995+ applyCommentRouterLedgerMerge ( { ledgerMerge, remoteCommit, localCommitMessage } ) ;
988996 continue ;
989997 }
990998 if ( rebaseStrategy === "apply-records" && resolveApplyRecordConflicts ( statusMerges ) ) {
991999 applySweepStatusMerges ( { statusMerges, remoteCommit, localCommitMessage } ) ;
1000+ applyCommentRouterLedgerMerge ( { ledgerMerge, remoteCommit, localCommitMessage } ) ;
9921001 continue ;
9931002 } else {
9941003 runGit ( [ "rebase" , "--abort" ] , { allowFailure : true } ) ;
@@ -1430,6 +1439,13 @@ function rebuildPublishCommit(options: {
14301439 includeIndependent : true ,
14311440 pathspecs : options . paths ,
14321441 } ) ;
1442+ const ledgerMerge = planCommentRouterLedgerMerge ( {
1443+ baseCommit : runGit ( [ "rev-parse" , `${ options . sourceCommit } ^` ] , { quiet : true } ) . trim ( ) ,
1444+ localCommit : options . sourceCommit ,
1445+ remoteCommit,
1446+ includeIndependent : true ,
1447+ pathspecs : options . paths ,
1448+ } ) ;
14331449 runGit ( [ "reset" , "--hard" , remoteCommit ] ) ;
14341450
14351451 for ( const path of uniqueNonEmpty ( options . paths ) ) {
@@ -1446,6 +1462,7 @@ function rebuildPublishCommit(options: {
14461462 }
14471463
14481464 applySweepStatusMergeFiles ( statusMerges ) ;
1465+ applyCommentRouterLedgerMergeFiles ( ledgerMerge ? [ ledgerMerge ] : [ ] ) ;
14491466 stagePaths ( options . paths ) ;
14501467 if ( ! hasStagedChanges ( ) ) {
14511468 console . log ( "No publish changes after syncing remote" ) ;
@@ -1564,6 +1581,90 @@ function readGitPath(commit: string, path: string): string | null {
15641581 return result . status === 0 ? result . stdout : null ;
15651582}
15661583
1584+ type CommentRouterLedgerMerge = { path : string ; content : string } ;
1585+
1586+ function planCommentRouterLedgerMerge ( options : {
1587+ baseCommit ?: string ;
1588+ localCommit : string ;
1589+ remoteCommit : string ;
1590+ includeIndependent ?: boolean ;
1591+ pathspecs ?: readonly string [ ] ;
1592+ } ) : CommentRouterLedgerMerge | null {
1593+ const path = "results/comment-router.json" ;
1594+ if ( options . pathspecs && ! gitPathspecsInclude ( options . pathspecs , path ) ) {
1595+ return null ;
1596+ }
1597+ const baseCommit =
1598+ options . baseCommit ??
1599+ runGit ( [ "merge-base" , options . localCommit , options . remoteCommit ] , { quiet : true } ) . trim ( ) ;
1600+ if ( ! baseCommit )
1601+ throw new Error ( "Refusing comment router ledger merge without a common Git base" ) ;
1602+ const base = readGitPath ( baseCommit , path ) ;
1603+ const local = readGitPath ( options . localCommit , path ) ;
1604+ const remote = readGitPath ( options . remoteCommit , path ) ;
1605+ if ( ! local || ! remote ) return null ;
1606+ const localChanged = local !== base ;
1607+ const remoteChanged = remote !== base ;
1608+ if ( ! localChanged || ( ! options . includeIndependent && ! remoteChanged ) ) return null ;
1609+ return { path, content : mergeCommentRouterLedgers ( local , remote ) } ;
1610+ }
1611+
1612+ function gitPathspecsInclude ( pathspecs : readonly string [ ] , path : string ) : boolean {
1613+ // Publish accepts Git pathspec syntax, including root and magic forms. Ask
1614+ // Git to evaluate that contract so semantic merges cannot be bypassed by a
1615+ // broader spelling than the literal generated path.
1616+ return runGit ( [ "ls-files" , "-z" , "--" , ...uniqueNonEmpty ( pathspecs ) ] , { quiet : true } )
1617+ . split ( "\0" )
1618+ . includes ( path ) ;
1619+ }
1620+
1621+ function applyCommentRouterLedgerMergeFiles ( merges : readonly CommentRouterLedgerMerge [ ] ) : void {
1622+ const root = publishRoot ( ) ?? resolve ( "." ) ;
1623+ for ( const merge of merges ) {
1624+ const destination = resolve ( root , merge . path ) ;
1625+ if ( ! isPathInsideOrEqual ( root , destination ) ) {
1626+ throw new Error (
1627+ `Refusing to merge comment router ledger outside publish root: ${ merge . path } ` ,
1628+ ) ;
1629+ }
1630+ mkdirSync ( dirname ( destination ) , { recursive : true } ) ;
1631+ writeFileSync ( destination , merge . content , "utf8" ) ;
1632+ runGit ( [ "add" , "--" , merge . path ] ) ;
1633+ }
1634+ }
1635+
1636+ function resolveCommentRouterLedgerConflict ( ledgerMerge : CommentRouterLedgerMerge | null ) : boolean {
1637+ if ( ! ledgerMerge ) return false ;
1638+ const conflicts = runGit ( [ "diff" , "--name-only" , "--diff-filter=U" ] , { allowFailure : true } )
1639+ . split ( / \r ? \n / )
1640+ . map ( ( path ) => path . trim ( ) )
1641+ . filter ( Boolean ) ;
1642+ if ( ! conflicts . includes ( ledgerMerge . path ) ) return false ;
1643+ applyCommentRouterLedgerMergeFiles ( [ ledgerMerge ] ) ;
1644+ if ( conflicts . length > 1 ) return false ;
1645+ return spawnGit ( [ "-c" , "core.editor=true" , "rebase" , "--continue" ] ) . status === 0 ;
1646+ }
1647+
1648+ function applyCommentRouterLedgerMerge ( options : {
1649+ ledgerMerge : CommentRouterLedgerMerge | null ;
1650+ remoteCommit : string ;
1651+ localCommitMessage : string ;
1652+ } ) : void {
1653+ if ( ! options . ledgerMerge ) return ;
1654+ applyCommentRouterLedgerMergeFiles ( [ options . ledgerMerge ] ) ;
1655+ if ( ! hasStagedChanges ( ) ) return ;
1656+ if ( spawnGit ( [ "diff" , "--cached" , "--quiet" , options . remoteCommit ] ) . status === 0 ) {
1657+ runGit ( [ "reset" , "--hard" , options . remoteCommit ] ) ;
1658+ return ;
1659+ }
1660+ const commitsAhead = Number (
1661+ runGit ( [ "rev-list" , "--count" , `${ options . remoteCommit } ..HEAD` ] , { quiet : true } ) . trim ( ) ,
1662+ ) ;
1663+ if ( Number . isInteger ( commitsAhead ) && commitsAhead > 0 )
1664+ runGit ( [ "commit" , "--amend" , "--no-edit" ] ) ;
1665+ else runGit ( [ "commit" , "-m" , options . localCommitMessage ] ) ;
1666+ }
1667+
15671668function applySweepStatusMergeFiles ( statusMerges : readonly SweepStatusMerge [ ] ) : void {
15681669 const root = publishRoot ( ) ?? resolve ( "." ) ;
15691670 for ( const statusMerge of statusMerges ) {
0 commit comments