Skip to content

Commit

Permalink
feat: add argument to limit length of commit message in checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jwidauer authored and Lee-W committed Jul 17, 2024
1 parent 458345e commit be744f9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ def __call__(
"If the message starts by one of these prefixes, "
"the message won't be checked against the regex",
},
{
"name": ["-l", "--message-length-limit"],
"type": int,
"default": 0,
"help": "length limit of the commit message; 0 for no limit",
},
],
},
{
Expand Down
5 changes: 5 additions & 0 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
self.allow_abort: bool = bool(
arguments.get("allow_abort", config.settings["allow_abort"])
)
self.max_msg_length: int = arguments.get("message_length_limit", 0)

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

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

if any(map(commit_msg.startswith, self.allowed_prefixes)):
return True
if self.max_msg_length:
msg_len = len(commit_msg.partition("\n")[0].strip())
if msg_len > self.max_msg_length:
return False
return bool(re.match(pattern, commit_msg))
10 changes: 10 additions & 0 deletions docs/commands/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,13 @@ By default, the the following prefixes are allowed: `Merge`, `Revert`, `Pull Req
```bash
cz check --message MESSAGE --allowed-prefixes 'Merge' 'Revert' 'Custom Prefix'
```

### Commit message length limit

The argument `-l` (or `--message-length-limmit`) followed by a positive number, can limit the length of commit messages.
For example, `cz check --message MESSAGE -l 3` would fail the check, since `MESSAGE` is more than 3 characters long.
By default, the limit is set to 0, which means no limit on the length.

**Note that the limit applies only to the first line of the message.***
Specifically, for `ConventionalCommitsCz` the length only counts from the type of change to the subject,
while the body, and the footer are not counted.
25 changes: 25 additions & 0 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,28 @@ def test_check_command_shows_description_when_use_help_option(

out, _ = capsys.readouterr()
file_regression.check(out, extension=".txt")


def test_check_command_with_message_length_limit(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
message = "fix(scope): some commit message"
check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": len(message) + 1},
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
message = "fix(scope): some commit message"
check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": len(message) - 1},
)

with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
usage: cz check [-h]
[--commit-msg-file COMMIT_MSG_FILE | --rev-range REV_RANGE | -m MESSAGE]
[--allow-abort] [--allowed-prefixes [ALLOWED_PREFIXES ...]]
[-l MESSAGE_LENGTH_LIMIT]

validates that a commit message matches the commitizen schema

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

0 comments on commit be744f9

Please sign in to comment.