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 @@ -541,6 +541,12 @@ def __call__(
"action": "store_true",
"exclusive_group": "group1",
},
{
"name": ["--tag"],
"help": "get the version with tag prefix",
"action": "store_true",
"exclusive_group": "group2",
},
{
"name": ["--major"],
"help": "get just the major version. Need to be used with --project or --verbose.",
Expand Down
17 changes: 13 additions & 4 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 @@ -67,10 +72,14 @@ def __call__(self) -> None:
)
return

if self.arguments.get("major") or self.arguments.get("minor"):
out.error(
"Major or minor version can only be used with --project or --verbose."
)
if self.arguments.get("major"):
out.error("Major can only be used with --project or --verbose.")
return
if self.arguments.get("minor"):
out.error("Minor can only be used with --project or --verbose.")
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
Expand Down
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] [--tag | --major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,6 +10,7 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--tag get the version with tag prefix
--major get just the major version. Need to be used with --project
or --verbose.
--minor get just the minor version. Need to be used with --project
Expand Down
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] [--tag | --major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,6 +10,7 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--tag get the version with tag prefix
--major get just the major version. Need to be used with --project
or --verbose.
--minor get just the minor version. Need to be used with --project
Expand Down
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] [--tag | --major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,6 +10,7 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--tag get the version with tag prefix
--major get just the major version. Need to be used with --project
or --verbose.
--minor get just the minor version. Need to be used with --project
Expand Down
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] [--tag | --major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,6 +10,7 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--tag get the version with tag prefix
--major get just the major version. Need to be used with --project
or --verbose.
--minor get just the minor version. Need to be used with --project
Expand Down
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] [--tag | --major | --minor]

get the version of the installed commitizen or the current project (default:
installed commitizen)
Expand All @@ -10,6 +10,7 @@ options:
-c, --commitizen get the version of the installed commitizen
-v, --verbose get the version of both the installed commitizen and the
current project
--tag get the version with tag prefix
--major get just the major version. Need to be used with --project
or --verbose.
--minor get just the minor version. Need to be used with --project
Expand Down
63 changes: 56 additions & 7 deletions tests/commands/test_version_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,66 @@ def test_version_just_minor(config, capsys, version: str, expected_version: str)
assert expected_version == captured.out


@pytest.mark.parametrize("argument", ("major", "minor"))
def test_version_just_major_error_no_project(config, capsys, argument: str):
def test_version_major_error_no_project(config, capsys):
"""Test --major requires --project or --verbose"""
commands.Version(
config,
{
argument: True, # type: ignore[misc]
"major": True,
},
)()
captured = capsys.readouterr()
assert not captured.out
assert (
"Major or minor version can only be used with --project or --verbose."
in captured.err
)
assert "Major can only be used with --project or --verbose." in captured.err


def test_version_minor_error_no_project(config, capsys):
"""Test --minor requires --project or --verbose"""
commands.Version(
config,
{
"minor": True,
},
)()
captured = capsys.readouterr()
assert not captured.out
assert "Minor can only be used with --project or --verbose." in captured.err


def test_version_tag_error_no_project(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


@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