Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,14 @@ export default {
},

"header-max-length-with-suggestions": (
{ header }: { header: any },
{ raw }: { raw: any },
_: any,
maxLineLength: number
) => {
const headerStr = extractStringFromCommitlintParam(
"header",
header
);
const rawStr = extractStringFromCommitlintParam("rawStr", raw);

return Plugins.headerMaxLengthWithSuggestions(
headerStr,
rawStr,
maxLineLength
);
},
Expand Down
1 change: 0 additions & 1 deletion commitlint/abbreviations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const abbr = {
"command": "cmd",
"commands": "cmds",
"command line": "cmdline",
"compare": "cmp",
"compress": "zip",
"compressed": "zipped",
"concatenate": "concat",
Expand Down
36 changes: 34 additions & 2 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,46 @@ export abstract class Plugins {
}

public static headerMaxLengthWithSuggestions(
headerStr: string,
rawStr: string,
maxLineLength: number
) {
let offence = false;

const lineBreakIndex = rawStr.indexOf("\n");
let headerStr = rawStr;
if (lineBreakIndex >= 0) {
// Extracting headerStr from rawStr rather than using header directly is a
// workaround for what must be a commitlint or conventional-changelog bug, TODO: report
headerStr = rawStr.substring(0, lineBreakIndex);
}

const headerLength = headerStr.length;
let message = `Please do not exceed ${maxLineLength} characters in title (found ${headerLength}).`;
if (!headerStr.startsWith("Merge ") && headerLength > maxLineLength) {
let theMaxLineLengthToCompareWith = maxLineLength;

// note: a revert of a revert, in new versions of git, is written as "Reapply..." which happens
// to have same length as "Revert"
const extraCharsAllowedBecauseOfARevertOrARevertOfARevert =
'Revert "'.length +
// we add one because revert commits end with '"'
1;

const maxLineLengthAfterAccountingForRevertException =
maxLineLength + extraCharsAllowedBecauseOfARevertOrARevertOfARevert;
if (
headerStr.endsWith('"') &&
(headerStr.startsWith('Revert "') ||
headerStr.startsWith('Reapply "'))
) {
theMaxLineLengthToCompareWith =
maxLineLengthAfterAccountingForRevertException;
message = `Please do not exceed ${maxLineLength} (${maxLineLengthAfterAccountingForRevertException} if it's a revert) characters in title (found ${headerLength}).`;
}

if (
!headerStr.startsWith("Merge ") &&
headerLength > theMaxLineLengthToCompareWith
) {
offence = true;

const colonIndex = headerStr.indexOf(":");
Expand Down
34 changes: 24 additions & 10 deletions commitlint/tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@

test("header-max-length-with-suggestions1", () => {
const commitMsgWithThatExceedsHeaderMaxLength =
"foo: this is only a title with a configuration in it that exceeds header max length";
"foo: this is only a title with the term 'configuration' in it that exceeds header max length";
const headerMaxLength1 = runCommitLintOnMsg(
commitMsgWithThatExceedsHeaderMaxLength
);
Expand All @@ -871,7 +871,7 @@

test("header-max-length-with-suggestions2", () => {
const commitMsgWithThatExceedsHeaderMaxLength =
"foo: this is only a title with a 1 second in it that exceeds header max length";
"foo: this is only a title with the term '1 second' (which includes a space) in it that exceeds header max length";
const headerMaxLength2 = runCommitLintOnMsg(
commitMsgWithThatExceedsHeaderMaxLength
);
Expand Down Expand Up @@ -986,16 +986,30 @@
});

test("header-max-length-with-suggestions12", () => {
const commitMsgThatExceedsHeaderMaxLength =
"Split that compares better because blah blah bla very very very long title";
const commitMsgThatExceedsHeaderMaxLengthBecauseItIsARevert =
'Revert "This header is a title with less than 50chars"';
const headerMaxLength12 = runCommitLintOnMsg(
commitMsgThatExceedsHeaderMaxLength
commitMsgThatExceedsHeaderMaxLengthBecauseItIsARevert
);
const not_expected_message = `"compares" -> "cmps"`;
expect(headerMaxLength12.status).not.toBe(0);
expect(
(headerMaxLength12.stdout + "").includes(not_expected_message)
).toEqual(false);
expect(headerMaxLength12.status).toBe(0);
});

test("header-max-length-with-suggestions13", () => {
const commitMsgThatExceedsHeaderMaxLengthBecauseItIsARevertOfARevert =
'Reapply "This header is a title with less than 50chars"';
const headerMaxLength13 = runCommitLintOnMsg(
commitMsgThatExceedsHeaderMaxLengthBecauseItIsARevertOfARevert
);
expect(headerMaxLength13.status).toBe(0);
});

test("header-max-length-with-suggestions14", () => {
const commitMsgThatExceedsHeaderMaxLengthEvenIfItIsARevert =
'Revert "This header is a title with moooooooooore than 50chars"';
const headerMaxLength14 = runCommitLintOnMsg(
commitMsgThatExceedsHeaderMaxLengthEvenIfItIsARevert
);
expect(headerMaxLength14.status).not.toBe(0);

Check failure on line 1012 in commitlint/tests/plugins.test.ts

View workflow job for this annotation

GitHub Actions / Run commitlint-related tests

commitlint/tests/plugins.test.ts > header-max-length-with-suggestions14

AssertionError: expected +0 not to be +0 // Object.is equality ❯ commitlint/tests/plugins.test.ts:1012:42
});

test("proper-issue-refs1", () => {
Expand Down
Loading