Skip to content

WIP fix semver not fully covered #1548

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

Draft
wants to merge 2 commits into
base: v4-9-0-test
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions commitizen/version_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,54 @@ def bump(
VersionScheme: TypeAlias = type[VersionProtocol]


# See https://github.com/pypa/packaging/blob/14b83e15dbb9caa87c63646ba7808b2b5e460ce6/src/packaging/version.py#L117
# TODO: add more test cases for this pattern
_VERSION_PATTERN = r"""^\s*
v?
(?:
(?:(?P<epoch>[0-9]+)!)? # epoch
(?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
(?P<pre> # pre-release
[-_\.]?
(?P<pre_l>
(?! # negative lookahead to prevent matching post, rev, r, dev
[-_\.]?
(post|rev|r|dev)
[-_\.]?
([0-9]+)?
$)
[a-z]+? # match any letters to support semver
)
[-_\.]?
(?P<pre_n>[0-9]+)?
)?
(?P<post> # post release
(?:-(?P<post_n1>[0-9]+))
|
(?:
[-_\.]?
(?P<post_l>post|rev|r)
[-_\.]?
(?P<post_n2>[0-9]+)?
)
)?
(?P<dev> # dev release
[-_\.]?
(?P<dev_l>dev)
[-_\.]?
(?P<dev_n>[0-9]+)?
)?
)
(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
\s*$"""


class BaseVersion(_BaseVersion):
"""
A base class implementing the `VersionProtocol` for PEP440-like versions.
"""

_regex: re.Pattern = re.compile(_VERSION_PATTERN, re.VERBOSE | re.IGNORECASE)
parser: ClassVar[re.Pattern] = _DEFAULT_VERSION_PARSER
"""Regex capturing this version scheme into a `version` group"""

Expand Down
3 changes: 3 additions & 0 deletions tests/test_version_scheme_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
(("1.0.0alpha1", None, "alpha", 0, None), "1.0.0-a2"),
(("1", None, "rc", 0, None), "1.0.0-rc0"),
(("1.0.0rc1+e20d7b57f3eb", "PATCH", None, 0, None), "1.0.0"),
(("1.0.0-reallyweird", "PATCH", "reallyweird", 0, None), "1.0.0-reallyweird1"),
(("v0.7.1-release", "PATCH", "release", 0, None), "0.7.1-release1"),
(("v0.0.1-SNAPSHOT", "PATCH", "SNAPSHOT", 0, None), "0.0.1-snapshot1"),
Comment on lines +67 to +69
Copy link
Contributor Author

@bearomorphism bearomorphism Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lee-W I'm not entirely sure if these test cases are correct. Could you help to review?

]

# test driven development
Expand Down
3 changes: 3 additions & 0 deletions tests/test_version_scheme_semver2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from commitizen.version_schemes import SemVer2, VersionProtocol

# current_version, increment, prerelease, prerelease_offset, devrelease

simple_flow = [
(("0.1.0", "PATCH", None, 0, None), "0.1.1"),
(("0.1.0", "PATCH", None, 0, 1), "0.1.1-dev.1"),
Expand Down Expand Up @@ -64,6 +66,7 @@
(("1.0.0-alpha.1", None, "alpha", 0, None), "1.0.0-alpha.2"),
(("1", None, "rc", 0, None), "1.0.0-rc.0"),
(("1.0.0-rc.1+e20d7b57f3eb", "PATCH", None, 0, None), "1.0.0"),
(("1.0.0-reallyweird", "PATCH", "reallyweird", 0, None), "1.0.0-reallyweird.1"),
]

# test driven development
Expand Down