mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Autoupdate works. Closes #44.
This commit is contained in:
parent
49da1ba72a
commit
26d563ee20
5 changed files with 104 additions and 7 deletions
|
|
@ -6,8 +6,12 @@ import pkg_resources
|
||||||
import stat
|
import stat
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.clientlib.validate_config import load_config
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
from pre_commit.repository import Repository
|
from pre_commit.repository import Repository
|
||||||
|
from pre_commit.yaml_extensions import ordered_dump
|
||||||
|
from pre_commit.yaml_extensions import ordered_load
|
||||||
|
|
||||||
|
|
||||||
def install(runner):
|
def install(runner):
|
||||||
|
|
@ -75,4 +79,41 @@ def _update_repository(repo_config):
|
||||||
|
|
||||||
def autoupdate(runner):
|
def autoupdate(runner):
|
||||||
"""Auto-update the pre-commit config to the latest versions of repos."""
|
"""Auto-update the pre-commit config to the latest versions of repos."""
|
||||||
pass
|
retv = 0
|
||||||
|
output_configs = []
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
input_configs = load_config(
|
||||||
|
runner.config_file_path,
|
||||||
|
load_strategy=ordered_load,
|
||||||
|
)
|
||||||
|
|
||||||
|
for repo_config in input_configs:
|
||||||
|
print('Updating {0}...'.format(repo_config['repo']), end='')
|
||||||
|
try:
|
||||||
|
new_repo_config = _update_repository(repo_config)
|
||||||
|
except RepositoryCannotBeUpdatedError as e:
|
||||||
|
print(e.args[0])
|
||||||
|
output_configs.append(repo_config)
|
||||||
|
retv = 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
if new_repo_config['sha'] != repo_config['sha']:
|
||||||
|
changed = True
|
||||||
|
print(
|
||||||
|
'updating {0} -> {1}.'.format(
|
||||||
|
repo_config['sha'], new_repo_config['sha'],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
output_configs.append(new_repo_config)
|
||||||
|
else:
|
||||||
|
print('already up to date.')
|
||||||
|
output_configs.append(repo_config)
|
||||||
|
|
||||||
|
if changed:
|
||||||
|
with open(runner.config_file_path, 'w') as config_file:
|
||||||
|
config_file.write(
|
||||||
|
ordered_dump(output_configs, **C.YAML_DUMP_KWARGS)
|
||||||
|
)
|
||||||
|
|
||||||
|
return retv
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,9 @@ SUPPORTED_LANGUAGES = set([
|
||||||
'ruby',
|
'ruby',
|
||||||
'node',
|
'node',
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
YAML_DUMP_KWARGS = {
|
||||||
|
'default_flow_style': False,
|
||||||
|
'indent': 4,
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ def ordered_load(s):
|
||||||
|
|
||||||
|
|
||||||
def ordered_dump(s, **kwargs):
|
def ordered_dump(s, **kwargs):
|
||||||
class OrderedDumper(yaml.dumper.Dumper): pass
|
class OrderedDumper(yaml.dumper.SafeDumper): pass
|
||||||
def dict_representer(dumper, data):
|
def dict_representer(dumper, data):
|
||||||
return dumper.represent_mapping(
|
return dumper.represent_mapping(
|
||||||
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,18 @@ import shutil
|
||||||
import stat
|
import stat
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
|
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_config import validate_config_extra
|
||||||
|
from pre_commit.commands import autoupdate
|
||||||
from pre_commit.commands import install
|
from pre_commit.commands import install
|
||||||
from pre_commit.commands import RepositoryCannotBeUpdatedError
|
from pre_commit.commands import RepositoryCannotBeUpdatedError
|
||||||
from pre_commit.commands import uninstall
|
from pre_commit.commands import uninstall
|
||||||
from pre_commit.commands import _update_repository
|
from pre_commit.commands import _update_repository
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
from pre_commit.runner import Runner
|
from pre_commit.runner import Runner
|
||||||
|
from pre_commit.yaml_extensions import ordered_dump
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
from testing.util import get_resource_path
|
from testing.util import get_resource_path
|
||||||
|
|
||||||
|
|
@ -54,10 +57,16 @@ def up_to_date_repo(python_hooks_repo):
|
||||||
config = OrderedDict((
|
config = OrderedDict((
|
||||||
('repo', python_hooks_repo),
|
('repo', python_hooks_repo),
|
||||||
('sha', git.get_head_sha(python_hooks_repo)),
|
('sha', git.get_head_sha(python_hooks_repo)),
|
||||||
('hooks', [{'id': 'foo', 'files': ''}]),
|
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
|
||||||
))
|
))
|
||||||
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
||||||
validate_config_extra([config])
|
validate_config_extra([config])
|
||||||
|
|
||||||
|
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
||||||
|
file_obj.write(
|
||||||
|
ordered_dump([config], **C.YAML_DUMP_KWARGS)
|
||||||
|
)
|
||||||
|
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(
|
||||||
repo_config=config,
|
repo_config=config,
|
||||||
python_hooks_repo=python_hooks_repo,
|
python_hooks_repo=python_hooks_repo,
|
||||||
|
|
@ -70,17 +79,32 @@ def test_up_to_date_repo(up_to_date_repo):
|
||||||
assert ret['sha'] == input_sha
|
assert ret['sha'] == input_sha
|
||||||
|
|
||||||
|
|
||||||
|
def test_autoupdate_up_to_date_repo(up_to_date_repo):
|
||||||
|
before = open(C.CONFIG_FILE).read()
|
||||||
|
runner = Runner(up_to_date_repo.python_hooks_repo)
|
||||||
|
ret = autoupdate(runner)
|
||||||
|
after = open(C.CONFIG_FILE).read()
|
||||||
|
assert ret == 0
|
||||||
|
assert before == after
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def out_of_date_repo(python_hooks_repo):
|
def out_of_date_repo(python_hooks_repo):
|
||||||
config = OrderedDict((
|
config = OrderedDict((
|
||||||
('repo', python_hooks_repo),
|
('repo', python_hooks_repo),
|
||||||
('sha', git.get_head_sha(python_hooks_repo)),
|
('sha', git.get_head_sha(python_hooks_repo)),
|
||||||
('hooks', [{'id': 'foo', 'files': ''}]),
|
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
|
||||||
))
|
))
|
||||||
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
||||||
validate_config_extra([config])
|
validate_config_extra([config])
|
||||||
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
local['git']['commit', '--allow-empty', '-m', 'foo']()
|
||||||
head_sha = git.get_head_sha(python_hooks_repo)
|
head_sha = git.get_head_sha(python_hooks_repo)
|
||||||
|
|
||||||
|
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
||||||
|
file_obj.write(
|
||||||
|
ordered_dump([config], **C.YAML_DUMP_KWARGS)
|
||||||
|
)
|
||||||
|
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(
|
||||||
repo_config=config,
|
repo_config=config,
|
||||||
head_sha=head_sha,
|
head_sha=head_sha,
|
||||||
|
|
@ -93,18 +117,34 @@ def test_out_of_date_repo(out_of_date_repo):
|
||||||
assert ret['sha'] == out_of_date_repo.head_sha
|
assert ret['sha'] == out_of_date_repo.head_sha
|
||||||
|
|
||||||
|
|
||||||
|
def test_autoupdate_out_of_date_repo(out_of_date_repo):
|
||||||
|
before = open(C.CONFIG_FILE).read()
|
||||||
|
runner = Runner(out_of_date_repo.python_hooks_repo)
|
||||||
|
ret = autoupdate(runner)
|
||||||
|
after = open(C.CONFIG_FILE).read()
|
||||||
|
assert ret == 0
|
||||||
|
assert before != after
|
||||||
|
assert out_of_date_repo.head_sha in after
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
def hook_disappearing_repo(python_hooks_repo):
|
def hook_disappearing_repo(python_hooks_repo):
|
||||||
config = OrderedDict((
|
config = OrderedDict((
|
||||||
('repo', python_hooks_repo),
|
('repo', python_hooks_repo),
|
||||||
('sha', git.get_head_sha(python_hooks_repo)),
|
('sha', git.get_head_sha(python_hooks_repo)),
|
||||||
('hooks', [{'id': 'foo', 'files': ''}]),
|
('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]),
|
||||||
))
|
))
|
||||||
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
|
||||||
validate_config_extra([config])
|
validate_config_extra([config])
|
||||||
shutil.copy(get_resource_path('manifest_without_foo.yaml'), 'manifest.yaml')
|
shutil.copy(get_resource_path('manifest_without_foo.yaml'), C.MANIFEST_FILE)
|
||||||
local['git']['add', '.']()
|
local['git']['add', '.']()
|
||||||
local['git']['commit', '-m', 'Remove foo']()
|
local['git']['commit', '-m', 'Remove foo']()
|
||||||
|
|
||||||
|
with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj:
|
||||||
|
file_obj.write(
|
||||||
|
ordered_dump([config], **C.YAML_DUMP_KWARGS)
|
||||||
|
)
|
||||||
|
|
||||||
yield auto_namedtuple(
|
yield auto_namedtuple(
|
||||||
repo_config=config,
|
repo_config=config,
|
||||||
python_hooks_repo=python_hooks_repo,
|
python_hooks_repo=python_hooks_repo,
|
||||||
|
|
@ -114,3 +154,12 @@ def hook_disappearing_repo(python_hooks_repo):
|
||||||
def test_hook_disppearing_repo_raises(hook_disappearing_repo):
|
def test_hook_disppearing_repo_raises(hook_disappearing_repo):
|
||||||
with pytest.raises(RepositoryCannotBeUpdatedError):
|
with pytest.raises(RepositoryCannotBeUpdatedError):
|
||||||
_update_repository(hook_disappearing_repo.repo_config)
|
_update_repository(hook_disappearing_repo.repo_config)
|
||||||
|
|
||||||
|
|
||||||
|
def test_autoupdate_hook_disappearing_repo(hook_disappearing_repo):
|
||||||
|
before = open(C.CONFIG_FILE).read()
|
||||||
|
runner = Runner(hook_disappearing_repo.python_hooks_repo)
|
||||||
|
ret = autoupdate(runner)
|
||||||
|
after = open(C.CONFIG_FILE).read()
|
||||||
|
assert ret == 1
|
||||||
|
assert before == after
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
from pre_commit.ordereddict import OrderedDict
|
from pre_commit.ordereddict import OrderedDict
|
||||||
from pre_commit.yaml_extensions import ordered_dump
|
from pre_commit.yaml_extensions import ordered_dump
|
||||||
from pre_commit.yaml_extensions import ordered_load
|
from pre_commit.yaml_extensions import ordered_load
|
||||||
|
|
@ -25,7 +26,7 @@ def test_ordered_dump():
|
||||||
OrderedDict(
|
OrderedDict(
|
||||||
(('a', 'herp'), ('c', 'derp'), ('b', 'harp'), ('d', 'darp'))
|
(('a', 'herp'), ('c', 'derp'), ('b', 'harp'), ('d', 'darp'))
|
||||||
),
|
),
|
||||||
default_flow_style=False,
|
**C.YAML_DUMP_KWARGS
|
||||||
)
|
)
|
||||||
assert ret == (
|
assert ret == (
|
||||||
'a: herp\n'
|
'a: herp\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue