Skip to content

feat: preserve prerelease linearity #800

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

Closed
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
12 changes: 12 additions & 0 deletions commitizen/version_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import importlib_metadata as metadata
from packaging.version import InvalidVersion # noqa: F401: Rexpose the common exception
from packaging.version import Version as _BaseVersion
from packaging.version import _parse_letter_version

from commitizen.config.base_config import BaseConfig
from commitizen.defaults import MAJOR, MINOR, PATCH
Expand Down Expand Up @@ -143,10 +144,21 @@ def generate_prerelease(
This function might return something like 'alpha1'
but it will be handled by Version.
"""

pre_order = ["a", "b", "rc"]

if not prerelease:
return ""

# version.pre is needed for mypy check

if self.is_prerelease and self.pre:
letter_version = _parse_letter_version(prerelease, offset)
if letter_version:
current_index = pre_order.index(self.pre[0])
new_index = pre_order.index(letter_version[0])
prerelease = pre_order[max(current_index, new_index)]

if self.is_prerelease and self.pre and prerelease.startswith(self.pre[0]):
prev_prerelease: int = self.pre[1]
new_prerelease_number = prev_prerelease + 1
Expand Down
16 changes: 14 additions & 2 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,20 @@ def test_bump_command_prelease(mocker: MockFixture):
mocker.patch.object(sys, "argv", testargs)
cli.main()

tag_exists = git.tag_exist("0.2.0a0")
assert tag_exists is True
assert git.tag_exist("0.2.0a0")

testargs = ["cz", "bump", "--prerelease", "rc", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

assert git.tag_exist("0.2.0rc0")

testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

assert not git.tag_exist("0.2.0a1")
assert git.tag_exist("0.2.0rc1")

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
testargs = ["cz", "bump"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0a0 (2021-06-11)
## 0.2.0b1 (2021-06-11)

## 0.2.0b0 (2021-06-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0a0 (2021-06-11)
## 0.2.0rc1 (2021-06-11)

## 0.2.0rc0 (2021-06-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0b0 (2021-06-11)
## 0.2.0rc1 (2021-06-11)

## 0.2.0rc0 (2021-06-11)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_version_scheme_pep440.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

# this cases should be handled gracefully
unexpected_cases = [
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1a0"),
(("0.1.1b1", None, "alpha", 0, None), "0.1.1a0"),
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1rc1"),
(("0.1.1b1", None, "alpha", 0, None), "0.1.1b2"),
]

weird_cases = [
Expand Down
4 changes: 2 additions & 2 deletions tests/test_version_scheme_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

# this cases should be handled gracefully
unexpected_cases = [
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1-a0"),
(("0.1.1b1", None, "alpha", 0, None), "0.1.1-a0"),
(("0.1.1rc0", None, "alpha", 0, None), "0.1.1-rc1"),
(("0.1.1b1", None, "alpha", 0, None), "0.1.1-b2"),
]

weird_cases = [
Expand Down