Skip to content

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions commitizen/commands/check.py
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
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
if c.return_code != 0:
raise GitCommandError(c.err)
return c.out.strip()

Check warning on line 319 in commitizen/git.py

View check run for this annotation

Codecov / codecov/patch

commitizen/git.py#L316-L319

Added lines #L316 - L319 were not covered by tests