Skip to content

Commit c366400

Browse files
committed
feat(version): add --tag tag to version command
1 parent 5857050 commit c366400

8 files changed

+69
-5
lines changed

commitizen/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ def __call__(
553553
"action": "store_true",
554554
"exclusive_group": "group2",
555555
},
556+
{
557+
"name": ["--tag"],
558+
"help": "get the version with tag prefix. Need to be used with --project or --verbose.",
559+
"action": "store_true",
560+
"exclusive_group": "group2",
561+
},
556562
],
557563
},
558564
],

commitizen/commands/version.py

Lines changed: 9 additions & 0 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}"
@@ -73,5 +78,9 @@ def __call__(self) -> None:
7378
)
7479
return
7580

81+
if self.arguments.get("tag"):
82+
out.error("Tag can only be used with --project or --verbose.")
83+
return
84+
7685
# If no arguments are provided, just show the installed commitizen version
7786
out.write(__version__)

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_version_.txt

Lines changed: 3 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] [--major | --minor | --tag]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -14,3 +14,5 @@ options:
1414
or --verbose.
1515
--minor get just the minor version. Need to be used with --project
1616
or --verbose.
17+
--tag get the version with tag prefix. Need to be used with
18+
--project or --verbose.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_version_.txt

Lines changed: 3 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] [--major | --minor | --tag]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -14,3 +14,5 @@ options:
1414
or --verbose.
1515
--minor get just the minor version. Need to be used with --project
1616
or --verbose.
17+
--tag get the version with tag prefix. Need to be used with
18+
--project or --verbose.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_version_.txt

Lines changed: 3 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] [--major | --minor | --tag]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -14,3 +14,5 @@ options:
1414
or --verbose.
1515
--minor get just the minor version. Need to be used with --project
1616
or --verbose.
17+
--tag get the version with tag prefix. Need to be used with
18+
--project or --verbose.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_version_.txt

Lines changed: 3 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] [--major | --minor | --tag]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -14,3 +14,5 @@ options:
1414
or --verbose.
1515
--minor get just the minor version. Need to be used with --project
1616
or --verbose.
17+
--tag get the version with tag prefix. Need to be used with
18+
--project or --verbose.

tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_version_.txt

Lines changed: 3 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] [--major | --minor | --tag]
22

33
get the version of the installed commitizen or the current project (default:
44
installed commitizen)
@@ -14,3 +14,5 @@ options:
1414
or --verbose.
1515
--minor get just the minor version. Need to be used with --project
1616
or --verbose.
17+
--tag get the version with tag prefix. Need to be used with
18+
--project or --verbose.

tests/commands/test_version_command.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,42 @@ def test_version_just_major_error_no_project(config, capsys, argument: str):
161161
"Major or minor version can only be used with --project or --verbose."
162162
in captured.err
163163
)
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 "Tag can only be used with --project or --verbose." in captured.err

0 commit comments

Comments
 (0)