Some minor fixups

This commit is contained in:
Anthony Sottile
2017-10-30 09:12:48 -07:00
parent 9db827ef9d
commit 5a8ca2ffbe
8 changed files with 79 additions and 50 deletions

View File

@@ -11,6 +11,7 @@ import pre_commit.constants as C
from pre_commit import output
from pre_commit.clientlib import CONFIG_SCHEMA
from pre_commit.clientlib import is_local_repo
from pre_commit.clientlib import is_meta_repo
from pre_commit.clientlib import load_config
from pre_commit.commands.migrate_config import migrate_config
from pre_commit.repository import Repository
@@ -115,7 +116,7 @@ def autoupdate(runner, tags_only):
input_config = load_config(runner.config_file_path)
for repo_config in input_config['repos']:
if is_local_repo(repo_config):
if is_local_repo(repo_config) or is_meta_repo(repo_config):
output_repos.append(repo_config)
continue
output.write('Updating {}...'.format(repo_config['repo']))

View File

@@ -1,16 +1,16 @@
import argparse
import pre_commit.constants as C
from pre_commit import git
from pre_commit.commands.run import _filter_by_include_exclude
from pre_commit.commands.run import _filter_by_types
from pre_commit.git import get_all_files
from pre_commit.runner import Runner
def check_all_hooks_match_files(config_file):
runner = Runner.create(config_file)
files = get_all_files()
files_matched = True
files = git.get_all_files()
retv = 0
for repo in runner.repositories:
for hook_id, hook in repo.hooks:
@@ -20,9 +20,9 @@ def check_all_hooks_match_files(config_file):
filtered = _filter_by_types(filtered, types, exclude_types)
if not filtered:
print('{} does not apply to this repository'.format(hook_id))
files_matched = False
retv = 1
return files_matched
return retv
def main(argv=None):
@@ -32,7 +32,7 @@ def main(argv=None):
retv = 0
for filename in args.filenames:
retv |= not check_all_hooks_match_files(filename)
retv |= check_all_hooks_match_files(filename)
return retv

View File

@@ -4,11 +4,15 @@ import argparse
import re
import pre_commit.constants as C
from pre_commit import git
from pre_commit.clientlib import load_config
from pre_commit.git import get_all_files
from pre_commit.clientlib import MANIFEST_HOOK_DICT
from pre_commit.schema import apply_defaults
def exclude_matches_any(filenames, include, exclude):
if exclude == '^$':
return True
include_re, exclude_re = re.compile(include), re.compile(exclude)
for filename in filenames:
if include_re.search(filename) and exclude_re.search(filename):
@@ -18,28 +22,31 @@ def exclude_matches_any(filenames, include, exclude):
def check_useless_excludes(config_file):
config = load_config(config_file)
files = get_all_files()
useless_excludes = False
files = git.get_all_files()
retv = 0
exclude = config.get('exclude')
if exclude != '^$' and not exclude_matches_any(files, '', exclude):
exclude = config['exclude']
if not exclude_matches_any(files, '', exclude):
print(
'The global exclude pattern {!r} does not match any files'
.format(exclude),
)
useless_excludes = True
retv = 1
for repo in config['repos']:
for hook in repo['hooks']:
include, exclude = hook.get('files', ''), hook.get('exclude')
if exclude and not exclude_matches_any(files, include, exclude):
# Not actually a manifest dict, but this more accurately reflects
# the defaults applied during runtime
hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
include, exclude = hook['files'], hook['exclude']
if not exclude_matches_any(files, include, exclude):
print(
'The exclude pattern {!r} for {} does not match any files'
.format(exclude, hook['id']),
)
useless_excludes = True
retv = 1
return useless_excludes
return retv
def main(argv=None):

View File

@@ -128,6 +128,12 @@ def _hook(*hook_dicts):
return ret
def _hook_from_manifest_dct(dct):
dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT)
dct = _hook(dct)
return dct
class Repository(object):
def __init__(self, repo_config, store):
self.repo_config = repo_config
@@ -226,14 +232,8 @@ class LocalRepository(Repository):
@cached_property
def hooks(self):
def _from_manifest_dct(dct):
dct = validate(dct, MANIFEST_HOOK_DICT)
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
dct = _hook(dct)
return dct
return tuple(
(hook['id'], _from_manifest_dct(hook))
(hook['id'], _hook_from_manifest_dct(hook))
for hook in self.repo_config['hooks']
)
@@ -258,33 +258,32 @@ class MetaRepository(LocalRepository):
from pre_commit.meta_hooks import check_files_matches_any
from pre_commit.meta_hooks import check_useless_excludes
# Note: 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.
def _make_entry(mod):
"""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), mod.__name__)
meta_hooks = [
{
'id': 'check-useless-excludes',
'name': 'Check for useless excludes',
'files': '.pre-commit-config.yaml',
'language': 'system',
'entry': pipes.quote(sys.executable),
'args': ['-m', check_useless_excludes.__name__],
},
{
'id': 'check-files-matches-any',
'name': 'Check hooks match any files',
'files': '.pre-commit-config.yaml',
'language': 'system',
'entry': pipes.quote(sys.executable),
'args': ['-m', check_files_matches_any.__name__],
'entry': _make_entry(check_files_matches_any),
},
{
'id': 'check-useless-excludes',
'name': 'Check for useless excludes',
'files': '.pre-commit-config.yaml',
'language': 'system',
'entry': _make_entry(check_useless_excludes),
},
]
return {
hook['id']: apply_defaults(
validate(hook, MANIFEST_HOOK_DICT),
MANIFEST_HOOK_DICT,
)
hook['id']: _hook_from_manifest_dct(hook)
for hook in meta_hooks
}