Skip to content

Commit 57205e4

Browse files
committed
refactor(bump_rule): deprecate wip
1 parent 4dea3fa commit 57205e4

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

commitizen/bump_rule.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import re
4+
from collections.abc import Iterable
45
from functools import cached_property
56
from typing import Callable, Protocol
67

@@ -10,7 +11,7 @@
1011

1112

1213
def find_increment_by_callable(
13-
commit_messages: list[str], get_increment: Callable[[str], Increment | None]
14+
commit_messages: Iterable[str], get_increment: Callable[[str], Increment | None]
1415
) -> Increment | None:
1516
"""Find the highest version increment from a list of messages.
1617
@@ -29,7 +30,7 @@ def find_increment_by_callable(
2930
3031
Example:
3132
>>> commit_messages = ["feat: new feature", "fix: bug fix"]
32-
>>> rule = DefaultBumpRule()
33+
>>> rule = ConventionalCommitBumpRule()
3334
>>> find_increment_by_callable(commit_messages, lambda x: rule.get_increment(x, False))
3435
'MINOR'
3536
"""
@@ -44,7 +45,7 @@ def get_increment(
4445
) -> Increment | None: ...
4546

4647

47-
class DefaultBumpRule(BumpRule):
48+
class ConventionalCommitBumpRule(BumpRule):
4849
_PATCH_CHANGE_TYPES = set(["fix", "perf", "refactor"])
4950

5051
def get_increment(
@@ -85,10 +86,4 @@ def _head_pattern(self) -> re.Pattern:
8586
return re.compile(f"^{re_change_type}{re_scope}{re_bang}:")
8687

8788

88-
class CustomBumpRule(BumpRule):
89-
"""TODO: Implement"""
90-
91-
def get_increment(
92-
self, commit_message: str, major_version_zero: bool
93-
) -> Increment | None:
94-
return None
89+
# TODO: Implement CustomBumpRule

commitizen/commands/bump.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +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
1011
from commitizen.changelog_formats import get_changelog_format
1112
from commitizen.commands.changelog import Changelog
1213
from commitizen.config import BaseConfig
@@ -122,7 +123,13 @@ def is_initial_tag(
122123
def find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
123124
# Update the bump map to ensure major version doesn't increment.
124125
is_major_version_zero: bool = self.bump_settings["major_version_zero"]
125-
# self.cz.bump_map = defaults.bump_map_major_version_zero
126+
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+
126133
bump_map = (
127134
self.cz.bump_map_major_version_zero
128135
if is_major_version_zero

commitizen/cz/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from prompt_toolkit.styles import Style, merge_styles
99

1010
from commitizen import git
11+
from commitizen.bump_rule import BumpRule
1112
from commitizen.config.base_config import BaseConfig
1213
from commitizen.defaults import Questions
1314

@@ -25,9 +26,13 @@ def __call__(
2526

2627

2728
class BaseCommitizen(metaclass=ABCMeta):
29+
bump_rule: BumpRule | None = None
30+
31+
# TODO: deprecate these
2832
bump_pattern: str | None = None
2933
bump_map: dict[str, str] | None = None
3034
bump_map_major_version_zero: dict[str, str] | None = None
35+
3136
default_style_config: list[tuple[str, str]] = [
3237
("qmark", "fg:#ff9d00 bold"),
3338
("question", "bold"),

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33

44
from commitizen import defaults
5+
from commitizen.bump_rule import ConventionalCommitBumpRule
56
from commitizen.cz.base import BaseCommitizen
67
from commitizen.cz.utils import multiple_line_breaker, required_validator
78
from commitizen.defaults import Questions
@@ -28,6 +29,8 @@ def parse_subject(text):
2829

2930

3031
class ConventionalCommitsCz(BaseCommitizen):
32+
bump_rule = ConventionalCommitBumpRule()
33+
3134
bump_pattern = defaults.BUMP_PATTERN
3235
bump_map = defaults.BUMP_MAP
3336
bump_map_major_version_zero = defaults.BUMP_MAP_MAJOR_VERSION_ZERO

tests/test_bump_rule.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import pytest
22

3-
from commitizen.bump_rule import DefaultBumpRule, find_increment_by_callable
3+
from commitizen.bump_rule import ConventionalCommitBumpRule, find_increment_by_callable
44
from commitizen.defaults import MAJOR, MINOR, PATCH
55

66

77
@pytest.fixture
88
def bump_rule():
9-
return DefaultBumpRule()
9+
return ConventionalCommitBumpRule()
1010

1111

12-
class TestDefaultBumpRule:
12+
class TestConventionalCommitBumpRule:
1313
def test_feat_commit(self, bump_rule):
1414
assert bump_rule.get_increment("feat: add new feature", False) == MINOR
1515
assert bump_rule.get_increment("feat: add new feature", True) == MINOR
@@ -137,7 +137,7 @@ def test_empty_commits(self, get_increment):
137137
assert find_increment_by_callable(commit_messages, get_increment) is None
138138

139139
def test_major_version_zero(self):
140-
bump_rule = DefaultBumpRule()
140+
bump_rule = ConventionalCommitBumpRule()
141141

142142
commit_messages = [
143143
"feat!: breaking change",

0 commit comments

Comments
 (0)