Skip to content

Commit 68c4360

Browse files
bearomorphismLee-W
authored andcommitted
refactor(Init): extract _get_config_data for readability
1 parent 3d635d2 commit 68c4360

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

commitizen/commands/init.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def is_pre_commit_installed(self) -> bool:
7979

8080

8181
class Init:
82+
_PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml"
83+
8284
def __init__(self, config: BaseConfig, *args: object) -> None:
8385
self.config: BaseConfig = config
8486
self.encoding = config.settings["encoding"]
@@ -320,9 +322,8 @@ def _gen_pre_commit_cmd(self, hook_types: list[str]) -> str:
320322
f"--hook-type {ty}" for ty in hook_types
321323
)
322324

323-
def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
324-
pre_commit_config_filename = ".pre-commit-config.yaml"
325-
cz_hook_config = {
325+
def _get_config_data(self) -> dict[str, Any]:
326+
CZ_HOOK_CONFIG = {
326327
"repo": "https://github.com/commitizen-tools/commitizen",
327328
"rev": f"v{__version__}",
328329
"hooks": [
@@ -331,31 +332,29 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
331332
],
332333
}
333334

334-
config_data = {}
335335
if not self.project_info.has_pre_commit_config:
336336
# .pre-commit-config.yaml does not exist
337-
config_data["repos"] = [cz_hook_config]
337+
return {"repos": [CZ_HOOK_CONFIG]}
338+
339+
with open(self._PRE_COMMIT_CONFIG_PATH, encoding=self.encoding) as config_file:
340+
config_data: dict[str, Any] = yaml.safe_load(config_file) or {}
341+
342+
if not isinstance(repos := config_data.get("repos"), list):
343+
# .pre-commit-config.yaml exists but there's no "repos" key
344+
config_data["repos"] = [CZ_HOOK_CONFIG]
345+
return config_data
346+
347+
# Check if commitizen pre-commit hook is already in the config
348+
if any("commitizen" in hook_config["repo"] for hook_config in repos):
349+
out.write("commitizen already in pre-commit config")
338350
else:
339-
with open(
340-
pre_commit_config_filename, encoding=self.encoding
341-
) as config_file:
342-
yaml_data = yaml.safe_load(config_file)
343-
if yaml_data:
344-
config_data = yaml_data
345-
346-
if "repos" in config_data:
347-
for pre_commit_hook in config_data["repos"]:
348-
if "commitizen" in pre_commit_hook["repo"]:
349-
out.write("commitizen already in pre-commit config")
350-
break
351-
else:
352-
config_data["repos"].append(cz_hook_config)
353-
else:
354-
# .pre-commit-config.yaml exists but there's no "repos" key
355-
config_data["repos"] = [cz_hook_config]
351+
repos.append(CZ_HOOK_CONFIG)
352+
return config_data
356353

354+
def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
355+
config_data = self._get_config_data()
357356
with smart_open(
358-
pre_commit_config_filename, "w", encoding=self.encoding
357+
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding
359358
) as config_file:
360359
yaml.safe_dump(config_data, stream=config_file)
361360

0 commit comments

Comments
 (0)