Allow --hook-type to be specified multiple times

This commit is contained in:
Anthony Sottile 2019-09-23 11:14:36 -07:00
parent 96c35185f0
commit a18646deb2
6 changed files with 119 additions and 79 deletions

View file

@ -8,10 +8,10 @@ from pre_commit.util import cmd_output
logger = logging.getLogger('pre_commit')
def init_templatedir(config_file, store, directory, hook_type):
def init_templatedir(config_file, store, directory, hook_types):
install(
config_file, store, overwrite=True, hook_type=hook_type,
skip_on_missing_config=True, git_dir=directory,
config_file, store, hook_types=hook_types,
overwrite=True, skip_on_missing_config=True, git_dir=directory,
)
try:
_, out, _ = cmd_output('git', 'config', 'init.templateDir')

View file

@ -67,19 +67,10 @@ def shebang():
return '#!/usr/bin/env {}'.format(py)
def install(
config_file, store,
overwrite=False, hooks=False, hook_type='pre-commit',
skip_on_missing_config=False, git_dir=None,
def _install_hook_script(
config_file, hook_type,
overwrite=False, skip_on_missing_config=False, git_dir=None,
):
"""Install the pre-commit hooks."""
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
logger.error(
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
'hint: `git config --unset-all core.hooksPath`',
)
return 1
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
mkdirp(os.path.dirname(hook_path))
@ -120,7 +111,27 @@ def install(
output.write_line('pre-commit installed at {}'.format(hook_path))
# If they requested we install all of the hooks, do so.
def install(
config_file, store, hook_types,
overwrite=False, hooks=False,
skip_on_missing_config=False, git_dir=None,
):
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
logger.error(
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
'hint: `git config --unset-all core.hooksPath`',
)
return 1
for hook_type in hook_types:
_install_hook_script(
config_file, hook_type,
overwrite=overwrite,
skip_on_missing_config=skip_on_missing_config,
git_dir=git_dir,
)
if hooks:
install_hooks(config_file, store)
@ -131,13 +142,12 @@ def install_hooks(config_file, store):
install_hook_envs(all_hooks(load_config(config_file), store), store)
def uninstall(hook_type='pre-commit'):
"""Uninstall the pre-commit hooks."""
def _uninstall_hook_script(hook_type): # type: (str) -> None
hook_path, legacy_path = _hook_paths(hook_type)
# If our file doesn't exist or it isn't ours, gtfo.
if not os.path.exists(hook_path) or not is_our_script(hook_path):
return 0
return
os.remove(hook_path)
output.write_line('{} uninstalled'.format(hook_type))
@ -146,4 +156,8 @@ def uninstall(hook_type='pre-commit'):
os.rename(legacy_path, hook_path)
output.write_line('Restored previous hooks to {}'.format(hook_path))
def uninstall(hook_types):
for hook_type in hook_types:
_uninstall_hook_script(hook_type)
return 0

View file

@ -60,7 +60,8 @@ def _add_hook_type_option(parser):
'-t', '--hook-type', choices=(
'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
),
default='pre-commit',
action='append',
dest='hook_types',
)
@ -120,6 +121,11 @@ def _adjust_args_and_chdir(args):
args.files = [os.path.relpath(filename) for filename in args.files]
if args.command == 'try-repo' and os.path.exists(args.repo):
args.repo = os.path.relpath(args.repo)
if (
args.command in {'install', 'uninstall', 'init-templatedir'} and
not args.hook_types
):
args.hook_types = ['pre-commit']
def main(argv=None):
@ -299,14 +305,14 @@ def main(argv=None):
elif args.command == 'install':
return install(
args.config, store,
hook_types=args.hook_types,
overwrite=args.overwrite, hooks=args.install_hooks,
hook_type=args.hook_type,
skip_on_missing_config=args.allow_missing_config,
)
elif args.command == 'init-templatedir':
return init_templatedir(
args.config, store,
args.directory, hook_type=args.hook_type,
args.config, store, args.directory,
hook_types=args.hook_types,
)
elif args.command == 'install-hooks':
return install_hooks(args.config, store)
@ -319,7 +325,7 @@ def main(argv=None):
elif args.command == 'try-repo':
return try_repo(args)
elif args.command == 'uninstall':
return uninstall(hook_type=args.hook_type)
return uninstall(hook_types=args.hook_types)
else:
raise NotImplementedError(
'Command {} not implemented.'.format(args.command),