Skip to content

Commit aa15fda

Browse files
committed
feat(commit): add a tag(--body-length-limit) and a function for command commit
1 parent 26e5d80 commit aa15fda

9 files changed

+65
-5
lines changed

commitizen/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ def __call__(
164164
"type": int,
165165
"help": "Set the length limit of the commit message; 0 for no limit.",
166166
},
167+
{
168+
"name": ["--body-length-limit"],
169+
"type": int,
170+
"help": "Set the length limit of the commit body. Commit message in body will be rewrapped to this length; 0 for no limit.",
171+
},
167172
{
168173
"name": ["--"],
169174
"action": "store_true",

commitizen/commands/commit.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import subprocess
77
import tempfile
8+
import textwrap
89
from typing import TYPE_CHECKING, TypedDict
910

1011
import questionary
@@ -37,6 +38,7 @@ class CommitArgs(TypedDict, total=False):
3738
edit: bool
3839
extra_cli_args: str
3940
message_length_limit: int
41+
body_length_limit: int
4042
no_retry: bool
4143
signoff: bool
4244
write_message_to_file: Path | None
@@ -84,6 +86,7 @@ def _get_message_by_prompt_commit_questions(self) -> str:
8486

8587
message = self.cz.message(answers)
8688
self._validate_subject_length(message)
89+
message = self._rewrap_body(message)
8790
return message
8891

8992
def _validate_subject_length(self, message: str) -> None:
@@ -102,6 +105,29 @@ def _validate_subject_length(self, message: str) -> None:
102105
f"Length of commit message exceeds limit ({len(subject)}/{message_length_limit}), subject: '{subject}'"
103106
)
104107

108+
def _rewrap_body(self, message: str) -> str:
109+
body_length_limit = self.arguments.get(
110+
"body_length_limit", self.config.settings.get("body_length_limit", 0)
111+
)
112+
# By the contract, body_length_limit is set to 0 for no limit
113+
if (
114+
body_length_limit is None or body_length_limit <= 0
115+
): # do nothing for no limit
116+
return message
117+
118+
message_parts = message.split("\n", 2)
119+
if len(message_parts) < 3:
120+
return message
121+
122+
# First line is subject, second is blank line, rest is body
123+
subject = message_parts[0]
124+
blank_line = message_parts[1]
125+
body = message_parts[2].strip()
126+
wrapped_body = textwrap.fill(
127+
body, width=body_length_limit, replace_whitespace=False
128+
)
129+
return f"{subject}\n{blank_line}\n{wrapped_body}"
130+
105131
def manual_edit(self, message: str) -> str:
106132
editor = git.get_core_editor()
107133
if editor is None:

commitizen/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Settings(TypedDict, total=False):
4949
legacy_tag_formats: Sequence[str]
5050
major_version_zero: bool
5151
message_length_limit: int
52+
body_length_limit: int
5253
name: str
5354
post_bump_hooks: list[str] | None
5455
pre_bump_hooks: list[str] | None
@@ -115,6 +116,7 @@ class Settings(TypedDict, total=False):
115116
"extras": {},
116117
"breaking_change_exclamation_in_title": False,
117118
"message_length_limit": 0, # 0 for no limit
119+
"body_length_limit": 0, # 0 for no limit
118120
}
119121

120122
MAJOR = "MAJOR"

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_commit_.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz commit [-h] [--retry] [--no-retry] [--dry-run]
22
[--write-message-to-file FILE_PATH] [-s] [-a] [-e]
3-
[-l MESSAGE_LENGTH_LIMIT] [--]
3+
[-l MESSAGE_LENGTH_LIMIT]
4+
[--body-length-limit BODY_LENGTH_LIMIT] [--]
45

56
Create new commit
67

@@ -22,4 +23,8 @@ options:
2223
-l MESSAGE_LENGTH_LIMIT, --message-length-limit MESSAGE_LENGTH_LIMIT
2324
Set the length limit of the commit message; 0 for no
2425
limit.
26+
--body-length-limit BODY_LENGTH_LIMIT
27+
Set the length limit of the commit body. Commit
28+
message in body will be rewrapped to this length; 0
29+
for no limit.
2530
-- Positional arguments separator (recommended).

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_commit_.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz commit [-h] [--retry] [--no-retry] [--dry-run]
22
[--write-message-to-file FILE_PATH] [-s] [-a] [-e]
3-
[-l MESSAGE_LENGTH_LIMIT] [--]
3+
[-l MESSAGE_LENGTH_LIMIT]
4+
[--body-length-limit BODY_LENGTH_LIMIT] [--]
45

56
Create new commit
67

@@ -22,4 +23,8 @@ options:
2223
-l MESSAGE_LENGTH_LIMIT, --message-length-limit MESSAGE_LENGTH_LIMIT
2324
Set the length limit of the commit message; 0 for no
2425
limit.
26+
--body-length-limit BODY_LENGTH_LIMIT
27+
Set the length limit of the commit body. Commit
28+
message in body will be rewrapped to this length; 0
29+
for no limit.
2530
-- Positional arguments separator (recommended).

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_commit_.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz commit [-h] [--retry] [--no-retry] [--dry-run]
22
[--write-message-to-file FILE_PATH] [-s] [-a] [-e]
3-
[-l MESSAGE_LENGTH_LIMIT] [--]
3+
[-l MESSAGE_LENGTH_LIMIT]
4+
[--body-length-limit BODY_LENGTH_LIMIT] [--]
45

56
Create new commit
67

@@ -22,4 +23,8 @@ options:
2223
-l MESSAGE_LENGTH_LIMIT, --message-length-limit MESSAGE_LENGTH_LIMIT
2324
Set the length limit of the commit message; 0 for no
2425
limit.
26+
--body-length-limit BODY_LENGTH_LIMIT
27+
Set the length limit of the commit body. Commit
28+
message in body will be rewrapped to this length; 0
29+
for no limit.
2530
-- Positional arguments separator (recommended).

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_commit_.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz commit [-h] [--retry] [--no-retry] [--dry-run]
22
[--write-message-to-file FILE_PATH] [-s] [-a] [-e]
3-
[-l MESSAGE_LENGTH_LIMIT] [--]
3+
[-l MESSAGE_LENGTH_LIMIT]
4+
[--body-length-limit BODY_LENGTH_LIMIT] [--]
45

56
Create new commit
67

@@ -22,4 +23,8 @@ options:
2223
-l, --message-length-limit MESSAGE_LENGTH_LIMIT
2324
Set the length limit of the commit message; 0 for no
2425
limit.
26+
--body-length-limit BODY_LENGTH_LIMIT
27+
Set the length limit of the commit body. Commit
28+
message in body will be rewrapped to this length; 0
29+
for no limit.
2530
-- Positional arguments separator (recommended).

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_commit_.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usage: cz commit [-h] [--retry] [--no-retry] [--dry-run]
22
[--write-message-to-file FILE_PATH] [-s] [-a] [-e]
3-
[-l MESSAGE_LENGTH_LIMIT] [--]
3+
[-l MESSAGE_LENGTH_LIMIT]
4+
[--body-length-limit BODY_LENGTH_LIMIT] [--]
45

56
Create new commit
67

@@ -22,4 +23,8 @@ options:
2223
-l, --message-length-limit MESSAGE_LENGTH_LIMIT
2324
Set the length limit of the commit message; 0 for no
2425
limit.
26+
--body-length-limit BODY_LENGTH_LIMIT
27+
Set the length limit of the commit body. Commit
28+
message in body will be rewrapped to this length; 0
29+
for no limit.
2530
-- Positional arguments separator (recommended).

tests/test_conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"extras": {},
113113
"breaking_change_exclamation_in_title": False,
114114
"message_length_limit": 0,
115+
"body_length_limit": 0,
115116
}
116117

117118
_new_settings: dict[str, Any] = {
@@ -152,6 +153,7 @@
152153
"extras": {},
153154
"breaking_change_exclamation_in_title": False,
154155
"message_length_limit": 0,
156+
"body_length_limit": 0,
155157
}
156158

157159

0 commit comments

Comments
 (0)