Skip to content

Commit 8efb89a

Browse files
committed
fix: preserve blank lines with body length limit
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> #2079
1 parent 4184174 commit 8efb89a

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

commitizen/commands/commit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ def _wrap_body(self, message: str) -> str:
120120
if len(lines) < 3:
121121
return message
122122

123-
# First line is subject, second is blank line, rest are body lines
123+
# Preserve intentional blank lines while wrapping non-empty body lines.
124124
wrapped_body_lines = chain.from_iterable(
125-
textwrap.wrap(line, width=body_length_limit) for line in lines[2:]
125+
textwrap.wrap(line, width=body_length_limit) if line else [line]
126+
for line in lines[2:]
126127
)
127128
return "\n".join(chain(lines[:2], wrapped_body_lines))
128129

tests/commands/test_commit_command.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ def test_commit_command_with_config_message_length_limit(
390390
100,
391391
id="preserves_line_breaks",
392392
),
393+
pytest.param(
394+
"Line1 is shorter than the limit but has paragraph break\n\nLine2 stays in a separate paragraph",
395+
100,
396+
id="preserves_blank_lines",
397+
),
393398
pytest.param(
394399
"This is a very long line that exceeds 72 characters and should NOT be wrapped when body_length_limit is set to 0",
395400
0,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
feat: add feature
2+
3+
Line1 is shorter than the limit but has paragraph break
4+
5+
Line2 stays in a separate paragraph

0 commit comments

Comments
 (0)