diff --git a/commitizen/cli.py b/commitizen/cli.py index 364bc93f85..2b0f102caf 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -553,6 +553,12 @@ def __call__( "action": "store_true", "exclusive_group": "group2", }, + { + "name": ["--tag"], + "help": "get the version with tag prefix. Need to be used with --project or --verbose.", + "action": "store_true", + "exclusive_group": "group2", + }, ], }, ], diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py index 9290e80b8f..c8a76fe27f 100644 --- a/commitizen/commands/version.py +++ b/commitizen/commands/version.py @@ -7,6 +7,7 @@ from commitizen.config import BaseConfig from commitizen.exceptions import NoVersionSpecifiedError, VersionSchemeUnknown from commitizen.providers import get_provider +from commitizen.tags import TagRules from commitizen.version_schemes import get_version_scheme @@ -17,6 +18,7 @@ class VersionArgs(TypedDict, total=False): verbose: bool major: bool minor: bool + tag: bool class Version: @@ -59,6 +61,9 @@ def __call__(self) -> None: version = f"{version_scheme.major}" elif self.arguments.get("minor"): version = f"{version_scheme.minor}" + elif self.arguments.get("tag"): + tag_rules = TagRules.from_settings(self.config.settings) + version = tag_rules.normalize_tag(version_scheme) out.write( f"Project Version: {version}" @@ -73,5 +78,9 @@ def __call__(self) -> None: ) return + if self.arguments.get("tag"): + out.error("Tag can only be used with --project or --verbose.") + return + # If no arguments are provided, just show the installed commitizen version out.write(__version__) diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt index a194615a98..96575f2982 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt @@ -1,4 +1,4 @@ -usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor] +usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor | --tag] get the version of the installed commitizen or the current project (default: installed commitizen) @@ -14,3 +14,5 @@ options: or --verbose. --minor get just the minor version. Need to be used with --project or --verbose. + --tag get the version with tag prefix. Need to be used with + --project or --verbose. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt index a194615a98..96575f2982 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt @@ -1,4 +1,4 @@ -usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor] +usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor | --tag] get the version of the installed commitizen or the current project (default: installed commitizen) @@ -14,3 +14,5 @@ options: or --verbose. --minor get just the minor version. Need to be used with --project or --verbose. + --tag get the version with tag prefix. Need to be used with + --project or --verbose. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt index a194615a98..96575f2982 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt @@ -1,4 +1,4 @@ -usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor] +usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor | --tag] get the version of the installed commitizen or the current project (default: installed commitizen) @@ -14,3 +14,5 @@ options: or --verbose. --minor get just the minor version. Need to be used with --project or --verbose. + --tag get the version with tag prefix. Need to be used with + --project or --verbose. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt index a194615a98..96575f2982 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt @@ -1,4 +1,4 @@ -usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor] +usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor | --tag] get the version of the installed commitizen or the current project (default: installed commitizen) @@ -14,3 +14,5 @@ options: or --verbose. --minor get just the minor version. Need to be used with --project or --verbose. + --tag get the version with tag prefix. Need to be used with + --project or --verbose. diff --git a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt index a194615a98..96575f2982 100644 --- a/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt +++ b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt @@ -1,4 +1,4 @@ -usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor] +usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor | --tag] get the version of the installed commitizen or the current project (default: installed commitizen) @@ -14,3 +14,5 @@ options: or --verbose. --minor get just the minor version. Need to be used with --project or --verbose. + --tag get the version with tag prefix. Need to be used with + --project or --verbose. diff --git a/tests/commands/test_version_command.py b/tests/commands/test_version_command.py index 17f7238403..af22ba7ad2 100644 --- a/tests/commands/test_version_command.py +++ b/tests/commands/test_version_command.py @@ -161,3 +161,42 @@ def test_version_just_major_error_no_project(config, capsys, argument: str): "Major or minor version can only be used with --project or --verbose." in captured.err ) + + +@pytest.mark.parametrize( + "version, tag_format, expected_output", + [ + ("1.2.3", "v$version", "v1.2.3\n"), + ("1.2.3", "$version", "1.2.3\n"), + ("2.0.0", "release-$version", "release-2.0.0\n"), + ("0.1.0", "ver$version", "ver0.1.0\n"), + ], +) +def test_version_with_tag_format( + config, capsys, version: str, tag_format: str, expected_output: str +): + """Test --tag option applies tag_format to version""" + config.settings["version"] = version + config.settings["tag_format"] = tag_format + commands.Version( + config, + { + "project": True, + "tag": True, + }, + )() + captured = capsys.readouterr() + assert captured.out == expected_output + + +def test_version_tag_without_project_error(config, capsys): + """Test --tag requires --project or --verbose""" + commands.Version( + config, + { + "tag": True, + }, + )() + captured = capsys.readouterr() + assert not captured.out + assert "Tag can only be used with --project or --verbose." in captured.err