diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 2a3a08848..21f06fd86 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -18,7 +18,8 @@ default branch on the origin repository. Useful for checking messages after the fact (e.g., pre-push or in CI) without an expensive check of the entire repository history. - entry: cz check + entry: cz -nr 3 check + # FIXME: origin/HEAD seems to be wrong. it probably should be origin/master or origin/main instead args: [--rev-range, origin/HEAD..HEAD] always_run: true pass_filenames: false diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index e22155cf7..0bb205eb3 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -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 msg = None # Get commit message from file (--commit-msg-file) if self.commit_msg_file is not None: diff --git a/commitizen/git.py b/commitizen/git.py index 19ca46b6c..ed3eb2ab0 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -310,3 +310,10 @@ def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]: 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") + if c.return_code != 0: + raise GitCommandError(c.err) + return c.out.strip()