Skip to content

Commit e87930c

Browse files
feat(bump): add --merge-prerelease flag
Closes #1934. Adds a `--merge-prerelease` CLI flag to `cz bump` that mirrors the existing flag of `cz changelog`. When passed, it overrides the `changelog_merge_prerelease` setting for a single bump invocation, which is useful when you want to keep prerelease entries in the changelog by default but collapse them on a release bump. The implementation adds the flag to `commitizen/cli.py`, threads it through the `BumpArgs` TypedDict, and forwards it to the embedded `Changelog` invocation in `commands/bump.py`. The pre-existing `Changelog` argument plumbing (`merge_prerelease` -> `tag_rules. merge_prereleases`) is unchanged. Also adds a parameterised test that proves the CLI flag enables merging even when the config setting is left at its default (`false`), reusing the existing fixture content. Co-authored-by: Copilot <[email protected]>
1 parent 4d99415 commit e87930c

12 files changed

Lines changed: 123 additions & 15 deletions

commitizen/cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,16 @@ def __call__(
312312
"default": False,
313313
"help": "Output changelog to stdout.",
314314
},
315+
{
316+
"name": "--merge-prerelease",
317+
"action": "store_true",
318+
"default": None,
319+
"help": (
320+
"Collect all changes from prereleases into the next non-prerelease "
321+
"when generating the changelog. "
322+
"Overrides the `changelog_merge_prerelease` setting."
323+
),
324+
},
315325
{
316326
"name": ["--git-output-to-stderr"],
317327
"action": "store_true",

commitizen/commands/bump.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class BumpArgs(Settings, total=False):
5656
increment: Increment | None
5757
local_version: bool
5858
manual_version: str | None
59+
merge_prerelease: bool | None
5960
no_verify: bool
6061
prerelease: Prerelease | None
6162
retry: bool
@@ -319,6 +320,9 @@ def __call__(self) -> None:
319320
# governs logic for merge_prerelease
320321
"during_version_bump": self.arguments["prerelease"] is None,
321322
}
323+
merge_prerelease = self.arguments.get("merge_prerelease")
324+
if merge_prerelease is not None:
325+
changelog_args["merge_prerelease"] = merge_prerelease
322326
if self.changelog_to_stdout:
323327
try:
324328
Changelog(

docs/commands/bump.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ Useful when used with `--changelog-to-stdout` and piping the output to a file.
226226

227227
For example, `git commit` output may pollute `stdout`, so it is recommended to use this flag when piping the output to a file.
228228

229+
### `--merge-prerelease`
230+
231+
Collect all changes from prereleases into the next non-prerelease entry when generating the incremental changelog.
232+
233+
This mirrors the `--merge-prerelease` flag of `cz changelog` and overrides the [`changelog_merge_prerelease`](./changelog.md#--merge-prerelease) setting for a single bump invocation. It is useful when you want to keep prerelease entries in the changelog by default but collapse them on a release bump:
234+
235+
```bash
236+
cz bump --changelog --merge-prerelease
237+
```
238+
229239
### `--retry`
230240

231241
If you use tools like [pre-commit](https://pre-commit.com/), you can add this flag.

tests/commands/test_bump_command.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,37 @@ def test_is_initial_tag(mocker: MockFixture, tmp_commitizen_project, util: UtilF
14231423
assert bump_cmd._is_initial_tag(None, is_yes=False) is False
14241424

14251425

1426+
@pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
1427+
@pytest.mark.usefixtures("tmp_commitizen_project")
1428+
@pytest.mark.freeze_time("2025-01-01")
1429+
def test_changelog_cli_flag_merge_prerelease(
1430+
mocker: MockFixture,
1431+
util: UtilFixture,
1432+
changelog_path: Path,
1433+
config_path: Path,
1434+
file_regression: FileRegressionFixture,
1435+
test_input: str,
1436+
):
1437+
"""`cz bump --merge-prerelease` overrides the (default false) config setting."""
1438+
with config_path.open("a") as f:
1439+
f.write("update_changelog_on_bump = true\n")
1440+
f.write("annotated_tag = true\n")
1441+
1442+
util.create_file_and_commit("irrelevant commit")
1443+
mocker.patch("commitizen.git.GitTag.date", "1970-01-01")
1444+
git.tag("0.1.0")
1445+
1446+
util.create_file_and_commit("feat: add new output")
1447+
util.create_file_and_commit("fix: output glitch")
1448+
util.run_cli("bump", "--prerelease", test_input, "--yes")
1449+
1450+
util.run_cli("bump", "--changelog", "--merge-prerelease")
1451+
1452+
out = changelog_path.read_text()
1453+
1454+
file_regression.check(out, extension=".md")
1455+
1456+
14261457
@pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
14271458
@pytest.mark.usefixtures("tmp_commitizen_project")
14281459
@pytest.mark.freeze_time("2025-01-01")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 0.2.0 (2025-01-01)
2+
3+
### Feat
4+
5+
- add new output
6+
7+
### Fix
8+
9+
- output glitch
10+
11+
## 0.1.0 (1970-01-01)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 0.2.0 (2025-01-01)
2+
3+
### Feat
4+
5+
- add new output
6+
7+
### Fix
8+
9+
- output glitch
10+
11+
## 0.1.0 (1970-01-01)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 0.2.0 (2025-01-01)
2+
3+
### Feat
4+
5+
- add new output
6+
7+
### Fix
8+
9+
- output glitch
10+
11+
## 0.1.0 (1970-01-01)

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_bump_.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only]
66
[--increment-mode {linear,exact}] [--check-consistency]
77
[--annotated-tag]
88
[--annotated-tag-message ANNOTATED_TAG_MESSAGE] [--gpg-sign]
9-
[--changelog-to-stdout] [--git-output-to-stderr] [--retry]
10-
[--major-version-zero] [--template TEMPLATE] [--extra EXTRA]
11-
[--file-name FILE_NAME] [--prerelease-offset PRERELEASE_OFFSET]
9+
[--changelog-to-stdout] [--merge-prerelease]
10+
[--git-output-to-stderr] [--retry] [--major-version-zero]
11+
[--template TEMPLATE] [--extra EXTRA] [--file-name FILE_NAME]
12+
[--prerelease-offset PRERELEASE_OFFSET]
1213
[--version-scheme {pep440,semver,semver2}]
1314
[--version-type {pep440,semver,semver2}]
1415
[--build-metadata BUILD_METADATA] [--get-next]
@@ -64,6 +65,9 @@ options:
6465
--gpg-sign, -s Sign tag instead of lightweight one.
6566
--changelog-to-stdout
6667
Output changelog to stdout.
68+
--merge-prerelease Collect all changes from prereleases into the next
69+
non-prerelease when generating the changelog.
70+
Overrides the `changelog_merge_prerelease` setting.
6771
--git-output-to-stderr
6872
Redirect git output to stderr.
6973
--retry Retry commit if it fails for the first time.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_bump_.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only]
66
[--increment-mode {linear,exact}] [--check-consistency]
77
[--annotated-tag]
88
[--annotated-tag-message ANNOTATED_TAG_MESSAGE] [--gpg-sign]
9-
[--changelog-to-stdout] [--git-output-to-stderr] [--retry]
10-
[--major-version-zero] [--template TEMPLATE] [--extra EXTRA]
11-
[--file-name FILE_NAME] [--prerelease-offset PRERELEASE_OFFSET]
9+
[--changelog-to-stdout] [--merge-prerelease]
10+
[--git-output-to-stderr] [--retry] [--major-version-zero]
11+
[--template TEMPLATE] [--extra EXTRA] [--file-name FILE_NAME]
12+
[--prerelease-offset PRERELEASE_OFFSET]
1213
[--version-scheme {pep440,semver,semver2}]
1314
[--version-type {pep440,semver,semver2}]
1415
[--build-metadata BUILD_METADATA] [--get-next]
@@ -64,6 +65,9 @@ options:
6465
--gpg-sign, -s Sign tag instead of lightweight one.
6566
--changelog-to-stdout
6667
Output changelog to stdout.
68+
--merge-prerelease Collect all changes from prereleases into the next
69+
non-prerelease when generating the changelog.
70+
Overrides the `changelog_merge_prerelease` setting.
6771
--git-output-to-stderr
6872
Redirect git output to stderr.
6973
--retry Retry commit if it fails for the first time.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_bump_.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only]
66
[--increment-mode {linear,exact}] [--check-consistency]
77
[--annotated-tag]
88
[--annotated-tag-message ANNOTATED_TAG_MESSAGE] [--gpg-sign]
9-
[--changelog-to-stdout] [--git-output-to-stderr] [--retry]
10-
[--major-version-zero] [--template TEMPLATE] [--extra EXTRA]
11-
[--file-name FILE_NAME] [--prerelease-offset PRERELEASE_OFFSET]
9+
[--changelog-to-stdout] [--merge-prerelease]
10+
[--git-output-to-stderr] [--retry] [--major-version-zero]
11+
[--template TEMPLATE] [--extra EXTRA] [--file-name FILE_NAME]
12+
[--prerelease-offset PRERELEASE_OFFSET]
1213
[--version-scheme {pep440,semver,semver2}]
1314
[--version-type {pep440,semver,semver2}]
1415
[--build-metadata BUILD_METADATA] [--get-next]
@@ -64,6 +65,9 @@ options:
6465
--gpg-sign, -s Sign tag instead of lightweight one.
6566
--changelog-to-stdout
6667
Output changelog to stdout.
68+
--merge-prerelease Collect all changes from prereleases into the next
69+
non-prerelease when generating the changelog.
70+
Overrides the `changelog_merge_prerelease` setting.
6771
--git-output-to-stderr
6872
Redirect git output to stderr.
6973
--retry Retry commit if it fails for the first time.

0 commit comments

Comments
 (0)