mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Implement pre-commit migrate-config
This commit is contained in:
parent
3e76cdaf25
commit
8f5675d813
6 changed files with 226 additions and 12 deletions
|
|
@ -11,6 +11,7 @@ import pre_commit.constants as C
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
from pre_commit.clientlib import is_local_repo
|
from pre_commit.clientlib import is_local_repo
|
||||||
from pre_commit.clientlib import load_config
|
from pre_commit.clientlib import load_config
|
||||||
|
from pre_commit.commands.migrate_config import migrate_config
|
||||||
from pre_commit.repository import Repository
|
from pre_commit.repository import Repository
|
||||||
from pre_commit.util import CalledProcessError
|
from pre_commit.util import CalledProcessError
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
@ -104,21 +105,22 @@ def _write_new_config_file(path, output):
|
||||||
def autoupdate(runner, tags_only):
|
def autoupdate(runner, tags_only):
|
||||||
"""Auto-update the pre-commit config to the latest versions of repos."""
|
"""Auto-update the pre-commit config to the latest versions of repos."""
|
||||||
retv = 0
|
retv = 0
|
||||||
output_configs = []
|
retv |= migrate_config(runner, quiet=True)
|
||||||
|
output_repos = []
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
input_configs = load_config(runner.config_file_path)
|
input_config = load_config(runner.config_file_path)
|
||||||
|
|
||||||
for repo_config in input_configs['repos']:
|
for repo_config in input_config['repos']:
|
||||||
if is_local_repo(repo_config):
|
if is_local_repo(repo_config):
|
||||||
output_configs.append(repo_config)
|
output_repos.append(repo_config)
|
||||||
continue
|
continue
|
||||||
output.write('Updating {}...'.format(repo_config['repo']))
|
output.write('Updating {}...'.format(repo_config['repo']))
|
||||||
try:
|
try:
|
||||||
new_repo_config = _update_repo(repo_config, runner, tags_only)
|
new_repo_config = _update_repo(repo_config, runner, tags_only)
|
||||||
except RepositoryCannotBeUpdatedError as error:
|
except RepositoryCannotBeUpdatedError as error:
|
||||||
output.write_line(error.args[0])
|
output.write_line(error.args[0])
|
||||||
output_configs.append(repo_config)
|
output_repos.append(repo_config)
|
||||||
retv = 1
|
retv = 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -127,12 +129,14 @@ def autoupdate(runner, tags_only):
|
||||||
output.write_line('updating {} -> {}.'.format(
|
output.write_line('updating {} -> {}.'.format(
|
||||||
repo_config['sha'], new_repo_config['sha'],
|
repo_config['sha'], new_repo_config['sha'],
|
||||||
))
|
))
|
||||||
output_configs.append(new_repo_config)
|
output_repos.append(new_repo_config)
|
||||||
else:
|
else:
|
||||||
output.write_line('already up to date.')
|
output.write_line('already up to date.')
|
||||||
output_configs.append(repo_config)
|
output_repos.append(repo_config)
|
||||||
|
|
||||||
if changed:
|
if changed:
|
||||||
_write_new_config_file(runner.config_file_path, output_configs)
|
output_config = input_config.copy()
|
||||||
|
output_config['repos'] = output_repos
|
||||||
|
_write_new_config_file(runner.config_file_path, output_config)
|
||||||
|
|
||||||
return retv
|
return retv
|
||||||
|
|
|
||||||
52
pre_commit/commands/migrate_config.py
Normal file
52
pre_commit/commands/migrate_config.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
from __future__ import print_function
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from aspy.yaml import ordered_load
|
||||||
|
|
||||||
|
|
||||||
|
def _indent(s):
|
||||||
|
lines = s.splitlines(True)
|
||||||
|
return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_header_line(line):
|
||||||
|
return (line.startswith(('#', '---')) or not line.strip())
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_config(runner, quiet=False):
|
||||||
|
retv = 0
|
||||||
|
|
||||||
|
with io.open(runner.config_file_path) as f:
|
||||||
|
contents = f.read()
|
||||||
|
|
||||||
|
# Find the first non-header line
|
||||||
|
lines = contents.splitlines(True)
|
||||||
|
i = 0
|
||||||
|
while _is_header_line(lines[i]):
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
header = ''.join(lines[:i])
|
||||||
|
rest = ''.join(lines[i:])
|
||||||
|
|
||||||
|
if isinstance(ordered_load(contents), list):
|
||||||
|
# If they are using the "default" flow style of yaml, this operation
|
||||||
|
# will yield a valid configuration
|
||||||
|
try:
|
||||||
|
trial_contents = header + 'repos:\n' + rest
|
||||||
|
yaml.load(trial_contents)
|
||||||
|
contents = trial_contents
|
||||||
|
except yaml.YAMLError:
|
||||||
|
contents = header + 'repos:\n' + _indent(rest)
|
||||||
|
|
||||||
|
with io.open(runner.config_file_path, 'w') as f:
|
||||||
|
f.write(contents)
|
||||||
|
|
||||||
|
print('Configuration has been migrated.')
|
||||||
|
retv = 1
|
||||||
|
elif not quiet:
|
||||||
|
print('Configuration is already migrated.')
|
||||||
|
|
||||||
|
return retv
|
||||||
|
|
@ -14,6 +14,7 @@ from pre_commit.commands.clean import clean
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
from pre_commit.commands.install_uninstall import install_hooks
|
from pre_commit.commands.install_uninstall import install_hooks
|
||||||
from pre_commit.commands.install_uninstall import uninstall
|
from pre_commit.commands.install_uninstall import uninstall
|
||||||
|
from pre_commit.commands.migrate_config import migrate_config
|
||||||
from pre_commit.commands.run import run
|
from pre_commit.commands.run import run
|
||||||
from pre_commit.commands.sample_config import sample_config
|
from pre_commit.commands.sample_config import sample_config
|
||||||
from pre_commit.error_handler import error_handler
|
from pre_commit.error_handler import error_handler
|
||||||
|
|
@ -131,6 +132,13 @@ def main(argv=None):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
migrate_config_parser = subparsers.add_parser(
|
||||||
|
'migrate-config',
|
||||||
|
help='Migrate list configuration to new map configuration.',
|
||||||
|
)
|
||||||
|
_add_color_option(migrate_config_parser)
|
||||||
|
_add_config_option(migrate_config_parser)
|
||||||
|
|
||||||
run_parser = subparsers.add_parser('run', help='Run hooks.')
|
run_parser = subparsers.add_parser('run', help='Run hooks.')
|
||||||
_add_color_option(run_parser)
|
_add_color_option(run_parser)
|
||||||
_add_config_option(run_parser)
|
_add_config_option(run_parser)
|
||||||
|
|
@ -217,6 +225,8 @@ def main(argv=None):
|
||||||
if args.tags_only:
|
if args.tags_only:
|
||||||
logger.warning('--tags-only is the default')
|
logger.warning('--tags-only is the default')
|
||||||
return autoupdate(runner, tags_only=not args.bleeding_edge)
|
return autoupdate(runner, tags_only=not args.bleeding_edge)
|
||||||
|
elif args.command == 'migrate-config':
|
||||||
|
return migrate_config(runner)
|
||||||
elif args.command == 'run':
|
elif args.command == 'run':
|
||||||
return run(runner, args)
|
return run(runner, args)
|
||||||
elif args.command == 'sample-config':
|
elif args.command == 'sample-config':
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,7 @@ def test_does_not_reformat(
|
||||||
out_of_date_repo, mock_out_store_directory, in_tmpdir,
|
out_of_date_repo, mock_out_store_directory, in_tmpdir,
|
||||||
):
|
):
|
||||||
fmt = (
|
fmt = (
|
||||||
|
'repos:\n'
|
||||||
'- repo: {}\n'
|
'- repo: {}\n'
|
||||||
' sha: {} # definitely the version I want!\n'
|
' sha: {} # definitely the version I want!\n'
|
||||||
' hooks:\n'
|
' hooks:\n'
|
||||||
|
|
@ -153,7 +154,7 @@ def test_loses_formatting_when_not_detectable(
|
||||||
is abandoned.
|
is abandoned.
|
||||||
"""
|
"""
|
||||||
config = (
|
config = (
|
||||||
'[\n'
|
'repos: [\n'
|
||||||
' {{\n'
|
' {{\n'
|
||||||
' repo: {}, sha: {},\n'
|
' repo: {}, sha: {},\n'
|
||||||
' hooks: [\n'
|
' hooks: [\n'
|
||||||
|
|
@ -171,6 +172,7 @@ def test_loses_formatting_when_not_detectable(
|
||||||
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
|
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
|
||||||
after = open(C.CONFIG_FILE).read()
|
after = open(C.CONFIG_FILE).read()
|
||||||
expected = (
|
expected = (
|
||||||
|
'repos:\n'
|
||||||
'- repo: {}\n'
|
'- repo: {}\n'
|
||||||
' sha: {}\n'
|
' sha: {}\n'
|
||||||
' hooks:\n'
|
' hooks:\n'
|
||||||
|
|
@ -284,10 +286,36 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
|
||||||
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
|
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
|
||||||
)
|
)
|
||||||
local_config = config_with_local_hooks()
|
local_config = config_with_local_hooks()
|
||||||
config = [local_config, stale_config]
|
config = {'repos': [local_config, stale_config]}
|
||||||
write_config('.', config)
|
write_config('.', config)
|
||||||
runner = Runner('.', C.CONFIG_FILE)
|
runner = Runner('.', C.CONFIG_FILE)
|
||||||
assert autoupdate(runner, tags_only=False) == 0
|
assert autoupdate(runner, tags_only=False) == 0
|
||||||
new_config_writen = load_config(runner.config_file_path)
|
new_config_writen = load_config(runner.config_file_path)
|
||||||
assert len(new_config_writen['repos']) == 2
|
assert len(new_config_writen['repos']) == 2
|
||||||
assert new_config_writen['repos'][0] == local_config
|
assert new_config_writen['repos'][0] == local_config
|
||||||
|
|
||||||
|
|
||||||
|
def test_updates_old_format_to_new_format(tmpdir, capsys):
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write(
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n',
|
||||||
|
)
|
||||||
|
ret = autoupdate(Runner(tmpdir.strpath, C.CONFIG_FILE), tags_only=True)
|
||||||
|
assert ret == 1
|
||||||
|
contents = cfg.read()
|
||||||
|
assert contents == (
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n'
|
||||||
|
)
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out == 'Configuration has been migrated.\n'
|
||||||
|
|
|
||||||
120
tests/commands/migrate_config_test.py
Normal file
120
tests/commands/migrate_config_test.py
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.commands.migrate_config import _indent
|
||||||
|
from pre_commit.commands.migrate_config import migrate_config
|
||||||
|
from pre_commit.runner import Runner
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('s', 'expected'),
|
||||||
|
(
|
||||||
|
('', ''),
|
||||||
|
('a', ' a'),
|
||||||
|
('foo\nbar', ' foo\n bar'),
|
||||||
|
('foo\n\nbar\n', ' foo\n\n bar\n'),
|
||||||
|
('\n\n\n', '\n\n\n'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_indent(s, expected):
|
||||||
|
assert _indent(s) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_migrate_config_normal_format(tmpdir, capsys):
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write(
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n',
|
||||||
|
)
|
||||||
|
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out == 'Configuration has been migrated.\n'
|
||||||
|
contents = cfg.read()
|
||||||
|
assert contents == (
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_migrate_config_document_marker(tmpdir):
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write(
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'---\n'
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n',
|
||||||
|
)
|
||||||
|
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||||
|
contents = cfg.read()
|
||||||
|
assert contents == (
|
||||||
|
'# comment\n'
|
||||||
|
'\n'
|
||||||
|
'---\n'
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_migrate_config_list_literal(tmpdir):
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write(
|
||||||
|
'[{\n'
|
||||||
|
' repo: local,\n'
|
||||||
|
' hooks: [{\n'
|
||||||
|
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
|
||||||
|
' language: script,\n'
|
||||||
|
' }]\n'
|
||||||
|
'}]',
|
||||||
|
)
|
||||||
|
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
|
||||||
|
contents = cfg.read()
|
||||||
|
assert contents == (
|
||||||
|
'repos:\n'
|
||||||
|
' [{\n'
|
||||||
|
' repo: local,\n'
|
||||||
|
' hooks: [{\n'
|
||||||
|
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
|
||||||
|
' language: script,\n'
|
||||||
|
' }]\n'
|
||||||
|
' }]'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_already_migrated_configuration_noop(tmpdir, capsys):
|
||||||
|
contents = (
|
||||||
|
'repos:\n'
|
||||||
|
'- repo: local\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: foo\n'
|
||||||
|
' name: foo\n'
|
||||||
|
' entry: ./bin/foo.sh\n'
|
||||||
|
' language: script\n'
|
||||||
|
)
|
||||||
|
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||||
|
cfg.write(contents)
|
||||||
|
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
|
||||||
|
out, _ = capsys.readouterr()
|
||||||
|
assert out == 'Configuration is already migrated.\n'
|
||||||
|
assert cfg.read() == contents
|
||||||
|
|
@ -12,8 +12,8 @@ from testing.auto_namedtuple import auto_namedtuple
|
||||||
|
|
||||||
|
|
||||||
FNS = (
|
FNS = (
|
||||||
'autoupdate', 'clean', 'install', 'install_hooks', 'run', 'sample_config',
|
'autoupdate', 'clean', 'install', 'install_hooks', 'migrate_config', 'run',
|
||||||
'uninstall',
|
'sample_config', 'uninstall',
|
||||||
)
|
)
|
||||||
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
|
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue