From fe7d58910d0c6f85a8b19879cb9d4ae44c010699 Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Wed, 23 Sep 2026 10:13:43 +0200 Subject: [PATCH] fix(commit): honor configured body length limit Treat an omitted CLI option as no override so project configuration controls body wrapping while an explicit zero continues to disable it. Fixes #2093 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- commitizen/commands/commit.py | 8 ++++---- tests/commands/test_commit_command.py | 29 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index ecec7f3f6..970a92450 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -38,7 +38,7 @@ class CommitArgs(TypedDict, total=False): edit: bool extra_cli_args: list[str] message_length_limit: int - body_length_limit: int + body_length_limit: int | None no_retry: bool signoff: bool write_message_to_file: Path | None @@ -109,9 +109,9 @@ def _wrap_body(self, message: str) -> str: Wrap the body of the commit message to the --body-length-limit length. """ - body_length_limit = self.arguments.get( - "body_length_limit", self.config.settings["body_length_limit"] - ) + body_length_limit = self.arguments.get("body_length_limit") + if body_length_limit is None: + body_length_limit = self.config.settings["body_length_limit"] # By the contract, body_length_limit is set to 0 for no limit if not body_length_limit or body_length_limit <= 0: return message diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index ed64a211f..867c7d28a 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -448,6 +448,35 @@ def test_commit_command_body_length_limit( ) +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_uses_configured_body_length_limit_when_cli_option_is_omitted( + config, + commit_mock, + mocker: MockFixture, +): + config.settings["body_length_limit"] = 20 + mocker.patch( + "questionary.prompt", + return_value={ + "prefix": "feat", + "subject": "add feature", + "scope": "", + "is_breaking_change": False, + "body": "This body line should wrap at the configured limit", + "footer": "", + }, + ) + + commands.Commit(config, {"body_length_limit": None})() + + committed_message = commit_mock.call_args[0][0] + assert committed_message.split("\n")[2:] == [ + "This body line", + "should wrap at the", + "configured limit", + ] + + @pytest.mark.usefixtures("staging_is_clean") def test_commit_command_body_length_limit_preserves_whitespace_only_lines( config,