Skip to content

Commit 7a5c754

Browse files
committed
chore(check): add customizable allowed prefixes
The allowed prefixes, which bypass the regex check, can now be configured.
1 parent 672ff94 commit 7a5c754

File tree

7 files changed

+78
-8
lines changed

7 files changed

+78
-8
lines changed

commitizen/cli.py

+7
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@
306306
"default": False,
307307
"help": "allow empty commit messages, which typically abort a commit",
308308
},
309+
{
310+
"name": ["--allowed-prefixes"],
311+
"nargs": "*",
312+
"help": "allowed commit message prefixes. "
313+
"If the message starts by one of these prefixes, "
314+
"the message won't be checked against the regex",
315+
},
309316
],
310317
},
311318
{

commitizen/commands/check.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import re
33
import sys
4-
from typing import Any, Dict, Optional
4+
from typing import Any, Dict, List, Optional
55

66
from commitizen import factory, git, out
77
from commitizen.config import BaseConfig
@@ -30,6 +30,15 @@ def __init__(self, config: BaseConfig, arguments: Dict[str, Any], cwd=os.getcwd(
3030
arguments.get("allow_abort", config.settings["allow_abort"])
3131
)
3232

33+
# we need to distinguish between None and [], which is a valid value
34+
35+
allowed_prefixes = arguments.get("allowed_prefixes")
36+
self.allowed_prefixes: List[str] = (
37+
allowed_prefixes
38+
if allowed_prefixes is not None
39+
else config.settings["allowed_prefixes"]
40+
)
41+
3342
self._valid_command_argument()
3443

3544
self.config: BaseConfig = config
@@ -132,12 +141,7 @@ def _filter_comments(msg: str) -> str:
132141
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
133142
if not commit_msg:
134143
return self.allow_abort
135-
if (
136-
commit_msg.startswith("Merge")
137-
or commit_msg.startswith("Revert")
138-
or commit_msg.startswith("Pull request")
139-
or commit_msg.startswith("fixup!")
140-
or commit_msg.startswith("squash!")
141-
):
144+
145+
if any(map(commit_msg.startswith, self.allowed_prefixes)):
142146
return True
143147
return bool(re.match(pattern, commit_msg))

commitizen/defaults.py

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Settings(TypedDict, total=False):
3838
tag_format: Optional[str]
3939
bump_message: Optional[str]
4040
allow_abort: bool
41+
allowed_prefixes: List[str]
4142
changelog_file: str
4243
changelog_incremental: bool
4344
changelog_start_rev: Optional[str]
@@ -71,6 +72,13 @@ class Settings(TypedDict, total=False):
7172
"tag_format": None, # example v$version
7273
"bump_message": None, # bumped v$current_version to $new_version
7374
"allow_abort": False,
75+
"allowed_prefixes": [
76+
"Merge",
77+
"Revert",
78+
"Pull request",
79+
"fixup!",
80+
"squash!",
81+
],
7482
"changelog_file": "CHANGELOG.md",
7583
"changelog_incremental": False,
7684
"changelog_start_rev": None,

docs/check.md

+9
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ cz check --message MESSAGE --allow-abort
6464

6565
Empty commit messages typically instruct Git to abort a commit, so you can pass `--allow-abort` to
6666
permit them. Since `git commit` accepts an `--allow-empty-message` flag (primarily for wrapper scripts), you may wish to disallow such commits in CI. `--allow-abort` may be used in conjunction with any of the other options.
67+
68+
### Allowed Prefixes
69+
70+
If the commit message starts by some specific prefixes, `cz check` returns `True` without checkign the regex.
71+
By default, the the following prefixes are allowed: `Merge`, `Revert`, `Pull Request`, `fixup!` and `squash!`.
72+
73+
```bash
74+
cz check --message MESSAGE --allowed-prefixes 'Merge' 'Revert' 'Custom Prefix'
75+
```

docs/config.md

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ Default: `false`
8282

8383
Disallow empty commit messages, useful in ci. [Read more][allow_abort]
8484

85+
### `allowed_prefixes`
86+
87+
Type: `list`
88+
Default: `[ "Merge", "Revert", "Pull request", "fixup!", "squash!"]`
89+
Allow some prefixes and do not try to match the regex when checking the message [Read more][allowed_prefixes]
90+
8591
### `changelog_file`
8692

8793
Type: `str`
@@ -346,6 +352,7 @@ setup(
346352
[version_type]: bump.md#version_type
347353
[pre_bump_hooks]: bump.md#pre_bump_hooks
348354
[post_bump_hooks]: bump.md#post_bump_hooks
355+
[allowed_prefixes]: check.md#allowed-prefixes
349356
[additional-features]: https://github.com/tmbo/questionary#additional-features
350357
[customization]: customization.md
351358
[shortcuts]: customization.md#shortcut-keys

tests/commands/test_check_command.py

+33
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,39 @@ def test_check_command_override_allow_abort_config(config, mocker: MockFixture):
312312
error_mock.assert_called_once()
313313

314314

315+
def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture):
316+
success_mock = mocker.patch("commitizen.out.success")
317+
check_cmd = commands.Check(
318+
config=config,
319+
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
320+
)
321+
322+
check_cmd()
323+
success_mock.assert_called_once()
324+
325+
326+
def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
327+
success_mock = mocker.patch("commitizen.out.success")
328+
config.settings["allowed_prefixes"] = ["custom!"]
329+
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
330+
331+
check_cmd()
332+
success_mock.assert_called_once()
333+
334+
335+
def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
336+
error_mock = mocker.patch("commitizen.out.error")
337+
config.settings["allow_abort"] = ["fixup!"]
338+
check_cmd = commands.Check(
339+
config=config,
340+
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
341+
)
342+
343+
with pytest.raises(InvalidCommitMessageError):
344+
check_cmd()
345+
error_mock.assert_called_once()
346+
347+
315348
def test_check_command_with_pipe_message(mocker: MockFixture, capsys):
316349
testargs = ["cz", "check"]
317350
mocker.patch.object(sys, "argv", testargs)

tests/test_conf.py

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"tag_format": None,
4949
"bump_message": None,
5050
"allow_abort": False,
51+
"allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"],
5152
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
5253
"style": [["pointer", "reverse"], ["question", "underline"]],
5354
"changelog_file": "CHANGELOG.md",
@@ -70,6 +71,7 @@
7071
"tag_format": None,
7172
"bump_message": None,
7273
"allow_abort": False,
74+
"allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"],
7375
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
7476
"style": [["pointer", "reverse"], ["question", "underline"]],
7577
"changelog_file": "CHANGELOG.md",

0 commit comments

Comments
 (0)