Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
},
{
Expand Down
69 changes: 52 additions & 17 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions docs/commands/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion docs/tutorials/release-scm.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Loading