fix: preserve blank lines with body length limit - #2080
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2080 +/- ##
==========================================
+ Coverage 98.19% 98.26% +0.07%
==========================================
Files 61 61
Lines 2829 2829
==========================================
+ Hits 2778 2780 +2
+ Misses 51 49 -2 ☔ View full report in Codecov by Harness. |
Manny7717
left a comment
There was a problem hiding this comment.
Review — Approved
Verified locally on head: 6/6 tests pass (body-length-limit and message-length-limit params, including the new preserves_blank_lines case).
The fix is correct and minimal: textwrap.wrap returns [] for empty input, which is why blank lines were silently dropped; short-circuiting with [line] preserves paragraph breaks exactly as intended.
One optional, non-blocking suggestion (inline): the if line check only catches fully-empty strings — a whitespace-only body line still gets dropped because textwrap.wrap(' ') is []. if line.strip() else [line] would preserve those too, if the intent is to preserve any intentional blank-looking line.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> commitizen-tools#2079
8efb89a to
171a234
Compare
Manny7717
left a comment
There was a problem hiding this comment.
Verified locally on head 171a234 (worktree against origin/master 4184174):
Fix is correct. textwrap.wrap(line, width=...) returns [] for both empty and whitespace-only lines, so _wrap_body silently dropped paragraph separators in commit bodies. The ternary keeps such lines verbatim ([line]) while still wrapping non-empty lines — exactly the right special-case. The body_length_limit <= 0 early-return ("no limit") is untouched, so no behavior change for the disabled case.
Regression-proven: copied the PR's two new tests onto the base worktree → both FAIL on 4184174 (preserves_blank_lines drops the blank separator; preserves_whitespace_only_lines drops the " " line), PASS on head. The direct assertion (rather than file_regression) for the whitespace-only case is a good call given the repo's trailing-whitespace pre-commit hook would strip the fixture.
No regressions: full tests/commands/test_commit_command.py 34/34 pass on head; ruff check clean.
One micro-note (non-blocking): the whitespace-only line is preserved byte-for-byte, so a body line of only spaces will survive into the commit message exactly as typed — that's the intent per the PR, just calling out the literal-preservation behavior.
Description
Fix
body_length_limitso it preserves intentional blank lines in commit bodies while still wrapping non-empty lines to the configured width.Checklist
Was generative AI tooling used to co-author this PR?
Generated-by: GitHub Copilot CLI following the guidelines
Code Changes
uv run poe alllocally to ensure this change passes linter check and testsUpdate the documentation for the changes— not required, just a fixDocumentation Changes
Run— no documentation changesuv run poe doclocally to ensure the documentation pages renders correctlyCheck and fix any broken links (internal or external)— no documentation changesExpected Behavior
With
body_length_limitenabled, Commitizen should preserve blank lines in the commit body and only wrap long non-empty lines.Steps to Test This Pull Request
Configure
body_length_limit = 80or runcz commit --body-length-limit 80.Create a commit message body with a paragraph break, for example:
Confirm the generated commit message still contains the blank separator line.
Run
uv run pytest tests/commands/test_commit_command.py -k body_length_limit.Run
uv run ruff check commitizen/commands/commit.py tests/commands/test_commit_command.py.Run
uv run ruff format --check commitizen/commands/commit.py tests/commands/test_commit_command.py.Additional Context
This fixes a regression where
textwrap.wrap("")caused empty body lines to be dropped whenbody_length_limitwas enabled (#2079).