Skip to content

Commit 1a9f4f7

Browse files
committed
feat(check): add check against default branch
1 parent 19efaa8 commit 1a9f4f7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CheckArgs(TypedDict, total=False):
2121
message_length_limit: int
2222
allowed_prefixes: list[str]
2323
message: str
24+
default_range: bool
2425

2526

2627
class Check:
@@ -40,6 +41,8 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4041
self.allow_abort = bool(
4142
arguments.get("allow_abort", config.settings["allow_abort"])
4243
)
44+
self.default_range = bool(arguments.get("default_range"))
45+
self.verbose = bool(arguments.get("verbose"))
4346
self.max_msg_length = arguments.get("message_length_limit", 0)
4447

4548
# we need to distinguish between None and [], which is a valid value
@@ -59,7 +62,12 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
5962
def _valid_command_argument(self) -> None:
6063
num_exclusive_args_provided = sum(
6164
arg is not None
62-
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range)
65+
for arg in (
66+
self.commit_msg_file,
67+
self.commit_msg,
68+
self.rev_range,
69+
self.default_range,
70+
)
6371
)
6472
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
6573
self.commit_msg = sys.stdin.read()
@@ -110,7 +118,10 @@ def _get_commits(self) -> list[git.GitCommit]:
110118
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]
111119

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

115126
@staticmethod
116127
def _filter_comments(msg: str) -> str:
@@ -144,6 +155,9 @@ def _filter_comments(msg: str) -> str:
144155
return "\n".join(lines)
145156

146157
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
158+
if self.verbose:
159+
out.info(commit_msg)
160+
147161
if not commit_msg:
148162
return self.allow_abort
149163

commitizen/git.py

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

0 commit comments

Comments
 (0)