-
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathinit.py
More file actions
344 lines (299 loc) · 12.3 KB
/
init.py
File metadata and controls
344 lines (299 loc) · 12.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any, NamedTuple
import questionary
import yaml
from commitizen import cmd, factory, out, project_info
from commitizen.__version__ import __version__
from commitizen.config.factory import create_config
from commitizen.cz import registry
from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS
from commitizen.exceptions import (
InitFailedError,
MissingCzCustomizeConfigError,
NoAnswersError,
)
from commitizen.git import get_latest_tag_name, get_tag_names, smart_open
from commitizen.version_schemes import KNOWN_SCHEMES, Version, get_version_scheme
if TYPE_CHECKING:
from commitizen.config import (
BaseConfig,
)
class _VersionProviderOption(NamedTuple):
provider_name: str
description: str
@property
def title(self) -> str:
return f"{self.provider_name}: {self.description}"
_VERSION_PROVIDER_CHOICES = tuple(
questionary.Choice(title=option.title, value=option.provider_name)
for option in (
_VersionProviderOption(
provider_name="commitizen",
description="Fetch and set version in commitizen config (default)",
),
_VersionProviderOption(
provider_name="cargo",
description="Get and set version from Cargo.toml:project.version field",
),
_VersionProviderOption(
provider_name="composer",
description="Get and set version from composer.json:project.version field",
),
_VersionProviderOption(
provider_name="npm",
description="Get and set version from package.json:project.version field",
),
_VersionProviderOption(
provider_name="pep621",
description="Get and set version from pyproject.toml:project.version field",
),
_VersionProviderOption(
provider_name="poetry",
description="Get and set version from pyproject.toml:tool.poetry.version field",
),
_VersionProviderOption(
provider_name="uv",
description="Get and set version from pyproject.toml and uv.lock",
),
_VersionProviderOption(
provider_name="scm",
description="Fetch the version from git and does not need to set it back",
),
)
)
class Init:
_PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml"
def __init__(self, config: BaseConfig, *args: object) -> None:
self.config: BaseConfig = config
self.cz = factory.committer_factory(self.config)
def __call__(self) -> None:
if self.config.path:
out.line(f"Config file {self.config.path} already exists")
return
out.info("Welcome to commitizen!\n")
out.line(
"Answer the following questions to configure your project.\n"
"For further configuration, visit:\n"
"\n"
"https://commitizen-tools.github.io/commitizen/config/"
"\n"
)
# Collect information
try:
config_path = self._ask_config_path() # select
cz_name = self._ask_name() # select
version_provider = self._ask_version_provider() # select
tag = self._ask_tag() # confirm & select
version_scheme = self._ask_version_scheme() # select
version = get_version_scheme(self.config.settings, version_scheme)(tag)
tag_format = self._ask_tag_format(tag) # confirm & text
update_changelog_on_bump = self._ask_update_changelog_on_bump() # confirm
major_version_zero = self._ask_major_version_zero(version) # confirm
hook_types: list[str] | None = questionary.checkbox(
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)",
choices=[
questionary.Choice("commit-msg", checked=False),
questionary.Choice("pre-push", checked=False),
],
).unsafe_ask()
except KeyboardInterrupt:
raise InitFailedError("Stopped by user")
if hook_types:
config_data = self._get_config_data()
with smart_open(
self._PRE_COMMIT_CONFIG_PATH,
"w",
encoding=self.config.settings["encoding"],
) as config_file:
yaml.safe_dump(config_data, stream=config_file)
if not project_info.is_pre_commit_installed():
raise InitFailedError(
"Failed to install pre-commit hook.\n"
"pre-commit is not installed in current environment."
)
cmd_str = "pre-commit install " + " ".join(
f"--hook-type {ty}" for ty in hook_types
)
c = cmd.run(cmd_str)
if c.return_code != 0:
raise InitFailedError(
"Failed to install pre-commit hook.\n"
f"Error running {cmd_str}."
"Outputs are attached below:\n"
f"stdout: {c.out}\n"
f"stderr: {c.err}"
)
out.write("commitizen pre-commit hook is now installed in your '.git'\n")
_write_config_to_file(
path=config_path,
cz_name=cz_name,
version_provider=version_provider,
version_scheme=version_scheme,
version=version,
tag_format=tag_format,
update_changelog_on_bump=update_changelog_on_bump,
major_version_zero=major_version_zero,
)
out.write("\nYou can bump the version running:\n")
out.info("\tcz bump\n")
out.success("Configuration complete 🚀")
def _ask_config_path(self) -> Path:
filename: str = questionary.select(
"Please choose a supported config file: ",
choices=CONFIG_FILES,
default=project_info.get_default_config_filename(),
style=self.cz.style,
).unsafe_ask()
return Path(filename)
def _ask_name(self) -> str:
name: str = questionary.select(
f"Please choose a cz (commit rule): (default: {DEFAULT_SETTINGS['name']})",
choices=self._construct_name_choices_from_registry(),
default=DEFAULT_SETTINGS["name"],
style=self.cz.style,
).unsafe_ask()
return name
def _construct_name_choices_from_registry(self) -> list[questionary.Choice]:
"""
Construct questionary choices of cz names from registry.
"""
choices = []
for cz_name, cz_class in registry.items():
try:
cz_obj = cz_class(self.config)
except MissingCzCustomizeConfigError:
# Fallback if description is not available
choices.append(questionary.Choice(title=cz_name, value=cz_name))
continue
# Get the first line of the schema as the description
# TODO(bearomorphism): schema is a workaround. Add a description method to the cz class.
description = cz_obj.schema().partition("\n")[0]
choices.append(
questionary.Choice(
title=cz_name, value=cz_name, description=description
)
)
return choices
def _ask_tag(self) -> str:
latest_tag = get_latest_tag_name()
if not latest_tag:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"
if questionary.confirm(
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False
).unsafe_ask():
return latest_tag
existing_tags = get_tag_names()
if not existing_tags:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"
answer: str = questionary.select(
"Please choose the latest tag: ",
# The latest tag is most likely with the largest number.
# Thus, listing the existing_tags in reverse order makes more sense.
choices=sorted(existing_tags, reverse=True),
style=self.cz.style,
).unsafe_ask()
if not answer:
raise NoAnswersError("Tag is required!")
return answer
def _ask_tag_format(self, latest_tag: str) -> str:
if latest_tag.startswith("v"):
v_tag_format = r"v$version"
if questionary.confirm(
f'Is "{v_tag_format}" the correct tag format?', style=self.cz.style
).unsafe_ask():
return v_tag_format
default_format = DEFAULT_SETTINGS["tag_format"]
tag_format: str = questionary.text(
f'Please enter the correct version format: (default: "{default_format}")',
style=self.cz.style,
).unsafe_ask()
return tag_format or default_format
def _ask_version_provider(self) -> str:
"""Ask for setting: version_provider"""
version_provider: str = questionary.select(
"Choose the source of the version:",
choices=_VERSION_PROVIDER_CHOICES,
style=self.cz.style,
default=project_info.get_default_version_provider(),
).unsafe_ask()
return version_provider
def _ask_version_scheme(self) -> str:
"""Ask for setting: version_scheme"""
scheme: str = questionary.select(
"Choose version scheme: ",
choices=KNOWN_SCHEMES,
style=self.cz.style,
default=project_info.get_default_version_scheme(),
).unsafe_ask()
return scheme
def _ask_major_version_zero(self, version: Version) -> bool:
"""Ask for setting: major_version_zero"""
if version.major > 0:
return False
major_version_zero: bool = questionary.confirm(
"Keep major version zero (0.x) during breaking changes",
default=True,
auto_enter=True,
).unsafe_ask()
return major_version_zero
def _ask_update_changelog_on_bump(self) -> bool:
"""Ask for setting: update_changelog_on_bump"""
update_changelog_on_bump: bool = questionary.confirm(
"Create changelog automatically on bump",
default=True,
auto_enter=True,
).unsafe_ask()
return update_changelog_on_bump
def _get_config_data(self) -> dict[str, Any]:
CZ_HOOK_CONFIG = {
"repo": "https://github.com/commitizen-tools/commitizen",
"rev": f"v{__version__}",
"hooks": [
{"id": "commitizen"},
{"id": "commitizen-branch", "stages": ["pre-push"]},
],
}
pre_commit_config_path = Path(self._PRE_COMMIT_CONFIG_PATH)
if not pre_commit_config_path.is_file():
return {"repos": [CZ_HOOK_CONFIG]}
with pre_commit_config_path.open(
encoding=self.config.settings["encoding"]
) as config_file:
config_data: dict[str, Any] = yaml.safe_load(config_file) or {}
if not isinstance(repos := config_data.get("repos"), list):
# .pre-commit-config.yaml exists but there's no "repos" key
config_data["repos"] = [CZ_HOOK_CONFIG]
return config_data
# Check if commitizen pre-commit hook is already in the config
if any("commitizen" in hook_config["repo"] for hook_config in repos):
out.write("commitizen already in pre-commit config")
else:
repos.append(CZ_HOOK_CONFIG)
return config_data
def _write_config_to_file(
*,
path: Path,
cz_name: str,
version_provider: str,
version_scheme: str,
version: Version,
tag_format: str,
update_changelog_on_bump: bool,
major_version_zero: bool,
) -> None:
out_config = create_config(path=path)
out_config.init_empty_config_content()
out_config.set_key("name", cz_name)
out_config.set_key("tag_format", tag_format)
out_config.set_key("version_scheme", version_scheme)
if version_provider == "commitizen":
out_config.set_key("version", version.public)
else:
out_config.set_key("version_provider", version_provider)
if update_changelog_on_bump:
out_config.set_key("update_changelog_on_bump", update_changelog_on_bump)
if major_version_zero:
out_config.set_key("major_version_zero", major_version_zero)