Skip to content
Open
31 changes: 30 additions & 1 deletion commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import logging
import platform
import sys
from copy import deepcopy
from functools import partial
Expand All @@ -13,6 +14,7 @@
from decli import cli

from commitizen import commands, config, out, version_schemes
from commitizen.__version__ import __version__
from commitizen.defaults import DEFAULT_SETTINGS
from commitizen.exceptions import (
CommitizenException,
Expand Down Expand Up @@ -104,10 +106,20 @@ def __call__(
"required": False,
"help": "Comma-separated error codes that won't raise error, e.g., cz -nr 1,2,3 bump. See codes at https://commitizen-tools.github.io/commitizen/exit_codes/",
},
{
"name": ["-v", "--version"],
"action": "store_true",
"help": "Show the version of the installed commitizen",
},
{
"name": ["--report"],
"action": "store_true",
"help": "Show system information for reporting bugs",
},
],
"subcommands": {
"title": "commands",
"required": True,
"required": False,
"commands": [
{
"name": ["init"],
Expand Down Expand Up @@ -685,6 +697,23 @@ def main() -> None:
raise NoCommandFoundError()
raise e

if getattr(args, "version", False):
out.write(__version__)
raise ExpectedExit()

if not hasattr(args, "func"):
if unknown_args:
try:
parser.parse_args()
except SystemExit:
raise NoCommandFoundError()
if getattr(args, "report", False):
out.write(f"Commitizen Version: {__version__}")
out.write(f"Python Version: {sys.version}")
out.write(f"Operating System: {platform.system()}")
raise ExpectedExit()
raise NoCommandFoundError()

arguments = vars(args)
if unknown_args:
# Raise error for extra-args without -- separation
Expand Down
13 changes: 13 additions & 0 deletions commitizen/commands/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import platform
import sys
import warnings
from typing import TypedDict

from packaging.version import InvalidVersion
Expand Down Expand Up @@ -45,6 +46,12 @@ def __init__(self, config: BaseConfig, arguments: VersionArgs) -> None:

def __call__(self) -> None:
if self.arguments.get("report"):
warnings.warn(
"`cz version --report` is deprecated and will be removed in v5. "
"Use `cz --report` instead.",
DeprecationWarning,
stacklevel=2,
)
out.write(f"Commitizen Version: {__version__}")
out.write(f"Python Version: {sys.version}")
out.write(f"Operating System: {platform.system()}")
Expand All @@ -54,6 +61,12 @@ def __call__(self) -> None:
out.write(f"Installed Commitizen Version: {__version__}")

if self.arguments.get("commitizen"):
warnings.warn(
"`cz version --commitizen` is deprecated and will be removed in v5. "
"Use `cz --version` instead.",
DeprecationWarning,
stacklevel=2,
)
out.write(__version__)
return

Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Commitizen provides a comprehensive CLI with various commands. Here's the comple

| Command | Description | Alias |
|---------|-------------|-------|
| `cz --version` | Show installed Commitizen version | `cz -v` |
| `cz --report` | Show system information for bug reports | - |
| `cz init` | Initialize Commitizen configuration | - |
| `cz commit` | Create a new commit | `cz c` |
| `cz bump` | Bump version and update changelog | - |
Expand Down
9 changes: 9 additions & 0 deletions docs/commands/version.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Get the version of the installed Commitizen or the current project (default: installed commitizen).

## Quick version and report

The following top-level flags are available for convenience:

- **`cz --version`** (or **`cz -v`**): print the installed Commitizen version and exit.
- **`cz --report`**: print system information (Commitizen version, Python version, operating system) useful for bug reports, and exit.

## Usage

![cz version --help](../images/cli_help/cz_version___help.svg)
Expand All @@ -20,6 +27,8 @@ Get the version of the installed Commitizen or the current project (default: ins
## Examples

```bash
cz --version # quick: installed commitizen version
cz --report # quick: system info for bug reports
cz version --project
cz version 2.0.0 --next MAJOR
cz version --project --major
Expand Down
24 changes: 16 additions & 8 deletions tests/commands/test_version_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ def test_version_for_showing_project_version(config, capsys):

@pytest.mark.parametrize("project", [True, False])
def test_version_for_showing_commitizen_version(config, capsys, project: bool):
commands.Version(
config,
{"project": project, "commitizen": True},
)()
with pytest.warns(
DeprecationWarning,
match=r"`cz version --commitizen` is deprecated.*Use `cz --version` instead",
):
commands.Version(
config,
{"project": project, "commitizen": True},
)()
captured = capsys.readouterr()
assert f"{__version__}" in captured.out

Expand Down Expand Up @@ -63,10 +67,14 @@ def test_version_for_showing_both_versions(config, capsys):


def test_version_for_showing_commitizen_system_info(config, capsys):
commands.Version(
config,
{"report": True},
)()
with pytest.warns(
DeprecationWarning,
match=r"`cz version --report` is deprecated.*Use `cz --report` instead",
):
commands.Version(
config,
{"report": True},
)()
captured = capsys.readouterr()
assert f"Commitizen Version: {__version__}" in captured.out
assert f"Python Version: {sys.version}" in captured.out
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytest_mock import MockFixture

from commitizen import cli
from commitizen.__version__ import __version__
from commitizen.exceptions import (
ConfigFileNotFound,
ExpectedExit,
Expand Down Expand Up @@ -55,6 +56,33 @@ def test_cz_with_arg_but_without_command(util: UtilFixture):
assert "Command is required" in str(excinfo.value)


@pytest.mark.parametrize(
"args",
[
("--version",),
("-v",),
("--version", "bump"),
],
ids=["long-flag", "short-flag", "precedence-over-subcommand"],
)
def test_cz_with_version_arg(util: UtilFixture, capsys, args):
"""Test that cz --version / -v shows the version and takes precedence."""
with pytest.raises(ExpectedExit):
util.run_cli(*args)
out, _ = capsys.readouterr()
assert __version__ in out


def test_cz_with_report_arg(util: UtilFixture, capsys):
"""Test that cz shows the report when --report is used."""
with pytest.raises(ExpectedExit):
util.run_cli("--report")
out, _ = capsys.readouterr()
assert "Commitizen Version:" in out
assert "Python Version:" in out
assert "Operating System:" in out


def test_name(util: UtilFixture, capsys):
util.run_cli("-n", "cz_jira", "example")
out, _ = capsys.readouterr()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
cz: error: unrecognized arguments: --invalid-arg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
cz: error: unrecognized arguments: --invalid-arg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
cz: error: unrecognized arguments: --invalid-arg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
cz: error: unrecognized arguments: --invalid-arg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
cz: error: unrecognized arguments: --invalid-arg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)
Comment on lines +1 to 4
5 changes: 4 additions & 1 deletion tests/test_cli/test_no_argv_py_3_10_.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Expand All @@ -16,6 +17,8 @@ options:
e.g., cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/
-v, --version Show the version of the installed commitizen
--report Show system information for reporting bugs

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Expand Down
5 changes: 4 additions & 1 deletion tests/test_cli/test_no_argv_py_3_11_.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Expand All @@ -16,6 +17,8 @@ options:
e.g., cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/
-v, --version Show the version of the installed commitizen
--report Show system information for reporting bugs

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Expand Down
5 changes: 4 additions & 1 deletion tests/test_cli/test_no_argv_py_3_12_.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Expand All @@ -16,6 +17,8 @@ options:
e.g., cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/
-v, --version Show the version of the installed commitizen
--report Show system information for reporting bugs

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Expand Down
5 changes: 4 additions & 1 deletion tests/test_cli/test_no_argv_py_3_13_.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
Expand All @@ -15,6 +16,8 @@ options:
e.g., cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/
-v, --version Show the version of the installed commitizen
--report Show system information for reporting bugs

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Expand Down
5 changes: 4 additions & 1 deletion tests/test_cli/test_no_argv_py_3_14_.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
[--report]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...

Commitizen is a powerful release management tool that helps teams maintain consistent and meaningful commit messages while automating version management.
Expand All @@ -15,6 +16,8 @@ options:
e.g., cz -nr 1,2,3 bump. See codes at
https://commitizen-
tools.github.io/commitizen/exit_codes/
-v, --version Show the version of the installed commitizen
--report Show system information for reporting bugs

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Expand Down
Loading