Skip to content

Commit b076927

Browse files
yjawLee-W
authored andcommitted
feat(commit): add a tag(--body-length-limit) and a function for command commit
1 parent 88c25e3 commit b076927

9 files changed

Lines changed: 65 additions & 5 deletions

‎commitizen/cli.py‎

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

‎commitizen/commands/commit.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import tempfile
88
from pathlib import Path
9+
import textwrap
910
from typing import TYPE_CHECKING, TypedDict
1011

1112
import questionary
@@ -36,6 +37,7 @@ class CommitArgs(TypedDict, total=False):
3637
edit: bool
3738
extra_cli_args: list[str]
3839
message_length_limit: int
40+
body_length_limit: int
3941
no_retry: bool
4042
signoff: bool
4143
write_message_to_file: Path | None
@@ -82,6 +84,7 @@ def _get_message_by_prompt_commit_questions(self) -> str:
8284

8385
message = self.cz.message(answers)
8486
self._validate_subject_length(message)
87+
message = self._rewrap_body(message)
8588
return message
8689

8790
def _validate_subject_length(self, message: str) -> None:
@@ -100,6 +103,29 @@ def _validate_subject_length(self, message: str) -> None:
100103
f"Length of commit message exceeds limit ({len(subject)}/{message_length_limit}), subject: '{subject}'"
101104
)
102105

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

118119
_new_settings: dict[str, Any] = {
@@ -153,6 +154,7 @@
153154
"extras": {},
154155
"breaking_change_exclamation_in_title": False,
155156
"message_length_limit": 0,
157+
"body_length_limit": 0,
156158
}
157159

158160

0 commit comments

Comments
 (0)