mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Enable map configurations (config v2).
This commit is contained in:
parent
ef8347cf2d
commit
3e76cdaf25
11 changed files with 70 additions and 52 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import absolute_import
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import collections
|
||||
import functools
|
||||
|
||||
from aspy.yaml import ordered_load
|
||||
|
|
@ -125,7 +126,11 @@ CONFIG_REPO_DICT = schema.Map(
|
|||
ensure_absent=True,
|
||||
),
|
||||
)
|
||||
CONFIG_SCHEMA = schema.Array(CONFIG_REPO_DICT)
|
||||
CONFIG_SCHEMA = schema.Map(
|
||||
'Config', None,
|
||||
|
||||
schema.RequiredRecurse('repos', schema.Array(CONFIG_REPO_DICT)),
|
||||
)
|
||||
|
||||
|
||||
def is_local_repo(repo_entry):
|
||||
|
|
@ -136,10 +141,19 @@ class InvalidConfigError(FatalError):
|
|||
pass
|
||||
|
||||
|
||||
def ordered_load_normalize_legacy_config(contents):
|
||||
data = ordered_load(contents)
|
||||
if isinstance(data, list):
|
||||
# TODO: Once happy, issue a deprecation warning and instructions
|
||||
return collections.OrderedDict([('repos', data)])
|
||||
else:
|
||||
return data
|
||||
|
||||
|
||||
load_config = functools.partial(
|
||||
schema.load_from_filename,
|
||||
schema=CONFIG_SCHEMA,
|
||||
load_strategy=ordered_load,
|
||||
load_strategy=ordered_load_normalize_legacy_config,
|
||||
exc_tp=InvalidConfigError,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ def autoupdate(runner, tags_only):
|
|||
|
||||
input_configs = load_config(runner.config_file_path)
|
||||
|
||||
for repo_config in input_configs:
|
||||
for repo_config in input_configs['repos']:
|
||||
if is_local_repo(repo_config):
|
||||
output_configs.append(repo_config)
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -10,8 +10,9 @@ from __future__ import unicode_literals
|
|||
SAMPLE_CONFIG = '''\
|
||||
# See http://pre-commit.com for more information
|
||||
# See http://pre-commit.com/hooks.html for more hooks
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
sha: v0.9.1
|
||||
sha: v0.9.2
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
|
|
|
|||
|
|
@ -40,11 +40,11 @@ class Runner(object):
|
|||
@cached_property
|
||||
def repositories(self):
|
||||
"""Returns a tuple of the configured repositories."""
|
||||
config = load_config(self.config_file_path)
|
||||
repositories = tuple(Repository.create(x, self.store) for x in config)
|
||||
for repository in repositories:
|
||||
repository.require_installed()
|
||||
return repositories
|
||||
repos = load_config(self.config_file_path)['repos']
|
||||
repos = tuple(Repository.create(x, self.store) for x in repos)
|
||||
for repo in repos:
|
||||
repo.require_installed()
|
||||
return repos
|
||||
|
||||
def get_hook_path(self, hook_type):
|
||||
return os.path.join(self.git_dir, 'hooks', hook_type)
|
||||
|
|
|
|||
|
|
@ -142,9 +142,13 @@ class Map(collections.namedtuple('Map', ('object_name', 'id_key', 'items'))):
|
|||
raise ValidationError('Expected a {} map but got a {}'.format(
|
||||
self.object_name, type(v).__name__,
|
||||
))
|
||||
with validate_context('At {}({}={!r})'.format(
|
||||
if self.id_key is None:
|
||||
context = 'At {}()'.format(self.object_name)
|
||||
else:
|
||||
context = 'At {}({}={!r})'.format(
|
||||
self.object_name, self.id_key, v.get(self.id_key, MISSING),
|
||||
)):
|
||||
)
|
||||
with validate_context(context):
|
||||
for item in self.items:
|
||||
item.check(v)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue