diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e7fee1c793..1ccfd530a5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,7 +44,7 @@ repos: args: ["--write-changes", "--ignore-words-list", "asend"] - repo: https://github.com/commitizen-tools/commitizen - rev: v3.4.1 # automatically updated by Commitizen + rev: v3.5.0 # automatically updated by Commitizen hooks: - id: commitizen - id: commitizen-branch diff --git a/CHANGELOG.md b/CHANGELOG.md index c04bb27616..86b158efeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ +## v3.5.0 (2023-06-23) + +### Feat + +- Add option in bump command to redirect git output to stderr + ## v3.4.1 (2023-06-23) ### Fix diff --git a/commitizen/__version__.py b/commitizen/__version__.py index a5cfdf595f..dcbfb52f61 100644 --- a/commitizen/__version__.py +++ b/commitizen/__version__.py @@ -1 +1 @@ -__version__ = "3.4.1" +__version__ = "3.5.0" diff --git a/commitizen/cli.py b/commitizen/cli.py index e00a528213..03371f0537 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -186,6 +186,12 @@ "default": False, "help": "Output changelog to the stdout", }, + { + "name": ["--git-output-to-stderr"], + "action": "store_true", + "default": False, + "help": "Redirect git output to stderr", + }, { "name": ["--retry"], "action": "store_true", diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index a5b73aca4b..13e30a90a6 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -59,6 +59,7 @@ def __init__(self, config: BaseConfig, arguments: dict): "update_changelog_on_bump" ) self.changelog_to_stdout = arguments["changelog_to_stdout"] + self.git_output_to_stderr = arguments["git_output_to_stderr"] self.no_verify = arguments["no_verify"] self.check_consistency = arguments["check_consistency"] self.retry = arguments["retry"] @@ -329,9 +330,15 @@ def __call__(self): # noqa: C901 raise BumpCommitFailedError(f'2nd git.commit error: "{c.err.strip()}"') if c.out: - out.write(c.out) + if self.git_output_to_stderr: + out.diagnostic(c.out) + else: + out.write(c.out) if c.err: - out.write(c.err) + if self.git_output_to_stderr: + out.diagnostic(c.err) + else: + out.write(c.err) c = git.tag( new_tag_version, diff --git a/docs/bump.md b/docs/bump.md index 28b7f25efa..5446ef3c42 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -57,7 +57,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--changelog] [--bump-message BUMP_MESSAGE] [--prerelease {alpha,beta,rc}] [--devrelease DEVRELEASE] [--increment {MAJOR,MINOR,PATCH}] [--check-consistency] [--annotated-tag] [--gpg-sign] - [--changelog-to-stdout] [--retry] [--major-version-zero] + [--changelog-to-stdout] [--git-output-to-stderr] [--retry] [--major-version-zero] [MANUAL_VERSION] positional arguments: @@ -91,6 +91,8 @@ options: --gpg-sign, -s sign tag instead of lightweight one --changelog-to-stdout Output changelog to the stdout + --git-output-to-stderr + Redirect git output to stderr --retry retry commit if it fails the 1st time --major-version-zero keep major version at zero, even for breaking changes --prerelease-offset start pre-releases with this offset @@ -196,6 +198,13 @@ Example: cz bump --changelog --changelog-to-stdout > body.md ``` +### `--git-output-to-stderr` + +If `--git-output-to-stderr` is used, git commands output is redirected to stderr. + +This command is useful when used with `--changelog-to-stdout` and piping the output to a file, +and you don't want the `git commit` output polluting the stdout. + ### `--retry` If you use tools like [pre-commit](https://pre-commit.com/), add this flag. diff --git a/pyproject.toml b/pyproject.toml index 6f99fd9c17..258a29a4ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.commitizen] -version = "3.4.1" +version = "3.5.0" tag_format = "v$version" version_files = [ "pyproject.toml:version", @@ -9,7 +9,7 @@ version_files = [ [tool.poetry] name = "commitizen" -version = "3.4.1" +version = "3.5.0" description = "Python commitizen client tool" authors = ["Santiago Fraire "] license = "MIT" diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index baa2a4249b..acc2ad69a6 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1,6 +1,7 @@ from __future__ import annotations import inspect +import re import sys from unittest.mock import MagicMock, call @@ -597,6 +598,34 @@ def test_bump_with_changelog_to_stdout_dry_run_arg( assert "0.2.0" in out +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_without_git_to_stdout_arg(mocker: MockFixture, capsys, changelog_path): + create_file_and_commit("feat(user): this should appear in stdout") + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + out, _ = capsys.readouterr() + + assert ( + re.search(r"^\[master \w+] bump: version 0.1.0 → 0.2.0", out, re.MULTILINE) + is not None + ) + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_with_git_to_stdout_arg(mocker: MockFixture, capsys, changelog_path): + create_file_and_commit("feat(user): this should appear in stdout") + testargs = ["cz", "bump", "--yes", "--git-output-to-stderr"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + out, _ = capsys.readouterr() + + assert ( + re.search(r"^\[master \w+] bump: version 0.1.0 → 0.2.0", out, re.MULTILINE) + is None + ) + + @pytest.mark.parametrize( "version_filepath, version_regex, version_file_content", [