From e1a8a65afda3389cb87c5a4d77d89889f5118ff4 Mon Sep 17 00:00:00 2001 From: Vamika Singhal Date: Thu, 19 Nov 2026 01:30:25 -0800 Subject: [PATCH] feat(bump): add --no-commit to tag the current HEAD without a bump commit `cz bump --no-commit` updates the version files and the changelog, runs the hooks and creates the tag as usual, but does not stage or commit anything: the tag points to the current HEAD and the updated files stay in the working tree. This is the explicit opt-out asked for in #753, for projects that keep the version in Git tags only (for example `version_provider = "scm"`) and do not want a release commit, such as a CI job tagging the commit that triggered it. Closes #753 Assisted By: AI Co-Authored-By: Claude Opus 5.5 --- commitizen/cli.py | 6 ++ commitizen/commands/bump.py | 69 ++++++++++++++----- docs/commands/bump.md | 21 ++++++ docs/tutorials/release-scm.md | 6 +- tests/commands/test_bump_command.py | 55 +++++++++++++++ ...ion_when_use_help_option_py_3_10_bump_.txt | 4 +- ...ion_when_use_help_option_py_3_11_bump_.txt | 4 +- ...ion_when_use_help_option_py_3_12_bump_.txt | 4 +- ...ion_when_use_help_option_py_3_13_bump_.txt | 4 +- ...ion_when_use_help_option_py_3_14_bump_.txt | 4 +- 10 files changed, 154 insertions(+), 23 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 3ee03d99ce..229539e77e 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -382,6 +382,12 @@ def __call__( "help": "Bump version without eligible commits.", "action": "store_true", }, + { + "name": ["--no-commit"], + "default": False, + "help": "Do not create the bump commit; tag the current HEAD and leave the updated files uncommitted.", + "action": "store_true", + }, ], }, { diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 3e92775e28..cd167772df 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -57,6 +57,7 @@ class BumpArgs(Settings, total=False): increment: Increment | None local_version: bool manual_version: str | None + no_commit: bool no_verify: bool prerelease: Prerelease | None retry: bool @@ -250,6 +251,53 @@ def _resolve_increment_and_new_version( exact_increment=self.arguments["increment_mode"] == "exact", ) + def _commit(self, message: str, updated_files: list[str]) -> None: + """Stage the files changed by the bump and create the bump commit. + + When ``--retry`` is set and the changelog was generated, a failed commit + (for example, a pre-commit hook that reformatted a file) is attempted once + more after staging the files again. + + Args: + message: The bump commit message. + updated_files: Paths of the files the bump modified. + + Raises: + BumpCommitFailedError: If the commit fails (after the optional retry). + """ + # FIXME: check if any changes have been staged + git.add(*updated_files) + c = git.commit(message, args=self._get_commit_args()) + if self.retry and c.return_code != 0 and self.changelog_flag: + # Maybe pre-commit reformatted some files? Retry once + logger.debug("1st git.commit error: %s", c.err) + logger.info("1st commit attempt failed; retrying once") + git.add(*updated_files) + c = git.commit(message, args=self._get_commit_args()) + if c.return_code != 0: + err = c.err.strip() or c.out + raise BumpCommitFailedError(f'2nd git.commit error: "{err}"') + + for msg in (c.out, c.err): + if msg: + out_func = out.diagnostic if self.git_output_to_stderr else out.write + out_func(msg) + + def _report_skipped_commit(self) -> None: + """Tell the user that ``--no-commit`` left the bump uncommitted. + + The files the bump modified stay in the working tree, neither staged nor + committed, and the tag is created on the current ``HEAD``. + """ + information = ( + "--no-commit: skipping the bump commit, tagging the current HEAD " + "(updated files stay uncommitted)" + ) + # Keep stdout clean when the changelog is written to it (same as the + # bump information above). + out_func = out.diagnostic if self.changelog_to_stdout else out.write + out_func(information) + def __call__(self) -> None: """Steps executed to bump.""" provider = get_provider(self.config) @@ -392,23 +440,10 @@ def __call__(self) -> None: if self.arguments.get("version_files_only"): raise ExpectedExit() - # FIXME: check if any changes have been staged - git.add(*updated_files) - c = git.commit(message, args=self._get_commit_args()) - if self.retry and c.return_code != 0 and self.changelog_flag: - # Maybe pre-commit reformatted some files? Retry once - logger.debug("1st git.commit error: %s", c.err) - logger.info("1st commit attempt failed; retrying once") - git.add(*updated_files) - c = git.commit(message, args=self._get_commit_args()) - if c.return_code != 0: - err = c.err.strip() or c.out - raise BumpCommitFailedError(f'2nd git.commit error: "{err}"') - - for msg in (c.out, c.err): - if msg: - out_func = out.diagnostic if self.git_output_to_stderr else out.write - out_func(msg) + if self.arguments.get("no_commit"): + self._report_skipped_commit() + else: + self._commit(message, updated_files) c = git.tag( new_tag_version, diff --git a/docs/commands/bump.md b/docs/commands/bump.md index 84666cbc66..ac6bdcea07 100644 --- a/docs/commands/bump.md +++ b/docs/commands/bump.md @@ -390,6 +390,26 @@ cz bump --allow-no-commit 2.0.0 This makes the new release visible in the changelog while still showing that no commit-based changes were included. +### `--no-commit` + +Create the tag without the bump commit. Commitizen still updates the [`version_files`][version_files] and the changelog, runs the hooks, and creates the tag, but the tag points to the current `HEAD` and the updated files stay uncommitted in the working tree. + +```bash +cz bump --no-commit +``` + +Use it when the version lives in the Git tags only (for example with the `scm` [version provider][version_provider]) and you do not want a release commit, such as a CI job that tags the commit that triggered it: + +```bash +cz bump --yes --no-commit +git push --tags +``` + +!!! note + `--no-commit` is not `--allow-no-commit`: `--allow-no-commit` bumps the version when there are no eligible commits, `--no-commit` bumps the version without creating a commit. + + `--version-files-only` also skips the commit, but it skips the tag and the hooks as well. + ### `--tag-format` `tag_format` and [version_scheme][version_scheme] are combined to make Git tag names from versions. @@ -539,3 +559,4 @@ Commitizen supports the [PEP 440][pep440] version format, which includes several [pep440]: https://www.python.org/dev/peps/pep-0440/ [semver]: https://semver.org/ [version_files]: ../config/bump.md#version_files +[version_provider]: ../config/version_provider.md diff --git a/docs/tutorials/release-scm.md b/docs/tutorials/release-scm.md index 00ae9d70fa..9364f084b8 100644 --- a/docs/tutorials/release-scm.md +++ b/docs/tutorials/release-scm.md @@ -1,6 +1,6 @@ # Releasing with the SCM Version Provider -In this tutorial, you will learn how to bump the version using the `scp` version provider; as you can't use `cz bump`. +In this tutorial, you will learn how to bump the version using the `scm` version provider, where `cz bump` has nothing to write the new version to. ## About @@ -15,6 +15,10 @@ version_provider = "scm" This means `cz bump` cannot complete its usual job of writing the new version anywhere (see [Why can't I bump with SCM?](#why-cant-i-bump-with-scm) below). In the next section, you will learn how to do it anyways. +!!! tip + `cz bump --no-commit` creates the tag on the current commit without a release commit, so it works with the `scm` provider. + See [`--no-commit`](../commands/bump.md#-no-commit). The next section shows how to do the same with `git` directly. + ## Creating a New Version Tag Manually To release a new version without an extra commit and using tags, diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 2e3921deab..9cd5ca6a59 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -485,6 +485,61 @@ def test_bump_files_only(tmp_commitizen_project, util: UtilFixture): assert "0.3.0" in tmp_commitizen_cfg_file.read_text(encoding="utf-8") +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_no_commit_tags_head_and_leaves_files_uncommitted( + util: UtilFixture, mocker: MockFixture, capsys: pytest.CaptureFixture +): + """Issue 753: `--no-commit` creates the tag without a bump commit. + + The new version is still written to the config file, but the file stays + unstaged and uncommitted in the working tree, and the tag points to the + commit that was already `HEAD` before the bump. + """ + # Arrange + util.create_file_and_commit("feat: new file") + head_before = cmd.run(["git", "rev-parse", "HEAD"]).out.strip() + git_commit_spy = mocker.spy(git, "commit") + + # Act + util.run_cli("bump", "--yes", "--no-commit") + out, _ = capsys.readouterr() + + # Assert + assert git.tag_exist("0.2.0") is True + assert cmd.run(["git", "rev-list", "-n", "1", "0.2.0"]).out.strip() == head_before + assert cmd.run(["git", "rev-parse", "HEAD"]).out.strip() == head_before + git_commit_spy.assert_not_called() + assert "0.2.0" in Path("pyproject.toml").read_text(encoding="utf-8") + assert cmd.run(["git", "status", "--porcelain"]).out.splitlines() == [ + " M pyproject.toml" + ] + assert "--no-commit: skipping the bump commit" in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_no_commit_with_changelog_to_stdout( + util: UtilFixture, capsys: pytest.CaptureFixture, changelog_path: Path +): + """`--no-commit` keeps stdout for the changelog and leaves the changelog file untracked.""" + # Arrange + util.create_file_and_commit("feat(user): this should appear in stdout") + + # Act + util.run_cli("bump", "--yes", "--no-commit", "--changelog-to-stdout") + out, err = capsys.readouterr() + + # Assert + assert git.tag_exist("0.2.0") is True + assert out.startswith("#") + assert "this should appear in stdout" in out + assert "--no-commit" not in out + assert "--no-commit: skipping the bump commit" in err + assert "0.2.0" in changelog_path.read_text(encoding="utf-8") + status = cmd.run(["git", "status", "--porcelain"]).out + assert "?? CHANGELOG.md" in status + assert " M pyproject.toml" in status + + def test_bump_local_version(tmp_commitizen_project, util: UtilFixture): tmp_version_file = tmp_commitizen_project / "__version__.py" tmp_version_file.write_text("4.5.1+0.1.0") diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_bump_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_bump_.txt index fa696d063d..7cf2ea6b58 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_bump_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_bump_.txt @@ -12,7 +12,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only] [--version-scheme {pep440,semver,semver2}] [--version-type {pep440,semver,semver2}] [--build-metadata BUILD_METADATA] [--get-next] - [--allow-no-commit] + [--allow-no-commit] [--no-commit] [MANUAL_VERSION] Bump semantic version based on the git log @@ -85,3 +85,5 @@ options: Add additional build-metadata to the version-number. --get-next Determine the next version and write to stdout. --allow-no-commit Bump version without eligible commits. + --no-commit Do not create the bump commit; tag the current HEAD + and leave the updated files uncommitted. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_bump_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_bump_.txt index fa696d063d..7cf2ea6b58 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_bump_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_bump_.txt @@ -12,7 +12,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only] [--version-scheme {pep440,semver,semver2}] [--version-type {pep440,semver,semver2}] [--build-metadata BUILD_METADATA] [--get-next] - [--allow-no-commit] + [--allow-no-commit] [--no-commit] [MANUAL_VERSION] Bump semantic version based on the git log @@ -85,3 +85,5 @@ options: Add additional build-metadata to the version-number. --get-next Determine the next version and write to stdout. --allow-no-commit Bump version without eligible commits. + --no-commit Do not create the bump commit; tag the current HEAD + and leave the updated files uncommitted. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_bump_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_bump_.txt index fa696d063d..7cf2ea6b58 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_bump_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_bump_.txt @@ -12,7 +12,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only] [--version-scheme {pep440,semver,semver2}] [--version-type {pep440,semver,semver2}] [--build-metadata BUILD_METADATA] [--get-next] - [--allow-no-commit] + [--allow-no-commit] [--no-commit] [MANUAL_VERSION] Bump semantic version based on the git log @@ -85,3 +85,5 @@ options: Add additional build-metadata to the version-number. --get-next Determine the next version and write to stdout. --allow-no-commit Bump version without eligible commits. + --no-commit Do not create the bump commit; tag the current HEAD + and leave the updated files uncommitted. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_bump_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_bump_.txt index 8e4c863585..ad7933ec2c 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_bump_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_bump_.txt @@ -12,7 +12,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only] [--version-scheme {pep440,semver,semver2}] [--version-type {pep440,semver,semver2}] [--build-metadata BUILD_METADATA] [--get-next] - [--allow-no-commit] + [--allow-no-commit] [--no-commit] [MANUAL_VERSION] Bump semantic version based on the git log @@ -84,3 +84,5 @@ options: Add additional build-metadata to the version-number. --get-next Determine the next version and write to stdout. --allow-no-commit Bump version without eligible commits. + --no-commit Do not create the bump commit; tag the current HEAD + and leave the updated files uncommitted. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_bump_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_bump_.txt index 8e4c863585..ad7933ec2c 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_bump_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_bump_.txt @@ -12,7 +12,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--version-files-only] [--version-scheme {pep440,semver,semver2}] [--version-type {pep440,semver,semver2}] [--build-metadata BUILD_METADATA] [--get-next] - [--allow-no-commit] + [--allow-no-commit] [--no-commit] [MANUAL_VERSION] Bump semantic version based on the git log @@ -84,3 +84,5 @@ options: Add additional build-metadata to the version-number. --get-next Determine the next version and write to stdout. --allow-no-commit Bump version without eligible commits. + --no-commit Do not create the bump commit; tag the current HEAD + and leave the updated files uncommitted.