@@ -316,35 +316,43 @@ export default class LandingSession extends Session {
316316
317317 // git has very specific rules about what is a trailer and what is not.
318318 // Instead of trying to implement those ourselves, let git parse the
319- // original commit message and see if it outputs any trailers.
320- const originalTrailers = runSync ( 'git' , [
319+ // commit message and see if it outputs any trailers.
320+ const interpretTrailers = commitMessage => runSync ( 'git' , [
321321 'interpret-trailers' , '--parse' , '--no-divider'
322322 ] , {
323- input : `${ original } \n`
324- } ) . split ( '\n' ) . map ( trailer => {
325- const separatorIndex = trailer . indexOf ( ':' ) ;
326- return [ trailer . slice ( 0 , separatorIndex ) , trailer . slice ( separatorIndex + 2 ) ] ;
327- } ) . slice ( 0 , - 1 ) ; // Remove the last line return entry
323+ input : `${ commitMessage } \n`
324+ } ) ;
328325
326+ const originalTrailers = interpretTrailers ( original ) ;
329327 const containCVETrailer = CVE_RE . test ( originalTrailers ) ;
330328
331329 const filterTrailer = ( line ) => ( [ key ] ) =>
332330 line . startsWith ( key ) &&
333331 new RegExp ( `^${ RegExp . escape ?. ( key ) ?? key } \\s*:` ) . test ( line ) ;
334- const amended = original . trim ( ) . split ( '\n' )
335- . filter ( line =>
336- ! line . includes ( ':' ) ||
337- ! originalTrailers . some ( filterTrailer ( line ) ) ) ;
338- for ( let i = amended . length - 1 ; amended [ i ] === '' ; i -- ) {
332+ const amended = original . trim ( ) . split ( '\n' ) ;
333+ const stillInTrailers = ( ) => {
334+ const result = interpretTrailers ( amended . join ( '\n' ) ) ;
335+ return result . length && originalTrailers . startsWith ( result . trim ( ) ) ;
336+ }
337+ for ( let i = amended . length - 1 ; amended [ i ] === '' || stillInTrailers ( ) ; i -- ) {
338+ // Remove last line until git no longer detects any trailers
339339 amended . pop ( ) ;
340340 }
341341
342342 // Only keep existing trailers that we won't add ourselves
343343 const trailersToFilterOut = [ 'BACKPORT-PR-URL' , 'REVIEWED-BY' ] ;
344344 if ( ! this . backport ) trailersToFilterOut . push ( 'PR-URL' ) ;
345- const keptTrailers = originalTrailers . filter (
346- ( [ key ] ) => ! trailersToFilterOut . includes ( key . toUpperCase ( ) )
347- ) ;
345+ const keptTrailers =
346+ originalTrailers
347+ . split ( '\n' )
348+ . map ( trailer => {
349+ const separatorIndex = trailer . indexOf ( ':' ) ;
350+ return [ trailer . slice ( 0 , separatorIndex ) , trailer . slice ( separatorIndex + 2 ) ] ;
351+ } )
352+ . slice ( 0 , - 1 ) // Remove the last line return entry
353+ . filter (
354+ ( [ key ] ) => ! trailersToFilterOut . includes ( key . toUpperCase ( ) )
355+ ) ;
348356 amended . push ( '' , ...keptTrailers . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) ) ;
349357
350358 for ( const line of metadata . trim ( ) . split ( '\n' ) ) {
0 commit comments