Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option in bump command to redirect git output to stderr #735

Merged
merged 1 commit into from
Jun 23, 2023
Merged
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 @@ -185,6 +185,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",
Expand Down
11 changes: 9 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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"]
Expand Down Expand Up @@ -327,9 +328,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,
Expand Down
11 changes: 10 additions & 1 deletion docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import re
import sys
from typing import Tuple
from unittest.mock import MagicMock, call
Expand Down Expand Up @@ -596,6 +597,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",
[
Expand Down