Skip to content

Commit b610bcd

Browse files
authored
fix(rules): ignore comments in signed-off-by (#2098)
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š <[email protected]>
1 parent 59667b3 commit b610bcd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

@commitlint/rules/src/signed-off-by.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const messages = {
77
without: `test: subject\nbody\nfooter\n\n`,
88
inSubject: `test: subject Signed-off-by:\nbody\nfooter\n\n`,
99
inBody: `test: subject\nbody Signed-off-by:\nfooter\n\n`,
10+
withSignoffAndComments: `test: subject
11+
12+
message body
13+
14+
Signed-off-by:
15+
16+
# Please enter the commit message for your changes. Lines starting
17+
# with '#' will be ignored, and an empty message aborts the commit.
18+
`,
1019
};
1120

1221
const parsed = {
@@ -15,6 +24,7 @@ const parsed = {
1524
without: parse(messages.without),
1625
inSubject: parse(messages.inSubject),
1726
inBody: parse(messages.inBody),
27+
withSignoffAndComments: parse(messages.withSignoffAndComments),
1828
};
1929

2030
test('empty against "always signed-off-by" should fail', async () => {
@@ -57,6 +67,16 @@ test('without against "never signed-off-by" should succeed', async () => {
5767
expect(actual).toEqual(expected);
5868
});
5969

70+
test('trailing comments should be ignored', async () => {
71+
const [actual] = signedOffBy(
72+
await parsed.withSignoffAndComments,
73+
'always',
74+
'Signed-off-by:'
75+
);
76+
const expected = true;
77+
expect(actual).toEqual(expected);
78+
});
79+
6080
test('inSubject against "always signed-off-by" should fail', async () => {
6181
const [actual] = signedOffBy(
6282
await parsed.inSubject,

@commitlint/rules/src/signed-off-by.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ export const signedOffBy: SyncRule<string> = (
77
when = 'always',
88
value = ''
99
) => {
10-
const lines = toLines(parsed.raw).filter(Boolean);
10+
const lines = toLines(parsed.raw).filter(
11+
(ln) =>
12+
// skip comments
13+
!ln.startsWith('#') &&
14+
// ignore empty lines
15+
Boolean(ln)
16+
);
17+
1118
const last = lines[lines.length - 1];
1219

1320
const negated = when === 'never';

0 commit comments

Comments
 (0)