-
-
Notifications
You must be signed in to change notification settings - Fork 282
fix(bump_rule): add BumpRule
, enum SemVerIncrement
and Prerelease
#1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
03aa596
4dea3fa
57205e4
afa0063
facb51c
be7cb23
88efb61
a4d6eaa
39f8bc7
42d965e
166f768
565ca9e
d9292b1
c77d672
17b82ad
7b3b9c6
0265ad5
5e81873
db8e93a
ac709ec
4b72cf8
83d6bde
86e35b7
6109d98
537904e
5e612bf
d820bf7
261e754
30a36da
47c994c
c45c96f
dd85165
7bf082d
2c9af08
0245469
2eaf52c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from collections.abc import Iterable | ||
from functools import cached_property | ||
from typing import Callable, Protocol | ||
|
||
from commitizen.exceptions import NoPatternMapError | ||
from commitizen.version_schemes import Increment | ||
|
||
_VERSION_ORDERING = dict(zip((None, "PATCH", "MINOR", "MAJOR"), range(4))) | ||
|
||
|
||
def find_increment_by_callable( | ||
bearomorphism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
commit_messages: Iterable[str], get_increment: Callable[[str], Increment | None] | ||
) -> Increment | None: | ||
"""Find the highest version increment from a list of messages. | ||
|
||
This function processes a list of messages and determines the highest version | ||
increment needed based on the commit messages. It splits multi-line commit messages | ||
and evaluates each line using the provided get_increment callable. | ||
|
||
Args: | ||
commit_messages: A list of messages to analyze. | ||
get_increment: A callable that takes a commit message string and returns an | ||
Increment value (MAJOR, MINOR, PATCH) or None if no increment is needed. | ||
|
||
Returns: | ||
The highest version increment needed (MAJOR, MINOR, PATCH) or None if no | ||
increment is needed. The order of precedence is MAJOR > MINOR > PATCH. | ||
|
||
Example: | ||
>>> commit_messages = ["feat: new feature", "fix: bug fix"] | ||
>>> rule = ConventionalCommitBumpRule() | ||
>>> find_increment_by_callable(commit_messages, lambda x: rule.get_increment(x, False)) | ||
'MINOR' | ||
""" | ||
lines = (line for message in commit_messages for line in message.split("\n")) | ||
increments = map(get_increment, lines) | ||
return max(increments, key=lambda x: _VERSION_ORDERING[x], default=None) | ||
|
||
|
||
class BumpRule(Protocol): | ||
def get_increment( | ||
self, commit_message: str, major_version_zero: bool | ||
) -> Increment | None: | ||
"""Determine the version increment based on a commit message. | ||
|
||
This method analyzes a commit message to determine what kind of version increment | ||
is needed according to the Conventional Commits specification. It handles special | ||
cases for breaking changes and respects the major_version_zero flag. | ||
|
||
Args: | ||
commit_message: The commit message to analyze. Should follow conventional commit format. | ||
major_version_zero: If True, breaking changes will result in a MINOR version bump | ||
instead of MAJOR. This is useful for projects in 0.x.x versions. | ||
|
||
Returns: | ||
Increment | None: The type of version increment needed: | ||
- "MAJOR": For breaking changes when major_version_zero is False | ||
- "MINOR": For breaking changes when major_version_zero is True, or for new features | ||
- "PATCH": For bug fixes, performance improvements, or refactors | ||
- None: For commits that don't require a version bump (docs, style, etc.) | ||
""" | ||
... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder how to bypass this Codecov warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I see how |
||
|
||
|
||
class ConventionalCommitBumpRule(BumpRule): | ||
_PATCH_CHANGE_TYPES = set(["fix", "perf", "refactor"]) | ||
_BREAKING_CHANGE = r"BREAKING[\-\ ]CHANGE" | ||
_RE_BREAKING_CHANGE = re.compile(_BREAKING_CHANGE) | ||
|
||
def get_increment( | ||
self, commit_message: str, major_version_zero: bool | ||
) -> Increment | None: | ||
if not (m := self._head_pattern.match(commit_message)): | ||
return None | ||
|
||
change_type = m.group("change_type") | ||
if m.group("bang") or self._RE_BREAKING_CHANGE.match(change_type): | ||
return "MINOR" if major_version_zero else "MAJOR" | ||
|
||
if change_type == "feat": | ||
return "MINOR" | ||
|
||
if change_type in self._PATCH_CHANGE_TYPES: | ||
return "PATCH" | ||
|
||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is based on |
||
|
||
@cached_property | ||
def _head_pattern(self) -> re.Pattern: | ||
change_types = [ | ||
self._BREAKING_CHANGE, | ||
"fix", | ||
"feat", | ||
"docs", | ||
"style", | ||
"refactor", | ||
"perf", | ||
"test", | ||
"build", | ||
"ci", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the pattern matching more strict. |
||
] | ||
re_change_type = r"(?P<change_type>" + "|".join(change_types) + r")" | ||
re_scope = r"(?P<scope>\(.+\))?" | ||
re_bang = r"(?P<bang>!)?" | ||
return re.compile(f"^{re_change_type}{re_scope}{re_bang}:") | ||
|
||
|
||
class OldSchoolBumpRule(BumpRule): | ||
"""TODO: rename?""" | ||
bearomorphism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__( | ||
self, | ||
bump_pattern: str, | ||
bump_map: dict[str, Increment], | ||
bump_map_major_version_zero: dict[str, Increment], | ||
): | ||
if not bump_map or not bump_pattern or not bump_map_major_version_zero: | ||
raise NoPatternMapError( | ||
f"Invalid bump rule: {bump_pattern=} and {bump_map=} and {bump_map_major_version_zero=}" | ||
) | ||
|
||
self.bump_pattern = re.compile(bump_pattern) | ||
self.bump_map = bump_map | ||
self.bump_map_major_version_zero = bump_map_major_version_zero | ||
|
||
def get_increment( | ||
self, commit_message: str, major_version_zero: bool | ||
) -> Increment | None: | ||
if not (m := self.bump_pattern.search(commit_message)): | ||
return None | ||
|
||
bump_map = ( | ||
self.bump_map_major_version_zero if major_version_zero else self.bump_map | ||
) | ||
|
||
found_keyword = m.group(1) | ||
for match_pattern, increment in bump_map.items(): | ||
if re.match(match_pattern, found_keyword): | ||
return increment | ||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from prompt_toolkit.styles import Style, merge_styles | ||
|
||
from commitizen import git | ||
from commitizen.bump_rule import BumpRule | ||
from commitizen.config.base_config import BaseConfig | ||
from commitizen.defaults import Questions | ||
|
||
|
@@ -25,9 +26,13 @@ def __call__( | |
|
||
|
||
class BaseCommitizen(metaclass=ABCMeta): | ||
bump_rule: BumpRule | None = None | ||
|
||
# TODO: deprecate these | ||
bump_pattern: str | None = None | ||
bump_map: dict[str, str] | None = None | ||
bump_map_major_version_zero: dict[str, str] | None = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how to address these fields if we decide to check in the |
||
|
||
default_style_config: list[tuple[str, str]] = [ | ||
("qmark", "fg:#ff9d00 bold"), | ||
("question", "bold"), | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need these logs. The algorithm can be very simple as the current implementation of
get_highest_by_messages