Skip to content

Commit a272305

Browse files
bearomorphismLee-W
authored andcommitted
refactor(check): compile once and rename variable
1 parent 626e67c commit a272305

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
@@ -79,20 +79,19 @@ def __call__(self) -> None:
7979
if not commits:
8080
raise NoCommitsFoundError(f"No commit found with range: '{self.rev_range}'")
8181

82-
pattern = self.cz.schema_pattern()
83-
displayed_msgs_content = "\n".join(
84-
[
85-
f'commit "{commit.rev}": "{commit.message}"'
86-
for commit in commits
87-
if not self.validate_commit_message(commit.message, pattern)
88-
]
82+
pattern = re.compile(self.cz.schema_pattern())
83+
invalid_msgs_content = "\n".join(
84+
f'commit "{commit.rev}": "{commit.message}"'
85+
for commit in commits
86+
if not self._validate_commit_message(commit.message, pattern)
8987
)
90-
if displayed_msgs_content:
88+
if invalid_msgs_content:
89+
# TODO: capitalize the first letter of the error message for consistency in v5
9190
raise InvalidCommitMessageError(
9291
"commit validation: failed!\n"
9392
"please enter a commit message in the commitizen format.\n"
94-
f"{displayed_msgs_content}\n"
95-
f"pattern: {pattern}"
93+
f"{invalid_msgs_content}\n"
94+
f"pattern: {pattern.pattern}"
9695
)
9796
out.success("Commit validation: successful!")
9897

@@ -143,14 +142,18 @@ def _filter_comments(msg: str) -> str:
143142
lines.append(line)
144143
return "\n".join(lines)
145144

146-
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
145+
def _validate_commit_message(
146+
self, commit_msg: str, pattern: re.Pattern[str]
147+
) -> bool:
147148
if not commit_msg:
148149
return self.allow_abort
149150

150151
if any(map(commit_msg.startswith, self.allowed_prefixes)):
151152
return True
153+
152154
if self.max_msg_length:
153155
msg_len = len(commit_msg.partition("\n")[0].strip())
154156
if msg_len > self.max_msg_length:
155157
return False
156-
return bool(re.match(pattern, commit_msg))
158+
159+
return bool(pattern.match(commit_msg))

0 commit comments

Comments
 (0)