Skip to content

Commit 7bafa42

Browse files
committed
fix: prevent prerelase from creating a bump when there are no commits
Closes #281
1 parent d42f744 commit 7bafa42

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

commitizen/commands/bump.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def __call__(self): # noqa: C901
112112
if increment is None:
113113
increment = self.find_increment(commits)
114114

115+
# It may happen that there are commits, but they are not elegible
116+
# for an increment, this generates a problem when using prerelease (#281)
117+
if (
118+
prerelease
119+
and increment is None
120+
and not current_version_instance.is_prerelease
121+
):
122+
raise NoCommitsFoundError(
123+
"[NO_COMMITS_FOUND]\n"
124+
"No commits found to generate a pre-release.\n"
125+
"To avoid this error, manually specify the type of increment with `--increment`"
126+
)
127+
115128
# Increment is removed when current and next version
116129
# are expected to be prereleases.
117130
if prerelease and current_version_instance.is_prerelease:
@@ -123,6 +136,7 @@ def __call__(self): # noqa: C901
123136
prerelease=prerelease,
124137
is_local_version=is_local_version,
125138
)
139+
126140
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)
127141
message = bump.create_commit_message(
128142
current_version, new_version, bump_commit_message

tests/commands/test_bump_command.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,29 @@ def test_bump_with_changelog_config(mocker, changelog_path, config_path):
358358
out = f.read()
359359
assert out.startswith("#")
360360
assert "0.2.0" in out
361+
362+
363+
def test_prevent_prerelease_when_no_increment_detected(
364+
mocker, capsys, tmp_commitizen_project
365+
):
366+
create_file_and_commit("feat: new file")
367+
368+
testargs = ["cz", "bump", "--yes"]
369+
mocker.patch.object(sys, "argv", testargs)
370+
371+
cli.main()
372+
out, _ = capsys.readouterr()
373+
374+
assert "0.2.0" in out
375+
376+
create_file_and_commit("test: new file")
377+
testargs = ["cz", "bump", "-pr", "beta"]
378+
mocker.patch.object(sys, "argv", testargs)
379+
380+
with pytest.raises(NoCommitsFoundError) as excinfo:
381+
cli.main()
382+
383+
expected_error_message = (
384+
"[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release."
385+
)
386+
assert expected_error_message in str(excinfo.value)

0 commit comments

Comments
 (0)