-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathversion.py
More file actions
141 lines (119 loc) · 4.79 KB
/
Copy pathversion.py
File metadata and controls
141 lines (119 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import platform
import sys
from typing import TypedDict
from packaging.version import InvalidVersion
from commitizen import out
from commitizen.__version__ import __version__
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_increment import VersionIncrement
from commitizen.version_schemes import Increment, get_version_scheme
class VersionArgs(TypedDict, total=False):
manual_version: str | None
next: str | None
# Exclusive groups 1
commitizen: bool
report: bool
project: bool
verbose: bool
# Exclusive groups 2
major: bool
minor: bool
patch: bool
tag: bool
class Version:
"""Get the version of the installed commitizen or the current project.
Precedence:
1. report
2. commitizen
3. verbose, project
"""
def __init__(self, config: BaseConfig, arguments: VersionArgs) -> None:
self.config: BaseConfig = config
self.arguments = arguments
def __call__(self) -> None:
if self.arguments.get("report"):
out.write(f"Commitizen Version: {__version__}")
out.write(f"Python Version: {sys.version}")
out.write(f"Operating System: {platform.system()}")
return
if self.arguments.get("verbose"):
out.write(f"Installed Commitizen Version: {__version__}")
if self.arguments.get("commitizen"):
out.write(__version__)
return
if (
self.arguments.get("project")
or self.arguments.get("verbose")
or self.arguments.get("next")
or self.arguments.get("manual_version")
):
version_str = self.arguments.get("manual_version")
if version_str is None:
try:
version_str = get_provider(self.config).get_version()
except NoVersionSpecifiedError:
out.error("No project information in this project.")
return
try:
scheme_factory = get_version_scheme(self.config.settings)
except VersionSchemeUnknown:
out.error("Unknown version scheme.")
return
try:
version = scheme_factory(version_str)
except InvalidVersion:
out.error(f"Invalid version: '{version_str}'")
return
if next_increment_str := self.arguments.get("next"):
if next_increment_str == "USE_GIT_COMMITS":
# TODO: implement USE_GIT_COMMITS by deriving the increment from
# git history. This requires refactoring the bump logic out of
# `commitizen/commands/bump.py` so it can be reused here. See #1678.
out.error("--next USE_GIT_COMMITS is not implemented yet.")
return
next_increment = VersionIncrement.from_value(next_increment_str)
increment: Increment | None
if next_increment == VersionIncrement.NONE:
increment = None
elif next_increment == VersionIncrement.PATCH:
increment = "PATCH"
elif next_increment == VersionIncrement.MINOR:
increment = "MINOR"
else:
increment = "MAJOR"
version = version.bump(increment=increment)
if self.arguments.get("major"):
out.write(version.major)
return
if self.arguments.get("minor"):
out.write(version.minor)
return
if self.arguments.get("patch"):
out.write(version.micro)
return
display_version: str
if self.arguments.get("tag"):
tag_rules = TagRules.from_settings(self.config.settings)
display_version = tag_rules.normalize_tag(version)
else:
display_version = str(version)
out.write(
f"Project Version: {display_version}"
if self.arguments.get("verbose")
else display_version
)
return
for argument in ("major", "minor", "patch"):
if self.arguments.get(argument):
out.error(
f"{argument} can only be used with MANUAL_VERSION, --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
out.write(__version__)