Skip to content

Commit ed183a5

Browse files
committed
feat(check): add check against default branch
1 parent d968f26 commit ed183a5

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

commitizen/cli.py

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

commitizen/commands/check.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class CheckArgs(TypedDict, total=False):
2121
message_length_limit: int
2222
allowed_prefixes: list[str]
2323
message: str
24+
default_range: bool
25+
verbose: bool
2426

2527

2628
class Check:
@@ -40,6 +42,8 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4042
self.allow_abort = bool(
4143
arguments.get("allow_abort", config.settings["allow_abort"])
4244
)
45+
self.default_range = bool(arguments.get("default_range"))
46+
self.verbose = bool(arguments.get("verbose"))
4347
self.max_msg_length = arguments.get("message_length_limit", 0)
4448

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

53-
self._valid_command_argument()
54-
55-
self.config: BaseConfig = config
56-
self.encoding = config.settings["encoding"]
57-
self.cz = factory.committer_factory(self.config)
58-
59-
def _valid_command_argument(self) -> None:
6057
num_exclusive_args_provided = sum(
6158
arg is not None
62-
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range)
59+
for arg in (
60+
self.commit_msg_file,
61+
self.commit_msg,
62+
self.rev_range,
63+
)
6364
)
64-
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
65-
self.commit_msg = sys.stdin.read()
66-
elif num_exclusive_args_provided != 1:
65+
66+
if num_exclusive_args_provided > 1:
6767
raise InvalidCommandArgumentError(
6868
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
6969
"See 'cz check -h' for more information"
7070
)
7171

72+
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
73+
self.commit_msg = sys.stdin.read()
74+
75+
self.config: BaseConfig = config
76+
self.encoding = config.settings["encoding"]
77+
self.cz = factory.committer_factory(self.config)
78+
7279
def __call__(self) -> None:
7380
"""Validate if commit messages follows the conventional pattern.
7481
@@ -109,7 +116,10 @@ def _get_commits(self) -> list[git.GitCommit]:
109116
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]
110117

111118
# Get commit messages from git log (--rev-range)
112-
return git.get_commits(end=self.rev_range)
119+
return git.get_commits(
120+
git.get_default_branch() if self.default_range else None,
121+
self.rev_range,
122+
)
113123

114124
@staticmethod
115125
def _filter_comments(msg: str) -> str:

commitizen/git.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,10 @@ def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
331331
if not c.out:
332332
return []
333333
return c.out.split(f"{delimiter}\n")
334+
335+
336+
def get_default_branch() -> str:
337+
c = cmd.run("git symbolic-ref refs/remotes/origin/HEAD")
338+
if c.return_code != 0:
339+
raise GitCommandError(c.err)
340+
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)