-
-
Notifications
You must be signed in to change notification settings - Fork 301
Add check against main branch hook #1400
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import re | ||
import sys | ||
from collections.abc import Generator | ||
from typing import Any | ||
|
||
from commitizen import factory, git, out | ||
|
@@ -17,13 +17,12 @@ | |
class Check: | ||
"""Check if the current commit msg matches the commitizen format.""" | ||
|
||
def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd()): | ||
def __init__(self, config: BaseConfig, arguments: dict[str, Any]) -> None: | ||
"""Initial check command. | ||
|
||
Args: | ||
config: The config object required for the command to perform its action | ||
arguments: All the flags provided by the user | ||
cwd: Current work directory | ||
""" | ||
self.commit_msg_file: str | None = arguments.get("commit_msg_file") | ||
self.commit_msg: str | None = arguments.get("message") | ||
|
@@ -34,7 +33,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 | ||
|
@@ -48,7 +46,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd( | |
self.encoding = config.settings["encoding"] | ||
self.cz = factory.commiter_factory(self.config) | ||
|
||
def _valid_command_argument(self): | ||
def _valid_command_argument(self) -> None: | ||
num_exclusive_args_provided = sum( | ||
arg is not None | ||
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range) | ||
|
@@ -61,7 +59,7 @@ def _valid_command_argument(self): | |
"See 'cz check -h' for more information" | ||
) | ||
|
||
def __call__(self): | ||
def __call__(self) -> None: | ||
"""Validate if commit messages follows the conventional pattern. | ||
|
||
Raises: | ||
|
@@ -72,12 +70,12 @@ def __call__(self): | |
raise NoCommitsFoundError(f"No commit found with range: '{self.rev_range}'") | ||
|
||
pattern = self.cz.schema_pattern() | ||
ill_formated_commits = [ | ||
ill_formated_commits: Generator[git.GitCommit] = ( | ||
commit | ||
for commit in commits | ||
if not self.validate_commit_message(commit.message, pattern) | ||
] | ||
displayed_msgs_content = "\n".join( | ||
) | ||
displayed_msgs_content: str = "\n".join( | ||
[ | ||
f'commit "{commit.rev}": "{commit.message}"' | ||
for commit in ill_formated_commits | ||
|
@@ -92,7 +90,8 @@ def __call__(self): | |
) | ||
out.success("Commit validation: successful!") | ||
|
||
def _get_commits(self): | ||
def _get_commits(self) -> list[git.GitCommit]: | ||
# TODO: this method seems to do a few different things. probably would be better if we could split it to smaller functions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already done in refactors branch |
||
msg = None | ||
# Get commit message from file (--commit-msg-file) | ||
if self.commit_msg_file is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -310,3 +310,10 @@ | |
if not c.out: | ||
return [] | ||
return c.out.split(f"{delimiter}\n") | ||
|
||
|
||
def guess_default_branch() -> str: | ||
c = cmd.run("git branch --format '%(refname:short)' --list master main") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes a strong hypothesis that the reference branch can only be To handle this, I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'll have to try those commands 👀 Looks interesting! I'm thinking of letting users set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's a good idea. Having guess the default but keeping the possibility to explicitly set it from the configuration 👍🏼 |
||
if c.return_code != 0: | ||
raise GitCommandError(c.err) | ||
return c.out.strip() | ||