Skip to content

Commit 3cc72e8

Browse files
committed
feat(check): add check against default branch
1 parent 6b4f8b0 commit 3cc72e8

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

commitizen/cli.py

Lines changed: 7 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",

commitizen/commands/check.py

Lines changed: 21 additions & 13 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,7 @@ 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"))
4345
self.max_msg_length = arguments.get("message_length_limit", 0)
4446

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

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:
6055
num_exclusive_args_provided = sum(
6156
arg is not None
62-
for arg in (self.commit_msg_file, self.commit_msg, self.rev_range)
57+
for arg in (
58+
self.commit_msg_file,
59+
self.commit_msg,
60+
self.rev_range,
61+
)
6362
)
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:
63+
64+
if num_exclusive_args_provided > 1:
6765
raise InvalidCommandArgumentError(
6866
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
6967
"See 'cz check -h' for more information"
7068
)
7169

70+
if num_exclusive_args_provided == 0 and not sys.stdin.isatty():
71+
self.commit_msg = sys.stdin.read()
72+
73+
self.config: BaseConfig = config
74+
self.encoding = config.settings["encoding"]
75+
self.cz = factory.committer_factory(self.config)
76+
7277
def __call__(self) -> None:
7378
"""Validate if commit messages follows the conventional pattern.
7479
@@ -109,7 +114,10 @@ def _get_commits(self) -> list[git.GitCommit]:
109114
return [git.GitCommit(rev="", title="", body=self._filter_comments(msg))]
110115

111116
# Get commit messages from git log (--rev-range)
112-
return git.get_commits(end=self.rev_range)
117+
return git.get_commits(
118+
git.get_default_branch() if self.default_range else None,
119+
self.rev_range,
120+
)
113121

114122
@staticmethod
115123
def _filter_comments(msg: str) -> str:
@@ -134,7 +142,7 @@ def _filter_comments(msg: str) -> str:
134142
The filtered commit message without comments.
135143
"""
136144

137-
lines = []
145+
lines: list[str] = []
138146
for line in msg.split("\n"):
139147
if "# ------------------------ >8 ------------------------" in line:
140148
break

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: 4 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

tests/test_git.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,22 @@ def test_create_commit_cmd_string(mocker, os_name, committer_date, expected_cmd)
480480
mocker.patch("os.name", os_name)
481481
result = git._create_commit_cmd_string("", committer_date, "temp.txt")
482482
assert result == expected_cmd
483+
484+
485+
def test_get_default_branch(mocker: MockFixture):
486+
# Test successful case
487+
mocker.patch(
488+
"commitizen.cmd.run", return_value=FakeCommand(out="refs/remotes/origin/main\n")
489+
)
490+
assert git.get_default_branch() == "refs/remotes/origin/main"
491+
492+
# Test error case
493+
mocker.patch(
494+
"commitizen.cmd.run",
495+
return_value=FakeCommand(
496+
err="fatal: ref refs/remotes/origin/HEAD is not a symbolic ref",
497+
return_code=1,
498+
),
499+
)
500+
with pytest.raises(exceptions.GitCommandError):
501+
git.get_default_branch()

0 commit comments

Comments
 (0)