Skip to content

Commit be7cb23

Browse files
committed
refactor(command_bump): use bump rules
1 parent facb51c commit be7cb23

File tree

5 files changed

+38
-199
lines changed

5 files changed

+38
-199
lines changed

‎commitizen/bump.py‎

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,20 @@
22

33
importos
44
importre
5-
fromcollectionsimportOrderedDict
65
fromglobimportiglob
76
fromloggingimportgetLogger
87
fromstringimportTemplate
9-
fromtypingimportcast
108

119
fromcommitizen.defaultsimportMAJOR, MINOR, PATCH, bump_message, encoding
1210
fromcommitizen.exceptionsimportCurrentVersionNotFoundError
13-
fromcommitizen.gitimportGitCommit, smart_open
14-
fromcommitizen.version_schemesimportIncrement, Version
11+
fromcommitizen.gitimportsmart_open
12+
fromcommitizen.version_schemesimportVersion
1513

1614
VERSION_TYPES= [None, PATCH, MINOR, MAJOR]
1715

1816
logger=getLogger("commitizen")
1917

2018

21-
# TODO: replace this with find_increment_by_callable?
22-
deffind_increment(
23-
commits: list[GitCommit], regex: str, increments_map: dict|OrderedDict
24-
) ->Increment|None:
25-
ifisinstance(increments_map, dict):
26-
increments_map=OrderedDict(increments_map)
27-
28-
# Most important cases are major and minor.
29-
# Everything else will be considered patch.
30-
select_pattern=re.compile(regex)
31-
increment: str|None=None
32-
33-
forcommitincommits:
34-
formessageincommit.message.split("\n"):
35-
result=select_pattern.search(message)
36-
37-
ifresult:
38-
found_keyword=result.group(1)
39-
new_increment=None
40-
formatch_patterninincrements_map.keys():
41-
ifre.match(match_pattern, found_keyword):
42-
new_increment=increments_map[match_pattern]
43-
break
44-
45-
ifnew_incrementisNone:
46-
logger.debug(
47-
f"no increment needed for '{found_keyword}' in '{message}'"
48-
)
49-
50-
ifVERSION_TYPES.index(increment) <VERSION_TYPES.index(new_increment):
51-
logger.debug(
52-
f"increment detected is '{new_increment}' due to '{found_keyword}' in '{message}'"
53-
)
54-
increment=new_increment
55-
56-
ifincrement==MAJOR:
57-
break
58-
59-
returncast(Increment, increment)
60-
61-
6219
defupdate_version_in_files(
6320
current_version: str,
6421
new_version: str,

‎commitizen/bump_rule.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def get_increment(
4848

4949
classConventionalCommitBumpRule(BumpRule):
5050
_PATCH_CHANGE_TYPES=set(["fix", "perf", "refactor"])
51+
_BREAKING_CHANGE=r"BREAKING[\-\ ]CHANGE"
52+
_RE_BREAKING_CHANGE=re.compile(_BREAKING_CHANGE)
5153

5254
defget_increment(
5355
self, commit_message: str, major_version_zero: bool
@@ -56,8 +58,8 @@ def get_increment(
5658
returnNone
5759

5860
change_type=m.group("change_type")
59-
ifm.group("bang") orchange_type=="BREAKING CHANGE":
60-
return"MAJOR"ifmajor_version_zeroelse"MINOR"
61+
ifm.group("bang") orself._RE_BREAKING_CHANGE.match(change_type):
62+
return"MINOR"ifmajor_version_zeroelse"MAJOR"
6163

6264
ifchange_type=="feat":
6365
return"MINOR"
@@ -70,7 +72,7 @@ def get_increment(
7072
@cached_property
7173
def_head_pattern(self) ->re.Pattern:
7274
change_types= [
73-
r"BREAKING[\-\ ]CHANGE",
75+
self._BREAKING_CHANGE,
7476
"fix",
7577
"feat",
7678
"docs",

‎commitizen/commands/bump.py‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
importquestionary
88

99
fromcommitizenimportbump, factory, git, hooks, out
10-
fromcommitizen.bump_ruleimportfind_increment_by_callable
10+
fromcommitizen.bump_ruleimportOldSchoolBumpRule, find_increment_by_callable
1111
fromcommitizen.changelog_formatsimportget_changelog_format
1212
fromcommitizen.commands.changelogimportChangelog
1313
fromcommitizen.configimportBaseConfig
@@ -124,27 +124,31 @@ def find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
124124
# Update the bump map to ensure major version doesn't increment.
125125
is_major_version_zero: bool=self.bump_settings["major_version_zero"]
126126

127-
ifrule:=self.cz.bump_rule:
128-
returnfind_increment_by_callable(
129-
(commit.messageforcommitincommits),
130-
lambdax: rule.get_increment(x, is_major_version_zero),
131-
)
132-
133-
bump_map= (
134-
self.cz.bump_map_major_version_zero
135-
ifis_major_version_zero
136-
elseself.cz.bump_map
127+
# Fallback to old school bump rule if no bump rule is provided
128+
rule=self.cz.bump_ruleorOldSchoolBumpRule(
129+
*self._get_validated_cz_bump(),
130+
)
131+
returnfind_increment_by_callable(
132+
(commit.messageforcommitincommits),
133+
lambdax: rule.get_increment(x, is_major_version_zero),
137134
)
138-
bump_pattern=self.cz.bump_pattern
139135

140-
ifnotbump_mapornotbump_pattern:
136+
def_get_validated_cz_bump(
137+
self,
138+
) ->tuple[str, dict[str, Increment], dict[str, Increment]]:
139+
"""For fixing the type errors"""
140+
bump_pattern=self.cz.bump_pattern
141+
bump_map=self.cz.bump_map
142+
bump_map_major_version_zero=self.cz.bump_map_major_version_zero
143+
ifnotbump_patternornotbump_mapornotbump_map_major_version_zero:
141144
raiseNoPatternMapError(
142145
f"'{self.config.settings['name']}' rule does not support bump"
143146
)
144-
increment=bump.find_increment(
145-
commits, regex=bump_pattern, increments_map=bump_map
147+
148+
returncast(
149+
tuple[str, dict[str, Increment], dict[str, Increment]],
150+
(bump_pattern, bump_map, bump_map_major_version_zero),
146151
)
147-
returnincrement
148152

149153
def__call__(self) ->None: # noqa: C901
150154
"""Steps executed to bump."""

‎tests/test_bump_find_increment.py‎

Lines changed: 0 additions & 124 deletions
This file was deleted.

‎tests/test_bump_rule.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def test_refactor_commit(self, bump_rule):
3939
assertbump_rule.get_increment("refactor: restructure code", True) ==PATCH
4040

4141
deftest_breaking_change_with_bang(self, bump_rule):
42-
assertbump_rule.get_increment("feat!: breaking change", False) ==MINOR
43-
assertbump_rule.get_increment("feat!: breaking change", True) ==MAJOR
42+
assertbump_rule.get_increment("feat!: breaking change", False) ==MAJOR
43+
assertbump_rule.get_increment("feat!: breaking change", True) ==MINOR
4444

4545
deftest_breaking_change_type(self, bump_rule):
46-
assertbump_rule.get_increment("BREAKING CHANGE: major change", False) ==MINOR
47-
assertbump_rule.get_increment("BREAKING CHANGE: major change", True) ==MAJOR
46+
assertbump_rule.get_increment("BREAKING CHANGE: major change", False) ==MAJOR
47+
assertbump_rule.get_increment("BREAKING CHANGE: major change", True) ==MINOR
4848

4949
deftest_commit_with_scope(self, bump_rule):
5050
assertbump_rule.get_increment("feat(api): add new endpoint", False) ==MINOR
@@ -72,25 +72,25 @@ def test_commit_with_complex_scopes(self, bump_rule):
7272
# Test with breaking changes and scopes
7373
assert (
7474
bump_rule.get_increment("feat(api)!: remove deprecated endpoints", False)
75-
==MINOR
75+
==MAJOR
7676
)
7777
assert (
7878
bump_rule.get_increment("feat(api)!: remove deprecated endpoints", True)
79-
==MAJOR
79+
==MINOR
8080
)
8181

8282
# Test with BREAKING CHANGE and scopes
8383
assert (
8484
bump_rule.get_increment(
8585
"BREAKING CHANGE(api): remove deprecated endpoints", False
8686
)
87-
==MINOR
87+
==MAJOR
8888
)
8989
assert (
9090
bump_rule.get_increment(
9191
"BREAKING CHANGE(api): remove deprecated endpoints", True
9292
)
93-
==MAJOR
93+
==MINOR
9494
)
9595

9696
deftest_invalid_commit_message(self, bump_rule):
@@ -129,13 +129,13 @@ def test_breaking_change(self, get_increment):
129129
"feat: new feature",
130130
"feat!: breaking change",
131131
]
132-
assertfind_increment_by_callable(commit_messages, get_increment) ==MINOR
132+
assertfind_increment_by_callable(commit_messages, get_increment) ==MAJOR
133133

134134
deftest_multi_line_commit(self, get_increment):
135135
commit_messages= [
136136
"feat: new feature\n\nBREAKING CHANGE: major change",
137137
]
138-
assertfind_increment_by_callable(commit_messages, get_increment) ==MINOR
138+
assertfind_increment_by_callable(commit_messages, get_increment) ==MAJOR
139139

140140
deftest_no_increment_needed(self, get_increment):
141141
commit_messages= [
@@ -159,7 +159,7 @@ def test_major_version_zero(self):
159159
find_increment_by_callable(
160160
commit_messages, lambdax: bump_rule.get_increment(x, True)
161161
)
162-
==MAJOR
162+
==MINOR
163163
)
164164

165165
deftest_mixed_commit_types(self, get_increment):

0 commit comments

Comments
(0)