Skip to content

Commit 8a0dd01

Browse files
committed
Implement check-files-matches-any meta hook
1 parent 8df11ee commit 8a0dd01

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import re
2+
import sys
3+
4+
import pre_commit.constants as C
5+
from pre_commit.clientlib import load_config
6+
from pre_commit.git import get_all_files
7+
8+
9+
def files_matches_any(filenames, include):
10+
include_re = re.compile(include)
11+
for filename in filenames:
12+
if include_re.search(filename):
13+
return True
14+
return False
15+
16+
17+
def check_files_matches_any(config_file=None):
18+
config = load_config(config_file or C.CONFIG_FILE)
19+
files = get_all_files()
20+
files_not_matched = False
21+
22+
for repo in config['repos']:
23+
for hook in repo['hooks']:
24+
include = hook.get('files', '')
25+
if include and not files_matches_any(files, include):
26+
print(
27+
'The files pattern for {} does not match any files'
28+
.format(hook['id'])
29+
)
30+
files_not_matched = True
31+
32+
return files_not_matched
33+
34+
35+
if __name__ == '__main__':
36+
sys.exit(check_files_matches_any())

pre_commit/repository.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ class MetaRepository(LocalRepository):
259259
'entry': pipes.quote(sys.executable),
260260
'args': ['-m', 'pre_commit.meta_hooks.check_useless_excludes'],
261261
},
262+
'check-files-matches-any': {
263+
'name': 'Check hooks match any files',
264+
'files': '.pre-commit-config.yaml',
265+
'language': 'system',
266+
'entry': pipes.quote(sys.executable),
267+
'args': ['-m', 'pre_commit.meta_hooks.check_files_matches_any'],
268+
},
262269
}
263270

264271
@cached_property

tests/commands/run_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,39 @@ def test_useless_exclude_for_hook(
735735
)
736736

737737

738+
def test_files_match_any(
739+
cap_out, repo_with_passing_hook, mock_out_store_directory,
740+
):
741+
config = OrderedDict((
742+
('repo', 'meta'),
743+
(
744+
'hooks', (
745+
OrderedDict((
746+
('id', 'check-files-matches-any'),
747+
)),
748+
OrderedDict((
749+
('id', 'check-useless-excludes'),
750+
('files', 'foo'),
751+
)),
752+
),
753+
),
754+
))
755+
add_config_to_repo(repo_with_passing_hook, config)
756+
757+
_test_run(
758+
cap_out,
759+
repo_with_passing_hook,
760+
opts={'all_files': True},
761+
expected_outputs=[
762+
b'Check hooks match any files',
763+
b'The files pattern for check-useless-excludes '
764+
b'does not match any files',
765+
],
766+
expected_ret=1,
767+
stage=False,
768+
)
769+
770+
738771
@pytest.yield_fixture
739772
def modified_config_repo(repo_with_passing_hook):
740773
with modify_config(repo_with_passing_hook, commit=False) as config:

0 commit comments

Comments
 (0)