Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CheckArgs(TypedDict, total=False):
commit_msg: str
rev_range: str
allow_abort: bool
message_length_limit: int
message_length_limit: int | None
allowed_prefixes: list[str]
message: str
use_default_range: bool
Expand Down Expand Up @@ -51,9 +51,9 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
)

self.use_default_range = bool(arguments.get("use_default_range"))
self.max_msg_length = arguments.get(
"message_length_limit", config.settings.get("message_length_limit", 0)
)
self.max_msg_length = arguments.get("message_length_limit")
if self.max_msg_length is None:
self.max_msg_length = config.settings.get("message_length_limit", 0)

# we need to distinguish between None and [], which is a valid value
allowed_prefixes = arguments.get("allowed_prefixes")
Expand Down
9 changes: 5 additions & 4 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CommitArgs(TypedDict, total=False):
dry_run: bool
edit: bool
extra_cli_args: list[str]
message_length_limit: int
message_length_limit: int | None
body_length_limit: int | None
no_retry: bool
signoff: bool
Expand Down Expand Up @@ -89,9 +89,10 @@ def _get_message_by_prompt_commit_questions(self) -> str:
return message

def _validate_subject_length(self, message: str) -> None:
message_length_limit = self.arguments.get(
"message_length_limit", self.config.settings.get("message_length_limit", 0)
)
"""Enforce the configured subject limit unless the CLI overrides it."""
message_length_limit = self.arguments.get("message_length_limit")
if message_length_limit is None:
message_length_limit = self.config.settings.get("message_length_limit", 0)
# By the contract, message_length_limit is set to 0 for no limit
if (
message_length_limit is None or message_length_limit <= 0
Expand Down
4 changes: 3 additions & 1 deletion docs/config/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ List of prefixes that commitizen ignores when verifying messages.
Maximum length of the commit message. Setting it to `0` disables the length limit.

!!! note
This option can be overridden by the `-l/--message-length-limit` command line argument.
Both `cz commit` and `cz check` use this setting when `-l/--message-length-limit`
is omitted. An explicit CLI value overrides the configuration. Use `-l 0` to
disable the limit, even when a limit is configured.
19 changes: 19 additions & 0 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,25 @@ def test_check_command_cli_overrides_config_message_length_limit(
success_mock.assert_called_once()


def test_check_command_uses_configured_message_length_limit_when_cli_option_is_omitted(
config: BaseConfig, success_mock: MockType
) -> None:
"""Reject an oversized subject when argparse supplies no CLI override."""
# Arrange
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) - 1

# Act
with pytest.raises(CommitMessageLengthExceededError):
commands.Check(
config,
{"message": message, "message_length_limit": None},
)()

# Assert
success_mock.assert_not_called()


class ValidationCz(BaseCommitizen):
def questions(self) -> list[CzQuestion]:
return [
Expand Down
29 changes: 28 additions & 1 deletion tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import re
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import ANY

import pytest
from pytest_mock import MockFixture, MockType

from commitizen import cli, cmd, commands
from commitizen.cz.exceptions import CzException
Expand All @@ -21,6 +23,11 @@
NothingToCommitError,
)

if TYPE_CHECKING:
from pytest_mock import MockFixture, MockType

from commitizen.config import BaseConfig


@pytest.fixture
def commit_mock(mocker: MockFixture):
Expand Down Expand Up @@ -376,6 +383,26 @@ def test_commit_command_with_config_message_length_limit(
success_mock.assert_called_once()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_uses_configured_message_length_limit_when_cli_option_is_omitted(
config: BaseConfig,
commit_mock: MockType,
prompt_mock_feat: MockType,
) -> None:
"""Reject an oversized subject when argparse supplies no CLI override."""
# Arrange
prefix = prompt_mock_feat.return_value["prefix"]
subject = prompt_mock_feat.return_value["subject"]
config.settings["message_length_limit"] = len(f"{prefix}: {subject}") - 1

# Act
with pytest.raises(CommitMessageLengthExceededError):
commands.Commit(config, {"message_length_limit": None})()

# Assert
commit_mock.assert_not_called()


@pytest.mark.usefixtures("staging_is_clean")
@pytest.mark.parametrize(
("body", "body_length_limit"),
Expand Down
Loading