Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f5675d

Browse files
committed
Implement pre-commit migrate-config
1 parent 3e76cda commit 8f5675d

6 files changed

Lines changed: 226 additions & 12 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit import output
1212
from pre_commit.clientlib import is_local_repo
1313
from pre_commit.clientlib import load_config
14+
from pre_commit.commands.migrate_config import migrate_config
1415
from pre_commit.repository import Repository
1516
from pre_commit.util import CalledProcessError
1617
from pre_commit.util import cmd_output
@@ -104,21 +105,22 @@ def _write_new_config_file(path, output):
104105
def autoupdate(runner, tags_only):
105106
"""Auto-update the pre-commit config to the latest versions of repos."""
106107
retv = 0
107-
output_configs = []
108+
retv |= migrate_config(runner, quiet=True)
109+
output_repos = []
108110
changed = False
109111

110-
input_configs = load_config(runner.config_file_path)
112+
input_config = load_config(runner.config_file_path)
111113

112-
for repo_config in input_configs['repos']:
114+
for repo_config in input_config['repos']:
113115
if is_local_repo(repo_config):
114-
output_configs.append(repo_config)
116+
output_repos.append(repo_config)
115117
continue
116118
output.write('Updating {}...'.format(repo_config['repo']))
117119
try:
118120
new_repo_config = _update_repo(repo_config, runner, tags_only)
119121
except RepositoryCannotBeUpdatedError as error:
120122
output.write_line(error.args[0])
121-
output_configs.append(repo_config)
123+
output_repos.append(repo_config)
122124
retv = 1
123125
continue
124126

@@ -127,12 +129,14 @@ def autoupdate(runner, tags_only):
127129
output.write_line('updating {} -> {}.'.format(
128130
repo_config['sha'], new_repo_config['sha'],
129131
))
130-
output_configs.append(new_repo_config)
132+
output_repos.append(new_repo_config)
131133
else:
132134
output.write_line('already up to date.')
133-
output_configs.append(repo_config)
135+
output_repos.append(repo_config)
134136

135137
if changed:
136-
_write_new_config_file(runner.config_file_path, output_configs)
138+
output_config = input_config.copy()
139+
output_config['repos'] = output_repos
140+
_write_new_config_file(runner.config_file_path, output_config)
137141

138142
return retv
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
4+
import io
5+
6+
import yaml
7+
from aspy.yaml import ordered_load
8+
9+
10+
def _indent(s):
11+
lines = s.splitlines(True)
12+
return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
13+
14+
15+
def _is_header_line(line):
16+
return (line.startswith(('#', '---')) or not line.strip())
17+
18+
19+
def migrate_config(runner, quiet=False):
20+
retv = 0
21+
22+
with io.open(runner.config_file_path) as f:
23+
contents = f.read()
24+
25+
# Find the first non-header line
26+
lines = contents.splitlines(True)
27+
i = 0
28+
while _is_header_line(lines[i]):
29+
i += 1
30+
31+
header = ''.join(lines[:i])
32+
rest = ''.join(lines[i:])
33+
34+
if isinstance(ordered_load(contents), list):
35+
# If they are using the "default" flow style of yaml, this operation
36+
# will yield a valid configuration
37+
try:
38+
trial_contents = header + 'repos:\n' + rest
39+
yaml.load(trial_contents)
40+
contents = trial_contents
41+
except yaml.YAMLError:
42+
contents = header + 'repos:\n' + _indent(rest)
43+
44+
with io.open(runner.config_file_path, 'w') as f:
45+
f.write(contents)
46+
47+
print('Configuration has been migrated.')
48+
retv = 1
49+
elif not quiet:
50+
print('Configuration is already migrated.')
51+
52+
return retv

pre_commit/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pre_commit.commands.install_uninstall import install
1515
from pre_commit.commands.install_uninstall import install_hooks
1616
from pre_commit.commands.install_uninstall import uninstall
17+
from pre_commit.commands.migrate_config import migrate_config
1718
from pre_commit.commands.run import run
1819
from pre_commit.commands.sample_config import sample_config
1920
from pre_commit.error_handler import error_handler
@@ -131,6 +132,13 @@ def main(argv=None):
131132
),
132133
)
133134

135+
migrate_config_parser = subparsers.add_parser(
136+
'migrate-config',
137+
help='Migrate list configuration to new map configuration.',
138+
)
139+
_add_color_option(migrate_config_parser)
140+
_add_config_option(migrate_config_parser)
141+
134142
run_parser = subparsers.add_parser('run', help='Run hooks.')
135143
_add_color_option(run_parser)
136144
_add_config_option(run_parser)
@@ -217,6 +225,8 @@ def main(argv=None):
217225
if args.tags_only:
218226
logger.warning('--tags-only is the default')
219227
return autoupdate(runner, tags_only=not args.bleeding_edge)
228+
elif args.command == 'migrate-config':
229+
return migrate_config(runner)
220230
elif args.command == 'run':
221231
return run(runner, args)
222232
elif args.command == 'sample-config':

tests/commands/autoupdate_test.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def test_does_not_reformat(
128128
out_of_date_repo, mock_out_store_directory, in_tmpdir,
129129
):
130130
fmt = (
131+
'repos:\n'
131132
'- repo: {}\n'
132133
' sha: {} # definitely the version I want!\n'
133134
' hooks:\n'
@@ -153,7 +154,7 @@ def test_loses_formatting_when_not_detectable(
153154
is abandoned.
154155
"""
155156
config = (
156-
'[\n'
157+
'repos: [\n'
157158
' {{\n'
158159
' repo: {}, sha: {},\n'
159160
' hooks: [\n'
@@ -171,6 +172,7 @@ def test_loses_formatting_when_not_detectable(
171172
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
172173
after = open(C.CONFIG_FILE).read()
173174
expected = (
175+
'repos:\n'
174176
'- repo: {}\n'
175177
' sha: {}\n'
176178
' hooks:\n'
@@ -284,10 +286,36 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
284286
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
285287
)
286288
local_config = config_with_local_hooks()
287-
config = [local_config, stale_config]
289+
config = {'repos': [local_config, stale_config]}
288290
write_config('.', config)
289291
runner = Runner('.', C.CONFIG_FILE)
290292
assert autoupdate(runner, tags_only=False) == 0
291293
new_config_writen = load_config(runner.config_file_path)
292294
assert len(new_config_writen['repos']) == 2
293295
assert new_config_writen['repos'][0] == local_config
296+
297+
298+
def test_updates_old_format_to_new_format(tmpdir, capsys):
299+
cfg = tmpdir.join(C.CONFIG_FILE)
300+
cfg.write(
301+
'- repo: local\n'
302+
' hooks:\n'
303+
' - id: foo\n'
304+
' name: foo\n'
305+
' entry: ./bin/foo.sh\n'
306+
' language: script\n',
307+
)
308+
ret = autoupdate(Runner(tmpdir.strpath, C.CONFIG_FILE), tags_only=True)
309+
assert ret == 1
310+
contents = cfg.read()
311+
assert contents == (
312+
'repos:\n'
313+
'- repo: local\n'
314+
' hooks:\n'
315+
' - id: foo\n'
316+
' name: foo\n'
317+
' entry: ./bin/foo.sh\n'
318+
' language: script\n'
319+
)
320+
out, _ = capsys.readouterr()
321+
assert out == 'Configuration has been migrated.\n'
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
import pre_commit.constants as C
7+
from pre_commit.commands.migrate_config import _indent
8+
from pre_commit.commands.migrate_config import migrate_config
9+
from pre_commit.runner import Runner
10+
11+
12+
@pytest.mark.parametrize(
13+
('s', 'expected'),
14+
(
15+
('', ''),
16+
('a', ' a'),
17+
('foo\nbar', ' foo\n bar'),
18+
('foo\n\nbar\n', ' foo\n\n bar\n'),
19+
('\n\n\n', '\n\n\n'),
20+
),
21+
)
22+
def test_indent(s, expected):
23+
assert _indent(s) == expected
24+
25+
26+
def test_migrate_config_normal_format(tmpdir, capsys):
27+
cfg = tmpdir.join(C.CONFIG_FILE)
28+
cfg.write(
29+
'- repo: local\n'
30+
' hooks:\n'
31+
' - id: foo\n'
32+
' name: foo\n'
33+
' entry: ./bin/foo.sh\n'
34+
' language: script\n',
35+
)
36+
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
37+
out, _ = capsys.readouterr()
38+
assert out == 'Configuration has been migrated.\n'
39+
contents = cfg.read()
40+
assert contents == (
41+
'repos:\n'
42+
'- repo: local\n'
43+
' hooks:\n'
44+
' - id: foo\n'
45+
' name: foo\n'
46+
' entry: ./bin/foo.sh\n'
47+
' language: script\n'
48+
)
49+
50+
51+
def test_migrate_config_document_marker(tmpdir):
52+
cfg = tmpdir.join(C.CONFIG_FILE)
53+
cfg.write(
54+
'# comment\n'
55+
'\n'
56+
'---\n'
57+
'- repo: local\n'
58+
' hooks:\n'
59+
' - id: foo\n'
60+
' name: foo\n'
61+
' entry: ./bin/foo.sh\n'
62+
' language: script\n',
63+
)
64+
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
65+
contents = cfg.read()
66+
assert contents == (
67+
'# comment\n'
68+
'\n'
69+
'---\n'
70+
'repos:\n'
71+
'- repo: local\n'
72+
' hooks:\n'
73+
' - id: foo\n'
74+
' name: foo\n'
75+
' entry: ./bin/foo.sh\n'
76+
' language: script\n'
77+
)
78+
79+
80+
def test_migrate_config_list_literal(tmpdir):
81+
cfg = tmpdir.join(C.CONFIG_FILE)
82+
cfg.write(
83+
'[{\n'
84+
' repo: local,\n'
85+
' hooks: [{\n'
86+
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
87+
' language: script,\n'
88+
' }]\n'
89+
'}]',
90+
)
91+
assert migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE)) == 1
92+
contents = cfg.read()
93+
assert contents == (
94+
'repos:\n'
95+
' [{\n'
96+
' repo: local,\n'
97+
' hooks: [{\n'
98+
' id: foo, name: foo, entry: ./bin/foo.sh,\n'
99+
' language: script,\n'
100+
' }]\n'
101+
' }]'
102+
)
103+
104+
105+
def test_already_migrated_configuration_noop(tmpdir, capsys):
106+
contents = (
107+
'repos:\n'
108+
'- repo: local\n'
109+
' hooks:\n'
110+
' - id: foo\n'
111+
' name: foo\n'
112+
' entry: ./bin/foo.sh\n'
113+
' language: script\n'
114+
)
115+
cfg = tmpdir.join(C.CONFIG_FILE)
116+
cfg.write(contents)
117+
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
118+
out, _ = capsys.readouterr()
119+
assert out == 'Configuration is already migrated.\n'
120+
assert cfg.read() == contents

tests/main_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
FNS = (
15-
'autoupdate', 'clean', 'install', 'install_hooks', 'run', 'sample_config',
16-
'uninstall',
15+
'autoupdate', 'clean', 'install', 'install_hooks', 'migrate_config', 'run',
16+
'sample_config', 'uninstall',
1717
)
1818
CMDS = tuple(fn.replace('_', '-') for fn in FNS)
1919

0 commit comments

Comments
 (0)