Skip to content

Commit

Permalink
fix(rules): ignore comments in signed-off-by (#2098)
Browse files Browse the repository at this point in the history
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š <mbajtoss@gmail.com>
  • Loading branch information
bajtos authored Sep 5, 2020
1 parent 59667b3 commit b610bcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions @commitlint/rules/src/signed-off-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion @commitlint/rules/src/signed-off-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ export const signedOffBy: SyncRule<string> = (
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';
Expand Down

0 comments on commit b610bcd

Please sign in to comment.