Skip to content

Commit efd1549

Browse files
committed
fix(message_length_limit): align the behavior of message_length_limit
The document says 0 is no limit
1 parent de24815 commit efd1549

File tree

7 files changed

+23
-74
lines changed

7 files changed

+23
-74
lines changed

commitizen/commands/check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CheckArgs(TypedDict, total=False):
2020
commit_msg: str
2121
rev_range: str
2222
allow_abort: bool
23-
message_length_limit: int | None
23+
message_length_limit: int
2424
allowed_prefixes: list[str]
2525
message: str
2626
use_default_range: bool
@@ -46,7 +46,7 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4646

4747
self.use_default_range = bool(arguments.get("use_default_range"))
4848
self.max_msg_length = arguments.get(
49-
"message_length_limit", config.settings.get("message_length_limit", None)
49+
"message_length_limit", config.settings.get("message_length_limit", 0)
5050
)
5151

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

commitizen/commands/commit.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CommitArgs(TypedDict, total=False):
3636
dry_run: bool
3737
edit: bool
3838
extra_cli_args: str
39-
message_length_limit: int | None
39+
message_length_limit: int
4040
no_retry: bool
4141
signoff: bool
4242
write_message_to_file: Path | None
@@ -83,19 +83,23 @@ def _get_message_by_prompt_commit_questions(self) -> str:
8383
raise NoAnswersError()
8484

8585
message = self.cz.message(answers)
86-
if limit := self.arguments.get(
87-
"message_length_limit", self.config.settings.get("message_length_limit", 0)
88-
):
89-
self._validate_subject_length(message=message, length_limit=limit)
90-
86+
self._validate_subject_length(message)
9187
return message
9288

93-
def _validate_subject_length(self, *, message: str, length_limit: int) -> None:
89+
def _validate_subject_length(self, message: str) -> None:
90+
message_length_limit = self.arguments.get(
91+
"message_length_limit", self.config.settings.get("message_length_limit", 0)
92+
)
9493
# By the contract, message_length_limit is set to 0 for no limit
94+
if (
95+
message_length_limit is None or message_length_limit <= 0
96+
): # do nothing for no limit
97+
return
98+
9599
subject = message.partition("\n")[0].strip()
96-
if len(subject) > length_limit:
100+
if len(subject) > message_length_limit:
97101
raise CommitMessageLengthExceededError(
98-
f"Length of commit message exceeds limit ({len(subject)}/{length_limit}), subject: '{subject}'"
102+
f"Length of commit message exceeds limit ({len(subject)}/{message_length_limit}), subject: '{subject}'"
99103
)
100104

101105
def manual_edit(self, message: str) -> str:

commitizen/cz/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def validate_commit_message(
130130
if any(map(commit_msg.startswith, allowed_prefixes)):
131131
return ValidationResult(True, [])
132132

133-
if max_msg_length is not None:
133+
if max_msg_length is not None and max_msg_length > 0:
134134
msg_len = len(commit_msg.partition("\n")[0].strip())
135135
if msg_len > max_msg_length:
136136
# TODO: capitalize the first letter of the error message for consistency in v5

commitizen/defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Settings(TypedDict, total=False):
4848
ignored_tag_formats: Sequence[str]
4949
legacy_tag_formats: Sequence[str]
5050
major_version_zero: bool
51-
message_length_limit: int | None
51+
message_length_limit: int
5252
name: str
5353
post_bump_hooks: list[str] | None
5454
pre_bump_hooks: list[str] | None
@@ -114,7 +114,7 @@ class Settings(TypedDict, total=False):
114114
"template": None, # default provided by plugin
115115
"extras": {},
116116
"breaking_change_exclamation_in_title": False,
117-
"message_length_limit": None, # None for no limit
117+
"message_length_limit": 0, # 0 for no limit
118118
}
119119

120120
MAJOR = "MAJOR"

tests/commands/test_check_command.py

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from commitizen import commands, git
99
from commitizen.cz import registry
10-
from commitizen.cz.base import BaseCommitizen, ValidationResult
10+
from commitizen.cz.base import BaseCommitizen
1111
from commitizen.exceptions import (
1212
CommitMessageLengthExceededError,
1313
InvalidCommandArgumentError,
@@ -16,7 +16,6 @@
1616
)
1717

1818
if TYPE_CHECKING:
19-
import re
2019
from collections.abc import Mapping
2120

2221
from pytest_mock import MockFixture, MockType
@@ -385,7 +384,7 @@ def test_check_command_cli_overrides_config_message_length_limit(
385384
):
386385
message = "fix(scope): some commit message"
387386
config.settings["message_length_limit"] = len(message) - 1
388-
for message_length_limit in [len(message) + 1, None]:
387+
for message_length_limit in [len(message) + 1, 0]:
389388
success_mock.reset_mock()
390389
commands.Check(
391390
config=config,
@@ -419,60 +418,6 @@ def example(self) -> str:
419418
def info(self) -> str:
420419
return "Commit message must start with an issue number like ABC-123"
421420

422-
def validate_commit_message(
423-
self,
424-
*,
425-
commit_msg: str,
426-
pattern: re.Pattern[str],
427-
allow_abort: bool,
428-
allowed_prefixes: list[str],
429-
max_msg_length: int | None,
430-
commit_hash: str,
431-
) -> ValidationResult:
432-
"""Validate commit message against the pattern."""
433-
if not commit_msg:
434-
return ValidationResult(
435-
allow_abort, [] if allow_abort else ["commit message is empty"]
436-
)
437-
438-
if any(map(commit_msg.startswith, allowed_prefixes)):
439-
return ValidationResult(True, [])
440-
441-
if max_msg_length:
442-
msg_len = len(commit_msg.partition("\n")[0].strip())
443-
if msg_len > max_msg_length:
444-
# TODO: capitalize the first letter of the error message for consistency in v5
445-
raise CommitMessageLengthExceededError(
446-
f"commit validation: failed!\n"
447-
f"commit message length exceeds the limit.\n"
448-
f'commit "{commit_hash}": "{commit_msg}"\n'
449-
f"message length limit: {max_msg_length} (actual: {msg_len})"
450-
)
451-
452-
return ValidationResult(
453-
bool(pattern.match(commit_msg)), [f"pattern: {pattern.pattern}"]
454-
)
455-
456-
def format_exception_message(
457-
self, invalid_commits: list[tuple[git.GitCommit, list]]
458-
) -> str:
459-
"""Format commit errors."""
460-
displayed_msgs_content = "\n".join(
461-
[
462-
(
463-
f'commit "{commit.rev}": "{commit.message}"\nerrors:\n\n'.join(
464-
f"- {error}" for error in errors
465-
)
466-
)
467-
for (commit, errors) in invalid_commits
468-
]
469-
)
470-
return (
471-
"commit validation: failed!\n"
472-
"please enter a commit message in the commitizen format.\n"
473-
f"{displayed_msgs_content}"
474-
)
475-
476421

477422
@pytest.fixture
478423
def use_cz_custom_validator(mocker):

tests/commands/test_commit_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,5 @@ def test_commit_command_with_config_message_length_limit(
363363
success_mock.assert_called_once()
364364

365365
success_mock.reset_mock()
366-
commands.Commit(config, {"message_length_limit": None})()
366+
commands.Commit(config, {"message_length_limit": 0})()
367367
success_mock.assert_called_once()

tests/test_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"template": None,
107107
"extras": {},
108108
"breaking_change_exclamation_in_title": False,
109-
"message_length_limit": None,
109+
"message_length_limit": 0,
110110
}
111111

112112
_new_settings: dict[str, Any] = {
@@ -146,7 +146,7 @@
146146
"template": None,
147147
"extras": {},
148148
"breaking_change_exclamation_in_title": False,
149-
"message_length_limit": None,
149+
"message_length_limit": 0,
150150
}
151151

152152

0 commit comments

Comments
 (0)