Allow multiple hooks with same id in .pre-commit-config.yaml

This commit is contained in:
Anthony Sottile 2014-07-31 08:37:37 -07:00
parent 32b662c35f
commit 62f13aea56
5 changed files with 26 additions and 22 deletions

View file

@ -43,7 +43,7 @@ def _update_repository(repo_config, runner):
new_repo = Repository.create(new_config, runner.store)
# See if any of our hooks were deleted with the new commits
hooks = set(repo.hooks.keys())
hooks = set(hook_id for hook_id, _ in repo.hooks)
hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks.keys()))
if hooks_missing:
raise RepositoryCannotBeUpdatedError(

View file

@ -47,7 +47,7 @@ def _print_user_skipped(hook, write, args):
))
def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
def _run_single_hook(runner, repository, hook, args, write, skips=set()):
if args.all_files:
get_filenames = git.get_all_files_matching
elif git.is_in_merge_conflict():
@ -55,10 +55,8 @@ def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
else:
get_filenames = git.get_staged_files_matching
hook = repository.hooks[hook_id]
filenames = get_filenames(hook['files'], hook['exclude'])
if hook_id in skips:
if hook['id'] in skips:
_print_user_skipped(hook, write, args)
return 0
elif not filenames:
@ -70,9 +68,9 @@ def _run_single_hook(runner, repository, hook_id, args, write, skips=set()):
write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
sys.stdout.flush()
retcode, stdout, stderr = repository.run_hook(hook_id, filenames)
retcode, stdout, stderr = repository.run_hook(hook, filenames)
if retcode != repository.hooks[hook_id]['expected_return_value']:
if retcode != hook['expected_return_value']:
retcode = 1
print_color = color.RED
pass_fail = 'Failed'
@ -101,9 +99,9 @@ def _run_hooks(runner, args, write, environ):
skips = _get_skips(environ)
for repo in runner.repositories:
for hook_id in repo.hooks:
for _, hook in repo.hooks:
retval |= _run_single_hook(
runner, repo, hook_id, args, write, skips=skips,
runner, repo, hook, args, write, skips=skips,
)
return retval
@ -112,8 +110,11 @@ def _run_hooks(runner, args, write, environ):
def _run_hook(runner, args, write):
hook_id = args.hook
for repo in runner.repositories:
if hook_id in repo.hooks:
return _run_single_hook(runner, repo, hook_id, args, write=write)
for hook_id_in_repo, hook in repo.hooks:
if hook_id == hook_id_in_repo:
return _run_single_hook(
runner, repo, hook, args, write=write,
)
else:
write('No hook with id `{0}`\n'.format(hook_id))
return 1

View file

@ -4,7 +4,6 @@ from cached_property import cached_property
from pre_commit.languages.all import languages
from pre_commit.manifest import Manifest
from pre_commit.ordereddict import OrderedDict
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
@ -33,13 +32,13 @@ class Repository(object):
def languages(self):
return set(
(hook['language'], hook['language_version'])
for hook in self.hooks.values()
for _, hook in self.hooks
)
@cached_property
def hooks(self):
# TODO: merging in manifest dicts is a smell imo
return OrderedDict(
return tuple(
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
for hook in self.repo_config['hooks']
)
@ -71,15 +70,14 @@ class Repository(object):
continue
language.install_environment(self.cmd_runner, language_version)
def run_hook(self, hook_id, file_args):
def run_hook(self, hook, file_args):
"""Run a hook.
Args:
hook_id - Id of the hook
hook - Hook dictionary
file_args - List of files to run
"""
self.require_installed()
hook = self.hooks[hook_id]
return languages[hook['language']].run_hook(
self.cmd_runner, hook, file_args,
)