Skip to content

Commit

Permalink
fix: respect existing trailers in commit messages (nodejs#632)
Browse files Browse the repository at this point in the history
Fixes: nodejs#602
  • Loading branch information
tniessen authored Jul 31, 2022
1 parent 0d49eda commit f442797
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/landing_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,24 @@ export default class LandingSession extends Session {
const original = runSync('git', [
'show', 'HEAD', '-s', '--format=%B'
]).trim();

// git has very specific rules about what is a trailer and what is not.
// Instead of trying to implement those ourselves, let git parse the
// original commit message and see if it outputs any trailers.
const originalHasTrailers = runSync('git', [
'interpret-trailers', '--parse', '--no-divider'
], {
input: `${original}\n`
}).trim().length !== 0;

const metadata = this.metadata.trim().split('\n');
const amended = original.split('\n');
if (amended[amended.length - 1] !== '') {

// If the original commit message already contains trailers (such as
// "Co-authored-by"), we simply add our own metadata after those. Otherwise,
// we have to add an empty line so that git recognizes our own metadata as
// trailers in the amended commit message.
if (!originalHasTrailers) {
amended.push('');
}

Expand All @@ -317,9 +332,13 @@ export default class LandingSession extends Session {
const REVIEW_RE = /Reviewed-By\s*:\s*(\S+)/i;

for (const line of metadata) {
if (original.includes(line)) {
if (line) {
if (line.length !== 0 && original.includes(line)) {
if (originalHasTrailers) {
cli.warn(`Found ${line}, skipping..`);
} else {
cli.error('Git found no trailers in the original commit message, ' +
`but '${line}' is present and should be a trailer.`);
process.exit(1); // make it work with git rebase -x
}
} else {
if (line.match(BACKPORT_RE)) {
Expand Down

0 comments on commit f442797

Please sign in to comment.