Skip to content

refactor(check): remove unnecessary list construction #1455

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 17 additions & 15 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import sys
from itertools import islice
from typing import Any

from commitizen import factory, git, out
Expand Down Expand Up @@ -34,7 +35,6 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
self.max_msg_length: int = arguments.get("message_length_limit", 0)

# we need to distinguish between None and [], which is a valid value

allowed_prefixes = arguments.get("allowed_prefixes")
self.allowed_prefixes: list[str] = (
allowed_prefixes
Expand All @@ -44,7 +44,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(

self._valid_command_argument()

self.config: BaseConfig = config
self.config = config
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason we remove it?

self.encoding = config.settings["encoding"]
self.cz = factory.committer_factory(self.config)

Expand Down Expand Up @@ -73,11 +73,9 @@ def __call__(self):

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)
]
f'commit "{commit.rev}": "{commit.message}"'
for commit in commits
if not self.validate_commit_message(commit.message, pattern)
)
if displayed_msgs_content:
raise InvalidCommitMessageError(
Expand Down Expand Up @@ -126,14 +124,18 @@ def _filter_comments(msg: str) -> str:
Returns:
The filtered commit message without comments.
"""

lines = []
for line in msg.split("\n"):
if "# ------------------------ >8 ------------------------" in line:
break
if not line.startswith("#"):
lines.append(line)
return "\n".join(lines)
msg_lines = msg.split("\n")
Copy link
Member

Choose a reason for hiding this comment

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

Not sure whether we want to do this. Look like we'll loop through lines twice and the logic looks more complicated

cutoff = next(
(
i
for i, line in enumerate(msg_lines)
if "# ------------------------ >8 ------------------------" in line
),
len(msg_lines),
)
return "\n".join(
line for line in islice(msg_lines, cutoff) if not line.startswith("#")
)

def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
if not commit_msg:
Expand Down