From 3c1295a9465d89b6cced8c78cdf459f7531cfac5 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Wed, 14 Jan 2026 12:26:11 +0800 Subject: [PATCH] feat(cli): add cz --version back and add cz --report to separate them from cz version --- commitizen/cli.py | 27 ++++++++++++++++++++++++++- tests/test_cli.py | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index e5538aeb4..a4a5a4d7a 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -2,6 +2,7 @@ import argparse import logging +import platform import sys from copy import deepcopy from functools import partial @@ -13,6 +14,7 @@ from decli import cli from commitizen import commands, config, out, version_schemes +from commitizen.__version__ import __version__ from commitizen.exceptions import ( CommitizenException, ExitCode, @@ -102,6 +104,16 @@ 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", @@ -629,6 +641,8 @@ class Args(argparse.Namespace): def main() -> None: + sys.excepthook = commitizen_excepthook + parser: argparse.ArgumentParser = cli(data) argcomplete.autocomplete(parser) # Show help if no arg provided @@ -636,6 +650,18 @@ def main() -> None: parser.print_help(sys.stderr) raise ExpectedExit() + # TODO(bearomorphism): mark `cz version --commitizen` as deprecated after `cz version` feature is stable + if "--version" in sys.argv: + out.write(__version__) + raise ExpectedExit() + + # TODO(bearomorphism): mark `cz version --report` as deprecated after `cz version` feature is stable + if "--report" in sys.argv: + out.write(f"Commitizen Version: {__version__}") + out.write(f"Python Version: {sys.version}") + out.write(f"Operating System: {platform.system()}") + raise ExpectedExit() + # This is for the command required constraint in 2.0 try: args, unknown_args = parser.parse_known_args() @@ -673,7 +699,6 @@ def main() -> None: elif not conf.path: conf.update({"name": "cz_conventional_commits"}) - sys.excepthook = commitizen_excepthook if args.debug: logging.getLogger("commitizen").setLevel(logging.DEBUG) sys.excepthook = partial(sys.excepthook, debug=True) diff --git a/tests/test_cli.py b/tests/test_cli.py index 9a362e81e..91ab6ba39 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -8,6 +8,7 @@ from pytest_mock import MockFixture from commitizen import cli +from commitizen.__version__ import __version__ from commitizen.exceptions import ( ConfigFileNotFound, ExpectedExit, @@ -37,6 +38,24 @@ def test_cz_with_arg_but_without_command(util: UtilFixture): assert "Command is required" in str(excinfo.value) +def test_cz_with_version_arg(util: UtilFixture, capsys): + """Test that cz shows the version when --version is used.""" + with pytest.raises(ExpectedExit): + util.run_cli("--version") + 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()