@@ -107,7 +107,10 @@ export async function merge(flags: MergeFlags = {}): Promise<void> {
107107 ) ;
108108 console . log ( ) ;
109109
110- for ( const { pr, bookmarkName : bookmark , changeId : chgId } of prsToMerge ) {
110+ for ( let i = 0 ; i < prsToMerge . length ; i ++ ) {
111+ const { pr, bookmarkName : bookmark , changeId : chgId } = prsToMerge [ i ] ;
112+ const nextPR = prsToMerge [ i + 1 ] ;
113+
111114 // SAFETY: Validate bookmark is not a protected branch
112115 const protectedBranches = [ trunk , "main" , "master" , "develop" ] ;
113116 if ( protectedBranches . includes ( bookmark ) ) {
@@ -122,29 +125,36 @@ export async function merge(flags: MergeFlags = {}): Promise<void> {
122125 console . log ( `Merging PR #${ cyan ( String ( pr . number ) ) } : ${ pr . title } ` ) ;
123126 console . log ( dim ( ` Branch: ${ bookmark } → ${ pr . baseRefName } ` ) ) ;
124127
125- // Before merging, update the PR base to trunk if it's not already
126- // (since we're merging bottom-up, each PR should target trunk after its base is merged)
128+ // CRITICAL: Before merging this PR, update the NEXT PR's base to trunk.
129+ // When we merge this PR with deleteHead:true, this PR's head branch gets deleted.
130+ // That branch is the next PR's base - if we don't update first, GitHub closes the next PR!
131+ if ( nextPR ) {
132+ console . log ( dim ( ` Rebasing PR #${ nextPR . pr . number } onto ${ trunk } ...` ) ) ;
133+ await github . updatePR ( nextPR . pr . number , { base : trunk } ) ;
134+ }
135+
136+ // Update current PR's base to trunk if needed
127137 if ( pr . baseRefName !== trunk ) {
128138 await github . updatePR ( pr . number , { base : trunk } ) ;
139+ }
140+
141+ // Wait for GitHub to recalculate merge status after base changes
142+ process . stdout . write ( dim ( " Waiting for GitHub..." ) ) ;
143+ const mergeableResult = unwrap (
144+ await github . waitForMergeable ( pr . number , {
145+ timeoutMs : 60000 ,
146+ pollIntervalMs : 2000 ,
147+ } ) ,
148+ ) ;
149+ process . stdout . write ( `\r${ " " . repeat ( 30 ) } \r` ) ;
129150
130- // Wait for GitHub to recalculate merge status after base change
131- process . stdout . write ( dim ( " Waiting for GitHub..." ) ) ;
132- const mergeableResult = unwrap (
133- await github . waitForMergeable ( pr . number , {
134- timeoutMs : 60000 ,
135- pollIntervalMs : 2000 ,
136- } ) ,
151+ if ( ! mergeableResult . mergeable ) {
152+ console . error (
153+ formatError (
154+ `PR #${ pr . number } is not mergeable: ${ mergeableResult . reason } ` ,
155+ ) ,
137156 ) ;
138- process . stdout . write ( `\r${ " " . repeat ( 30 ) } \r` ) ;
139-
140- if ( ! mergeableResult . mergeable ) {
141- console . error (
142- formatError (
143- `PR #${ pr . number } is not mergeable: ${ mergeableResult . reason } ` ,
144- ) ,
145- ) ;
146- process . exit ( 1 ) ;
147- }
157+ process . exit ( 1 ) ;
148158 }
149159
150160 unwrap (
0 commit comments