Skip to content

Commit 83e11ab

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

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

commitizen/commands/bump.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ def __call__(self) -> None:
332332
self.config,
333333
{**changelog_args, "file_name": self.file_name}, # type: ignore[typeddict-item]
334334
)
335-
changelog_cmd()
335+
changelog_cmd(
336+
allow_no_commit=bool(self.arguments["allow_no_commit"])
337+
) # This is a workaround
336338
changelog_file_name = changelog_cmd.file_name
337339
updated_files.append(changelog_file_name)
338340

commitizen/commands/changelog.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _export_template(self, dist: str) -> None:
186186
text = Path(filename).read_text()
187187
Path(dist).write_text(text)
188188

189-
def __call__(self) -> None:
189+
def __call__(self, *, allow_no_commit: bool = False) -> None:
190190
commit_parser = self.cz.commit_parser
191191
changelog_pattern = self.cz.changelog_pattern
192192
start_rev = self.start_rev
@@ -255,8 +255,10 @@ def __call__(self) -> None:
255255
changelog_meta.unreleased_end = latest_full_release_info.index + 1
256256

257257
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
258+
if (
259+
not allow_no_commit
260+
and not commits
261+
and (self.current_version is None or not self.current_version.is_prerelease)
260262
):
261263
raise NoCommitsFoundError("No commits found")
262264

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)