@@ -286,6 +286,12 @@ export default class LandingSession extends Session {
286286 cli . warn ( 'Not yet ready to amend, run `git node land --abort`' ) ;
287287 return ;
288288 }
289+
290+ const BACKPORT_RE = / ^ B A C K P O R T - P R - U R L \s * : \s * ( \S + ) $ / i;
291+ const PR_RE = / ^ P R - U R L \s * : \s * ( \S + ) $ / i;
292+ const REVIEW_RE = / ^ R e v i e w e d - B y \s * : \s * ( \S + ) $ / i;
293+ const CVE_RE = / ^ C V E - I D \s * : \s * ( \S + ) $ / i;
294+
289295 this . startAmending ( ) ;
290296
291297 const rev = this . getCurrentRev ( ) ;
@@ -303,7 +309,12 @@ export default class LandingSession extends Session {
303309 } ) . trim ( ) . length !== 0 ;
304310
305311 const metadata = this . metadata . trim ( ) . split ( '\n' ) ;
306- const amended = original . split ( '\n' ) ;
312+ // Filtering out metadata that we will add ourselves:
313+ const isFiltered = line =>
314+ ( ! PR_RE . test ( line ) || this . backport ) &&
315+ ! BACKPORT_RE . test ( line ) &&
316+ ! REVIEW_RE . test ( line ) ;
317+ const amended = original . split ( '\n' ) . filter ( isFiltered ) ;
307318
308319 // If the original commit message already contains trailers (such as
309320 // "Co-authored-by"), we simply add our own metadata after those. Otherwise,
@@ -313,35 +324,29 @@ export default class LandingSession extends Session {
313324 amended . push ( '' ) ;
314325 }
315326
316- const BACKPORT_RE = / B A C K P O R T - P R - U R L \s * : \s * ( \S + ) / i;
317- const PR_RE = / P R - U R L \s * : \s * ( \S + ) / i;
318- const REVIEW_RE = / R e v i e w e d - B y \s * : \s * ( \S + ) / i;
319- const CVE_RE = / C V E - I D \s * : \s * ( \S + ) / i;
320-
321327 let containCVETrailer = false ;
322328 for ( const line of metadata ) {
323- if ( line . length !== 0 && original . includes ( line ) ) {
324- if ( line . match ( CVE_RE ) ) {
325- containCVETrailer = true ;
326- }
329+ if ( line . length !== 0 && original . includes ( line ) && ! isFiltered ( line ) ) {
330+ containCVETrailer ||= CVE_RE . test ( line ) ;
327331 if ( originalHasTrailers ) {
328332 cli . warn ( `Found ${ line } , skipping..` ) ;
333+ continue ;
329334 } else {
330335 cli . error ( 'Git found no trailers in the original commit message, ' +
331336 `but '${ line } ' is present and should be a trailer.` ) ;
332337 process . exit ( 1 ) ; // make it work with git rebase -x
333338 }
334- } else {
335- if ( line . match ( BACKPORT_RE ) ) {
336- let prIndex = amended . findIndex ( datum => datum . match ( PR_RE ) ) ;
337- if ( prIndex === - 1 ) {
338- prIndex = amended . findIndex ( datum => datum . match ( REVIEW_RE ) ) - 1 ;
339- }
340- amended . splice ( prIndex + 1 , 0 , line ) ;
341- } else {
342- amended . push ( line ) ;
339+ }
340+ if ( BACKPORT_RE . test ( line ) ) {
341+ const prIndex =
342+ ( amended . findIndex ( datum => PR_RE . test ( datum ) ) + 1 ) ||
343+ amended . findIndex ( datum => REVIEW_RE . test ( datum ) ) ;
344+ if ( prIndex !== - 1 ) {
345+ amended . splice ( prIndex , 0 , line ) ;
346+ continue ;
343347 }
344348 }
349+ amended . push ( line ) ;
345350 }
346351
347352 if ( ! containCVETrailer && this . includeCVE ) {
0 commit comments