Skip to content

feat: add check against default branch #1499

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 1 commit into
base: refactors
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
13 changes: 13 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ def __call__(
"help": "a range of git rev to check. e.g, master..HEAD",
"exclusive_group": "group1",
},
{
"name": ["-d", "--default-range"],
"action": "store_true",
"default": False,
"help": "check from the default branch to HEAD. e.g, refs/remotes/origin/master..HEAD",
"exclusive_group": "group1",
},
{
"name": ["-m", "--message"],
"help": "commit message that needs to be checked",
Expand All @@ -498,6 +505,12 @@ def __call__(
"default": 0,
"help": "length limit of the commit message; 0 for no limit",
},
{
"name": ["-v", "--verbose"],
"action": "store_true",
"default": False,
"help": "show verbose output",
},
],
},
{
Expand Down
34 changes: 22 additions & 12 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class CheckArgs(TypedDict, total=False):
message_length_limit: int
allowed_prefixes: list[str]
message: str
default_range: bool
verbose: bool


class Check:
Expand All @@ -40,6 +42,8 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
self.allow_abort = bool(
arguments.get("allow_abort", config.settings["allow_abort"])
)
self.default_range = bool(arguments.get("default_range"))
self.verbose = bool(arguments.get("verbose"))
self.max_msg_length = arguments.get("message_length_limit", 0)

# we need to distinguish between None and [], which is a valid value
Expand All @@ -50,25 +54,28 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
else config.settings["allowed_prefixes"]
)

self._valid_command_argument()

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

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)
for arg in (
self.commit_msg_file,
self.commit_msg,
self.rev_range,
)
)
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
self.commit_msg = sys.stdin.read()
elif num_exclusive_args_provided != 1:

if num_exclusive_args_provided > 1:
raise InvalidCommandArgumentError(
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
"See 'cz check -h' for more information"
)

if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
self.commit_msg = sys.stdin.read()

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

def __call__(self) -> None:
"""Validate if commit messages follows the conventional pattern.

Expand Down Expand Up @@ -109,7 +116,10 @@ def _get_commits(self) -> list[git.GitCommit]:
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]

# Get commit messages from git log (--rev-range)
return git.get_commits(end=self.rev_range)
return git.get_commits(
git.get_default_branch() if self.default_range else None,
self.rev_range,
)

@staticmethod
def _filter_comments(msg: str) -> str:
Expand Down
7 changes: 7 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,10 @@
if not c.out:
return []
return c.out.split(f"{delimiter}\n")


def get_default_branch() -> str:
c = cmd.run("git symbolic-ref refs/remotes/origin/HEAD")
if c.return_code != 0:
raise GitCommandError(c.err)
return c.out.strip()

Check warning on line 340 in commitizen/git.py

View check run for this annotation

Codecov / codecov/patch

commitizen/git.py#L337-L340

Added lines #L337 - L340 were not covered by tests
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
usage: cz check [-h] [--commit-msg-file COMMIT_MSG_FILE |
--rev-range REV_RANGE | -m MESSAGE] [--allow-abort]
--rev-range REV_RANGE | -d | -m MESSAGE] [--allow-abort]
[--allowed-prefixes [ALLOWED_PREFIXES ...]]
[-l MESSAGE_LENGTH_LIMIT]
[-l MESSAGE_LENGTH_LIMIT] [-v]

validates that a commit message matches the commitizen schema

Expand All @@ -13,6 +13,8 @@ options:
MSG_FILE=$1
--rev-range REV_RANGE
a range of git rev to check. e.g, master..HEAD
-d, --default-range check from the default branch to HEAD. e.g,
refs/remotes/origin/master..HEAD
-m, --message MESSAGE
commit message that needs to be checked
--allow-abort allow empty commit messages, which typically abort a
Expand All @@ -23,3 +25,4 @@ options:
against the regex
-l, --message-length-limit MESSAGE_LENGTH_LIMIT
length limit of the commit message; 0 for no limit
-v, --verbose show verbose output