Skip to content

Commit

Permalink
feat: Add option in bump command to redirect git output to stderr
Browse files Browse the repository at this point in the history
This is useful used in conjunction with `--changelog-to-stdout` if you
don't need/want the resulting output to contain the `git commit` log
messages.
  • Loading branch information
audricschiltknecht authored and Lee-W committed Jun 23, 2023
1 parent 54f8423 commit 9b137cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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,
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,6 +1,7 @@
from __future__ import annotations

import inspect
import re
import sys
from unittest.mock import MagicMock, call

Expand Down Expand Up @@ -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",
[
Expand Down

0 comments on commit 9b137cd

Please sign in to comment.