Autoupdate roundtrips. Resolves #210

This commit is contained in:
Anthony Sottile 2015-03-18 10:06:23 -07:00
parent da7e85c851
commit de33fad483
11 changed files with 78 additions and 98 deletions

View file

@ -5,17 +5,17 @@ import contextlib
import io
import os.path
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
from ruamel.yaml.comments import CommentedMap
import pre_commit.constants as C
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.clientlib.validate_manifest import load_manifest
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.ordereddict import OrderedDict
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import yaml_dump
from pre_commit.util import yaml_load
from testing.util import copy_tree_to_path
from testing.util import get_head_sha
from testing.util import get_resource_path
@ -41,10 +41,10 @@ def make_repo(tempdir_factory, repo_source):
def modify_manifest(path):
"""Modify the manifest yielded by this context to write to hooks.yaml."""
manifest_path = os.path.join(path, C.MANIFEST_FILE)
manifest = ordered_load(io.open(manifest_path).read())
manifest = yaml_load(io.open(manifest_path).read())
yield manifest
with io.open(manifest_path, 'w') as manifest_file:
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
manifest_file.write(yaml_dump(manifest))
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
@ -54,18 +54,18 @@ def modify_config(path='.', commit=True):
.pre-commit-config.yaml
"""
config_path = os.path.join(path, C.CONFIG_FILE)
config = ordered_load(io.open(config_path).read())
config = yaml_load(io.open(config_path).read())
yield config
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
config_file.write(yaml_dump(config))
if commit:
cmd_output('git', 'commit', '-am', 'update config', cwd=path)
def config_with_local_hooks():
return OrderedDict((
return CommentedMap((
('repo', 'local'),
('hooks', [OrderedDict((
('hooks', [CommentedMap((
('id', 'do_not_commit'),
('name', 'Block if "DO NOT COMMIT" is found'),
('entry', 'DO NOT COMMIT'),
@ -77,13 +77,10 @@ def config_with_local_hooks():
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
config = OrderedDict((
('repo', repo_path),
config = CommentedMap((
('sha', sha or get_head_sha(repo_path)),
(
'hooks',
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
),
('repo', repo_path),
('hooks', hooks or [{'id': hook['id']} for hook in manifest]),
))
if check:
@ -96,10 +93,10 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
def write_config(directory, config):
if type(config) is not list:
assert type(config) is OrderedDict
assert type(config) is CommentedMap
config = [config]
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
config_file.write(yaml_dump(config))
def add_config_to_repo(git_path, config):