Skip to content

Commit be744f9

Browse files
jwidauerLee-W
authored andcommitted
feat: add argument to limit length of commit message in checks
1 parent 458345e commit be744f9

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

Diff for: commitizen/cli.py

+6
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ def __call__(
466466
"If the message starts by one of these prefixes, "
467467
"the message won't be checked against the regex",
468468
},
469+
{
470+
"name": ["-l", "--message-length-limit"],
471+
"type": int,
472+
"default": 0,
473+
"help": "length limit of the commit message; 0 for no limit",
474+
},
469475
],
470476
},
471477
{

Diff for: commitizen/commands/check.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
3131
self.allow_abort: bool = bool(
3232
arguments.get("allow_abort", config.settings["allow_abort"])
3333
)
34+
self.max_msg_length: int = arguments.get("message_length_limit", 0)
3435

3536
# we need to distinguish between None and [], which is a valid value
3637

@@ -145,4 +146,8 @@ def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
145146

146147
if any(map(commit_msg.startswith, self.allowed_prefixes)):
147148
return True
149+
if self.max_msg_length:
150+
msg_len = len(commit_msg.partition("\n")[0].strip())
151+
if msg_len > self.max_msg_length:
152+
return False
148153
return bool(re.match(pattern, commit_msg))

Diff for: docs/commands/check.md

+10
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ By default, the the following prefixes are allowed: `Merge`, `Revert`, `Pull Req
7575
```bash
7676
cz check --message MESSAGE --allowed-prefixes 'Merge' 'Revert' 'Custom Prefix'
7777
```
78+
79+
### Commit message length limit
80+
81+
The argument `-l` (or `--message-length-limmit`) followed by a positive number, can limit the length of commit messages.
82+
For example, `cz check --message MESSAGE -l 3` would fail the check, since `MESSAGE` is more than 3 characters long.
83+
By default, the limit is set to 0, which means no limit on the length.
84+
85+
**Note that the limit applies only to the first line of the message.***
86+
Specifically, for `ConventionalCommitsCz` the length only counts from the type of change to the subject,
87+
while the body, and the footer are not counted.

Diff for: tests/commands/test_check_command.py

+25
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,28 @@ def test_check_command_shows_description_when_use_help_option(
427427

428428
out, _ = capsys.readouterr()
429429
file_regression.check(out, extension=".txt")
430+
431+
432+
def test_check_command_with_message_length_limit(config, mocker: MockFixture):
433+
success_mock = mocker.patch("commitizen.out.success")
434+
message = "fix(scope): some commit message"
435+
check_cmd = commands.Check(
436+
config=config,
437+
arguments={"message": message, "message_length_limit": len(message) + 1},
438+
)
439+
440+
check_cmd()
441+
success_mock.assert_called_once()
442+
443+
444+
def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
445+
error_mock = mocker.patch("commitizen.out.error")
446+
message = "fix(scope): some commit message"
447+
check_cmd = commands.Check(
448+
config=config,
449+
arguments={"message": message, "message_length_limit": len(message) - 1},
450+
)
451+
452+
with pytest.raises(InvalidCommitMessageError):
453+
check_cmd()
454+
error_mock.assert_called_once()

Diff for: tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz check [-h]
22
[--commit-msg-file COMMIT_MSG_FILE | --rev-range REV_RANGE | -m MESSAGE]
33
[--allow-abort] [--allowed-prefixes [ALLOWED_PREFIXES ...]]
4+
[-l MESSAGE_LENGTH_LIMIT]
45

56
validates that a commit message matches the commitizen schema
67

@@ -20,3 +21,5 @@ options:
2021
allowed commit message prefixes. If the message starts
2122
by one of these prefixes, the message won't be checked
2223
against the regex
24+
-l MESSAGE_LENGTH_LIMIT, --message-length-limit MESSAGE_LENGTH_LIMIT
25+
length limit of the commit message; 0 for no limit

0 commit comments

Comments
 (0)