Skip to content

Commit 72a2638

Browse files
feat: add --allow-no-commit to changelog command (#1868)
* fix: bump command called changelog command with allow_no_commit=True, but changelog command raised NoCommitsFoundError * fix: clarify allow-no-commit changelog behavior and docs Align internal typing/forwarding for allow_no_commit, simplify the regression test setup, and document how changelog entries are generated during no-commit bumps. Made-with: Cursor * chore: clarify internal changelog allow_no_commit argument Document that allow_no_commit in ChangelogArgs is internal-only for bump-triggered changelog generation. Made-with: Cursor * chore: Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent bcfb089 commit 72a2638

5 files changed

Lines changed: 34 additions & 3 deletions

File tree

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": bool(self.arguments["allow_no_commit"]),
337+
},
334338
)
335339
changelog_cmd()
336340
changelog_file_name = changelog_cmd.file_name

commitizen/commands/changelog.py

Lines changed: 7 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 | None # Internal-only when invoked by bump.
4647

4748

4849
class Changelog:
@@ -124,6 +125,8 @@ 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+
# Internal flag used when changelog is invoked from `cz bump --allow-no-commit`.
129+
self.allow_no_commit: bool = bool(arguments.get("allow_no_commit"))
127130

128131
def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) -> str:
129132
"""Try to find the 'start_rev'.
@@ -255,8 +258,10 @@ def __call__(self) -> None:
255258
changelog_meta.unreleased_end = latest_full_release_info.index + 1
256259

257260
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
261+
if (
262+
not self.allow_no_commit
263+
and not commits
264+
and (self.current_version is None or not self.current_version.is_prerelease)
260265
):
261266
raise NoCommitsFoundError("No commits found")
262267

docs/commands/bump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ cz bump --allow-no-commit 2.0.0
385385
cz bump --allow-no-commit 2.0.0
386386
```
387387

388+
!!! note "Behavior with changelog updates"
389+
When `update_changelog_on_bump = true` (or `--changelog` is used), `cz bump --allow-no-commit` also generates a changelog entry even if there are no commits in the selected range.
390+
391+
This makes the new release visible in the changelog while still showing that no commit-based changes were included.
392+
388393
### `--tag-format`
389394

390395
`tag_format` and [version_scheme][version_scheme] are combined to make Git tag names from versions.

docs/commands/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Generates a changelog following the committing rules established.
44

5+
When changelog generation is triggered by `cz bump --allow-no-commit` (with `--changelog` or `update_changelog_on_bump = true`), Commitizen still creates a release entry even when no commits are found in the selected revision range.
6+
57
!!! tip
68
To create the changelog automatically on bump, add the setting [update_changelog_on_bump](../config/bump.md#update_changelog_on_bump)
79

tests/commands/test_bump_command.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,3 +1537,18 @@ def test_changelog_merge_preserves_header(
15371537
out = changelog_path.read_text()
15381538

15391539
file_regression.check(out, extension=".md")
1540+
1541+
1542+
@pytest.mark.freeze_time("2025-01-01")
1543+
def test_bump_allow_no_commit_issue(
1544+
tmp_commitizen_project_initial,
1545+
util: UtilFixture,
1546+
) -> None:
1547+
"""Issue #1866: bump command called changelog command with allow_no_commit=True, but changelog command raised NoCommitsFoundError"""
1548+
tmp_commitizen_project_initial(
1549+
version="1.0.0", config_extra="update_changelog_on_bump = true\n"
1550+
)
1551+
util.run_cli("bump", "--yes", "--allow-no-commit", "--prerelease", "beta")
1552+
util.run_cli(
1553+
"bump", "--allow-no-commit", "--prerelease", "rc"
1554+
) # Should not fail when changelog generation runs with no new commits

0 commit comments

Comments
 (0)