Skip to content

Commit 7d4e3b9

Browse files
feat(bump): support for ! as BREAKING change in commit message
* feat(find_increment): allow to use regex keys for bump_map Default bump_map doesnt consider breaking change identificator "!". Regex keys should take care of it. * test: return check for black and isort * feat(defaults): change default `bump_pattern` and `bump_map`
1 parent dbafb75 commit 7d4e3b9

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

commitizen/bump.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from collections import defaultdict
2+
from collections import OrderedDict
33
from itertools import zip_longest
44
from string import Template
55
from typing import List, Optional, Union
@@ -18,28 +18,36 @@
1818

1919

2020
def find_increment(
21-
commits: List[GitCommit], regex: str = bump_pattern, increments_map: dict = bump_map
21+
commits: List[GitCommit],
22+
regex: str = bump_pattern,
23+
increments_map: Union[dict, OrderedDict] = bump_map,
2224
) -> Optional[str]:
2325

26+
if isinstance(increments_map, dict):
27+
increments_map = OrderedDict(increments_map)
28+
2429
# Most important cases are major and minor.
2530
# Everything else will be considered patch.
26-
increments_map_default = defaultdict(lambda: None, increments_map)
27-
pattern = re.compile(regex)
31+
select_pattern = re.compile(regex)
2832
increment = None
2933

3034
for commit in commits:
3135
for message in commit.message.split("\n"):
32-
result = pattern.search(message)
33-
if not result:
34-
continue
35-
found_keyword = result.group(0)
36-
new_increment = increments_map_default[found_keyword]
37-
if increment == "MAJOR":
38-
continue
39-
elif increment == "MINOR" and new_increment == "MAJOR":
40-
increment = new_increment
41-
elif increment == "PATCH" or increment is None:
42-
increment = new_increment
36+
result = select_pattern.search(message)
37+
if result:
38+
found_keyword = result.group(0)
39+
new_increment = None
40+
for match_pattern in increments_map.keys():
41+
if re.match(match_pattern, found_keyword):
42+
new_increment = increments_map[match_pattern]
43+
break
44+
45+
if increment == "MAJOR":
46+
continue
47+
elif increment == "MINOR" and new_increment == "MAJOR":
48+
increment = new_increment
49+
elif increment == "PATCH" or increment is None:
50+
increment = new_increment
4351

4452
return increment
4553

commitizen/defaults.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from collections import OrderedDict
2+
13
name: str = "cz_conventional_commits"
24
# TODO: .cz, setup.cfg, .cz.cfg should be removed in 2.0
35
long_term_support_config_files: list = ["pyproject.toml", ".cz.toml"]
46
deprcated_config_files: list = [".cz", "setup.cfg", ".cz.cfg"]
57
config_files: list = long_term_support_config_files + deprcated_config_files
68

7-
DEFAULT_SETTINGS = {
9+
DEFAULT_SETTINGS: dict = {
810
"name": "cz_conventional_commits",
911
"version": None,
1012
"version_files": [],
@@ -16,12 +18,15 @@
1618
MINOR = "MINOR"
1719
PATCH = "PATCH"
1820

19-
bump_pattern = r"^(BREAKING CHANGE|feat|fix|refactor|perf)"
20-
bump_map = {
21-
"BREAKING CHANGE": MAJOR,
22-
"feat": MINOR,
23-
"fix": PATCH,
24-
"refactor": PATCH,
25-
"perf": PATCH,
26-
}
21+
bump_pattern = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?"
22+
bump_map = OrderedDict(
23+
(
24+
(r"^.+!$", MAJOR),
25+
(r"^BREAKING[\-\ ]CHANGE", MAJOR),
26+
(r"^feat", MINOR),
27+
(r"^fix", PATCH),
28+
(r"^refactor", PATCH),
29+
(r"^perf", PATCH),
30+
)
31+
)
2732
bump_message = "bump: version $current_version → $new_version"

tests/test_bump_find_increment.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@
2020
"fix(setup.py): future is now required for every python version",
2121
]
2222

23-
MAJOR_INCREMENTS_CC = [
23+
MAJOR_INCREMENTS_BREAKING_CHANGE_CC = [
2424
"feat(cli): added version",
2525
"docs(README): motivation",
2626
"BREAKING CHANGE: `extends` key in config file is now used for extending other config files", # noqa
2727
"fix(setup.py): future is now required for every python version",
2828
]
2929

30+
MAJOR_INCREMENTS_BREAKING_CHANGE_ALT_CC = [
31+
"feat(cli): added version",
32+
"docs(README): motivation",
33+
"BREAKING-CHANGE: `extends` key in config file is now used for extending other config files", # noqa
34+
"fix(setup.py): future is now required for every python version",
35+
]
36+
37+
MAJOR_INCREMENTS_EXCLAMATION_CC = [
38+
"feat(cli)!: added version",
39+
"docs(README): motivation",
40+
"fix(setup.py): future is now required for every python version",
41+
]
42+
3043
PATCH_INCREMENTS_SVE = ["readme motivation PATCH", "fix setup.py PATCH"]
3144

3245
MINOR_INCREMENTS_SVE = [
@@ -51,7 +64,9 @@
5164
(
5265
(PATCH_INCREMENTS_CC, "PATCH"),
5366
(MINOR_INCREMENTS_CC, "MINOR"),
54-
(MAJOR_INCREMENTS_CC, "MAJOR"),
67+
(MAJOR_INCREMENTS_BREAKING_CHANGE_CC, "MAJOR"),
68+
(MAJOR_INCREMENTS_BREAKING_CHANGE_ALT_CC, "MAJOR"),
69+
(MAJOR_INCREMENTS_EXCLAMATION_CC, "MAJOR"),
5570
(NONE_INCREMENT_CC, None),
5671
),
5772
)

0 commit comments

Comments
 (0)