summaryrefslogtreecommitdiffstats
path: root/pre_commit/commands/install_uninstall.py
diff options
context:
space:
mode:
Diffstat (limited to 'pre_commit/commands/install_uninstall.py')
-rw-r--r--pre_commit/commands/install_uninstall.py36
1 files changed, 24 insertions, 12 deletions
diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py
index 50c6443..5ff6cba 100644
--- a/pre_commit/commands/install_uninstall.py
+++ b/pre_commit/commands/install_uninstall.py
@@ -1,14 +1,14 @@
+from __future__ import annotations
+
import logging
import os.path
import shlex
import shutil
import sys
-from typing import Optional
-from typing import Sequence
-from typing import Tuple
from pre_commit import git
from pre_commit import output
+from pre_commit.clientlib import InvalidConfigError
from pre_commit.clientlib import load_config
from pre_commit.repository import all_hooks
from pre_commit.repository import install_hook_envs
@@ -32,11 +32,23 @@ TEMPLATE_START = '# start templated\n'
TEMPLATE_END = '# end templated\n'
+def _hook_types(cfg_filename: str, hook_types: list[str] | None) -> list[str]:
+ if hook_types is not None:
+ return hook_types
+ else:
+ try:
+ cfg = load_config(cfg_filename)
+ except InvalidConfigError:
+ return ['pre-commit']
+ else:
+ return cfg['default_install_hook_types']
+
+
def _hook_paths(
hook_type: str,
- git_dir: Optional[str] = None,
-) -> Tuple[str, str]:
- git_dir = git_dir if git_dir is not None else git.get_git_dir()
+ git_dir: str | None = None,
+) -> tuple[str, str]:
+ git_dir = git_dir if git_dir is not None else git.get_git_common_dir()
pth = os.path.join(git_dir, 'hooks', hook_type)
return pth, f'{pth}.legacy'
@@ -54,7 +66,7 @@ def _install_hook_script(
hook_type: str,
overwrite: bool = False,
skip_on_missing_config: bool = False,
- git_dir: Optional[str] = None,
+ git_dir: str | None = None,
) -> None:
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
@@ -103,11 +115,11 @@ def _install_hook_script(
def install(
config_file: str,
store: Store,
- hook_types: Sequence[str],
+ hook_types: list[str] | None,
overwrite: bool = False,
hooks: bool = False,
skip_on_missing_config: bool = False,
- git_dir: Optional[str] = None,
+ git_dir: str | None = None,
) -> int:
if git_dir is None and git.has_core_hookpaths_set():
logger.error(
@@ -116,7 +128,7 @@ def install(
)
return 1
- for hook_type in hook_types:
+ for hook_type in _hook_types(config_file, hook_types):
_install_hook_script(
config_file, hook_type,
overwrite=overwrite,
@@ -150,7 +162,7 @@ def _uninstall_hook_script(hook_type: str) -> None:
output.write_line(f'Restored previous hooks to {hook_path}')
-def uninstall(hook_types: Sequence[str]) -> int:
- for hook_type in hook_types:
+def uninstall(config_file: str, hook_types: list[str] | None) -> int:
+ for hook_type in _hook_types(config_file, hook_types):
_uninstall_hook_script(hook_type)
return 0