Implement check-useless-excludes meta hook

This commit is contained in:
Paul Hooijenga 2017-10-23 14:29:08 +02:00
parent 88c676a7c1
commit 8df11ee7aa
4 changed files with 118 additions and 8 deletions

View file

View file

@ -0,0 +1,41 @@
import re
import sys
import pre_commit.constants as C
from pre_commit.clientlib import load_config
from pre_commit.git import get_all_files
def exclude_matches_any(filenames, include, exclude):
include_re, exclude_re = re.compile(include), re.compile(exclude)
for filename in filenames:
if include_re.search(filename) and exclude_re.search(filename):
return True
return False
def check_useless_excludes(config_file=None):
config = load_config(config_file or C.CONFIG_FILE)
files = get_all_files()
useless_excludes = False
exclude = config.get('exclude')
if exclude != '^$' and not exclude_matches_any(files, '', exclude):
print('The global exclude pattern does not match any files')
useless_excludes = True
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):
print(
'The exclude pattern for {} does not match any files'
.format(hook['id'])
)
useless_excludes = True
return useless_excludes
if __name__ == '__main__':
sys.exit(check_useless_excludes())

View file

@ -4,6 +4,7 @@ import io
import json
import logging
import os
import pipes
import shutil
import sys
from collections import defaultdict
@ -247,13 +248,16 @@ class LocalRepository(Repository):
class MetaRepository(LocalRepository):
# 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.
meta_hooks = {
'test-hook': {
'name': 'Test Hook',
'files': '',
'check-useless-excludes': {
'name': 'Check for useless excludes',
'files': '.pre-commit-config.yaml',
'language': 'system',
'entry': 'echo "Hello World!"',
'always_run': True,
'entry': pipes.quote(sys.executable),
'args': ['-m', 'pre_commit.meta_hooks.check_useless_excludes'],
},
}