1
1
from __future__ import annotations
2
2
3
- import os
4
- import shutil
5
3
from typing import Any , NamedTuple
6
4
7
5
import questionary
8
6
import yaml
9
7
10
- from commitizen import cmd , factory , out
8
+ from commitizen import cmd , factory , out , project_info
11
9
from commitizen .__version__ import __version__
12
10
from commitizen .config import BaseConfig , JsonConfig , TomlConfig , YAMLConfig
13
11
from commitizen .cz import registry
@@ -65,65 +63,13 @@ def title(self) -> str:
65
63
)
66
64
67
65
68
- class ProjectInfo :
69
- """Discover information about the current folder."""
70
-
71
- @property
72
- def has_pyproject (self ) -> bool :
73
- return os .path .isfile ("pyproject.toml" )
74
-
75
- @property
76
- def has_uv_lock (self ) -> bool :
77
- return os .path .isfile ("uv.lock" )
78
-
79
- @property
80
- def has_setup (self ) -> bool :
81
- return os .path .isfile ("setup.py" )
82
-
83
- @property
84
- def has_pre_commit_config (self ) -> bool :
85
- return os .path .isfile (".pre-commit-config.yaml" )
86
-
87
- @property
88
- def is_python_uv (self ) -> bool :
89
- return self .has_pyproject and self .has_uv_lock
90
-
91
- @property
92
- def is_python_poetry (self ) -> bool :
93
- if not self .has_pyproject :
94
- return False
95
- with open ("pyproject.toml" ) as f :
96
- return "[tool.poetry]" in f .read ()
97
-
98
- @property
99
- def is_python (self ) -> bool :
100
- return self .has_pyproject or self .has_setup
101
-
102
- @property
103
- def is_rust_cargo (self ) -> bool :
104
- return os .path .isfile ("Cargo.toml" )
105
-
106
- @property
107
- def is_npm_package (self ) -> bool :
108
- return os .path .isfile ("package.json" )
109
-
110
- @property
111
- def is_php_composer (self ) -> bool :
112
- return os .path .isfile ("composer.json" )
113
-
114
- @property
115
- def is_pre_commit_installed (self ) -> bool :
116
- return bool (shutil .which ("pre-commit" ))
117
-
118
-
119
66
class Init :
120
67
_PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml"
121
68
122
69
def __init__ (self , config : BaseConfig , * args : object ) -> None :
123
70
self .config : BaseConfig = config
124
71
self .encoding = config .settings ["encoding" ]
125
72
self .cz = factory .committer_factory (self .config )
126
- self .project_info = ProjectInfo ()
127
73
128
74
def __call__ (self ) -> None :
129
75
if self .config .path :
@@ -195,14 +141,10 @@ def __call__(self) -> None:
195
141
out .success ("Configuration complete 🚀" )
196
142
197
143
def _ask_config_path (self ) -> str :
198
- default_path = (
199
- "pyproject.toml" if self .project_info .has_pyproject else ".cz.toml"
200
- )
201
-
202
144
name : str = questionary .select (
203
145
"Please choose a supported config file: " ,
204
146
choices = CONFIG_FILES ,
205
- default = default_path ,
147
+ default = project_info . get_default_config_path () ,
206
148
style = self .cz .style ,
207
149
).unsafe_ask ()
208
150
return name
@@ -267,37 +209,17 @@ def _ask_version_provider(self) -> str:
267
209
"Choose the source of the version:" ,
268
210
choices = _VERSION_PROVIDER_CHOICES ,
269
211
style = self .cz .style ,
270
- default = self . _default_version_provider ,
212
+ default = project_info . get_default_version_provider () ,
271
213
).unsafe_ask ()
272
214
return version_provider
273
215
274
- @property
275
- def _default_version_provider (self ) -> str :
276
- if self .project_info .is_python :
277
- if self .project_info .is_python_poetry :
278
- return "poetry"
279
- if self .project_info .is_python_uv :
280
- return "uv"
281
- return "pep621"
282
-
283
- if self .project_info .is_rust_cargo :
284
- return "cargo"
285
- if self .project_info .is_npm_package :
286
- return "npm"
287
- if self .project_info .is_php_composer :
288
- return "composer"
289
-
290
- return "commitizen"
291
-
292
216
def _ask_version_scheme (self ) -> str :
293
217
"""Ask for setting: version_scheme"""
294
- default_scheme = "pep440" if self .project_info .is_python else "semver"
295
-
296
218
scheme : str = questionary .select (
297
219
"Choose version scheme: " ,
298
220
choices = KNOWN_SCHEMES ,
299
221
style = self .cz .style ,
300
- default = default_scheme ,
222
+ default = project_info . get_default_version_scheme () ,
301
223
).unsafe_ask ()
302
224
return scheme
303
225
@@ -351,7 +273,7 @@ def _get_config_data(self) -> dict[str, Any]:
351
273
],
352
274
}
353
275
354
- if not self . project_info .has_pre_commit_config :
276
+ if not project_info .has_pre_commit_config () :
355
277
# .pre-commit-config.yaml does not exist
356
278
return {"repos" : [CZ_HOOK_CONFIG ]}
357
279
@@ -377,7 +299,7 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
377
299
) as config_file :
378
300
yaml .safe_dump (config_data , stream = config_file )
379
301
380
- if not self . project_info .is_pre_commit_installed :
302
+ if not project_info .is_pre_commit_installed () :
381
303
raise InitFailedError ("pre-commit is not installed in current environment." )
382
304
if hook_types is None :
383
305
hook_types = ["commit-msg" , "pre-push" ]
0 commit comments