Skip to content

Commit 5a8ca2f

Browse files
committed
Some minor fixups
1 parent 9db827e commit 5a8ca2f

File tree

8 files changed

+77
-48
lines changed

8 files changed

+77
-48
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ repos:
2525
sha: v0.6.4
2626
hooks:
2727
- id: add-trailing-comma
28+
- repo: meta
29+
hooks:
30+
- id: check-useless-excludes
31+
- id: check-files-matches-any

pre_commit/commands/autoupdate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit import output
1212
from pre_commit.clientlib import CONFIG_SCHEMA
1313
from pre_commit.clientlib import is_local_repo
14+
from pre_commit.clientlib import is_meta_repo
1415
from pre_commit.clientlib import load_config
1516
from pre_commit.commands.migrate_config import migrate_config
1617
from pre_commit.repository import Repository
@@ -115,7 +116,7 @@ def autoupdate(runner, tags_only):
115116
input_config = load_config(runner.config_file_path)
116117

117118
for repo_config in input_config['repos']:
118-
if is_local_repo(repo_config):
119+
if is_local_repo(repo_config) or is_meta_repo(repo_config):
119120
output_repos.append(repo_config)
120121
continue
121122
output.write('Updating {}...'.format(repo_config['repo']))

pre_commit/meta_hooks/check_files_matches_any.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import argparse
22

33
import pre_commit.constants as C
4+
from pre_commit import git
45
from pre_commit.commands.run import _filter_by_include_exclude
56
from pre_commit.commands.run import _filter_by_types
6-
from pre_commit.git import get_all_files
77
from pre_commit.runner import Runner
88

99

1010
def check_all_hooks_match_files(config_file):
1111
runner = Runner.create(config_file)
12-
files = get_all_files()
13-
files_matched = True
12+
files = git.get_all_files()
13+
retv = 0
1414

1515
for repo in runner.repositories:
1616
for hook_id, hook in repo.hooks:
@@ -20,9 +20,9 @@ def check_all_hooks_match_files(config_file):
2020
filtered = _filter_by_types(filtered, types, exclude_types)
2121
if not filtered:
2222
print('{} does not apply to this repository'.format(hook_id))
23-
files_matched = False
23+
retv = 1
2424

25-
return files_matched
25+
return retv
2626

2727

2828
def main(argv=None):
@@ -32,7 +32,7 @@ def main(argv=None):
3232

3333
retv = 0
3434
for filename in args.filenames:
35-
retv |= not check_all_hooks_match_files(filename)
35+
retv |= check_all_hooks_match_files(filename)
3636
return retv
3737

3838

pre_commit/meta_hooks/check_useless_excludes.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import re
55

66
import pre_commit.constants as C
7+
from pre_commit import git
78
from pre_commit.clientlib import load_config
8-
from pre_commit.git import get_all_files
9+
from pre_commit.clientlib import MANIFEST_HOOK_DICT
10+
from pre_commit.schema import apply_defaults
911

1012

1113
def exclude_matches_any(filenames, include, exclude):
14+
if exclude == '^$':
15+
return True
1216
include_re, exclude_re = re.compile(include), re.compile(exclude)
1317
for filename in filenames:
1418
if include_re.search(filename) and exclude_re.search(filename):
@@ -18,28 +22,31 @@ def exclude_matches_any(filenames, include, exclude):
1822

1923
def check_useless_excludes(config_file):
2024
config = load_config(config_file)
21-
files = get_all_files()
22-
useless_excludes = False
25+
files = git.get_all_files()
26+
retv = 0
2327

24-
exclude = config.get('exclude')
25-
if exclude != '^$' and not exclude_matches_any(files, '', exclude):
28+
exclude = config['exclude']
29+
if not exclude_matches_any(files, '', exclude):
2630
print(
2731
'The global exclude pattern {!r} does not match any files'
2832
.format(exclude),
2933
)
30-
useless_excludes = True
34+
retv = 1
3135

3236
for repo in config['repos']:
3337
for hook in repo['hooks']:
34-
include, exclude = hook.get('files', ''), hook.get('exclude')
35-
if exclude and not exclude_matches_any(files, include, exclude):
38+
# Not actually a manifest dict, but this more accurately reflects
39+
# the defaults applied during runtime
40+
hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
41+
include, exclude = hook['files'], hook['exclude']
42+
if not exclude_matches_any(files, include, exclude):
3643
print(
3744
'The exclude pattern {!r} for {} does not match any files'
3845
.format(exclude, hook['id']),
3946
)
40-
useless_excludes = True
47+
retv = 1
4148

42-
return useless_excludes
49+
return retv
4350

4451

4552
def main(argv=None):

pre_commit/repository.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def _hook(*hook_dicts):
128128
return ret
129129

130130

131+
def _hook_from_manifest_dct(dct):
132+
dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT)
133+
dct = _hook(dct)
134+
return dct
135+
136+
131137
class Repository(object):
132138
def __init__(self, repo_config, store):
133139
self.repo_config = repo_config
@@ -226,14 +232,8 @@ def manifest(self):
226232

227233
@cached_property
228234
def hooks(self):
229-
def _from_manifest_dct(dct):
230-
dct = validate(dct, MANIFEST_HOOK_DICT)
231-
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
232-
dct = _hook(dct)
233-
return dct
234-
235235
return tuple(
236-
(hook['id'], _from_manifest_dct(hook))
236+
(hook['id'], _hook_from_manifest_dct(hook))
237237
for hook in self.repo_config['hooks']
238238
)
239239

@@ -258,33 +258,32 @@ def manifest_hooks(self):
258258
from pre_commit.meta_hooks import check_files_matches_any
259259
from pre_commit.meta_hooks import check_useless_excludes
260260

261-
# Note: the hook `entry` is passed through `shlex.split()` by the
262-
# command runner, so to prevent issues with spaces and backslashes
263-
# (on Windows) it must be quoted here.
261+
def _make_entry(mod):
262+
"""the hook `entry` is passed through `shlex.split()` by the
263+
command runner, so to prevent issues with spaces and backslashes
264+
(on Windows) it must be quoted here.
265+
"""
266+
return '{} -m {}'.format(pipes.quote(sys.executable), mod.__name__)
267+
264268
meta_hooks = [
265269
{
266-
'id': 'check-useless-excludes',
267-
'name': 'Check for useless excludes',
270+
'id': 'check-files-matches-any',
271+
'name': 'Check hooks match any files',
268272
'files': '.pre-commit-config.yaml',
269273
'language': 'system',
270-
'entry': pipes.quote(sys.executable),
271-
'args': ['-m', check_useless_excludes.__name__],
274+
'entry': _make_entry(check_files_matches_any),
272275
},
273276
{
274-
'id': 'check-files-matches-any',
275-
'name': 'Check hooks match any files',
277+
'id': 'check-useless-excludes',
278+
'name': 'Check for useless excludes',
276279
'files': '.pre-commit-config.yaml',
277280
'language': 'system',
278-
'entry': pipes.quote(sys.executable),
279-
'args': ['-m', check_files_matches_any.__name__],
281+
'entry': _make_entry(check_useless_excludes),
280282
},
281283
]
282284

283285
return {
284-
hook['id']: apply_defaults(
285-
validate(hook, MANIFEST_HOOK_DICT),
286-
MANIFEST_HOOK_DICT,
287-
)
286+
hook['id']: _hook_from_manifest_dct(hook)
288287
for hook in meta_hooks
289288
}
290289

tests/commands/autoupdate_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,24 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
295295
assert new_config_writen['repos'][0] == local_config
296296

297297

298+
def test_autoupdate_meta_hooks(tmpdir, capsys):
299+
cfg = tmpdir.join(C.CONFIG_FILE)
300+
cfg.write(
301+
'repos:\n'
302+
'- repo: meta\n'
303+
' hooks:\n'
304+
' - id: check-useless-excludes\n',
305+
)
306+
ret = autoupdate(Runner(tmpdir.strpath, C.CONFIG_FILE), tags_only=True)
307+
assert ret == 0
308+
assert cfg.read() == (
309+
'repos:\n'
310+
'- repo: meta\n'
311+
' hooks:\n'
312+
' - id: check-useless-excludes\n'
313+
)
314+
315+
298316
def test_updates_old_format_to_new_format(tmpdir, capsys):
299317
cfg = tmpdir.join(C.CONFIG_FILE)
300318
cfg.write(

tests/meta_hooks/hook_matches_any_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_hook_excludes_everything(
2525
add_config_to_repo(repo, config)
2626

2727
with cwd(repo):
28-
assert check_files_matches_any.main(argv=[]) == 1
28+
assert check_files_matches_any.main(()) == 1
2929

3030
out, _ = capsys.readouterr()
3131
assert 'check-useless-excludes does not apply to this repository' in out
@@ -50,7 +50,7 @@ def test_hook_includes_nothing(
5050
add_config_to_repo(repo, config)
5151

5252
with cwd(repo):
53-
assert check_files_matches_any.main(argv=[]) == 1
53+
assert check_files_matches_any.main(()) == 1
5454

5555
out, _ = capsys.readouterr()
5656
assert 'check-useless-excludes does not apply to this repository' in out
@@ -75,7 +75,7 @@ def test_hook_types_not_matched(
7575
add_config_to_repo(repo, config)
7676

7777
with cwd(repo):
78-
assert check_files_matches_any.main(argv=[]) == 1
78+
assert check_files_matches_any.main(()) == 1
7979

8080
out, _ = capsys.readouterr()
8181
assert 'check-useless-excludes does not apply to this repository' in out
@@ -100,7 +100,7 @@ def test_hook_types_excludes_everything(
100100
add_config_to_repo(repo, config)
101101

102102
with cwd(repo):
103-
assert check_files_matches_any.main(argv=[]) == 1
103+
assert check_files_matches_any.main(()) == 1
104104

105105
out, _ = capsys.readouterr()
106106
assert 'check-useless-excludes does not apply to this repository' in out
@@ -124,7 +124,7 @@ def test_valid_includes(
124124
add_config_to_repo(repo, config)
125125

126126
with cwd(repo):
127-
assert check_files_matches_any.main(argv=[]) == 0
127+
assert check_files_matches_any.main(()) == 0
128128

129129
out, _ = capsys.readouterr()
130130
assert out == ''

tests/meta_hooks/useless_excludes_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_useless_exclude_global(capsys, tempdir_factory):
2929
add_config_to_repo(repo, config)
3030

3131
with cwd(repo):
32-
assert check_useless_excludes.main(argv=[]) == 1
32+
assert check_useless_excludes.main(()) == 1
3333

3434
out, _ = capsys.readouterr()
3535
assert "The global exclude pattern 'foo' does not match any files" in out
@@ -52,7 +52,7 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory):
5252
add_config_to_repo(repo, config)
5353

5454
with cwd(repo):
55-
assert check_useless_excludes.main(argv=[]) == 1
55+
assert check_useless_excludes.main(()) == 1
5656

5757
out, _ = capsys.readouterr()
5858
expected = (
@@ -78,7 +78,7 @@ def test_no_excludes(capsys, tempdir_factory):
7878
add_config_to_repo(repo, config)
7979

8080
with cwd(repo):
81-
assert check_useless_excludes.main(argv=[]) == 0
81+
assert check_useless_excludes.main(()) == 0
8282

8383
out, _ = capsys.readouterr()
8484
assert out == ''
@@ -101,7 +101,7 @@ def test_valid_exclude(capsys, tempdir_factory):
101101
add_config_to_repo(repo, config)
102102

103103
with cwd(repo):
104-
assert check_useless_excludes.main(argv=[]) == 0
104+
assert check_useless_excludes.main(()) == 0
105105

106106
out, _ = capsys.readouterr()
107107
assert out == ''

0 commit comments

Comments
 (0)