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

refactor(check): rename variable and compile pattern once #1470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Lee-W merged 1 commit into commitizen-tools:refactors from bearomorphism:refactor-check-2
Jun 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions commitizen/commands/check.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,19 @@ def __call__(self) -> None:
if not commits:
raise NoCommitsFoundError(f"No commit found with range: '{self.rev_range}'")

pattern = self.cz.schema_pattern()
displayed_msgs_content = "\n".join(
[
f'commit "{commit.rev}": "{commit.message}"'
for commit in commits
if not self.validate_commit_message(commit.message, pattern)
]
pattern = re.compile(self.cz.schema_pattern())
invalid_msgs_content = "\n".join(
f'commit "{commit.rev}": "{commit.message}"'
for commit in commits
if not self._validate_commit_message(commit.message, pattern)
)
if displayed_msgs_content:
if invalid_msgs_content:
# TODO: capitalize the first letter of the error message for consistency in v5
raise InvalidCommitMessageError(
"commit validation: failed!\n"
"please enter a commit message in the commitizen format.\n"
f"{displayed_msgs_content}\n"
f"pattern: {pattern}"
f"{invalid_msgs_content}\n"
f"pattern: {pattern.pattern}"
)
out.success("Commit validation: successful!")

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

def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
def _validate_commit_message(
self, commit_msg: str, pattern: re.Pattern[str]
) -> bool:
if not commit_msg:
return self.allow_abort

if any(map(commit_msg.startswith, self.allowed_prefixes)):
return True

if self.max_msg_length:
msg_len = len(commit_msg.partition("\n")[0].strip())
if msg_len > self.max_msg_length:
return False
return bool(re.match(pattern, commit_msg))
Copy link
Contributor Author

@bearomorphism bearomorphism Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get the purpose of this PR after the changes. It seems the pattern only compiles once before this PR. or is there anywhere else I missed?

@Lee-W Before this change, each self._validate_commit_message evaluates re.match(pattern, _) once, where re.match compiles the input string every time.

Lee-W reacted with thumbs up emoji
Copy link
Contributor Author

@bearomorphism bearomorphism Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence self.cz.schema_pattern() is compiled len(commits) times (assuming no early return)

Lee-W reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. make sense. let's merge it 🙂


return bool(pattern.match(commit_msg))

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