Add support for meta hooks

This commit is contained in:
Paul Hooijenga 2017-10-22 16:40:19 +02:00
parent 39d5205e31
commit 88c676a7c1
6 changed files with 130 additions and 1 deletions

View file

@ -98,6 +98,8 @@ def validate_manifest_main(argv=None):
_LOCAL_SENTINEL = 'local'
_META_SENTINEL = 'meta'
CONFIG_HOOK_DICT = schema.Map(
'Hook', 'id',
@ -121,7 +123,8 @@ CONFIG_REPO_DICT = schema.Map(
schema.Conditional(
'sha', schema.check_string,
condition_key='repo', condition_value=schema.Not(_LOCAL_SENTINEL),
condition_key='repo',
condition_value=schema.NotIn((_LOCAL_SENTINEL, _META_SENTINEL)),
ensure_absent=True,
),
)
@ -138,6 +141,10 @@ def is_local_repo(repo_entry):
return repo_entry['repo'] == _LOCAL_SENTINEL
def is_meta_repo(repo_entry):
return repo_entry['repo'] == _META_SENTINEL
class InvalidConfigError(FatalError):
pass

View file

@ -5,6 +5,7 @@ import json
import logging
import os
import shutil
import sys
from collections import defaultdict
import pkg_resources
@ -14,6 +15,7 @@ import pre_commit.constants as C
from pre_commit import five
from pre_commit import git
from pre_commit.clientlib import is_local_repo
from pre_commit.clientlib import is_meta_repo
from pre_commit.clientlib import MANIFEST_HOOK_DICT
from pre_commit.languages.all import languages
from pre_commit.languages.helpers import environment_dir
@ -128,6 +130,8 @@ class Repository(object):
def create(cls, config, store):
if is_local_repo(config):
return LocalRepository(config, store)
elif is_meta_repo(config):
return MetaRepository(config, store)
else:
return cls(config, store)
@ -242,6 +246,45 @@ class LocalRepository(Repository):
return tuple(ret)
class MetaRepository(LocalRepository):
meta_hooks = {
'test-hook': {
'name': 'Test Hook',
'files': '',
'language': 'system',
'entry': 'echo "Hello World!"',
'always_run': True,
},
}
@cached_property
def hooks(self):
for hook in self.repo_config['hooks']:
if hook['id'] not in self.meta_hooks:
logger.error(
'`{}` is not a valid meta hook. '
'Typo? Perhaps it is introduced in a newer version? '
'Often `pre-commit autoupdate` fixes this.'.format(
hook['id'],
),
)
exit(1)
return tuple(
(
hook['id'],
apply_defaults(
validate(
dict(self.meta_hooks[hook['id']], **hook),
MANIFEST_HOOK_DICT,
),
MANIFEST_HOOK_DICT,
),
)
for hook in self.repo_config['hooks']
)
class _UniqueList(list):
def __init__(self):
self._set = set()

View file

@ -101,6 +101,9 @@ def _check_conditional(self, dct):
if isinstance(self.condition_value, Not):
op = 'is'
cond_val = self.condition_value.val
elif isinstance(self.condition_value, NotIn):
op = 'is any of'
cond_val = self.condition_value.values
else:
op = 'is not'
cond_val = self.condition_value
@ -206,6 +209,14 @@ class Not(object):
return other is not MISSING and other != self.val
class NotIn(object):
def __init__(self, values):
self.values = values
def __eq__(self, other):
return other is not MISSING and other not in self.values
def check_any(_):
pass