Skip to content

Commit 82ed078

Browse files
committed
feat(version): add --tag for version command
1 parent 5857050 commit 82ed078

8 files changed

+70
-8
lines changed

commitizen/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,12 @@ def __call__(
541541
"action": "store_true",
542542
"exclusive_group": "group1",
543543
},
544+
{
545+
"name": ["--tag"],
546+
"help": "get the version with tag prefix",
547+
"action": "store_true",
548+
"exclusive_group": "group2",
549+
},
544550
{
545551
"name": ["--major"],
546552
"help": "get just the major version. Need to be used with --project or --verbose.",

commitizen/commands/version.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from commitizen.config import BaseConfig
88
from commitizen.exceptions import NoVersionSpecifiedError, VersionSchemeUnknown
99
from commitizen.providers import get_provider
10+
from commitizen.tags import TagRules
1011
from commitizen.version_schemes import get_version_scheme
1112

1213

@@ -17,6 +18,7 @@ class VersionArgs(TypedDict, total=False):
1718
verbose: bool
1819
major: bool
1920
minor: bool
21+
tag: bool
2022

2123

2224
class Version:
@@ -59,6 +61,9 @@ def __call__(self) -> None:
5961
version = f"{version_scheme.major}"
6062
elif self.arguments.get("minor"):
6163
version = f"{version_scheme.minor}"
64+
elif self.arguments.get("tag"):
65+
tag_rules = TagRules.from_settings(self.config.settings)
66+
version = tag_rules.normalize_tag(version_scheme)
6267

6368
out.write(
6469
f"Project Version: {version}"
@@ -67,9 +72,13 @@ def __call__(self) -> None:
6772
)
6873
return
6974

70-
if self.arguments.get("major") or self.arguments.get("minor"):
75+
if (
76+
self.arguments.get("major")
77+
or self.arguments.get("minor")
78+
or self.arguments.get("tag")
79+
):
7180
out.error(
72-
"Major or minor version can only be used with --project or --verbose."
81+
"Major or minor or tag can only be used with --project or --verbose."
7382
)
7483
return
7584

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]
1+
usage: cz version [-h] [-r | -p | -c | -v] [--tag | --major | --minor]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -10,6 +10,7 @@ options:
1010
-c, --commitizen get the version of the installed commitizen
1111
-v, --verbose get the version of both the installed commitizen and the
1212
current project
13+
--tag get the version with tag prefix
1314
--major get just the major version. Need to be used with --project
1415
or --verbose.
1516
--minor get just the minor version. Need to be used with --project

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]
1+
usage: cz version [-h] [-r | -p | -c | -v] [--tag | --major | --minor]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -10,6 +10,7 @@ options:
1010
-c, --commitizen get the version of the installed commitizen
1111
-v, --verbose get the version of both the installed commitizen and the
1212
current project
13+
--tag get the version with tag prefix
1314
--major get just the major version. Need to be used with --project
1415
or --verbose.
1516
--minor get just the minor version. Need to be used with --project

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]
1+
usage: cz version [-h] [-r | -p | -c | -v] [--tag | --major | --minor]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -10,6 +10,7 @@ options:
1010
-c, --commitizen get the version of the installed commitizen
1111
-v, --verbose get the version of both the installed commitizen and the
1212
current project
13+
--tag get the version with tag prefix
1314
--major get just the major version. Need to be used with --project
1415
or --verbose.
1516
--minor get just the minor version. Need to be used with --project

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]
1+
usage: cz version [-h] [-r | -p | -c | -v] [--tag | --major | --minor]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -10,6 +10,7 @@ options:
1010
-c, --commitizen get the version of the installed commitizen
1111
-v, --verbose get the version of both the installed commitizen and the
1212
current project
13+
--tag get the version with tag prefix
1314
--major get just the major version. Need to be used with --project
1415
or --verbose.
1516
--minor get just the minor version. Need to be used with --project

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: cz version [-h] [-r | -p | -c | -v] [--major | --minor]
1+
usage: cz version [-h] [-r | -p | -c | -v] [--tag | --major | --minor]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -10,6 +10,7 @@ options:
1010
-c, --commitizen get the version of the installed commitizen
1111
-v, --verbose get the version of both the installed commitizen and the
1212
current project
13+
--tag get the version with tag prefix
1314
--major get just the major version. Need to be used with --project
1415
or --verbose.
1516
--minor get just the minor version. Need to be used with --project

tests/commands/test_version_command.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,48 @@ def test_version_just_major_error_no_project(config, capsys, argument: str):
158158
captured = capsys.readouterr()
159159
assert not captured.out
160160
assert (
161-
"Major or minor version can only be used with --project or --verbose."
161+
"Major or minor or tag can only be used with --project or --verbose."
162+
in captured.err
163+
)
164+
165+
166+
@pytest.mark.parametrize(
167+
"version, tag_format, expected_output",
168+
[
169+
("1.2.3", "v$version", "v1.2.3\n"),
170+
("1.2.3", "$version", "1.2.3\n"),
171+
("2.0.0", "release-$version", "release-2.0.0\n"),
172+
("0.1.0", "ver$version", "ver0.1.0\n"),
173+
],
174+
)
175+
def test_version_with_tag_format(
176+
config, capsys, version: str, tag_format: str, expected_output: str
177+
):
178+
"""Test --tag option applies tag_format to version"""
179+
config.settings["version"] = version
180+
config.settings["tag_format"] = tag_format
181+
commands.Version(
182+
config,
183+
{
184+
"project": True,
185+
"tag": True,
186+
},
187+
)()
188+
captured = capsys.readouterr()
189+
assert captured.out == expected_output
190+
191+
192+
def test_version_tag_without_project_error(config, capsys):
193+
"""Test --tag requires --project or --verbose"""
194+
commands.Version(
195+
config,
196+
{
197+
"tag": True,
198+
},
199+
)()
200+
captured = capsys.readouterr()
201+
assert not captured.out
202+
assert (
203+
"Major or minor or tag can only be used with --project or --verbose."
162204
in captured.err
163205
)

0 commit comments

Comments
 (0)