Skip to content

Commit c162290

Browse files
committed
refactor: use None instead of 0 for message_length_limit and update related logic, types, and tests
1 parent c300b70 commit c162290

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

commitizen/commands/check.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CheckArgs(TypedDict, total=False):
1919
commit_msg: str
2020
rev_range: str
2121
allow_abort: bool
22-
message_length_limit: int
22+
message_length_limit: int | None
2323
allowed_prefixes: list[str]
2424
message: str
2525

@@ -42,12 +42,7 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4242
arguments.get("allow_abort", config.settings["allow_abort"])
4343
)
4444

45-
# Use command line argument if provided, otherwise use config setting
46-
cmd_length_limit = arguments.get("message_length_limit")
47-
if cmd_length_limit is None:
48-
self.max_msg_length = config.settings.get("message_length_limit", 0)
49-
else:
50-
self.max_msg_length = cmd_length_limit
45+
self.max_msg_length = arguments.get("message_length_limit", config.settings.get("message_length_limit", None))
5146

5247
# we need to distinguish between None and [], which is a valid value
5348
allowed_prefixes = arguments.get("allowed_prefixes")
@@ -90,7 +85,7 @@ def __call__(self) -> None:
9085
invalid_msgs_content = "\n".join(
9186
f'commit "{commit.rev}": "{commit.message}"'
9287
for commit in commits
93-
if not self._validate_commit_message(commit.message, pattern)
88+
if not self._validate_commit_message(commit.message, pattern, commit.rev)
9489
)
9590
if invalid_msgs_content:
9691
# TODO: capitalize the first letter of the error message for consistency in v5
@@ -150,21 +145,21 @@ def _filter_comments(msg: str) -> str:
150145
return "\n".join(lines)
151146

152147
def _validate_commit_message(
153-
self, commit_msg: str, pattern: re.Pattern[str]
148+
self, commit_msg: str, pattern: re.Pattern[str], commit_hash: str
154149
) -> bool:
155150
if not commit_msg:
156151
return self.allow_abort
157152

158153
if any(map(commit_msg.startswith, self.allowed_prefixes)):
159154
return True
160155

161-
if self.max_msg_length:
156+
if self.max_msg_length is not None:
162157
msg_len = len(commit_msg.partition("\n")[0].strip())
163158
if msg_len > self.max_msg_length:
164159
raise CommitMessageLengthExceededError(
165160
f"commit validation: failed!\n"
166161
f"commit message length exceeds the limit.\n"
167-
f'commit "": "{commit_msg}"\n'
162+
f'commit "{commit_hash}": "{commit_msg}"\n'
168163
f"message length limit: {self.max_msg_length} (actual: {msg_len})"
169164
)
170165

commitizen/commands/commit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CommitArgs(TypedDict, total=False):
3333
dry_run: bool
3434
edit: bool
3535
extra_cli_args: str
36-
message_length_limit: int
36+
message_length_limit: int | None
3737
no_retry: bool
3838
signoff: bool
3939
write_message_to_file: Path | None
@@ -82,11 +82,12 @@ def _prompt_commit_questions(self) -> str:
8282
message = cz.message(answers)
8383
message_len = len(message.partition("\n")[0].strip())
8484

85-
message_length_limit = self.arguments.get("message_length_limit")
86-
if message_length_limit is None:
87-
message_length_limit = self.config.settings.get("message_length_limit", 0)
85+
message_length_limit = self.arguments.get(
86+
"message_length_limit",
87+
self.config.settings.get("message_length_limit", None),
88+
)
8889

89-
if 0 < message_length_limit < message_len:
90+
if message_length_limit is not None and message_len > message_length_limit:
9091
raise CommitMessageLengthExceededError(
9192
f"Length of commit message exceeds limit ({message_len}/{message_length_limit})"
9293
)

commitizen/defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Settings(TypedDict, total=False):
4646
ignored_tag_formats: Sequence[str]
4747
legacy_tag_formats: Sequence[str]
4848
major_version_zero: bool
49-
message_length_limit: int
49+
message_length_limit: int | None
5050
name: str
5151
post_bump_hooks: list[str] | None
5252
pre_bump_hooks: list[str] | None
@@ -109,7 +109,7 @@ class Settings(TypedDict, total=False):
109109
"always_signoff": False,
110110
"template": None, # default provided by plugin
111111
"extras": {},
112-
"message_length_limit": 0, # 0 for no limit
112+
"message_length_limit": None, # None for no limit
113113
}
114114

115115
MAJOR = "MAJOR"

tests/commands/test_check_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def test_check_command_cli_overrides_config_message_length_limit(
507507
success_mock.reset_mock()
508508
check_cmd = commands.Check(
509509
config=config,
510-
arguments={"message": message, "message_length_limit": 0},
510+
arguments={"message": message, "message_length_limit": None},
511511
)
512512

513513
check_cmd()

tests/commands/test_commit_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,5 +611,5 @@ def test_commit_command_cli_overrides_config_message_length_limit(
611611
success_mock.assert_called_once()
612612

613613
success_mock.reset_mock()
614-
commands.Commit(config, {"message_length_limit": 0})()
614+
commands.Commit(config, {"message_length_limit": None})()
615615
success_mock.assert_called_once()

tests/test_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"always_signoff": False,
9696
"template": None,
9797
"extras": {},
98-
"message_length_limit": 0,
98+
"message_length_limit": None,
9999
}
100100

101101
_new_settings: dict[str, Any] = {
@@ -127,7 +127,7 @@
127127
"always_signoff": False,
128128
"template": None,
129129
"extras": {},
130-
"message_length_limit": 0,
130+
"message_length_limit": None,
131131
}
132132

133133

0 commit comments

Comments
 (0)