-
-
Notifications
You must be signed in to change notification settings - Fork 298
fix(Init): raise InitFailedError on keyboard interrupt on pre-commit ... #1604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,9 +150,43 @@ def __call__(self) -> None: | |
tag_format = self._ask_tag_format(tag) # confirm & text | ||
update_changelog_on_bump = self._ask_update_changelog_on_bump() # confirm | ||
major_version_zero = self._ask_major_version_zero(version) # confirm | ||
hook_types: list[str] | None = questionary.checkbox( | ||
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)", | ||
choices=[ | ||
questionary.Choice("commit-msg", checked=False), | ||
questionary.Choice("pre-push", checked=False), | ||
], | ||
).unsafe_ask() | ||
except KeyboardInterrupt: | ||
raise InitFailedError("Stopped by user") | ||
|
||
if hook_types: | ||
config_data = self._get_config_data() | ||
with smart_open( | ||
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding | ||
) as config_file: | ||
yaml.safe_dump(config_data, stream=config_file) | ||
|
||
if not self.project_info.is_pre_commit_installed: | ||
raise InitFailedError( | ||
"Failed to install pre-commit hook.\n" | ||
"pre-commit is not installed in current environment." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is also added to keep the original behavior. |
||
) | ||
|
||
cmd_str = "pre-commit install " + " ".join( | ||
f"--hook-type {ty}" for ty in hook_types | ||
) | ||
c = cmd.run(cmd_str) | ||
if c.return_code != 0: | ||
raise InitFailedError( | ||
"Failed to install pre-commit hook.\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just added this line to keep the original behavior. |
||
f"Error running {cmd_str}." | ||
"Outputs are attached below:\n" | ||
f"stdout: {c.out}\n" | ||
f"stderr: {c.err}" | ||
) | ||
out.write("commitizen pre-commit hook is now installed in your '.git'\n") | ||
|
||
# Initialize configuration | ||
if "toml" in config_path: | ||
self.config = TomlConfig(data="", path=config_path) | ||
|
@@ -161,20 +195,6 @@ def __call__(self) -> None: | |
elif "yaml" in config_path: | ||
self.config = YAMLConfig(data="", path=config_path) | ||
|
||
# Collect hook data | ||
hook_types = questionary.checkbox( | ||
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)", | ||
choices=[ | ||
questionary.Choice("commit-msg", checked=False), | ||
questionary.Choice("pre-push", checked=False), | ||
], | ||
).unsafe_ask() | ||
if hook_types: | ||
try: | ||
self._install_pre_commit_hook(hook_types) | ||
except InitFailedError as e: | ||
raise InitFailedError(f"Failed to install pre-commit hook.\n{e}") | ||
Lee-W marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Create and initialize config | ||
self.config.init_empty_config_content() | ||
|
||
|
@@ -321,26 +341,6 @@ def _ask_update_changelog_on_bump(self) -> bool: | |
).unsafe_ask() | ||
return update_changelog_on_bump | ||
|
||
def _exec_install_pre_commit_hook(self, hook_types: list[str]) -> None: | ||
cmd_str = self._gen_pre_commit_cmd(hook_types) | ||
c = cmd.run(cmd_str) | ||
if c.return_code != 0: | ||
err_msg = ( | ||
f"Error running {cmd_str}." | ||
"Outputs are attached below:\n" | ||
f"stdout: {c.out}\n" | ||
f"stderr: {c.err}" | ||
) | ||
raise InitFailedError(err_msg) | ||
|
||
def _gen_pre_commit_cmd(self, hook_types: list[str]) -> str: | ||
"""Generate pre-commit command according to given hook types""" | ||
if not hook_types: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
raise ValueError("At least 1 hook type should be provided.") | ||
return "pre-commit install " + " ".join( | ||
f"--hook-type {ty}" for ty in hook_types | ||
) | ||
|
||
def _get_config_data(self) -> dict[str, Any]: | ||
CZ_HOOK_CONFIG = { | ||
"repo": "https://github.com/commitizen-tools/commitizen", | ||
|
@@ -369,17 +369,3 @@ def _get_config_data(self) -> dict[str, Any]: | |
else: | ||
repos.append(CZ_HOOK_CONFIG) | ||
return config_data | ||
|
||
def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None: | ||
config_data = self._get_config_data() | ||
with smart_open( | ||
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding | ||
) as config_file: | ||
yaml.safe_dump(config_data, stream=config_file) | ||
|
||
if not self.project_info.is_pre_commit_installed: | ||
raise InitFailedError("pre-commit is not installed in current environment.") | ||
if hook_types is None: | ||
hook_types = ["commit-msg", "pre-push"] | ||
Comment on lines
-382
to
-383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch is redundant. |
||
self._exec_install_pre_commit_hook(hook_types) | ||
out.write("commitizen pre-commit hook is now installed in your '.git'\n") |