Skip to content

Commit 692c3b1

Browse files
committed
refactor(Init): simplify hook_types related logic and remove unreachable branches
1 parent 38e1e98 commit 692c3b1

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

commitizen/commands/init.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,19 @@ def __call__(self) -> None:
169169

170170
if not self.project_info.is_pre_commit_installed:
171171
raise InitFailedError("Failed to install pre-commit hook.")
172-
self._exec_install_pre_commit_hook(hook_types)
172+
173+
cmd_str = "pre-commit install " + " ".join(
174+
f"--hook-type {ty}" for ty in hook_types
175+
)
176+
c = cmd.run(cmd_str)
177+
if c.return_code != 0:
178+
raise InitFailedError(
179+
"Failed to install pre-commit hook.\n"
180+
f"Error running {cmd_str}."
181+
"Outputs are attached below:\n"
182+
f"stdout: {c.out}\n"
183+
f"stderr: {c.err}"
184+
)
173185
out.write("commitizen pre-commit hook is now installed in your '.git'\n")
174186

175187
# Initialize configuration
@@ -326,26 +338,6 @@ def _ask_update_changelog_on_bump(self) -> bool:
326338
).unsafe_ask()
327339
return update_changelog_on_bump
328340

329-
def _exec_install_pre_commit_hook(self, hook_types: list[str]) -> None:
330-
cmd_str = self._gen_pre_commit_cmd(hook_types)
331-
c = cmd.run(cmd_str)
332-
if c.return_code != 0:
333-
err_msg = (
334-
f"Error running {cmd_str}."
335-
"Outputs are attached below:\n"
336-
f"stdout: {c.out}\n"
337-
f"stderr: {c.err}"
338-
)
339-
raise InitFailedError(err_msg)
340-
341-
def _gen_pre_commit_cmd(self, hook_types: list[str]) -> str:
342-
"""Generate pre-commit command according to given hook types"""
343-
if not hook_types:
344-
raise ValueError("At least 1 hook type should be provided.")
345-
return "pre-commit install " + " ".join(
346-
f"--hook-type {ty}" for ty in hook_types
347-
)
348-
349341
def _get_config_data(self) -> dict[str, Any]:
350342
CZ_HOOK_CONFIG = {
351343
"repo": "https://github.com/commitizen-tools/commitizen",

0 commit comments

Comments
 (0)