Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
},
],
Expand Down
9 changes: 9 additions & 0 deletions commitizen/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -17,6 +18,7 @@ class VersionArgs(TypedDict, total=False):
verbose: bool
major: bool
minor: bool
tag: bool


class Version:
Expand Down Expand Up @@ -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}"
Expand All @@ -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__)
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
39 changes: 39 additions & 0 deletions tests/commands/test_version_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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