mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-17 14:30:05 -06:00
Default local / meta through cfgv
This commit is contained in:
@@ -3,6 +3,8 @@ from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import functools
|
||||
import pipes
|
||||
import sys
|
||||
|
||||
import cfgv
|
||||
from aspy.yaml import ordered_load
|
||||
@@ -88,8 +90,8 @@ def validate_manifest_main(argv=None):
|
||||
return ret
|
||||
|
||||
|
||||
_LOCAL_SENTINEL = 'local'
|
||||
_META_SENTINEL = 'meta'
|
||||
_LOCAL = 'local'
|
||||
_META = 'meta'
|
||||
|
||||
|
||||
class MigrateShaToRev(object):
|
||||
@@ -98,12 +100,12 @@ class MigrateShaToRev(object):
|
||||
return cfgv.Conditional(
|
||||
key, cfgv.check_string,
|
||||
condition_key='repo',
|
||||
condition_value=cfgv.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
|
||||
condition_value=cfgv.NotIn(_LOCAL, _META),
|
||||
ensure_absent=True,
|
||||
)
|
||||
|
||||
def check(self, dct):
|
||||
if dct.get('repo') in {_LOCAL_SENTINEL, _META_SENTINEL}:
|
||||
if dct.get('repo') in {_LOCAL, _META}:
|
||||
self._cond('rev').check(dct)
|
||||
self._cond('sha').check(dct)
|
||||
elif 'sha' in dct and 'rev' in dct:
|
||||
@@ -121,6 +123,61 @@ class MigrateShaToRev(object):
|
||||
pass
|
||||
|
||||
|
||||
def _entry(modname):
|
||||
"""the hook `entry` is passed through `shlex.split()` by the command
|
||||
runner, so to prevent issues with spaces and backslashes (on Windows)
|
||||
it must be quoted here.
|
||||
"""
|
||||
return '{} -m pre_commit.meta_hooks.{}'.format(
|
||||
pipes.quote(sys.executable), modname,
|
||||
)
|
||||
|
||||
|
||||
_meta = (
|
||||
(
|
||||
'check-hooks-apply', (
|
||||
('name', 'Check hooks apply to the repository'),
|
||||
('files', C.CONFIG_FILE),
|
||||
('entry', _entry('check_hooks_apply')),
|
||||
),
|
||||
),
|
||||
(
|
||||
'check-useless-excludes', (
|
||||
('name', 'Check for useless excludes'),
|
||||
('files', C.CONFIG_FILE),
|
||||
('entry', _entry('check_useless_excludes')),
|
||||
),
|
||||
),
|
||||
(
|
||||
'identity', (
|
||||
('name', 'identity'),
|
||||
('verbose', True),
|
||||
('entry', _entry('identity')),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
META_HOOK_DICT = cfgv.Map(
|
||||
'Hook', 'id',
|
||||
*([
|
||||
cfgv.Required('id', cfgv.check_string),
|
||||
cfgv.Required('id', cfgv.check_one_of(tuple(k for k, _ in _meta))),
|
||||
# language must be system
|
||||
cfgv.Optional('language', cfgv.check_one_of({'system'}), 'system'),
|
||||
] + [
|
||||
# default to the hook definition for the meta hooks
|
||||
cfgv.ConditionalOptional(key, cfgv.check_any, value, 'id', hook_id)
|
||||
for hook_id, values in _meta
|
||||
for key, value in values
|
||||
] + [
|
||||
# default to the "manifest" parsing
|
||||
cfgv.OptionalNoDefault(item.key, item.check_fn)
|
||||
# these will always be defaulted above
|
||||
if item.key in {'name', 'language', 'entry'} else
|
||||
item
|
||||
for item in MANIFEST_HOOK_DICT.items
|
||||
])
|
||||
)
|
||||
CONFIG_HOOK_DICT = cfgv.Map(
|
||||
'Hook', 'id',
|
||||
|
||||
@@ -140,7 +197,19 @@ CONFIG_REPO_DICT = cfgv.Map(
|
||||
'Repository', 'repo',
|
||||
|
||||
cfgv.Required('repo', cfgv.check_string),
|
||||
cfgv.RequiredRecurse('hooks', cfgv.Array(CONFIG_HOOK_DICT)),
|
||||
|
||||
cfgv.ConditionalRecurse(
|
||||
'hooks', cfgv.Array(CONFIG_HOOK_DICT),
|
||||
'repo', cfgv.NotIn(_LOCAL, _META),
|
||||
),
|
||||
cfgv.ConditionalRecurse(
|
||||
'hooks', cfgv.Array(MANIFEST_HOOK_DICT),
|
||||
'repo', _LOCAL,
|
||||
),
|
||||
cfgv.ConditionalRecurse(
|
||||
'hooks', cfgv.Array(META_HOOK_DICT),
|
||||
'repo', _META,
|
||||
),
|
||||
|
||||
MigrateShaToRev(),
|
||||
)
|
||||
@@ -154,11 +223,11 @@ CONFIG_SCHEMA = cfgv.Map(
|
||||
|
||||
|
||||
def is_local_repo(repo_entry):
|
||||
return repo_entry['repo'] == _LOCAL_SENTINEL
|
||||
return repo_entry['repo'] == _LOCAL
|
||||
|
||||
|
||||
def is_meta_repo(repo_entry):
|
||||
return repo_entry['repo'] == _META_SENTINEL
|
||||
return repo_entry['repo'] == _META
|
||||
|
||||
|
||||
class InvalidConfigError(FatalError):
|
||||
|
||||
@@ -5,18 +5,9 @@ from pre_commit import git
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.commands.run import _filter_by_include_exclude
|
||||
from pre_commit.commands.run import _filter_by_types
|
||||
from pre_commit.meta_hooks.helpers import make_meta_entry
|
||||
from pre_commit.repository import all_hooks
|
||||
from pre_commit.store import Store
|
||||
|
||||
HOOK_DICT = {
|
||||
'id': 'check-hooks-apply',
|
||||
'name': 'Check hooks apply to the repository',
|
||||
'files': C.CONFIG_FILE,
|
||||
'language': 'system',
|
||||
'entry': make_meta_entry(__name__),
|
||||
}
|
||||
|
||||
|
||||
def check_all_hooks_match_files(config_file):
|
||||
files = git.get_all_files()
|
||||
|
||||
@@ -10,15 +10,6 @@ from pre_commit import git
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.clientlib import MANIFEST_HOOK_DICT
|
||||
from pre_commit.commands.run import _filter_by_types
|
||||
from pre_commit.meta_hooks.helpers import make_meta_entry
|
||||
|
||||
HOOK_DICT = {
|
||||
'id': 'check-useless-excludes',
|
||||
'name': 'Check for useless excludes',
|
||||
'files': C.CONFIG_FILE,
|
||||
'language': 'system',
|
||||
'entry': make_meta_entry(__name__),
|
||||
}
|
||||
|
||||
|
||||
def exclude_matches_any(filenames, include, exclude):
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import pipes
|
||||
import sys
|
||||
|
||||
|
||||
def make_meta_entry(modname):
|
||||
"""the hook `entry` is passed through `shlex.split()` by the command
|
||||
runner, so to prevent issues with spaces and backslashes (on Windows)
|
||||
it must be quoted here.
|
||||
"""
|
||||
return '{} -m {}'.format(pipes.quote(sys.executable), modname)
|
||||
@@ -1,15 +1,6 @@
|
||||
import sys
|
||||
|
||||
from pre_commit import output
|
||||
from pre_commit.meta_hooks.helpers import make_meta_entry
|
||||
|
||||
HOOK_DICT = {
|
||||
'id': 'identity',
|
||||
'name': 'identity',
|
||||
'language': 'system',
|
||||
'verbose': True,
|
||||
'entry': make_meta_entry(__name__),
|
||||
}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
||||
@@ -6,9 +6,6 @@ import json
|
||||
import logging
|
||||
import os
|
||||
|
||||
from cfgv import apply_defaults
|
||||
from cfgv import validate
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import five
|
||||
from pre_commit.clientlib import is_local_repo
|
||||
@@ -137,15 +134,8 @@ def _hook(*hook_dicts):
|
||||
return ret
|
||||
|
||||
|
||||
def _hook_from_manifest_dct(dct):
|
||||
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
|
||||
dct = validate(dct, MANIFEST_HOOK_DICT)
|
||||
dct = _hook(dct)
|
||||
return dct
|
||||
|
||||
|
||||
def _local_repository_hooks(repo_config, store):
|
||||
def _local_prefix(language_name, deps):
|
||||
def _non_cloned_repository_hooks(repo_config, store):
|
||||
def _prefix(language_name, deps):
|
||||
language = languages[language_name]
|
||||
# pcre / pygrep / script / system / docker_image do not have
|
||||
# environments so they work out of the current directory
|
||||
@@ -154,45 +144,11 @@ def _local_repository_hooks(repo_config, store):
|
||||
else:
|
||||
return Prefix(store.make_local(deps))
|
||||
|
||||
hook_dcts = [_hook_from_manifest_dct(h) for h in repo_config['hooks']]
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo_config['repo'],
|
||||
_local_prefix(hook['language'], hook['additional_dependencies']),
|
||||
hook,
|
||||
)
|
||||
for hook in hook_dcts
|
||||
)
|
||||
|
||||
|
||||
def _meta_repository_hooks(repo_config, store):
|
||||
# imported here to prevent circular imports.
|
||||
from pre_commit.meta_hooks import check_hooks_apply
|
||||
from pre_commit.meta_hooks import check_useless_excludes
|
||||
from pre_commit.meta_hooks import identity
|
||||
|
||||
meta_hooks = [
|
||||
_hook_from_manifest_dct(mod.HOOK_DICT)
|
||||
for mod in (check_hooks_apply, check_useless_excludes, identity)
|
||||
]
|
||||
by_id = {hook['id']: hook for hook in meta_hooks}
|
||||
|
||||
for hook in repo_config['hooks']:
|
||||
if hook['id'] not in by_id:
|
||||
logger.error(
|
||||
'`{}` is not a valid meta hook. '
|
||||
'Typo? Perhaps it is introduced in a newer version? '
|
||||
'Often `pip install --upgrade pre-commit` fixes this.'
|
||||
.format(hook['id']),
|
||||
)
|
||||
exit(1)
|
||||
|
||||
prefix = Prefix(os.getcwd())
|
||||
return tuple(
|
||||
Hook.create(
|
||||
repo_config['repo'],
|
||||
prefix,
|
||||
_hook(by_id[hook['id']], hook),
|
||||
_prefix(hook['language'], hook['additional_dependencies']),
|
||||
_hook(hook),
|
||||
)
|
||||
for hook in repo_config['hooks']
|
||||
)
|
||||
@@ -225,10 +181,8 @@ def _cloned_repository_hooks(repo_config, store):
|
||||
|
||||
|
||||
def repository_hooks(repo_config, store):
|
||||
if is_local_repo(repo_config):
|
||||
return _local_repository_hooks(repo_config, store)
|
||||
elif is_meta_repo(repo_config):
|
||||
return _meta_repository_hooks(repo_config, store)
|
||||
if is_local_repo(repo_config) or is_meta_repo(repo_config):
|
||||
return _non_cloned_repository_hooks(repo_config, store)
|
||||
else:
|
||||
return _cloned_repository_hooks(repo_config, store)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user