Skip to content

Commit a1d944c

Browse files
committed
fix: bump command called changelog command with allow_no_commit=True, but changelog command raised NoCommitsFoundError
1 parent dd972c9 commit a1d944c

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

commitizen/commands/bump.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ def __call__(self) -> None:
330330

331331
changelog_cmd = Changelog(
332332
self.config,
333-
{**changelog_args, "file_name": self.file_name}, # type: ignore[typeddict-item]
333+
{
334+
**changelog_args, # type: ignore[typeddict-item]
335+
"file_name": self.file_name,
336+
"allow_no_commit": self.arguments["allow_no_commit"],
337+
},
334338
)
335339
changelog_cmd()
336340
changelog_file_name = changelog_cmd.file_name

commitizen/commands/changelog.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ChangelogArgs(TypedDict, total=False):
4343
extras: dict[str, Any]
4444
export_template: str
4545
during_version_bump: bool | None
46+
allow_no_commit: bool # --allow-no-commit is still invalid in the changelog command
4647

4748

4849
class Changelog:
@@ -124,6 +125,7 @@ def __init__(self, config: BaseConfig, arguments: ChangelogArgs) -> None:
124125
self.export_template_to = arguments.get("export_template")
125126

126127
self.during_version_bump: bool = arguments.get("during_version_bump") or False
128+
self.allow_no_commit: bool = arguments.get("allow_no_commit") or False
127129

128130
def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) -> str:
129131
"""Try to find the 'start_rev'.
@@ -255,8 +257,10 @@ def __call__(self) -> None:
255257
changelog_meta.unreleased_end = latest_full_release_info.index + 1
256258

257259
commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")
258-
if not commits and (
259-
self.current_version is None or not self.current_version.is_prerelease
260+
if (
261+
not self.allow_no_commit
262+
and not commits
263+
and (self.current_version is None or not self.current_version.is_prerelease)
260264
):
261265
raise NoCommitsFoundError("No commits found")
262266

tests/commands/test_bump_command.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,3 +1540,46 @@ def test_changelog_merge_preserves_header(
15401540
out = changelog_path.read_text()
15411541

15421542
file_regression.check(out, extension=".md")
1543+
1544+
1545+
@pytest.mark.freeze_time("2025-01-01")
1546+
def test_bump_allow_no_commit_issue(
1547+
tmp_commitizen_project_initial,
1548+
util: UtilFixture,
1549+
) -> None:
1550+
"""Issue #1866: bump command called changelog command with allow_no_commit=True, but changelog command raised NoCommitsFoundError"""
1551+
tmp_commitizen_project = tmp_commitizen_project_initial(version="1.0.0")
1552+
with (tmp_commitizen_project / "pyproject.toml").open("w") as f:
1553+
f.write(
1554+
dedent(
1555+
r"""
1556+
[project]
1557+
name = "abc"
1558+
version = "4.14.0"
1559+
1560+
[tool.commitizen]
1561+
name = "cz_customize"
1562+
tag_format = "$version"
1563+
version_scheme = "semver2"
1564+
version_provider = "pep621"
1565+
update_changelog_on_bump = true
1566+
1567+
[tool.commitizen.customize]
1568+
bump_pattern = '^(feat|fix|ci|build|perf|refactor|chore|remove|style|test)'
1569+
bump_map = {feat = "MINOR", fix = "PATCH", ci = "PATCH", build = "PATCH", perf = "PATCH", refactor = "PATCH", chore = "PATCH", remove = "PATCH", style = "PATCH", test = "PATCH" }
1570+
schema_pattern = "(build|bump|chore|ci|dev|docs|feat|fix|perf|refactor|remove|style|test):(\\s.*)"
1571+
commit_parser = "^(?P<change_type>build|bump|chore|ci|dev|docs|feat|fix|perf|refactor|remove|style|test):\\s(?P<message>.*)?"
1572+
# By excluding 'bump', 'ci', 'dev', and 'docs' from
1573+
# 'change_type_map', 'change_type_order' and 'changelog_pattern'
1574+
# we omit the corresponding commit comments from the CHANGELOG.
1575+
# The order of types in the CHANGELOG is dictated by the type_order
1576+
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"}
1577+
change_type_order = ["BREAKING CHANGE", "New Features", "Bug Fixes", "Performance Improvements", "Refactoring", "General Improvements", "Removed", "Stylistic Changes", "Testing", "Build"]
1578+
changelog_pattern = "^(build|chore|feat|fix|perf|refactor|remove|style|test)"
1579+
"""
1580+
)
1581+
)
1582+
util.run_cli("bump", "--yes", "--allow-no-commit", "--prerelease", "beta")
1583+
util.run_cli(
1584+
"bump", "--allow-no-commit", "--prerelease", "rc"
1585+
) # Failed because the bump command called changelog command

0 commit comments

Comments
 (0)