Skip to content

Commit 3b94be0

Browse files
feat: preserve prerelease linearity
1 parent d1c06eb commit 3b94be0

7 files changed

+33
-9
lines changed

commitizen/version_schemes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import importlib_metadata as metadata
1010
from packaging.version import InvalidVersion # noqa: F401: Rexpose the common exception
1111
from packaging.version import Version as _BaseVersion
12+
from packaging.version import _parse_letter_version
1213

1314
from commitizen.config.base_config import BaseConfig
1415
from commitizen.defaults import MAJOR, MINOR, PATCH
@@ -148,10 +149,21 @@ def generate_prerelease(
148149
This function might return something like 'alpha1'
149150
but it will be handled by Version.
150151
"""
152+
153+
pre_order = ["a", "b", "rc"]
154+
151155
if not prerelease:
152156
return ""
153157

154158
# version.pre is needed for mypy check
159+
160+
if self.is_prerelease and self.pre:
161+
letter_version = _parse_letter_version(prerelease, offset)
162+
if letter_version:
163+
current_index = pre_order.index(self.pre[0])
164+
new_index = pre_order.index(letter_version[0])
165+
prerelease = pre_order[max(current_index, new_index)]
166+
155167
if self.is_prerelease and self.pre and prerelease.startswith(self.pre[0]):
156168
prev_prerelease: int = self.pre[1]
157169
new_prerelease_number = prev_prerelease + 1

tests/commands/test_bump_command.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,20 @@ def test_bump_command_prelease(mocker: MockFixture):
211211
mocker.patch.object(sys, "argv", testargs)
212212
cli.main()
213213

214-
tag_exists = git.tag_exist("0.2.0a0")
215-
assert tag_exists is True
214+
assert git.tag_exist("0.2.0a0")
215+
216+
testargs = ["cz", "bump", "--prerelease", "rc", "--yes"]
217+
mocker.patch.object(sys, "argv", testargs)
218+
cli.main()
219+
220+
assert git.tag_exist("0.2.0rc0")
221+
222+
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
223+
mocker.patch.object(sys, "argv", testargs)
224+
cli.main()
225+
226+
assert not git.tag_exist("0.2.0a1")
227+
assert git.tag_exist("0.2.0rc1")
216228

217229
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
218230
testargs = ["cz", "bump"]

tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_beta_alpha_.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## 0.2.0a0 (2021-06-11)
7+
## 0.2.0b1 (2021-06-11)
88

99
## 0.2.0b0 (2021-06-11)
1010

tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_alpha_.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## 0.2.0a0 (2021-06-11)
7+
## 0.2.0rc1 (2021-06-11)
88

99
## 0.2.0rc0 (2021-06-11)
1010

tests/commands/test_changelog_command/test_changelog_incremental_with_prerelease_version_to_prerelease_version_rc_beta_.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## 0.2.0b0 (2021-06-11)
7+
## 0.2.0rc1 (2021-06-11)
88

99
## 0.2.0rc0 (2021-06-11)
1010

tests/test_version_scheme_pep440.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545

4646
# this cases should be handled gracefully
4747
unexpected_cases = [
48-
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1a0"),
49-
(("0.1.1b1", None, "alpha", 0, None), "0.1.1a0"),
48+
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1rc1"),
49+
(("0.1.1b1", None, "alpha", 0, None), "0.1.1b2"),
5050
]
5151

5252
weird_cases = [

tests/test_version_scheme_semver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646

4747
# this cases should be handled gracefully
4848
unexpected_cases = [
49-
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1-a0"),
50-
(("0.1.1b1", None, "alpha", 0, None), "0.1.1-a0"),
49+
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1-rc1"),
50+
(("0.1.1b1", None, "alpha", 0, None), "0.1.1-b2"),
5151
]
5252

5353
weird_cases = [

0 commit comments

Comments
 (0)