Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44c145d

Browse files
feat: check if commit message is no more than 72 characters
1 parent d33f5fc commit 44c145d

File tree

9 files changed

+37
-8
lines changed

9 files changed

+37
-8
lines changed

‎commitizen/cli.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
"action": "store_true",
6767
"help": "Sign off the commit",
6868
},
69+
{
70+
"name": ["-cl", "--check-length"],
71+
"action": "store_true",
72+
"help": "check if length of commit message is within limit",
73+
},
6974
],
7075
},
7176
{

‎commitizen/commands/commit.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def __init__(self, config: BaseConfig, arguments: dict):
2929
self.config: BaseConfig = config
3030
self.cz = factory.commiter_factory(self.config)
3131
self.arguments = arguments
32+
print(self.config)
33+
print(self.arguments)
3234
self.temp_file: str = os.path.join(
3335
tempfile.gettempdir(),
3436
"cz.commit{user}.backup".format(user=os.environ.get("USER", "")),
@@ -59,7 +61,9 @@ def prompt_commit_questions(self) -> str:
5961

6062
if not answers:
6163
raise NoAnswersError()
62-
return cz.message(answers)
64+
65+
check_length: bool = self.arguments.get("check_length", False)
66+
return cz.message(answers, check_length=check_length)
6367

6468
def __call__(self):
6569
dry_run: bool = self.arguments.get("dry_run")

‎commitizen/cz/base.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def questions(self) -> Questions:
5050
"""Questions regarding the commit message."""
5151

5252
@abstractmethod
53-
def message(self, answers: dict) -> str:
53+
def message(self, answers: dict, check_length: Optional[bool] =False) -> str:
5454
"""Format your git message."""
5555

5656
@property

‎commitizen/cz/conventional_commits/conventional_commits.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
import re
3+
from typing import Optional
34

45
from commitizen import defaults
56
from commitizen.cz.base import BaseCommitizen
67
from commitizen.cz.utils import multiple_line_breaker, required_validator
78
from commitizen.defaults import Questions
9+
from commitizen.exceptions import CommitMessageLineLengthExceededError
810

911
__all__ = ["ConventionalCommitsCz"]
1012

@@ -150,7 +152,7 @@ def questions(self) -> Questions:
150152
]
151153
return questions
152154

153-
def message(self, answers: dict) -> str:
155+
def message(self, answers: dict, check_length: Optional[bool] =False) -> str:
154156
prefix = answers["prefix"]
155157
scope = answers["scope"]
156158
subject = answers["subject"]
@@ -167,7 +169,15 @@ def message(self, answers: dict) -> str:
167169
if footer:
168170
footer = f"\n\n{footer}"
169171

170-
message = f"{prefix}{scope}: {subject}{body}{footer}"
172+
message = f"{prefix}{scope}: {subject}"
173+
message_len = len(message)
174+
MESSAGE_LEN_LIMIT = 72
175+
if check_length and message_len > MESSAGE_LEN_LIMIT:
176+
raise CommitMessageLineLengthExceededError(
177+
f"Length of commit message exceeded limit ({message_len}/{MESSAGE_LEN_LIMIT})"
178+
)
179+
180+
message = f"{message}{body}{footer}"
171181

172182
return message
173183

‎commitizen/cz/customize/customize.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, config: BaseConfig):
5353
def questions(self) -> Questions:
5454
return self.custom_settings.get("questions", [{}])
5555

56-
def message(self, answers: dict) -> str:
56+
def message(self, answers: dict, check_length: Optional[bool] =False) -> str:
5757
message_template = Template(self.custom_settings.get("message_template", ""))
5858
if getattr(Template, "substitute", None):
5959
return message_template.substitute(**answers) # type: ignore

‎commitizen/cz/jira/jira.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Optional
23

34
from commitizen.cz.base import BaseCommitizen
45
from commitizen.defaults import Questions
@@ -44,7 +45,7 @@ def questions(self) -> Questions:
4445
]
4546
return questions
4647

47-
def message(self, answers) -> str:
48+
def message(self, answers: dict, check_length: Optional[bool] =False) -> str:
4849
return " ".join(
4950
filter(
5051
bool,

‎commitizen/exceptions.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ExitCode(enum.IntEnum):
2727
NOT_ALLOWED = 20
2828
NO_INCREMENT = 21
2929
UNRECOGNIZED_CHARACTERSET_ENCODING = 22
30+
COMMIT_MESSAGE_LINE_LENGTH_EXCEEDED = 23
3031

3132

3233
class CommitizenException(Exception):
@@ -153,3 +154,7 @@ class NotAllowed(CommitizenException):
153154

154155
class CharacterSetDecodeError(CommitizenException):
155156
exit_code = ExitCode.UNRECOGNIZED_CHARACTERSET_ENCODING
157+
158+
159+
class CommitMessageLineLengthExceededError(CommitizenException):
160+
exit_code = ExitCode.COMMIT_MESSAGE_LINE_LENGTH_EXCEEDED

‎docs/customization.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ Create a file starting with `cz_`, for example `cz_jira.py`. This prefix is used
210210
Inherit from `BaseCommitizen`, and you must define `questions` and `message`. The others are optional.
211211

212212
```python
213+
from typing import Optional
214+
213215
from commitizen.cz.base import BaseCommitizen
214216
from commitizen.defaults import Questions
215217
@@ -232,7 +234,7 @@ class JiraCz(BaseCommitizen):
232234
]
233235
return questions
234236
235-
def message(self, answers: dict) -> str:
237+
def message(self, answers: dict, check_length: Optional[bool] = False) -> str:
236238
"""Generate the message with the given answers."""
237239
return '{0} (#{1})'.format(answers['title'], answers['issue'])
238240

‎tests/test_cz_base.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import pytest
24

35
from commitizen.cz.base import BaseCommitizen
@@ -7,7 +9,7 @@ class DummyCz(BaseCommitizen):
79
def questions(self):
810
return [{"type": "input", "name": "commit", "message": "Initial commit:\n"}]
911

10-
def message(self, answers):
12+
def message(self, answers: dict, check_length: Optional[bool] =False):
1113
return answers["commit"]
1214

1315

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /