Skip to content

Commit 5d52748

Browse files
committed
commitlint.config: add rule
Add default-revert-message rule.
1 parent 853e9c5 commit 5d52748

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

commitlint.config.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ module.exports = {
5050
// disabled because most of the time it doesn't work, due to https://github.com/conventional-changelog/commitlint/issues/3404
5151
// and anyway we were using this rule only as a warning, not an error (because a scope is not required, e.g. when too broad)
5252
"type-empty": [RuleConfigSeverity.Disabled, "never"],
53+
"default-revert-message": [RuleConfigSeverity.Error, "never"],
5354
},
55+
56+
// Commitlint automatically ignores some kinds of commits like Revert commit messages.
57+
// We need to set this value to false to apply our rules on these messages.
58+
defaultIgnores: false,
5459
plugins: [
5560
// TODO (ideas for more rules):
5661
// * Detect if paragraphs in body have been cropped too shortly (less than 64 chars), and suggest same auto-wrap command that body-soft-max-line-length suggests, since it unwraps and wraps (both).
57-
// * Detect reverts which have not been elaborated.
5862
// * Reject some stupid obvious words: change, update, modify (if first word after colon, error; otherwise warning).
5963
// * Think of how to reject this shitty commit message: https://github.com/nblockchain/NOnion/pull/34/commits/9ffcb373a1147ed1c729e8aca4ffd30467255594
6064
// * Workflow: detect if wip commit in a branch not named "wip/*" or whose name contains "squashed".
@@ -139,6 +143,32 @@ module.exports = {
139143
return Plugins.properIssueRefs(rawStr);
140144
},
141145

146+
"default-revert-message": (
147+
{
148+
header,
149+
body,
150+
}: {
151+
header: any;
152+
body: any;
153+
},
154+
when: string
155+
) => {
156+
let bodyStr = Helpers.convertAnyToString(body, "body");
157+
let headerUncastedStr = Helpers.convertAnyToString(
158+
header,
159+
"header"
160+
);
161+
let headerStr = Helpers.assertNotNull(
162+
headerUncastedStr,
163+
notNullStringErrorMessage("header")
164+
);
165+
return Plugins.defaultRevertMessage(
166+
headerStr,
167+
bodyStr,
168+
when
169+
);
170+
},
171+
142172
"title-uppercase": ({ header }: { header: any }) => {
143173
let headerStr = Helpers.assertNotNull(
144174
Helpers.convertAnyToString(header, "header"),

commitlint/plugins.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,44 @@ export abstract class Plugins {
262262
];
263263
}
264264

265+
public static defaultRevertMessage(
266+
headerStr: string,
267+
bodyStr: string | null,
268+
when: string
269+
) {
270+
let offence = false;
271+
let isRevertCommitMessage = headerStr.toLowerCase().includes("revert");
272+
273+
const negated = when === "never";
274+
275+
if (isRevertCommitMessage) {
276+
let isDefaultRevertHeader =
277+
headerStr.match(/^[Rr]evert ".+"$/) !== null;
278+
279+
if (isDefaultRevertHeader) {
280+
if (bodyStr !== null) {
281+
let lines = bodyStr.split("\n");
282+
offence =
283+
// 40 is the length of git commit hash.
284+
lines.length == 1 &&
285+
lines[0].match(/^This reverts commit [^ ]{40}\.$/) !==
286+
null;
287+
} else {
288+
offence = true;
289+
}
290+
}
291+
292+
offence = negated ? offence : !offence;
293+
}
294+
return [
295+
!offence,
296+
(negated
297+
? `Please explain why you're reverting`
298+
: `Please don't change the default revert commit message`) +
299+
Helpers.errMessageSuffix,
300+
];
301+
}
302+
265303
public static titleUppercase(headerStr: string) {
266304
let firstWord = headerStr.split(" ")[0];
267305
let offence =

0 commit comments

Comments
 (0)