Skip to content

Commit 34fce34

Browse files
committed
feat(check): add check against default branch
1 parent ec97453 commit 34fce34

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

commitizen/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@ def __call__(
475475
"help": "a range of git rev to check. e.g, master..HEAD",
476476
"exclusive_group": "group1",
477477
},
478+
{
479+
"name": ["-d", "--default-range"],
480+
"action": "store_true",
481+
"default": False,
482+
"help": "check from the default branch to HEAD. e.g, refs/remotes/origin/master..HEAD",
483+
"exclusive_group": "group1",
484+
},
478485
{
479486
"name": ["-m", "--message"],
480487
"help": "commit message that needs to be checked",
@@ -499,6 +506,12 @@ def __call__(
499506
"default": 0,
500507
"help": "length limit of the commit message; 0 for no limit",
501508
},
509+
{
510+
"name": ["-v", "--verbose"],
511+
"action": "store_true",
512+
"default": False,
513+
"help": "show verbose output",
514+
},
502515
],
503516
},
504517
{

commitizen/commands/check.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import re
55
import sys
6-
from typing import Any
6+
from typing import Any, TypedDict
77

88
from commitizen import factory, git, out
99
from commitizen.config import BaseConfig
@@ -14,6 +14,18 @@
1414
)
1515

1616

17+
class CheckArgs(TypedDict, total=False):
18+
commit_msg_file: str
19+
commit_msg: str
20+
rev_range: str
21+
allow_abort: bool
22+
message_length_limit: int
23+
allowed_prefixes: list[str]
24+
message: str
25+
default_range: bool
26+
verbose: bool
27+
28+
1729
class Check:
1830
"""Check if the current commit msg matches the commitizen format."""
1931

@@ -31,7 +43,9 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
3143
self.allow_abort: bool = bool(
3244
arguments.get("allow_abort", config.settings["allow_abort"])
3345
)
34-
self.max_msg_length: int = arguments.get("message_length_limit", 0)
46+
self.default_range = bool(arguments.get("default_range"))
47+
self.verbose = bool(arguments.get("verbose"))
48+
self.max_msg_length = arguments.get("message_length_limit", 0)
3549

3650
# we need to distinguish between None and [], which is a valid value
3751

@@ -42,25 +56,28 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
4256
else config.settings["allowed_prefixes"]
4357
)
4458

45-
self._valid_command_argument()
46-
47-
self.config: BaseConfig = config
48-
self.encoding = config.settings["encoding"]
49-
self.cz = factory.committer_factory(self.config)
50-
51-
def _valid_command_argument(self):
52-
num_exclusive_args_provided = sum(
59+
num_exclusive_args_provided = self.default_range + sum(
5360
arg is not None
54-
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range)
61+
for arg in (
62+
self.commit_msg_file,
63+
self.commit_msg,
64+
self.rev_range,
65+
)
5566
)
56-
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
57-
self.commit_msg = sys.stdin.read()
58-
elif num_exclusive_args_provided != 1:
67+
68+
if num_exclusive_args_provided > 1:
5969
raise InvalidCommandArgumentError(
6070
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
6171
"See 'cz check -h' for more information"
6272
)
6373

74+
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
75+
self.commit_msg = sys.stdin.read()
76+
77+
self.config: BaseConfig = config
78+
self.encoding = config.settings["encoding"]
79+
self.cz = factory.committer_factory(self.config)
80+
6481
def __call__(self):
6582
"""Validate if commit messages follows the conventional pattern.
6683
@@ -102,7 +119,10 @@ def _get_commits(self):
102119
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]
103120

104121
# Get commit messages from git log (--rev-range)
105-
return git.get_commits(end=self.rev_range or "HEAD")
122+
return git.get_commits(
123+
git.get_default_branch() if self.default_range else None,
124+
self.rev_range or "HEAD",
125+
)
106126

107127
@staticmethod
108128
def _filter_comments(msg: str) -> str:
@@ -136,6 +156,9 @@ def _filter_comments(msg: str) -> str:
136156
return "\n".join(lines)
137157

138158
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
159+
if self.verbose:
160+
out.info(commit_msg)
161+
139162
if not commit_msg:
140163
return self.allow_abort
141164

commitizen/git.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,10 @@ def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
326326
if not c.out:
327327
return []
328328
return c.out.split(f"{delimiter}\n")
329+
330+
331+
def get_default_branch() -> str:
332+
c = cmd.run("git symbolic-ref refs/remotes/origin/HEAD")
333+
if c.return_code != 0:
334+
raise GitCommandError(c.err)
335+
return c.out.strip()

tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
usage: cz check [-h] [--commit-msg-file COMMIT_MSG_FILE |
2-
--rev-range REV_RANGE | -m MESSAGE] [--allow-abort]
2+
--rev-range REV_RANGE | -d | -m MESSAGE] [--allow-abort]
33
[--allowed-prefixes [ALLOWED_PREFIXES ...]]
4-
[-l MESSAGE_LENGTH_LIMIT]
4+
[-l MESSAGE_LENGTH_LIMIT] [-v]
55

66
validates that a commit message matches the commitizen schema
77

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

0 commit comments

Comments
 (0)