Skip to content

Commit c77d672

Browse files
committed
refactor(bump_rule): renaming
1 parent d9292b1 commit c77d672

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

commitizen/bump_rule.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@ def safe_cast_dict(cls, d: dict[str, Any]) -> dict[str, SemVerIncrement]:
3434
if v is not None
3535
}
3636

37+
@staticmethod
38+
def get_highest_by_messages(
39+
commit_messages: Iterable[str],
40+
get_increment: Callable[[str], SemVerIncrement | None],
41+
) -> SemVerIncrement | None:
42+
"""Find the highest version increment from a list of messages.
43+
44+
This function processes a list of messages and determines the highest version
45+
increment needed based on the commit messages. It splits multi-line commit messages
46+
and evaluates each line using the provided get_increment callable.
47+
48+
Args:
49+
commit_messages: A list of messages to analyze.
50+
get_increment: A callable that takes a commit message string and returns an
51+
SemVerIncrement value (MAJOR, MINOR, PATCH) or None if no increment is needed.
52+
53+
Returns:
54+
The highest version increment needed (MAJOR, MINOR, PATCH) or None if no
55+
increment is needed. The order of precedence is MAJOR > MINOR > PATCH.
56+
57+
Example:
58+
>>> commit_messages = ["feat: new feature", "fix: bug fix"]
59+
>>> rule = ConventionalCommitBumpRule()
60+
>>> SemVerIncrement.get_highest_by_messages(commit_messages, lambda x: rule.get_increment(x, False))
61+
'MINOR'
62+
"""
63+
lines = (line for message in commit_messages for line in message.split("\n"))
64+
increments = map(get_increment, lines)
65+
return _find_highest_increment(increments)
66+
3767

3868
_VERSION_ORDERING = dict(
3969
zip(
@@ -43,36 +73,6 @@ def safe_cast_dict(cls, d: dict[str, Any]) -> dict[str, SemVerIncrement]:
4373
)
4474

4575

46-
def find_increment_by_callable(
47-
commit_messages: Iterable[str],
48-
get_increment: Callable[[str], SemVerIncrement | None],
49-
) -> SemVerIncrement | None:
50-
"""Find the highest version increment from a list of messages.
51-
52-
This function processes a list of messages and determines the highest version
53-
increment needed based on the commit messages. It splits multi-line commit messages
54-
and evaluates each line using the provided get_increment callable.
55-
56-
Args:
57-
commit_messages: A list of messages to analyze.
58-
get_increment: A callable that takes a commit message string and returns an
59-
SemVerIncrement value (MAJOR, MINOR, PATCH) or None if no increment is needed.
60-
61-
Returns:
62-
The highest version increment needed (MAJOR, MINOR, PATCH) or None if no
63-
increment is needed. The order of precedence is MAJOR > MINOR > PATCH.
64-
65-
Example:
66-
>>> commit_messages = ["feat: new feature", "fix: bug fix"]
67-
>>> rule = ConventionalCommitBumpRule()
68-
>>> find_increment_by_callable(commit_messages, lambda x: rule.get_increment(x, False))
69-
'MINOR'
70-
"""
71-
lines = (line for message in commit_messages for line in message.split("\n"))
72-
increments = map(get_increment, lines)
73-
return _find_highest_increment(increments)
74-
75-
7676
def _find_highest_increment(
7777
increments: Iterable[SemVerIncrement | None],
7878
) -> SemVerIncrement | None:

commitizen/commands/bump.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from commitizen.bump_rule import (
1111
CustomBumpRule,
1212
SemVerIncrement,
13-
find_increment_by_callable,
1413
)
1514
from commitizen.changelog_formats import get_changelog_format
1615
from commitizen.commands.changelog import Changelog
@@ -132,7 +131,7 @@ def find_increment(self, commits: list[git.GitCommit]) -> SemVerIncrement | None
132131
*self._get_validated_cz_bump(),
133132
)
134133

135-
return find_increment_by_callable(
134+
return SemVerIncrement.get_highest_by_messages(
136135
(commit.message for commit in commits),
137136
lambda x: rule.get_increment(x, is_major_version_zero),
138137
)

tests/test_bump_rule.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
CustomBumpRule,
66
SemVerIncrement,
77
_find_highest_increment,
8-
find_increment_by_callable,
98
)
109
from commitizen.defaults import (
1110
BUMP_MAP,
@@ -186,7 +185,7 @@ def get_increment(self, bump_rule):
186185
def test_single_commit(self, get_increment):
187186
commit_messages = ["feat: add new feature"]
188187
assert (
189-
find_increment_by_callable(commit_messages, get_increment)
188+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
190189
== SemVerIncrement.MINOR
191190
)
192191

@@ -197,7 +196,7 @@ def test_multiple_commits(self, get_increment):
197196
"docs: update readme",
198197
]
199198
assert (
200-
find_increment_by_callable(commit_messages, get_increment)
199+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
201200
== SemVerIncrement.MINOR
202201
)
203202

@@ -207,7 +206,7 @@ def test_breaking_change(self, get_increment):
207206
"feat!: breaking change",
208207
]
209208
assert (
210-
find_increment_by_callable(commit_messages, get_increment)
209+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
211210
== SemVerIncrement.MAJOR
212211
)
213212

@@ -216,7 +215,7 @@ def test_multi_line_commit(self, get_increment):
216215
"feat: new feature\n\nBREAKING CHANGE: major change",
217216
]
218217
assert (
219-
find_increment_by_callable(commit_messages, get_increment)
218+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
220219
== SemVerIncrement.MAJOR
221220
)
222221

@@ -225,11 +224,17 @@ def test_no_increment_needed(self, get_increment):
225224
"docs: update documentation",
226225
"style: format code",
227226
]
228-
assert find_increment_by_callable(commit_messages, get_increment) is None
227+
assert (
228+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
229+
is None
230+
)
229231

230232
def test_empty_commits(self, get_increment):
231233
commit_messages = []
232-
assert find_increment_by_callable(commit_messages, get_increment) is None
234+
assert (
235+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
236+
is None
237+
)
233238

234239
def test_major_version_zero(self):
235240
bump_rule = ConventionalCommitBumpRule()
@@ -239,7 +244,7 @@ def test_major_version_zero(self):
239244
"BREAKING CHANGE: major change",
240245
]
241246
assert (
242-
find_increment_by_callable(
247+
SemVerIncrement.get_highest_by_messages(
243248
commit_messages, lambda x: bump_rule.get_increment(x, True)
244249
)
245250
== SemVerIncrement.MINOR
@@ -253,7 +258,7 @@ def test_mixed_commit_types(self, get_increment):
253258
"refactor: restructure code",
254259
]
255260
assert (
256-
find_increment_by_callable(commit_messages, get_increment)
261+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
257262
== SemVerIncrement.MINOR
258263
)
259264

@@ -263,7 +268,7 @@ def test_commit_with_scope(self, get_increment):
263268
"fix(ui): fix button alignment",
264269
]
265270
assert (
266-
find_increment_by_callable(commit_messages, get_increment)
271+
SemVerIncrement.get_highest_by_messages(commit_messages, get_increment)
267272
== SemVerIncrement.MINOR
268273
)
269274

@@ -393,7 +398,7 @@ def test_with_find_increment_by_callable(self, custom_bump_rule):
393398
"docs: update readme [MINOR]",
394399
]
395400
assert (
396-
find_increment_by_callable(
401+
SemVerIncrement.get_highest_by_messages(
397402
commit_messages, lambda x: custom_bump_rule.get_increment(x, False)
398403
)
399404
== SemVerIncrement.MAJOR
@@ -577,7 +582,7 @@ def test_with_find_increment_by_callable(self, custom_bump_rule):
577582
"perf: improve performance",
578583
]
579584
assert (
580-
find_increment_by_callable(
585+
SemVerIncrement.get_highest_by_messages(
581586
commit_messages, lambda x: custom_bump_rule.get_increment(x, False)
582587
)
583588
== SemVerIncrement.MAJOR

0 commit comments

Comments
 (0)