From 8efb89a8cdd85c1677874f12c8c318e73e0fd191 Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Tue, 25 Aug 2026 16:31:47 +0200 Subject: [PATCH] fix: preserve blank lines with body length limit Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> #2079 --- commitizen/commands/commit.py | 5 +++-- tests/commands/test_commit_command.py | 5 +++++ ...mmit_command_body_length_limit_preserves_blank_lines_.txt | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/commands/test_commit_command/test_commit_command_body_length_limit_preserves_blank_lines_.txt diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index f0b50e519a..3c88bdf6f3 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -120,9 +120,10 @@ def _wrap_body(self, message: str) -> str: if len(lines) < 3: return message - # First line is subject, second is blank line, rest are body lines + # Preserve intentional blank lines while wrapping non-empty body lines. wrapped_body_lines = chain.from_iterable( - textwrap.wrap(line, width=body_length_limit) for line in lines[2:] + textwrap.wrap(line, width=body_length_limit) if line else [line] + for line in lines[2:] ) return "\n".join(chain(lines[:2], wrapped_body_lines)) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 9d214c04e1..82a0133061 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -390,6 +390,11 @@ def test_commit_command_with_config_message_length_limit( 100, id="preserves_line_breaks", ), + pytest.param( + "Line1 is shorter than the limit but has paragraph break\n\nLine2 stays in a separate paragraph", + 100, + id="preserves_blank_lines", + ), pytest.param( "This is a very long line that exceeds 72 characters and should NOT be wrapped when body_length_limit is set to 0", 0, diff --git a/tests/commands/test_commit_command/test_commit_command_body_length_limit_preserves_blank_lines_.txt b/tests/commands/test_commit_command/test_commit_command_body_length_limit_preserves_blank_lines_.txt new file mode 100644 index 0000000000..138ccb81b7 --- /dev/null +++ b/tests/commands/test_commit_command/test_commit_command_body_length_limit_preserves_blank_lines_.txt @@ -0,0 +1,5 @@ +feat: add feature + +Line1 is shorter than the limit but has paragraph break + +Line2 stays in a separate paragraph