From b610bcd15215cc5f14fb6de07914ed595cc3047b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Sat, 5 Sep 2020 07:43:05 +0200 Subject: [PATCH] fix(rules): ignore comments in `signed-off-by` (#2098) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix `signed-off-by` rule to ignore lines starting with `#`. These lines are comments. They are added by `git commit` at the end of the commit message template to show which files are included in the commit, and removed from the actual commit message. Before this change, the rule `signed-off-by` was rejecting pretty much all commit messages created by `git commit` from the command line. With this change in place, the rule ignores commits and accepts commit message created by `git commit` from the command line. Signed-off-by: Miroslav Bajtoš --- @commitlint/rules/src/signed-off-by.test.ts | 20 ++++++++++++++++++++ @commitlint/rules/src/signed-off-by.ts | 9 ++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/@commitlint/rules/src/signed-off-by.test.ts b/@commitlint/rules/src/signed-off-by.test.ts index 8cf2a6683f..29fc665501 100644 --- a/@commitlint/rules/src/signed-off-by.test.ts +++ b/@commitlint/rules/src/signed-off-by.test.ts @@ -7,6 +7,15 @@ const messages = { without: `test: subject\nbody\nfooter\n\n`, inSubject: `test: subject Signed-off-by:\nbody\nfooter\n\n`, inBody: `test: subject\nbody Signed-off-by:\nfooter\n\n`, + withSignoffAndComments: `test: subject + +message body + +Signed-off-by: + +# Please enter the commit message for your changes. Lines starting +# with '#' will be ignored, and an empty message aborts the commit. +`, }; const parsed = { @@ -15,6 +24,7 @@ const parsed = { without: parse(messages.without), inSubject: parse(messages.inSubject), inBody: parse(messages.inBody), + withSignoffAndComments: parse(messages.withSignoffAndComments), }; test('empty against "always signed-off-by" should fail', async () => { @@ -57,6 +67,16 @@ test('without against "never signed-off-by" should succeed', async () => { expect(actual).toEqual(expected); }); +test('trailing comments should be ignored', async () => { + const [actual] = signedOffBy( + await parsed.withSignoffAndComments, + 'always', + 'Signed-off-by:' + ); + const expected = true; + expect(actual).toEqual(expected); +}); + test('inSubject against "always signed-off-by" should fail', async () => { const [actual] = signedOffBy( await parsed.inSubject, diff --git a/@commitlint/rules/src/signed-off-by.ts b/@commitlint/rules/src/signed-off-by.ts index 38d68b068f..674a206197 100644 --- a/@commitlint/rules/src/signed-off-by.ts +++ b/@commitlint/rules/src/signed-off-by.ts @@ -7,7 +7,14 @@ export const signedOffBy: SyncRule = ( when = 'always', value = '' ) => { - const lines = toLines(parsed.raw).filter(Boolean); + const lines = toLines(parsed.raw).filter( + (ln) => + // skip comments + !ln.startsWith('#') && + // ignore empty lines + Boolean(ln) + ); + const last = lines[lines.length - 1]; const negated = when === 'never';