11from __future__ import annotations
22
3- import os
4- import shutil
53from typing import Any , NamedTuple
64
75import questionary
86import yaml
97
10- from commitizen import cmd , factory , out
8+ from commitizen import cmd , factory , out , project_info
119from commitizen .__version__ import __version__
1210from commitizen .config import BaseConfig , JsonConfig , TomlConfig , YAMLConfig
1311from commitizen .cz import registry
@@ -65,65 +63,13 @@ def title(self) -> str:
6563)
6664
6765
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-
11966class Init :
12067 _PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml"
12168
12269 def __init__ (self , config : BaseConfig , * args : object ) -> None :
12370 self .config : BaseConfig = config
12471 self .encoding = config .settings ["encoding" ]
12572 self .cz = factory .committer_factory (self .config )
126- self .project_info = ProjectInfo ()
12773
12874 def __call__ (self ) -> None :
12975 if self .config .path :
@@ -195,14 +141,10 @@ def __call__(self) -> None:
195141 out .success ("Configuration complete 🚀" )
196142
197143 def _ask_config_path (self ) -> str :
198- default_path = (
199- "pyproject.toml" if self .project_info .has_pyproject else ".cz.toml"
200- )
201-
202144 name : str = questionary .select (
203145 "Please choose a supported config file: " ,
204146 choices = CONFIG_FILES ,
205- default = default_path ,
147+ default = project_info . get_default_config_path () ,
206148 style = self .cz .style ,
207149 ).unsafe_ask ()
208150 return name
@@ -267,37 +209,17 @@ def _ask_version_provider(self) -> str:
267209 "Choose the source of the version:" ,
268210 choices = _VERSION_PROVIDER_CHOICES ,
269211 style = self .cz .style ,
270- default = self . _default_version_provider ,
212+ default = project_info . get_default_version_provider () ,
271213 ).unsafe_ask ()
272214 return version_provider
273215
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-
292216 def _ask_version_scheme (self ) -> str :
293217 """Ask for setting: version_scheme"""
294- default_scheme = "pep440" if self .project_info .is_python else "semver"
295-
296218 scheme : str = questionary .select (
297219 "Choose version scheme: " ,
298220 choices = KNOWN_SCHEMES ,
299221 style = self .cz .style ,
300- default = default_scheme ,
222+ default = project_info . get_default_version_scheme () ,
301223 ).unsafe_ask ()
302224 return scheme
303225
@@ -351,7 +273,7 @@ def _get_config_data(self) -> dict[str, Any]:
351273 ],
352274 }
353275
354- if not self . project_info .has_pre_commit_config :
276+ if not project_info .has_pre_commit_config () :
355277 # .pre-commit-config.yaml does not exist
356278 return {"repos" : [CZ_HOOK_CONFIG ]}
357279
@@ -377,7 +299,7 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
377299 ) as config_file :
378300 yaml .safe_dump (config_data , stream = config_file )
379301
380- if not self . project_info .is_pre_commit_installed :
302+ if not project_info .is_pre_commit_installed () :
381303 raise InitFailedError ("pre-commit is not installed in current environment." )
382304 if hook_types is None :
383305 hook_types = ["commit-msg" , "pre-push" ]
0 commit comments