Skip to content

Commit 478e90e

Browse files
authored
Merge pull request pre-commit#583 from pre-commit/autoupdate_reformatting
When possible, preserve config format on autoupdate
2 parents 72efbb3 + ee39227 commit 478e90e

4 files changed

Lines changed: 95 additions & 72 deletions

File tree

‎pre_commit/commands/autoupdate.py‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
from __future__ import print_function
22
from __future__ import unicode_literals
33

4+
import re
45
from collections import OrderedDict
56

67
from aspy.yaml import ordered_dump
78
from aspy.yaml import ordered_load
89

910
import pre_commit.constants as C
1011
from pre_commit import output
11-
from pre_commit.clientlib import CONFIG_SCHEMA
1212
from pre_commit.clientlib import is_local_repo
1313
from pre_commit.clientlib import load_config
1414
from pre_commit.repository import Repository
15-
from pre_commit.schema import remove_defaults
1615
from pre_commit.util import CalledProcessError
1716
from pre_commit.util import cmd_output
1817
from pre_commit.util import cwd
@@ -65,6 +64,43 @@ def _update_repo(repo_config, runner, tags_only):
6564
return new_config
6665

6766

67+
SHA_LINE_RE = re.compile(r'^(\s+)sha:(\s*)([^\s#]+)(.*)$', re.DOTALL)
68+
SHA_LINE_FMT = '{}sha:{}{}{}'
69+
70+
71+
def _write_new_config_file(path, output):
72+
original_contents = open(path).read()
73+
new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS)
74+
75+
lines = original_contents.splitlines(True)
76+
sha_line_indices_rev = list(reversed([
77+
i for i, line in enumerate(lines) if SHA_LINE_RE.match(line)
78+
]))
79+
80+
for line in new_contents.splitlines(True):
81+
if SHA_LINE_RE.match(line):
82+
# It's possible we didn't identify the sha lines in the original
83+
if not sha_line_indices_rev:
84+
break
85+
line_index = sha_line_indices_rev.pop()
86+
original_line = lines[line_index]
87+
orig_match = SHA_LINE_RE.match(original_line)
88+
new_match = SHA_LINE_RE.match(line)
89+
lines[line_index] = SHA_LINE_FMT.format(
90+
orig_match.group(1), orig_match.group(2),
91+
new_match.group(3), orig_match.group(4),
92+
)
93+
94+
# If we failed to intelligently rewrite the sha lines, fall back to the
95+
# pretty-formatted yaml output
96+
to_write = ''.join(lines)
97+
if ordered_load(to_write) != output:
98+
to_write = new_contents
99+
100+
with open(path, 'w') as f:
101+
f.write(to_write)
102+
103+
68104
def autoupdate(runner, tags_only):
69105
"""Auto-update the pre-commit config to the latest versions of repos."""
70106
retv = 0
@@ -100,10 +136,6 @@ def autoupdate(runner, tags_only):
100136
output_configs.append(repo_config)
101137

102138
if changed:
103-
with open(runner.config_file_path, 'w') as config_file:
104-
config_file.write(ordered_dump(
105-
remove_defaults(output_configs, CONFIG_SCHEMA),
106-
**C.YAML_DUMP_KWARGS
107-
))
139+
_write_new_config_file(runner.config_file_path, output_configs)
108140

109141
return retv

‎pre_commit/schema.py‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ def _apply_default_optional(self, dct):
6464
dct.setdefault(self.key, self.default)
6565

6666

67-
def _remove_default_optional(self, dct):
68-
if dct.get(self.key, MISSING) == self.default:
69-
del dct[self.key]
70-
71-
7267
def _require_key(self, dct):
7368
if self.key not in dct:
7469
raise ValidationError('Missing required key: {}'.format(self.key))
@@ -90,10 +85,6 @@ def _apply_default_required_recurse(self, dct):
9085
dct[self.key] = apply_defaults(dct[self.key], self.schema)
9186

9287

93-
def _remove_default_required_recurse(self, dct):
94-
dct[self.key] = remove_defaults(dct[self.key], self.schema)
95-
96-
9788
def _check_conditional(self, dct):
9889
if dct.get(self.condition_key, MISSING) == self.condition_value:
9990
_check_required(self, dct)
@@ -119,30 +110,25 @@ def _check_conditional(self, dct):
119110
Required = collections.namedtuple('Required', ('key', 'check_fn'))
120111
Required.check = _check_required
121112
Required.apply_default = _dct_noop
122-
Required.remove_default = _dct_noop
123113
RequiredRecurse = collections.namedtuple('RequiredRecurse', ('key', 'schema'))
124114
RequiredRecurse.check = _check_required
125115
RequiredRecurse.check_fn = _check_fn_required_recurse
126116
RequiredRecurse.apply_default = _apply_default_required_recurse
127-
RequiredRecurse.remove_default = _remove_default_required_recurse
128117
Optional = collections.namedtuple('Optional', ('key', 'check_fn', 'default'))
129118
Optional.check = _check_optional
130119
Optional.apply_default = _apply_default_optional
131-
Optional.remove_default = _remove_default_optional
132120
OptionalNoDefault = collections.namedtuple(
133121
'OptionalNoDefault', ('key', 'check_fn'),
134122
)
135123
OptionalNoDefault.check = _check_optional
136124
OptionalNoDefault.apply_default = _dct_noop
137-
OptionalNoDefault.remove_default = _dct_noop
138125
Conditional = collections.namedtuple(
139126
'Conditional',
140127
('key', 'check_fn', 'condition_key', 'condition_value', 'ensure_absent'),
141128
)
142129
Conditional.__new__.__defaults__ = (False,)
143130
Conditional.check = _check_conditional
144131
Conditional.apply_default = _dct_noop
145-
Conditional.remove_default = _dct_noop
146132

147133

148134
class Map(collections.namedtuple('Map', ('object_name', 'id_key', 'items'))):
@@ -168,12 +154,6 @@ def apply_defaults(self, v):
168154
item.apply_default(ret)
169155
return ret
170156

171-
def remove_defaults(self, v):
172-
ret = v.copy()
173-
for item in self.items:
174-
item.remove_default(ret)
175-
return ret
176-
177157

178158
class Array(collections.namedtuple('Array', ('of',))):
179159
__slots__ = ()
@@ -190,9 +170,6 @@ def check(self, v):
190170
def apply_defaults(self, v):
191171
return [apply_defaults(val, self.of) for val in v]
192172

193-
def remove_defaults(self, v):
194-
return [remove_defaults(val, self.of) for val in v]
195-
196173

197174
class Not(object):
198175
def __init__(self, val):
@@ -257,10 +234,6 @@ def apply_defaults(v, schema):
257234
return schema.apply_defaults(v)
258235

259236

260-
def remove_defaults(v, schema):
261-
return schema.remove_defaults(v)
262-
263-
264237
def load_from_filename(filename, schema, load_strategy, exc_tp):
265238
with reraise_as(exc_tp):
266239
if not os.path.exists(filename):

‎tests/commands/autoupdate_test.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import pipes
34
import shutil
45
from collections import OrderedDict
56

@@ -123,6 +124,61 @@ def test_autoupdate_out_of_date_repo(
123124
assert out_of_date_repo.head_sha in after
124125

125126

127+
def test_does_not_reformat(
128+
out_of_date_repo, mock_out_store_directory, in_tmpdir,
129+
):
130+
fmt = (
131+
'- repo: {}\n'
132+
' sha: {} # definitely the version I want!\n'
133+
' hooks:\n'
134+
' - id: foo\n'
135+
' # These args are because reasons!\n'
136+
' args: [foo, bar, baz]\n'
137+
)
138+
config = fmt.format(out_of_date_repo.path, out_of_date_repo.original_sha)
139+
with open(C.CONFIG_FILE, 'w') as f:
140+
f.write(config)
141+
142+
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
143+
after = open(C.CONFIG_FILE).read()
144+
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_sha)
145+
assert after == expected
146+
147+
148+
def test_loses_formatting_when_not_detectable(
149+
out_of_date_repo, mock_out_store_directory, in_tmpdir,
150+
):
151+
"""A best-effort attempt is made at updating sha without rewriting
152+
formatting. When the original formatting cannot be detected, this
153+
is abandoned.
154+
"""
155+
config = (
156+
'[\n'
157+
' {{\n'
158+
' repo: {}, sha: {},\n'
159+
' hooks: [\n'
160+
' # A comment!\n'
161+
' {{id: foo}},\n'
162+
' ],\n'
163+
' }}\n'
164+
']\n'.format(
165+
pipes.quote(out_of_date_repo.path), out_of_date_repo.original_sha,
166+
)
167+
)
168+
with open(C.CONFIG_FILE, 'w') as f:
169+
f.write(config)
170+
171+
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
172+
after = open(C.CONFIG_FILE).read()
173+
expected = (
174+
'- repo: {}\n'
175+
' sha: {}\n'
176+
' hooks:\n'
177+
' - id: foo\n'
178+
).format(out_of_date_repo.path, out_of_date_repo.head_sha)
179+
assert after == expected
180+
181+
126182
@pytest.yield_fixture
127183
def tagged_repo(out_of_date_repo):
128184
with cwd(out_of_date_repo.path):

‎tests/schema_test.py‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from pre_commit.schema import Not
2222
from pre_commit.schema import Optional
2323
from pre_commit.schema import OptionalNoDefault
24-
from pre_commit.schema import remove_defaults
2524
from pre_commit.schema import Required
2625
from pre_commit.schema import RequiredRecurse
2726
from pre_commit.schema import validate
@@ -281,37 +280,6 @@ def test_apply_defaults_map_in_list():
281280
assert ret == [{'key': False}]
282281

283282

284-
def test_remove_defaults_copies_object():
285-
val = {'key': False}
286-
ret = remove_defaults(val, map_optional)
287-
assert ret is not val
288-
289-
290-
def test_remove_defaults_removes_defaults():
291-
ret = remove_defaults({'key': False}, map_optional)
292-
assert ret == {}
293-
294-
295-
def test_remove_defaults_nothing_to_remove():
296-
ret = remove_defaults({}, map_optional)
297-
assert ret == {}
298-
299-
300-
def test_remove_defaults_does_not_change_non_default():
301-
ret = remove_defaults({'key': True}, map_optional)
302-
assert ret == {'key': True}
303-
304-
305-
def test_remove_defaults_map_in_list():
306-
ret = remove_defaults([{'key': False}], Array(map_optional))
307-
assert ret == [{}]
308-
309-
310-
def test_remove_defaults_does_nothing_on_non_optional():
311-
ret = remove_defaults({'key': True}, map_required)
312-
assert ret == {'key': True}
313-
314-
315283
nested_schema_required = Map(
316284
'Repository', 'repo',
317285
Required('repo', check_any),
@@ -342,12 +310,6 @@ def test_apply_defaults_nested():
342310
assert ret == {'repo': 'repo1', 'hooks': [{'key': False}]}
343311

344312

345-
def test_remove_defaults_nested():
346-
val = {'repo': 'repo1', 'hooks': [{'key': False}]}
347-
ret = remove_defaults(val, nested_schema_optional)
348-
assert ret == {'repo': 'repo1', 'hooks': [{}]}
349-
350-
351313
class Error(Exception):
352314
pass
353315

0 commit comments

Comments
 (0)