From e2b9789e4884cef5e1fa0298801a527e38ac4406 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Tue, 6 May 2025 10:08:35 +0800 Subject: [PATCH 1/4] feat(git): add guess_default_branch function --- commitizen/git.py | 7 +++++++ 1 file changed, 7 insertions(+) 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() From caaa696db165a51fcb686de94b63eaaf97c32e7c Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Tue, 6 May 2025 10:17:33 +0800 Subject: [PATCH 2/4] refactor(commands/check): remove unused cwd argument --- commitizen/commands/check.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index e22155cf7..940bc2945 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -1,6 +1,5 @@ from __future__ import annotations -import os import re import sys from typing import Any @@ -17,13 +16,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") From 4fd84b22494a7947313765d69f3150da6bc738a0 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Tue, 6 May 2025 10:24:17 +0800 Subject: [PATCH 3/4] style(commands/check): improve type checking --- commitizen/commands/check.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index 940bc2945..0bb205eb3 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -2,6 +2,7 @@ import re import sys +from collections.abc import Generator from typing import Any from commitizen import factory, git, out @@ -32,7 +33,6 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any]) -> None: 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 @@ -46,7 +46,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any]) -> None: 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) @@ -59,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: @@ -70,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 @@ -90,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: From 5fafc37b67e5ea8647f3dfaf22f2c1e260eed116 Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Tue, 6 May 2025 10:26:06 +0800 Subject: [PATCH 4/4] docs(pre-commit): add pre-commit hook todo --- .pre-commit-hooks.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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