Skip to content

Commit 9b137cd

Browse files
audricschiltknechtLee-W
authored andcommitted
feat: Add option in bump command to redirect git output to stderr
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.
1 parent 54f8423 commit 9b137cd

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

‎commitizen/cli.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@
186186
"default": False,
187187
"help": "Output changelog to the stdout",
188188
},
189+
{
190+
"name": ["--git-output-to-stderr"],
191+
"action": "store_true",
192+
"default": False,
193+
"help": "Redirect git output to stderr",
194+
},
189195
{
190196
"name": ["--retry"],
191197
"action": "store_true",

‎commitizen/commands/bump.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
5959
"update_changelog_on_bump"
6060
)
6161
self.changelog_to_stdout=arguments["changelog_to_stdout"]
62+
self.git_output_to_stderr=arguments["git_output_to_stderr"]
6263
self.no_verify=arguments["no_verify"]
6364
self.check_consistency=arguments["check_consistency"]
6465
self.retry=arguments["retry"]
@@ -329,9 +330,15 @@ def __call__(self): # noqa: C901
329330
raiseBumpCommitFailedError(f'2nd git.commit error: "{c.err.strip()}"')
330331

331332
ifc.out:
332-
out.write(c.out)
333+
ifself.git_output_to_stderr:
334+
out.diagnostic(c.out)
335+
else:
336+
out.write(c.out)
333337
ifc.err:
334-
out.write(c.err)
338+
ifself.git_output_to_stderr:
339+
out.diagnostic(c.err)
340+
else:
341+
out.write(c.err)
335342

336343
c=git.tag(
337344
new_tag_version,

‎docs/bump.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--changelog]
5757
[--bump-message BUMP_MESSAGE] [--prerelease{alpha,beta,rc}]
5858
[--devrelease DEVRELEASE] [--increment{MAJOR,MINOR,PATCH}]
5959
[--check-consistency] [--annotated-tag] [--gpg-sign]
60-
[--changelog-to-stdout] [--retry] [--major-version-zero]
60+
[--changelog-to-stdout] [--git-output-to-stderr] [--retry] [--major-version-zero]
6161
[MANUAL_VERSION]
6262

6363
positional arguments:
@@ -91,6 +91,8 @@ options:
9191
--gpg-sign, -s sign tag instead of lightweight one
9292
--changelog-to-stdout
9393
Output changelog to the stdout
94+
--git-output-to-stderr
95+
Redirect git output to stderr
9496
--retry retry commit if it fails the 1st time
9597
--major-version-zero keep major version at zero, even for breaking changes
9698
--prerelease-offset start pre-releases with this offset
@@ -196,6 +198,13 @@ Example:
196198
cz bump --changelog --changelog-to-stdout > body.md
197199
```
198200
201+
### `--git-output-to-stderr`
202+
203+
If `--git-output-to-stderr` is used, git commands output is redirected to stderr.
204+
205+
This command is useful when used with `--changelog-to-stdout` and piping the output to a file,
206+
and you don't want the `git commit` output polluting the stdout.
207+
199208
### `--retry`
200209
201210
If you use tools like [pre-commit](https://pre-commit.com/), add this flag.

‎tests/commands/test_bump_command.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ importannotations
22

33
importinspect
4+
importre
45
importsys
56
fromunittest.mockimportMagicMock, call
67

@@ -597,6 +598,34 @@ def test_bump_with_changelog_to_stdout_dry_run_arg(
597598
assert"0.2.0"inout
598599

599600

601+
@pytest.mark.usefixtures("tmp_commitizen_project")
602+
deftest_bump_without_git_to_stdout_arg(mocker: MockFixture, capsys, changelog_path):
603+
create_file_and_commit("feat(user): this should appear in stdout")
604+
testargs= ["cz", "bump", "--yes"]
605+
mocker.patch.object(sys, "argv", testargs)
606+
cli.main()
607+
out, _=capsys.readouterr()
608+
609+
assert (
610+
re.search(r"^\[master \w+] bump: version 0.1.0 → 0.2.0", out, re.MULTILINE)
611+
isnotNone
612+
)
613+
614+
615+
@pytest.mark.usefixtures("tmp_commitizen_project")
616+
deftest_bump_with_git_to_stdout_arg(mocker: MockFixture, capsys, changelog_path):
617+
create_file_and_commit("feat(user): this should appear in stdout")
618+
testargs= ["cz", "bump", "--yes", "--git-output-to-stderr"]
619+
mocker.patch.object(sys, "argv", testargs)
620+
cli.main()
621+
out, _=capsys.readouterr()
622+
623+
assert (
624+
re.search(r"^\[master \w+] bump: version 0.1.0 → 0.2.0", out, re.MULTILINE)
625+
isNone
626+
)
627+
628+
600629
@pytest.mark.parametrize(
601630
"version_filepath, version_regex, version_file_content",
602631
[

0 commit comments

Comments
(0)