diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 6084c8c15..37173f098 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -330,7 +330,11 @@ def __call__(self) -> None: changelog_cmd = Changelog( self.config, - {**changelog_args, "file_name": self.file_name}, # type: ignore[typeddict-item] + { + **changelog_args, # type: ignore[typeddict-item] + "file_name": self.file_name, + "allow_no_commit": self.arguments["allow_no_commit"], + }, ) changelog_cmd() changelog_file_name = changelog_cmd.file_name diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 5093ed9f2..f995b88b1 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -43,6 +43,7 @@ class ChangelogArgs(TypedDict, total=False): extras: dict[str, Any] export_template: str during_version_bump: bool | None + allow_no_commit: bool # --allow-no-commit is still invalid in the changelog command class Changelog: @@ -124,6 +125,7 @@ def __init__(self, config: BaseConfig, arguments: ChangelogArgs) -> None: self.export_template_to = arguments.get("export_template") self.during_version_bump: bool = arguments.get("during_version_bump") or False + self.allow_no_commit: bool = arguments.get("allow_no_commit") or False def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) -> str: """Try to find the 'start_rev'. @@ -255,8 +257,10 @@ def __call__(self) -> None: changelog_meta.unreleased_end = latest_full_release_info.index + 1 commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order") - if not commits and ( - self.current_version is None or not self.current_version.is_prerelease + if ( + not self.allow_no_commit + and not commits + and (self.current_version is None or not self.current_version.is_prerelease) ): raise NoCommitsFoundError("No commits found") diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 94b40e94e..a0c8ce5fd 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1540,3 +1540,42 @@ def test_changelog_merge_preserves_header( out = changelog_path.read_text() file_regression.check(out, extension=".md") + + +@pytest.mark.freeze_time("2025-01-01") +def test_bump_allow_no_commit_issue( + tmp_commitizen_project_initial, + util: UtilFixture, +) -> None: + """Issue #1866: bump command called changelog command with allow_no_commit=True, but changelog command raised NoCommitsFoundError""" + tmp_commitizen_project = tmp_commitizen_project_initial(version="1.0.0") + with (tmp_commitizen_project / "pyproject.toml").open("w") as f: + f.write( + dedent( + r""" + [project] + name = "abc" + version = "4.14.0" + + [tool.commitizen] + name = "cz_customize" + tag_format = "$version" + version_scheme = "semver2" + version_provider = "pep621" + update_changelog_on_bump = true + + [tool.commitizen.customize] + bump_pattern = '^(feat|fix|ci|build|perf|refactor|chore|remove|style|test)' + bump_map = {feat = "MINOR", fix = "PATCH", ci = "PATCH", build = "PATCH", perf = "PATCH", refactor = "PATCH", chore = "PATCH", remove = "PATCH", style = "PATCH", test = "PATCH" } + schema_pattern = "(build|bump|chore|ci|dev|docs|feat|fix|perf|refactor|remove|style|test):(\\s.*)" + commit_parser = "^(?Pbuild|bump|chore|ci|dev|docs|feat|fix|perf|refactor|remove|style|test):\\s(?P.*)?" + change_type_map = {"feat" = "New Features", "fix" = "Bug Fixes", "perf" = "Performance Improvements", "refactor" = "Refactoring", "chore" = "General Improvements", "remove" = "Removed", "style" = "Stylistic Changes", "test" = "Testing", "build" = "Build"} + change_type_order = ["BREAKING CHANGE", "New Features", "Bug Fixes", "Performance Improvements", "Refactoring", "General Improvements", "Removed", "Stylistic Changes", "Testing", "Build"] + changelog_pattern = "^(build|chore|feat|fix|perf|refactor|remove|style|test)" +""" + ) + ) + util.run_cli("bump", "--yes", "--allow-no-commit", "--prerelease", "beta") + util.run_cli( + "bump", "--allow-no-commit", "--prerelease", "rc" + ) # Failed because the bump command called changelog command