Skip to content

Commit 5118f34

Browse files
committed
fix(git-node): fix handling of existing trailers
1 parent 72e8772 commit 5118f34

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

lib/landing_session.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export default class LandingSession extends Session {
316316
const BACKPORT_RE = /^BACKPORT-PR-URL\s*:\s*(\S+)$/i;
317317
const PR_RE = /^PR-URL\s*:\s*(\S+)$/i;
318318
const REVIEW_RE = /^Reviewed-By\s*:\s*(\S+)$/i;
319-
const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/i;
319+
const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/im;
320320

321321
this.startAmending();
322322

@@ -328,41 +328,31 @@ export default class LandingSession extends Session {
328328
// git has very specific rules about what is a trailer and what is not.
329329
// Instead of trying to implement those ourselves, let git parse the
330330
// original commit message and see if it outputs any trailers.
331-
const originalHasTrailers = runSync('git', [
331+
const originalTrailers = runSync('git', [
332332
'interpret-trailers', '--parse', '--no-divider'
333333
], {
334334
input: `${original}\n`
335-
}).trim().length !== 0;
335+
});
336+
const containCVETrailer = CVE_RE.test(originalTrailers);
336337

337338
const metadata = this.metadata.trim().split('\n');
338-
// Filtering out metadata that we will add ourselves:
339-
const isFiltered = line =>
339+
// Only keep existing trailers that we won't add ourselves
340+
const shouldKeepTrailer = line =>
341+
!!line &&
340342
(!PR_RE.test(line) || this.backport) &&
341343
!BACKPORT_RE.test(line) &&
342344
!REVIEW_RE.test(line);
343-
const amended = original.split('\n').filter(isFiltered);
344-
345-
// If the original commit message already contains trailers (such as
346-
// "Co-authored-by"), we simply add our own metadata after those. Otherwise,
347-
// we have to add an empty line so that git recognizes our own metadata as
348-
// trailers in the amended commit message.
349-
if (!originalHasTrailers) {
350-
amended.push('');
351-
}
345+
const amended = original.slice(0, -originalTrailers.length).trim().split('\n');
346+
347+
amended.push('', ...originalTrailers.split('\n').filter(shouldKeepTrailer));
352348

353-
let containCVETrailer = false;
354349
for (const line of metadata) {
355-
if (line.length !== 0 && original.includes(line) && !isFiltered(line)) {
356-
containCVETrailer ||= CVE_RE.test(line);
357-
if (originalHasTrailers) {
358-
cli.warn(`Found ${line}, skipping..`);
359-
continue;
360-
} else {
361-
cli.error('Git found no trailers in the original commit message, ' +
350+
if (originalTrailers.includes(line)) {
351+
cli.error('Git found no trailers in the original commit message, ' +
362352
`but '${line}' is present and should be a trailer.`);
363-
process.exit(1); // make it work with git rebase -x
364-
}
353+
process.exit(1); // make it work with git rebase -x
365354
}
355+
366356
if (BACKPORT_RE.test(line)) {
367357
const prIndex =
368358
(amended.findIndex(datum => PR_RE.test(datum)) + 1) ||

0 commit comments

Comments
 (0)