Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76ad54f

Browse files
authored
Merge pull request #726 from commitizen-tools/fix/major-version-zero-bump-map
Fix/major version zero bump map
2 parents 996bff8 + 6096b68 commit 76ad54f

File tree

7 files changed

+141
-5
lines changed

7 files changed

+141
-5
lines changed

‎commitizen/commands/bump.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import questionary
66
from packaging.version import InvalidVersion, Version
77

8-
from commitizen import bump, cmd, defaults, factory, git, hooks, out, version_types
8+
from commitizen import bump, cmd, factory, git, hooks, out, version_types
99
from commitizen.commands.changelog import Changelog
1010
from commitizen.config import BaseConfig
1111
from commitizen.exceptions import (
@@ -86,8 +86,16 @@ def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool
8686
return is_initial
8787

8888
def find_increment(self, commits: List[git.GitCommit]) -> Optional[str]:
89+
# Update the bump map to ensure major version doesn't increment.
90+
is_major_version_zero: bool = self.bump_settings["major_version_zero"]
91+
# self.cz.bump_map = defaults.bump_map_major_version_zero
92+
bump_map = (
93+
self.cz.bump_map_major_version_zero
94+
if is_major_version_zero
95+
else self.cz.bump_map
96+
)
8997
bump_pattern = self.cz.bump_pattern
90-
bump_map=self.cz.bump_map
98+
9199
if not bump_map or not bump_pattern:
92100
raise NoPatternMapError(
93101
f"'{self.config.settings['name']}' rule does not support bump"
@@ -153,9 +161,6 @@ def __call__(self): # noqa: C901
153161
f"--major-version-zero is meaningless for current version {current_version}"
154162
)
155163

156-
# Update the bump map to ensure major version doesn't increment.
157-
self.cz.bump_map = defaults.bump_map_major_version_zero
158-
159164
current_tag_version: str = bump.normalize_tag(
160165
current_version,
161166
tag_format=tag_format,

‎commitizen/cz/base.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class BaseCommitizen(metaclass=ABCMeta):
1212
bump_pattern: Optional[str] = None
1313
bump_map: Optional[Dict[str, str]] = None
14+
bump_map_major_version_zero: Optional[Dict[str, str]] = None
1415
default_style_config: List[Tuple[str, str]] = [
1516
("qmark", "fg:#ff9d00 bold"),
1617
("question", "bold"),

‎commitizen/cz/conventional_commits/conventional_commits.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def parse_subject(text):
3030
class ConventionalCommitsCz(BaseCommitizen):
3131
bump_pattern = defaults.bump_pattern
3232
bump_map = defaults.bump_map
33+
bump_map_major_version_zero = defaults.bump_map_major_version_zero
3334
commit_parser = defaults.commit_parser
3435
version_parser = defaults.version_parser
3536
change_type_map = {

‎commitizen/cz/customize/customize.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class CustomizeCommitsCz(BaseCommitizen):
1818
bump_pattern = defaults.bump_pattern
1919
bump_map = defaults.bump_map
20+
bump_map_major_version_zero = defaults.bump_map_major_version_zero
2021
change_type_order = defaults.change_type_order
2122

2223
def __init__(self, config: BaseConfig):
@@ -34,6 +35,12 @@ def __init__(self, config: BaseConfig):
3435
if custom_bump_map:
3536
self.bump_map = custom_bump_map
3637

38+
custom_bump_map_major_version_zero = self.custom_settings.get(
39+
"bump_map_major_version_zero"
40+
)
41+
if custom_bump_map_major_version_zero:
42+
self.bump_map_major_version_zero = custom_bump_map_major_version_zero
43+
3744
custom_change_type_order = self.custom_settings.get("change_type_order")
3845
if custom_change_type_order:
3946
self.change_type_order = custom_change_type_order

‎commitizen/defaults.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class CzSettings(TypedDict, total=False):
1616
bump_pattern: str
1717
bump_map: "OrderedDict[str, str]"
18+
bump_map_major_version_zero: "OrderedDict[str, str]"
1819
change_type_order: List[str]
1920

2021
questions: Questions

‎tests/commands/test_bump_command.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,3 +980,47 @@ def test_bump_command_prelease_version_type_check_old_tags(
980980
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
981981
with open(version_file, "r") as f:
982982
assert "0.2.0" in f.read()
983+
984+
985+
@pytest.mark.usefixtures("tmp_commitizen_project")
986+
@pytest.mark.usefixtures("use_cz_semver")
987+
@pytest.mark.parametrize(
988+
"message, expected_tag",
989+
[
990+
("minor: add users", "0.2.0"),
991+
("patch: bug affecting users", "0.1.1"),
992+
("major: bug affecting users", "1.0.0"),
993+
],
994+
)
995+
def test_bump_with_plugin(mocker: MockFixture, message: str, expected_tag: str):
996+
create_file_and_commit(message)
997+
998+
testargs = ["cz", "--name", "cz_semver", "bump", "--yes"]
999+
mocker.patch.object(sys, "argv", testargs)
1000+
cli.main()
1001+
1002+
tag_exists = git.tag_exist(expected_tag)
1003+
assert tag_exists is True
1004+
1005+
1006+
@pytest.mark.usefixtures("tmp_commitizen_project")
1007+
@pytest.mark.usefixtures("use_cz_semver")
1008+
@pytest.mark.parametrize(
1009+
"message, expected_tag",
1010+
[
1011+
("minor: add users", "0.2.0"),
1012+
("patch: bug affecting users", "0.1.1"),
1013+
("major: bug affecting users", "0.2.0"),
1014+
],
1015+
)
1016+
def test_bump_with_major_version_zero_with_plugin(
1017+
mocker: MockFixture, message: str, expected_tag: str
1018+
):
1019+
create_file_and_commit(message)
1020+
1021+
testargs = ["cz", "--name", "cz_semver", "bump", "--yes", "--major-version-zero"]
1022+
mocker.patch.object(sys, "argv", testargs)
1023+
cli.main()
1024+
1025+
tag_exists = git.tag_exist(expected_tag)
1026+
assert tag_exists is True

‎tests/conftest.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from commitizen import cmd, defaults
1010
from commitizen.config import BaseConfig
11+
from commitizen.cz.base import BaseCommitizen
12+
from commitizen.cz import registry
1113
from tests.utils import create_file_and_commit
1214

1315
SIGNER = "GitHub Action"
@@ -122,3 +124,78 @@ def config():
122124
@pytest.fixture()
123125
def config_path() -> str:
124126
return os.path.join(os.getcwd(), "pyproject.toml")
127+
128+
129+
class SemverCommitizen(BaseCommitizen):
130+
"""A minimal cz rules used to test changelog and bump.
131+
132+
Samples:
133+
```
134+
minor(users): add email to user
135+
major: removed user profile
136+
patch(deps): updated dependency for security
137+
```
138+
"""
139+
140+
bump_pattern = r"^(patch|minor|major)"
141+
bump_map = {
142+
"major": "MAJOR",
143+
"minor": "MINOR",
144+
"patch": "PATCH",
145+
}
146+
bump_map_major_version_zero = {
147+
"major": "MINOR",
148+
"minor": "MINOR",
149+
"patch": "PATCH",
150+
}
151+
changelog_pattern = r"^(patch|minor|major)"
152+
commit_parser = r"^(?P<change_type>patch|minor|major)(?:\((?P<scope>[^()\r\n]*)\)|\()?:?\s(?P<message>.+)" # noqa
153+
change_type_map = {
154+
"major": "Breaking Changes",
155+
"minor": "Features",
156+
"patch": "Bugs",
157+
}
158+
159+
def questions(self) -> list:
160+
return [
161+
{
162+
"type": "list",
163+
"name": "prefix",
164+
"message": "Select the type of change you are committing",
165+
"choices": [
166+
{
167+
"value": "patch",
168+
"name": "patch: a bug fix",
169+
"key": "p",
170+
},
171+
{
172+
"value": "minor",
173+
"name": "minor: a new feature, non-breaking",
174+
"key": "m",
175+
},
176+
{
177+
"value": "major",
178+
"name": "major: a breaking change",
179+
"key": "b",
180+
},
181+
],
182+
},
183+
{
184+
"type": "input",
185+
"name": "subject",
186+
"message": (
187+
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
188+
),
189+
},
190+
]
191+
192+
def message(self, answers: dict) -> str:
193+
prefix = answers["prefix"]
194+
subject = answers.get("subject", "default message").trim()
195+
return f"{prefix}: {subject}"
196+
197+
198+
@pytest.fixture()
199+
def use_cz_semver(mocker):
200+
new_cz = {**registry, "cz_semver": SemverCommitizen}
201+
mocker.patch.dict("commitizen.cz.registry", new_cz)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /