diff --git a/commitizen/cli.py b/commitizen/cli.py index e00a52821..03371f053 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 a5b73aca4..13e30a90a 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 28b7f25ef..5446ef3c4 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/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index baa2a4249..acc2ad69a 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", [