From 13334a68bc69af7e4bca43a58563b17dc8158515 Mon Sep 17 00:00:00 2001 From: Jakob Widauer Date: Wed, 10 Jul 2024 00:33:37 +0200 Subject: [PATCH] feat: add argument to limit length of commit message in checks --- commitizen/cli.py | 6 +++++ commitizen/commands/check.py | 5 ++++ docs/commands/check.md | 10 ++++++++ tests/commands/test_check_command.py | 25 +++++++++++++++++++ ...shows_description_when_use_help_option.txt | 3 +++ 5 files changed, 49 insertions(+) diff --git a/commitizen/cli.py b/commitizen/cli.py index 1d1ebe1f6..00be6daf1 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -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", + }, ], }, { diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index 6e98f8cb3..13b8555b6 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -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 @@ -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)) diff --git a/docs/commands/check.md b/docs/commands/check.md index c31fd085e..751a47aa2 100644 --- a/docs/commands/check.md +++ b/docs/commands/check.md @@ -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. diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 47bdafd65..328ebd78a 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -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() diff --git a/tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt b/tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt index 56e42388d..74b9df719 100644 --- a/tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt +++ b/tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt @@ -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 @@ -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