Skip to content

Commit 6663767

Browse files
committed
fix(Init): raise InitFailedError on keyboard interrupt on pre-commit hook question, remove unreachable code path
1 parent cc981fc commit 6663767

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

commitizen/commands/init.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,30 @@ def __call__(self) -> None:
150150
tag_format = self._ask_tag_format(tag) # confirm & text
151151
update_changelog_on_bump = self._ask_update_changelog_on_bump() # confirm
152152
major_version_zero = self._ask_major_version_zero(version) # confirm
153+
hook_types: list[str] | None = questionary.checkbox(
154+
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)",
155+
choices=[
156+
questionary.Choice("commit-msg", checked=False),
157+
questionary.Choice("pre-push", checked=False),
158+
],
159+
).unsafe_ask()
153160
except KeyboardInterrupt:
154161
raise InitFailedError("Stopped by user")
155162

163+
if hook_types:
164+
config_data = self._get_config_data()
165+
with smart_open(
166+
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding
167+
) as config_file:
168+
yaml.safe_dump(config_data, stream=config_file)
169+
170+
if not self.project_info.is_pre_commit_installed:
171+
raise InitFailedError(
172+
"pre-commit is not installed in current environment."
173+
)
174+
self._exec_install_pre_commit_hook(hook_types)
175+
out.write("commitizen pre-commit hook is now installed in your '.git'\n")
176+
156177
# Initialize configuration
157178
if "toml" in config_path:
158179
self.config = TomlConfig(data="", path=config_path)
@@ -161,20 +182,6 @@ def __call__(self) -> None:
161182
elif "yaml" in config_path:
162183
self.config = YAMLConfig(data="", path=config_path)
163184

164-
# Collect hook data
165-
hook_types = questionary.checkbox(
166-
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)",
167-
choices=[
168-
questionary.Choice("commit-msg", checked=False),
169-
questionary.Choice("pre-push", checked=False),
170-
],
171-
).unsafe_ask()
172-
if hook_types:
173-
try:
174-
self._install_pre_commit_hook(hook_types)
175-
except InitFailedError as e:
176-
raise InitFailedError(f"Failed to install pre-commit hook.\n{e}")
177-
178185
# Create and initialize config
179186
self.config.init_empty_config_content()
180187

@@ -369,17 +376,3 @@ def _get_config_data(self) -> dict[str, Any]:
369376
else:
370377
repos.append(CZ_HOOK_CONFIG)
371378
return config_data
372-
373-
def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
374-
config_data = self._get_config_data()
375-
with smart_open(
376-
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding
377-
) as config_file:
378-
yaml.safe_dump(config_data, stream=config_file)
379-
380-
if not self.project_info.is_pre_commit_installed:
381-
raise InitFailedError("pre-commit is not installed in current environment.")
382-
if hook_types is None:
383-
hook_types = ["commit-msg", "pre-push"]
384-
self._exec_install_pre_commit_hook(hook_types)
385-
out.write("commitizen pre-commit hook is now installed in your '.git'\n")

0 commit comments

Comments
 (0)