@@ -312,6 +312,12 @@ export default class LandingSession extends Session {
312312 cli . warn ( 'Not yet ready to amend, run `git node land --abort`' ) ;
313313 return ;
314314 }
315+
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+
315321 this . startAmending ( ) ;
316322
317323 const rev = this . getCurrentRev ( ) ;
@@ -329,7 +335,12 @@ export default class LandingSession extends Session {
329335 } ) . trim ( ) . length !== 0 ;
330336
331337 const metadata = this . metadata . trim ( ) . split ( '\n' ) ;
332- const amended = original . split ( '\n' ) ;
338+ // Filtering out metadata that we will add ourselves:
339+ const isFiltered = line =>
340+ ( ! PR_RE . test ( line ) || this . backport ) &&
341+ ! BACKPORT_RE . test ( line ) &&
342+ ! REVIEW_RE . test ( line ) ;
343+ const amended = original . split ( '\n' ) . filter ( isFiltered ) ;
333344
334345 // If the original commit message already contains trailers (such as
335346 // "Co-authored-by"), we simply add our own metadata after those. Otherwise,
@@ -339,35 +350,29 @@ export default class LandingSession extends Session {
339350 amended . push ( '' ) ;
340351 }
341352
342- const BACKPORT_RE = / B A C K P O R T - P R - U R L \s * : \s * ( \S + ) / i;
343- const PR_RE = / P R - U R L \s * : \s * ( \S + ) / i;
344- const REVIEW_RE = / R e v i e w e d - B y \s * : \s * ( \S + ) / i;
345- const CVE_RE = / C V E - I D \s * : \s * ( \S + ) / i;
346-
347353 let containCVETrailer = false ;
348354 for ( const line of metadata ) {
349- if ( line . length !== 0 && original . includes ( line ) ) {
350- if ( line . match ( CVE_RE ) ) {
351- containCVETrailer = true ;
352- }
355+ if ( line . length !== 0 && original . includes ( line ) && ! isFiltered ( line ) ) {
356+ containCVETrailer ||= CVE_RE . test ( line ) ;
353357 if ( originalHasTrailers ) {
354358 cli . warn ( `Found ${ line } , skipping..` ) ;
359+ continue ;
355360 } else {
356361 cli . error ( 'Git found no trailers in the original commit message, ' +
357362 `but '${ line } ' is present and should be a trailer.` ) ;
358363 process . exit ( 1 ) ; // make it work with git rebase -x
359364 }
360- } else {
361- if ( line . match ( BACKPORT_RE ) ) {
362- let prIndex = amended . findIndex ( datum => datum . match ( PR_RE ) ) ;
363- if ( prIndex === - 1 ) {
364- prIndex = amended . findIndex ( datum => datum . match ( REVIEW_RE ) ) - 1 ;
365- }
366- amended . splice ( prIndex + 1 , 0 , line ) ;
367- } else {
368- amended . push ( line ) ;
365+ }
366+ if ( BACKPORT_RE . test ( line ) ) {
367+ const prIndex =
368+ ( amended . findIndex ( datum => PR_RE . test ( datum ) ) + 1 ) ||
369+ amended . findIndex ( datum => REVIEW_RE . test ( datum ) ) ;
370+ if ( prIndex !== - 1 ) {
371+ amended . splice ( prIndex , 0 , line ) ;
372+ continue ;
369373 }
370374 }
375+ amended . push ( line ) ;
371376 }
372377
373378 if ( ! containCVETrailer && this . includeCVE ) {
0 commit comments