mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Some minor fixups
This commit is contained in:
parent
9db827ef9d
commit
5a8ca2ffbe
8 changed files with 79 additions and 50 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue