diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index f188c6ab40..666e29535d 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -112,6 +112,19 @@ def __call__(self): # noqa: C901 if increment is None: increment = self.find_increment(commits) + # It may happen that there are commits, but they are not elegible + # for an increment, this generates a problem when using prerelease (#281) + if ( + prerelease + and increment is None + and not current_version_instance.is_prerelease + ): + raise NoCommitsFoundError( + "[NO_COMMITS_FOUND]\n" + "No commits found to generate a pre-release.\n" + "To avoid this error, manually specify the type of increment with `--increment`" + ) + # Increment is removed when current and next version # are expected to be prereleases. if prerelease and current_version_instance.is_prerelease: @@ -123,6 +136,7 @@ def __call__(self): # noqa: C901 prerelease=prerelease, is_local_version=is_local_version, ) + new_tag_version = bump.create_tag(new_version, tag_format=tag_format) message = bump.create_commit_message( current_version, new_version, bump_commit_message diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 806c930594..77ea4840b0 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -358,3 +358,29 @@ def test_bump_with_changelog_config(mocker, changelog_path, config_path): out = f.read() assert out.startswith("#") assert "0.2.0" in out + + +def test_prevent_prerelease_when_no_increment_detected( + mocker, capsys, tmp_commitizen_project +): + create_file_and_commit("feat: new file") + + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + + cli.main() + out, _ = capsys.readouterr() + + assert "0.2.0" in out + + create_file_and_commit("test: new file") + testargs = ["cz", "bump", "-pr", "beta"] + mocker.patch.object(sys, "argv", testargs) + + with pytest.raises(NoCommitsFoundError) as excinfo: + cli.main() + + expected_error_message = ( + "[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release." + ) + assert expected_error_message in str(excinfo.value)