Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be7cb23

Browse files
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
import os
44
import re
5-
from collections import OrderedDict
65
from glob import iglob
76
from logging import getLogger
87
from string import Template
9-
from typing import cast
108

119
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message, encoding
1210
from commitizen.exceptions import CurrentVersionNotFoundError
13-
from commitizen.git import GitCommit, smart_open
14-
from commitizen.version_schemes import Increment, Version
11+
from commitizen.git import smart_open
12+
from commitizen.version_schemes import Version
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-
def find_increment(
23-
commits: list[GitCommit], regex: str, increments_map: dict | OrderedDict
24-
) -> Increment | None:
25-
if isinstance(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-
for commit in commits:
34-
for message in commit.message.split("\n"):
35-
result = select_pattern.search(message)
36-
37-
if result:
38-
found_keyword = result.group(1)
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 new_increment is None:
46-
logger.debug(
47-
f"no increment needed for '{found_keyword}' in '{message}'"
48-
)
49-
50-
if VERSION_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-
if increment == MAJOR:
57-
break
58-
59-
return cast(Increment, increment)
60-
61-
6219
def update_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
class ConventionalCommitBumpRule(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
def get_increment(
5355
self, commit_message: str, major_version_zero: bool
@@ -56,8 +58,8 @@ def get_increment(
5658
return None
5759

5860
change_type = m.group("change_type")
59-
if m.group("bang") or change_type=="BREAKING CHANGE":
60-
return "MAJOR" if major_version_zero else "MINOR"
61+
if m.group("bang") or self._RE_BREAKING_CHANGE.match(change_type):
62+
return "MINOR" if major_version_zero else "MAJOR"
6163

6264
if change_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
import questionary
88

99
from commitizen import bump, factory, git, hooks, out
10-
from commitizen.bump_rule import find_increment_by_callable
10+
from commitizen.bump_rule import OldSchoolBumpRule, find_increment_by_callable
1111
from commitizen.changelog_formats import get_changelog_format
1212
from commitizen.commands.changelog import Changelog
1313
from commitizen.config import BaseConfig
@@ -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-
if rule := self.cz.bump_rule:
128-
return find_increment_by_callable(
129-
(commit.message for commit in commits),
130-
lambda x: rule.get_increment(x, is_major_version_zero),
131-
)
132-
133-
bump_map = (
134-
self.cz.bump_map_major_version_zero
135-
if is_major_version_zero
136-
else self.cz.bump_map
127+
# Fallback to old school bump rule if no bump rule is provided
128+
rule = self.cz.bump_rule or OldSchoolBumpRule(
129+
*self._get_validated_cz_bump(),
130+
)
131+
return find_increment_by_callable(
132+
(commit.message for commit in commits),
133+
lambda x: rule.get_increment(x, is_major_version_zero),
137134
)
138-
bump_pattern = self.cz.bump_pattern
139135

140-
if not bump_map or not bump_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+
if not bump_pattern or not bump_map or not bump_map_major_version_zero:
141144
raise NoPatternMapError(
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+
return cast(
149+
tuple[str, dict[str, Increment], dict[str, Increment]],
150+
(bump_pattern, bump_map, bump_map_major_version_zero),
146151
)
147-
return increment
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
assert bump_rule.get_increment("refactor: restructure code", True) == PATCH
4040

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

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

4949
def test_commit_with_scope(self, bump_rule):
5050
assert bump_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
def test_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-
assert find_increment_by_callable(commit_messages, get_increment) == MINOR
132+
assert find_increment_by_callable(commit_messages, get_increment) == MAJOR
133133

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

140140
def test_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, lambda x: bump_rule.get_increment(x, True)
161161
)
162-
== MAJOR
162+
== MINOR
163163
)
164164

165165
def test_mixed_commit_types(self, get_increment):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /