Default local / meta through cfgv · precommit/pre-commit@fc84567 · GitHub
Skip to content

Commit fc84567

Browse files
committed
Default local / meta through cfgv
1 parent 46ae88c commit fc84567

11 files changed

Lines changed: 109 additions & 112 deletions

File tree

pre_commit/clientlib.py

Lines changed: 76 additions & 7 deletions

pre_commit/meta_hooks/check_hooks_apply.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@
55
from pre_commit.clientlib import load_config
66
from pre_commit.commands.run import _filter_by_include_exclude
77
from pre_commit.commands.run import _filter_by_types
8-
from pre_commit.meta_hooks.helpers import make_meta_entry
98
from pre_commit.repository import all_hooks
109
from pre_commit.store import Store
1110

12-
HOOK_DICT = {
13-
'id': 'check-hooks-apply',
14-
'name': 'Check hooks apply to the repository',
15-
'files': C.CONFIG_FILE,
16-
'language': 'system',
17-
'entry': make_meta_entry(__name__),
18-
}
19-
2011

2112
def check_all_hooks_match_files(config_file):
2213
files = git.get_all_files()

pre_commit/meta_hooks/check_useless_excludes.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
from pre_commit.clientlib import load_config
1111
from pre_commit.clientlib import MANIFEST_HOOK_DICT
1212
from pre_commit.commands.run import _filter_by_types
13-
from pre_commit.meta_hooks.helpers import make_meta_entry
14-
15-
HOOK_DICT = {
16-
'id': 'check-useless-excludes',
17-
'name': 'Check for useless excludes',
18-
'files': C.CONFIG_FILE,
19-
'language': 'system',
20-
'entry': make_meta_entry(__name__),
21-
}
2213

2314

2415
def exclude_matches_any(filenames, include, exclude):

pre_commit/meta_hooks/helpers.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

pre_commit/meta_hooks/identity.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import sys
22

33
from pre_commit import output
4-
from pre_commit.meta_hooks.helpers import make_meta_entry
5-
6-
HOOK_DICT = {
7-
'id': 'identity',
8-
'name': 'identity',
9-
'language': 'system',
10-
'verbose': True,
11-
'entry': make_meta_entry(__name__),
12-
}
134

145

156
def main(argv=None):

pre_commit/repository.py

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import logging
77
import os
88

9-
from cfgv import apply_defaults
10-
from cfgv import validate
11-
129
import pre_commit.constants as C
1310
from pre_commit import five
1411
from pre_commit.clientlib import is_local_repo
@@ -137,15 +134,8 @@ def _hook(*hook_dicts):
137134
return ret
138135

139136

140-
def _hook_from_manifest_dct(dct):
141-
dct = apply_defaults(dct, MANIFEST_HOOK_DICT)
142-
dct = validate(dct, MANIFEST_HOOK_DICT)
143-
dct = _hook(dct)
144-
return dct
145-
146-
147-
def _local_repository_hooks(repo_config, store):
148-
def _local_prefix(language_name, deps):
137+
def _non_cloned_repository_hooks(repo_config, store):
138+
def _prefix(language_name, deps):
149139
language = languages[language_name]
150140
# pcre / pygrep / script / system / docker_image do not have
151141
# environments so they work out of the current directory
@@ -154,45 +144,11 @@ def _local_prefix(language_name, deps):
154144
else:
155145
return Prefix(store.make_local(deps))
156146

157-
hook_dcts = [_hook_from_manifest_dct(h) for h in repo_config['hooks']]
158-
return tuple(
159-
Hook.create(
160-
repo_config['repo'],
161-
_local_prefix(hook['language'], hook['additional_dependencies']),
162-
hook,
163-
)
164-
for hook in hook_dcts
165-
)
166-
167-
168-
def _meta_repository_hooks(repo_config, store):
169-
# imported here to prevent circular imports.
170-
from pre_commit.meta_hooks import check_hooks_apply
171-
from pre_commit.meta_hooks import check_useless_excludes
172-
from pre_commit.meta_hooks import identity
173-
174-
meta_hooks = [
175-
_hook_from_manifest_dct(mod.HOOK_DICT)
176-
for mod in (check_hooks_apply, check_useless_excludes, identity)
177-
]
178-
by_id = {hook['id']: hook for hook in meta_hooks}
179-
180-
for hook in repo_config['hooks']:
181-
if hook['id'] not in by_id:
182-
logger.error(
183-
'`{}` is not a valid meta hook. '
184-
'Typo? Perhaps it is introduced in a newer version? '
185-
'Often `pip install --upgrade pre-commit` fixes this.'
186-
.format(hook['id']),
187-
)
188-
exit(1)
189-
190-
prefix = Prefix(os.getcwd())
191147
return tuple(
192148
Hook.create(
193149
repo_config['repo'],
194-
prefix,
195-
_hook(by_id[hook['id']], hook),
150+
_prefix(hook['language'], hook['additional_dependencies']),
151+
_hook(hook),
196152
)
197153
for hook in repo_config['hooks']
198154
)
@@ -225,10 +181,8 @@ def _cloned_repository_hooks(repo_config, store):
225181

226182

227183
def repository_hooks(repo_config, store):
228-
if is_local_repo(repo_config):
229-
return _local_repository_hooks(repo_config, store)
230-
elif is_meta_repo(repo_config):
231-
return _meta_repository_hooks(repo_config, store)
184+
if is_local_repo(repo_config) or is_meta_repo(repo_config):
185+
return _non_cloned_repository_hooks(repo_config, store)
232186
else:
233187
return _cloned_repository_hooks(repo_config, store)
234188

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
install_requires=[
3838
'aspy.yaml',
39-
'cfgv>=1.0.0',
39+
'cfgv>=1.3.0',
4040
'identify>=1.0.0',
4141
# if this makes it into python3.8 move to extras_require
4242
'importlib-metadata',

tests/clientlib_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pre_commit.clientlib import check_type_tag
77
from pre_commit.clientlib import CONFIG_HOOK_DICT
8+
from pre_commit.clientlib import CONFIG_REPO_DICT
89
from pre_commit.clientlib import CONFIG_SCHEMA
910
from pre_commit.clientlib import is_local_repo
1011
from pre_commit.clientlib import MANIFEST_SCHEMA
@@ -236,3 +237,19 @@ def test_migrate_to_sha_ok():
236237
dct = {'repo': 'a', 'rev': 'b'}
237238
MigrateShaToRev().apply_default(dct)
238239
assert dct == {'repo': 'a', 'rev': 'b'}
240+
241+
242+
@pytest.mark.parametrize(
243+
'config_repo',
244+
(
245+
# i-dont-exist isn't a valid hook
246+
{'repo': 'meta', 'hooks': [{'id': 'i-dont-exist'}]},
247+
# invalid to set a language for a meta hook
248+
{'repo': 'meta', 'hooks': [{'id': 'identity', 'language': 'python'}]},
249+
# name override must be string
250+
{'repo': 'meta', 'hooks': [{'id': 'identity', 'name': False}]},
251+
),
252+
)
253+
def test_meta_hook_invalid_id(config_repo):
254+
with pytest.raises(cfgv.ValidationError):
255+
cfgv.validate(config_repo, CONFIG_REPO_DICT)

tests/commands/autoupdate_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import pre_commit.constants as C
1010
from pre_commit import git
11-
from pre_commit.clientlib import load_config
1211
from pre_commit.commands.autoupdate import _update_repo
1312
from pre_commit.commands.autoupdate import autoupdate
1413
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
@@ -17,6 +16,7 @@
1716
from testing.fixtures import add_config_to_repo
1817
from testing.fixtures import make_config_from_repo
1918
from testing.fixtures import make_repo
19+
from testing.fixtures import read_config
2020
from testing.fixtures import sample_local_config
2121
from testing.fixtures import write_config
2222
from testing.util import get_resource_path
@@ -319,7 +319,7 @@ def test_autoupdate_local_hooks(in_git_dir, store):
319319
config = sample_local_config()
320320
add_config_to_repo('.', config)
321321
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
322-
new_config_writen = load_config(C.CONFIG_FILE)
322+
new_config_writen = read_config('.')
323323
assert len(new_config_writen['repos']) == 1
324324
assert new_config_writen['repos'][0] == config
325325

@@ -334,7 +334,7 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
334334
config = {'repos': [local_config, stale_config]}
335335
write_config('.', config)
336336
assert autoupdate(C.CONFIG_FILE, store, tags_only=False) == 0
337-
new_config_writen = load_config(C.CONFIG_FILE)
337+
new_config_writen = read_config('.')
338338
assert len(new_config_writen['repos']) == 2
339339
assert new_config_writen['repos'][0] == local_config
340340

tests/commands/gc_test.py

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)