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 4a0b84d

Browse files
refactor(command_bump): use bump rules
1 parent facb51c commit 4a0b84d

File tree

3 files changed

+22
-185
lines changed

3 files changed

+22
-185
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/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.

0 commit comments

Comments
(0)

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