Adding support for locally-defined hooks

This commit is contained in:
Lucas Cimon
2015-05-10 18:38:58 +02:00
parent d97ea30c4b
commit b68261c720
8 changed files with 241 additions and 40 deletions

View File

@@ -6,6 +6,13 @@ from pre_commit.clientlib.validate_base import is_regex_valid
from pre_commit.errors import FatalError
_LOCAL_HOOKS_MAGIC_REPO_STRING = 'local'
def is_local_hooks(repo_entry):
return repo_entry['repo'] == _LOCAL_HOOKS_MAGIC_REPO_STRING
class InvalidConfigError(FatalError):
pass
@@ -53,7 +60,12 @@ def try_regex(repo, hook, value, field_name):
def validate_config_extra(config):
for repo in config:
if 'sha' not in repo:
if is_local_hooks(repo):
if 'sha' in repo:
raise InvalidConfigError(
'"sha" property provided for local hooks'
)
elif 'sha' not in repo:
raise InvalidConfigError(
'Missing "sha" field for repository {0}'.format(repo['repo'])
)

View File

@@ -18,15 +18,6 @@ from pre_commit.util import noop_context
logger = logging.getLogger('pre_commit')
class HookExecutor(object):
def __init__(self, hook, invoker):
self.hook = hook
self._invoker = invoker
def invoke(self, filenames):
return self._invoker(self.hook, filenames)
def _get_skips(environ):
skips = environ.get('SKIP', '')
return set(skip.strip() for skip in skips.split(',') if skip.strip())
@@ -80,8 +71,7 @@ def get_filenames(args, include_expr, exclude_expr):
return getter(include_expr, exclude_expr)
def _run_single_hook(hook_executor, args, write, skips=frozenset()):
hook = hook_executor.hook
def _run_single_hook(hook, repo, args, write, skips=frozenset()):
filenames = get_filenames(args, hook['files'], hook['exclude'])
if hook['id'] in skips:
_print_user_skipped(hook, write, args)
@@ -95,7 +85,7 @@ def _run_single_hook(hook_executor, args, write, skips=frozenset()):
write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6))
sys.stdout.flush()
retcode, stdout, stderr = hook_executor.invoke(filenames)
retcode, stdout, stderr = repo.run_hook(hook, filenames)
if retcode != hook['expected_return_value']:
retcode = 1
@@ -119,19 +109,19 @@ def _run_single_hook(hook_executor, args, write, skips=frozenset()):
return retcode
def _run_hooks(hook_executors, args, write, environ):
def _run_hooks(repo_hooks, args, write, environ):
"""Actually run the hooks."""
skips = _get_skips(environ)
retval = 0
for hook_executor in hook_executors:
retval |= _run_single_hook(hook_executor, args, write, skips)
for repo, hook in repo_hooks:
retval |= _run_single_hook(hook, repo, args, write, skips)
return retval
def get_hook_executors(runner):
def get_repo_hooks(runner):
for repo in runner.repositories:
for _, repo_hook in repo.hooks:
yield HookExecutor(repo_hook, repo.run_hook)
for _, hook in repo.hooks:
yield (repo, hook)
def _has_unmerged_paths(runner):
@@ -159,13 +149,13 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
ctx = staged_files_only(runner.cmd_runner)
with ctx:
hook_executors = list(get_hook_executors(runner))
repo_hooks = list(get_repo_hooks(runner))
if args.hook:
hook_executors = [
he for he in hook_executors
if he.hook['id'] == args.hook
repo_hooks = [
(repo, hook) for repo, hook in repo_hooks
if hook['id'] == args.hook
]
if not hook_executors:
if not repo_hooks:
write('No hook with id `{0}`\n'.format(args.hook))
return 1
return _run_hooks(hook_executors, args, write, environ)
return _run_hooks(repo_hooks, args, write, environ)

View File

@@ -5,6 +5,10 @@ import shutil
from cached_property import cached_property
from pre_commit import git
from pre_commit.clientlib.validate_config import is_local_hooks
from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.languages.all import languages
from pre_commit.manifest import Manifest
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
@@ -21,10 +25,13 @@ class Repository(object):
@classmethod
def create(cls, config, store):
repo_path_getter = store.get_repo_path_getter(
config['repo'], config['sha']
)
return cls(config, repo_path_getter)
if is_local_hooks(config):
return LocalRepository(config)
else:
repo_path_getter = store.get_repo_path_getter(
config['repo'], config['sha']
)
return cls(config, repo_path_getter)
@cached_property
def repo_url(self):
@@ -111,3 +118,28 @@ class Repository(object):
return languages[hook['language']].run_hook(
self.cmd_runner, hook, file_args,
)
class LocalRepository(Repository):
def __init__(self, repo_config, repo_path_getter=None):
repo_path_getter = None
super(LocalRepository, self).__init__(repo_config, repo_path_getter)
@cached_property
def hooks(self):
return tuple(
(hook['id'], apply_defaults(hook, MANIFEST_JSON_SCHEMA['items']))
for hook in self.repo_config['hooks']
)
@cached_property
def cmd_runner(self):
return PrefixedCommandRunner(git.get_root())
@cached_property
def sha(self):
raise NotImplementedError
@cached_property
def manifest(self):
raise NotImplementedError