Skip to content

Commit a5802cf

Browse files
LotramLee-W
authored andcommitted
chore(check): add customizable allowed prefixes
The allowed prefixes, which bypass the regex check, can now be configured.
1 parent f7fcaf5 commit a5802cf

File tree

7 files changed

+78
-8
lines changed

7 files changed

+78
-8
lines changed

commitizen/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@
325325
"default": False,
326326
"help": "allow empty commit messages, which typically abort a commit",
327327
},
328+
{
329+
"name": ["--allowed-prefixes"],
330+
"nargs": "*",
331+
"help": "allowed commit message prefixes. "
332+
"If the message starts by one of these prefixes, "
333+
"the message won't be checked against the regex",
334+
},
328335
],
329336
},
330337
{

commitizen/commands/check.py

Lines changed: 12 additions & 8 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, List
77

88
from commitizen import factory, git, out
99
from commitizen.config import BaseConfig
@@ -32,6 +32,15 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
3232
arguments.get("allow_abort", config.settings["allow_abort"])
3333
)
3434

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

3746
self.config: BaseConfig = config
@@ -134,12 +143,7 @@ def _filter_comments(msg: str) -> str:
134143
def validate_commit_message(self, commit_msg: str, pattern: str) -> bool:
135144
if not commit_msg:
136145
return self.allow_abort
137-
if (
138-
commit_msg.startswith("Merge")
139-
or commit_msg.startswith("Revert")
140-
or commit_msg.startswith("Pull request")
141-
or commit_msg.startswith("fixup!")
142-
or commit_msg.startswith("squash!")
143-
):
146+
147+
if any(map(commit_msg.startswith, self.allowed_prefixes)):
144148
return True
145149
return bool(re.match(pattern, commit_msg))

commitizen/defaults.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Settings(TypedDict, total=False):
4242
tag_format: str | None
4343
bump_message: str | None
4444
allow_abort: bool
45+
allowed_prefixes: List[str]
4546
changelog_file: str
4647
changelog_incremental: bool
4748
changelog_start_rev: str | None
@@ -75,6 +76,13 @@ class Settings(TypedDict, total=False):
7576
"tag_format": None, # example v$version
7677
"bump_message": None, # bumped v$current_version to $new_version
7778
"allow_abort": False,
79+
"allowed_prefixes": [
80+
"Merge",
81+
"Revert",
82+
"Pull request",
83+
"fixup!",
84+
"squash!",
85+
],
7886
"changelog_file": "CHANGELOG.md",
7987
"changelog_incremental": False,
8088
"changelog_start_rev": None,

docs/check.md

Lines changed: 9 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Default: `false`
9090

9191
Disallow empty commit messages, useful in ci. [Read more][allow_abort]
9292

93+
### `allowed_prefixes`
94+
95+
Type: `list`
96+
Default: `[ "Merge", "Revert", "Pull request", "fixup!", "squash!"]`
97+
Allow some prefixes and do not try to match the regex when checking the message [Read more][allowed_prefixes]
98+
9399
### `changelog_file`
94100

95101
Type: `str`
@@ -346,6 +352,7 @@ setup(
346352
[version-scheme]: bump.md#version-scheme
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"tag_format": None,
5050
"bump_message": None,
5151
"allow_abort": False,
52+
"allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"],
5253
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
5354
"style": [["pointer", "reverse"], ["question", "underline"]],
5455
"changelog_file": "CHANGELOG.md",
@@ -71,6 +72,7 @@
7172
"tag_format": None,
7273
"bump_message": None,
7374
"allow_abort": False,
75+
"allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"],
7476
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
7577
"style": [["pointer", "reverse"], ["question", "underline"]],
7678
"changelog_file": "CHANGELOG.md",

0 commit comments

Comments
 (0)