Skip to content

Commit 8932925

Browse files
committed
refactor(check): compile once and rename variable
1 parent baee83e commit 8932925

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

commitizen/commands/check.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,19 @@ def __call__(self):
7171
if not commits:
7272
raise NoCommitsFoundError(f"No commit found with range: '{self.rev_range}'")
7373

74-
pattern = self.cz.schema_pattern()
75-
displayed_msgs_content = "\n".join(
76-
[
77-
f'commit "{commit.rev}": "{commit.message}"'
78-
for commit in commits
79-
if not self.validate_commit_message(commit.message, pattern)
80-
]
74+
pattern = re.compile(self.cz.schema_pattern())
75+
invalid_msgs_content = "\n".join(
76+
f'commit "{commit.rev}": "{commit.message}"'
77+
for commit in commits
78+
if not self._validate_commit_message(commit.message, pattern)
8179
)
82-
if displayed_msgs_content:
80+
if invalid_msgs_content:
81+
# TODO: capitalize the first letter of the error message for consistency in v5
8382
raise InvalidCommitMessageError(
8483
"commit validation: failed!\n"
8584
"please enter a commit message in the commitizen format.\n"
86-
f"{displayed_msgs_content}\n"
87-
f"pattern: {pattern}"
85+
f"{invalid_msgs_content}\n"
86+
f"pattern: {pattern.pattern}"
8887
)
8988
out.success("Commit validation: successful!")
9089

@@ -135,14 +134,18 @@ def _filter_comments(msg: str) -> str:
135134
lines.append(line)
136135
return "\n".join(lines)
137136

138-
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
137+
def _validate_commit_message(
138+
self, commit_msg: str, pattern: re.Pattern[str]
139+
) -> bool:
139140
if not commit_msg:
140141
return self.allow_abort
141142

142143
if any(map(commit_msg.startswith, self.allowed_prefixes)):
143144
return True
145+
144146
if self.max_msg_length:
145147
msg_len = len(commit_msg.partition("\n")[0].strip())
146148
if msg_len > self.max_msg_length:
147149
return False
148-
return bool(re.match(pattern, commit_msg))
150+
151+
return bool(pattern.match(commit_msg))

0 commit comments

Comments
 (0)