Refactor pre_commit.repository and factor out cached-property

This commit is contained in:
Anthony Sottile 2018-12-30 18:11:28 -08:00
parent 7448e588ff
commit c577ed92e7
14 changed files with 390 additions and 446 deletions

View file

@ -5,25 +5,33 @@ 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.repository import repositories
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()
retv = 0
for repo in repositories(load_config(config_file), Store()):
for hook_id, hook in repo.hooks:
if hook['always_run'] or hook['language'] == 'fail':
continue
include, exclude = hook['files'], hook['exclude']
filtered = _filter_by_include_exclude(files, include, exclude)
types, exclude_types = hook['types'], hook['exclude_types']
filtered = _filter_by_types(filtered, types, exclude_types)
if not filtered:
print('{} does not apply to this repository'.format(hook_id))
retv = 1
for hook in all_hooks(load_config(config_file), Store()):
if hook.always_run or hook.language == 'fail':
continue
include, exclude = hook.files, hook.exclude
filtered = _filter_by_include_exclude(files, include, exclude)
types, exclude_types = hook.types, hook.exclude_types
filtered = _filter_by_types(filtered, types, exclude_types)
if not filtered:
print('{} does not apply to this repository'.format(hook.id))
retv = 1
return retv

View file

@ -10,6 +10,15 @@ 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):

View file

@ -0,0 +1,10 @@
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)

View file

@ -1,6 +1,15 @@
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):