Skip to content

Commit 6a2c9ec

Browse files
committed
fix(schemes): recognize PEP 440 devrelease tags
The default version parser did not match PEP 440 development release tags such as 0.2.0.dev1. As a result, cz bump --devrelease created tags that were not recognized as version tags, and the next incremental changelog run failed with "No tag found to do an incremental changelog". Use the PEP 440 regular expression from the specification appendix, while keeping the version group and the -next pre-release support relied upon by existing tag formats. Closes #1367
1 parent b00c86a commit 6a2c9ec

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎commitizen/version_schemes.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
Increment: TypeAlias = Literal["MAJOR", "MINOR", "PATCH"] # TODO: deprecate
3535
Prerelease: TypeAlias = Literal["alpha", "beta", "rc"]
3636
_DEFAULT_VERSION_PARSER = re.compile(
37-
r"v?(?P<version>([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z.]+)?(\w+)?)"
37+
r"v?(?P<version>(?:(?P<epoch>[0-9]+)!)?(?P<release>[0-9]+\.[0-9]+(?:\.[0-9]+)*)"
38+
r"(?P<pre>[-_\.]?(?P<pre_l>a|b|c|rc|alpha|beta|pre|preview|next|dev)[-_\.]?(?P<pre_n>[0-9]+)?)?"
39+
r"(?P<post>(?:-(?P<post_n1>[0-9]+))|(?:[-_\.]?(?P<post_l>post|rev|r)[-_\.]?(?P<post_n2>[0-9]+)?))?"
40+
r"(?P<dev>[-_\.]?(?P<dev_l>dev)[-_\.]?(?P<dev_n>[0-9]+)?)?"
41+
r"(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?)",
42+
re.IGNORECASE,
3843
)
3944

4045

‎tests/commands/test_changelog_command.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,38 @@ def test_changelog_incremental_with_prerelease_version_to_prerelease_version(
750750
file_regression.check(out, extension=".md")
751751

752752

753+
@pytest.mark.usefixtures("tmp_commitizen_project")
754+
@pytest.mark.freeze_time("2021-06-11")
755+
def test_changelog_incremental_with_devrelease(
756+
changelog_path: Path,
757+
file_regression: FileRegressionFixture,
758+
util: UtilFixture,
759+
):
760+
"""Regression test for #1367: bumping with ``--devrelease`` twice must
761+
update the incremental changelog.
762+
763+
The second devrelease bump used to fail with
764+
"No tag found to do an incremental changelog" because the PEP 440
765+
development-release tag (e.g. ``0.2.0.dev0``) was not recognised as a
766+
version tag by the default version parser.
767+
"""
768+
util.create_file_and_commit("irrelevant commit")
769+
util.create_tag("1.0.0", annotated=True)
770+
771+
util.create_file_and_commit("feat: add new output")
772+
util.create_file_and_commit("fix: output glitch")
773+
774+
util.run_cli("bump", "--changelog", "--devrelease", "0", "--yes")
775+
776+
util.create_file_and_commit("feat: add more stuff")
777+
778+
util.run_cli("bump", "--changelog", "--devrelease", "1", "--yes")
779+
780+
out = changelog_path.read_text()
781+
782+
file_regression.check(out, extension=".md")
783+
784+
753785
@pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
754786
@pytest.mark.usefixtures("tmp_commitizen_project")
755787
@pytest.mark.freeze_time("2025-12-29")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 0.2.0.dev1 (2021-06-11)
2+
3+
### Feat
4+
5+
- add more stuff
6+
7+
## 0.2.0.dev0 (2021-06-11)
8+
9+
### Feat
10+
11+
- add new output
12+
13+
### Fix
14+
15+
- output glitch
16+
17+
## 1.0.0 (2021-06-11)

‎tests/test_tags.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ def test_is_version_tag_accepts_dotless_devrelease_in_custom_tag_format():
144144
assert str(extracted) == "1.2.3.dev1"
145145

146146

147+
def test_is_version_tag_accepts_pep440_devrelease_in_default_tag_format():
148+
"""Regression test for #1367: a PEP 440 development release tag such as
149+
``0.2.0.dev1`` (created by ``cz bump --devrelease``) must be recognised as
150+
a version tag. Previously the default version parser only accepted
151+
``.dev`` suffixes after a hyphen, so the tag was dropped and the
152+
incremental changelog failed with "No tag found to do an incremental
153+
changelog".
154+
"""
155+
rules = TagRules()
156+
157+
assert rules.is_version_tag("0.2.0.dev1") is True
158+
assert rules.is_version_tag("0.2.0.dev0") is True
159+
assert rules.is_version_tag("1.2.3.dev1+build.5") is True
160+
assert rules.is_version_tag("1.0.0a2.dev1") is True
161+
162+
extracted = rules.extract_version(_git_tag("0.2.0.dev1"))
163+
assert str(extracted) == "0.2.0.dev1"
164+
assert extracted.dev == 1
165+
166+
147167
def test_monorepo_ignored_tag_formats_keep_own_tags(capsys: pytest.CaptureFixture):
148168
"""Regression test for the monorepo workflow in
149169
``docs/tutorials/monorepo_guidance.md``.

0 commit comments

Comments
 (0)