diff --git a/dev-infra/commit-message/parse.spec.ts b/dev-infra/commit-message/parse.spec.ts index 71f4d6875b..22a79934a9 100644 --- a/dev-infra/commit-message/parse.spec.ts +++ b/dev-infra/commit-message/parse.spec.ts @@ -82,4 +82,24 @@ describe('commit message parsing:', () => { const message2 = buildCommitMessage({prefix: 'squash! '}); expect(parseCommitMessage(message2).isSquash).toBe(true); }); + + it('ignores comment lines', () => { + const message = buildCommitMessage({ + prefix: '# This is a comment line before the header.\n' + + '## This is another comment line before the headers.\n', + body: '# This is a comment line befor the body.\n' + + 'This is line 1 of the actual body.\n' + + '## This is another comment line inside the body.\n' + + 'This is line 2 of the actual body (and it also contains a # but it not a comment).\n' + + '### This is yet another comment line after the body.\n', + }); + const parsedMessage = parseCommitMessage(message); + + expect(parsedMessage.header) + .toBe(`${commitValues.type}(${commitValues.scope}): ${commitValues.summary}`); + expect(parsedMessage.body) + .toBe( + 'This is line 1 of the actual body.\n' + + 'This is line 2 of the actual body (and it also contains a # but it not a comment).\n'); + }); }); diff --git a/dev-infra/commit-message/parse.ts b/dev-infra/commit-message/parse.ts index f8acd0fb60..3ccd870620 100644 --- a/dev-infra/commit-message/parse.ts +++ b/dev-infra/commit-message/parse.ts @@ -36,6 +36,10 @@ const COMMIT_BODY_RE = /^.*\n\n([\s\S]*)$/; /** Parse a full commit message into its composite parts. */ export function parseCommitMessage(commitMsg: string): ParsedCommitMessage { + // Ignore comments (i.e. lines starting with `#`). Comments are automatically removed by git and + // should not be considered part of the final commit message. + commitMsg = commitMsg.split('\n').filter(line => !line.startsWith('#')).join('\n'); + let header = ''; let body = ''; let bodyWithoutLinking = '';