Skip to content

Commit 5a3578e

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

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
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: 30 additions & 6 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

@@ -48,10 +62,14 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
4862
self.encoding = config.settings["encoding"]
4963
self.cz = factory.committer_factory(self.config)
5064

51-
def _valid_command_argument(self):
52-
num_exclusive_args_provided = sum(
65+
def _valid_command_argument(self) -> None:
66+
num_exclusive_args_provided = self.default_range + sum(
5367
arg is not None
54-
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range)
68+
for arg in (
69+
self.commit_msg_file,
70+
self.commit_msg,
71+
self.rev_range,
72+
)
5573
)
5674
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
5775
self.commit_msg = sys.stdin.read()
@@ -102,7 +120,10 @@ def _get_commits(self):
102120
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]
103121

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

107128
@staticmethod
108129
def _filter_comments(msg: str) -> str:
@@ -136,6 +157,9 @@ def _filter_comments(msg: str) -> str:
136157
return "\n".join(lines)
137158

138159
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
160+
if self.verbose:
161+
out.info(commit_msg)
162+
139163
if not commit_msg:
140164
return self.allow_abort
141165

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)