Skip to content

Commit 6109d98

Browse files
committed
refactor(bump_rule): make bump_rule a property
1 parent 86e35b7 commit 6109d98

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

commitizen/commands/bump.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from commitizen import bump, factory, git, hooks, out
1010
from commitizen.bump_rule import (
11-
CustomBumpRule,
1211
SemVerIncrement,
1312
)
1413
from commitizen.changelog_formats import get_changelog_format
@@ -24,7 +23,6 @@
2423
InvalidManualVersion,
2524
NoCommitsFoundError,
2625
NoneIncrementExit,
27-
NoPatternMapError,
2826
NotAGitProjectError,
2927
NotAllowed,
3028
NoVersionSpecifiedError,
@@ -124,34 +122,11 @@ def is_initial_tag(
124122

125123
def find_increment(self, commits: list[git.GitCommit]) -> SemVerIncrement | None:
126124
# Update the bump map to ensure major version doesn't increment.
127-
is_major_version_zero: bool = self.bump_settings["major_version_zero"]
128-
129-
# Fallback to custom bump rule if no bump rule is provided
130-
rule = self.cz.bump_rule or CustomBumpRule(
131-
*self._get_validated_cz_bump(),
132-
)
125+
is_major_version_zero = bool(self.bump_settings["major_version_zero"])
133126

134127
return SemVerIncrement.get_highest_by_messages(
135128
(commit.message for commit in commits),
136-
lambda x: rule.get_increment(x, is_major_version_zero),
137-
)
138-
139-
def _get_validated_cz_bump(
140-
self,
141-
) -> tuple[str, dict[str, SemVerIncrement], dict[str, SemVerIncrement]]:
142-
"""For fixing the type errors"""
143-
bump_pattern = self.cz.bump_pattern
144-
bump_map = self.cz.bump_map
145-
bump_map_major_version_zero = self.cz.bump_map_major_version_zero
146-
if not bump_pattern or not bump_map or not bump_map_major_version_zero:
147-
raise NoPatternMapError(
148-
f"'{self.config.settings['name']}' rule does not support bump"
149-
)
150-
151-
return (
152-
bump_pattern,
153-
SemVerIncrement.safe_cast_dict(bump_map),
154-
SemVerIncrement.safe_cast_dict(bump_map_major_version_zero),
129+
lambda x: self.cz.bump_rule.get_increment(x, is_major_version_zero),
155130
)
156131

157132
def __call__(self) -> None: # noqa: C901

commitizen/cz/base.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
from abc import ABCMeta, abstractmethod
44
from collections.abc import Iterable
5+
from functools import cached_property
56
from typing import Any, Callable, Protocol
67

78
from jinja2 import BaseLoader, PackageLoader
89
from prompt_toolkit.styles import Style, merge_styles
910

1011
from commitizen import git
11-
from commitizen.bump_rule import BumpRule
12+
from commitizen.bump_rule import BumpRule, CustomBumpRule, SemVerIncrement
1213
from commitizen.config.base_config import BaseConfig
1314
from commitizen.defaults import Questions
15+
from commitizen.exceptions import NoPatternMapError
1416

1517

1618
class MessageBuilderHook(Protocol):
@@ -26,9 +28,9 @@ def __call__(
2628

2729

2830
class BaseCommitizen(metaclass=ABCMeta):
29-
bump_rule: BumpRule | None = None
31+
_bump_rule: BumpRule | None = None
3032

31-
# TODO: deprecate these
33+
# TODO: decide if these should be removed
3234
bump_pattern: str | None = None
3335
bump_map: dict[str, str] | None = None
3436
bump_map_major_version_zero: dict[str, str] | None = None
@@ -89,6 +91,25 @@ def style(self):
8991
]
9092
)
9193

94+
@cached_property
95+
def bump_rule(self) -> BumpRule:
96+
if self._bump_rule:
97+
return self._bump_rule
98+
99+
# Fallback to custom bump rule if no bump rule is provided
100+
bump_pattern = self.bump_pattern
101+
bump_map = self.bump_map
102+
bump_map_major_version_zero = self.bump_map_major_version_zero
103+
if not bump_pattern or not bump_map or not bump_map_major_version_zero:
104+
raise NoPatternMapError(
105+
f"Rule does not support bump: {bump_pattern=}, {bump_map=}, {bump_map_major_version_zero=}"
106+
)
107+
return CustomBumpRule(
108+
bump_pattern,
109+
SemVerIncrement.safe_cast_dict(bump_map),
110+
SemVerIncrement.safe_cast_dict(bump_map_major_version_zero),
111+
)
112+
92113
def example(self) -> str:
93114
"""Example of the commit message."""
94115
raise NotImplementedError("Not Implemented yet")

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def parse_subject(text):
2929

3030

3131
class ConventionalCommitsCz(BaseCommitizen):
32-
bump_rule = ConventionalCommitBumpRule()
32+
_bump_rule = ConventionalCommitBumpRule()
3333

3434
bump_pattern = defaults.BUMP_PATTERN
3535
bump_map = defaults.BUMP_MAP

0 commit comments

Comments
 (0)