Skip to content

Commit de85900

Browse files
authored
Merge pull request pre-commit#3302 from pre-commit/migrate-config-stages
migrate-config rewrites deprecated stages
2 parents a7b671a + 5679399 commit de85900

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pre_commit/commands/migrate_config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import functools
4+
import itertools
45
import textwrap
56
from typing import Callable
67

@@ -49,6 +50,10 @@ def _preserve_style(n: ScalarNode, *, s: str) -> str:
4950
return f'{n.style}{s}{n.style}'
5051

5152

53+
def _fix_stage(n: ScalarNode) -> str:
54+
return _preserve_style(n, s=f'pre-{n.value}')
55+
56+
5257
def _migrate_composed(contents: str) -> str:
5358
tree = yaml_compose(contents)
5459
rewrites: list[tuple[ScalarNode, Callable[[ScalarNode], str]]] = []
@@ -76,6 +81,22 @@ def _migrate_composed(contents: str) -> str:
7681
if node.value == 'python_venv':
7782
rewrites.append((node, python_venv_replace))
7883

84+
# stages rewrites
85+
default_stages_matcher = (MappingValue('default_stages'), SequenceItem())
86+
default_stages_match = match(tree, default_stages_matcher)
87+
hook_stages_matcher = (
88+
MappingValue('repos'),
89+
SequenceItem(),
90+
MappingValue('hooks'),
91+
SequenceItem(),
92+
MappingValue('stages'),
93+
SequenceItem(),
94+
)
95+
hook_stages_match = match(tree, hook_stages_matcher)
96+
for node in itertools.chain(default_stages_match, hook_stages_match):
97+
if node.value in {'commit', 'push', 'merge-commit'}:
98+
rewrites.append((node, _fix_stage))
99+
79100
rewrites.sort(reverse=True, key=lambda nf: nf[0].start_mark.index)
80101

81102
src_parts = []

tests/commands/migrate_config_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,48 @@ def test_migrate_config_quoted_python_venv(tmp_path):
213213
assert cfg.read_text() == expected
214214

215215

216+
def test_migrate_config_default_stages(tmp_path):
217+
src = '''\
218+
default_stages: [commit, push, merge-commit, commit-msg]
219+
repos: []
220+
'''
221+
expected = '''\
222+
default_stages: [pre-commit, pre-push, pre-merge-commit, commit-msg]
223+
repos: []
224+
'''
225+
cfg = tmp_path.joinpath('cfg.yaml')
226+
cfg.write_text(src)
227+
assert migrate_config(str(cfg)) == 0
228+
assert cfg.read_text() == expected
229+
230+
231+
def test_migrate_config_hook_stages(tmp_path):
232+
src = '''\
233+
repos:
234+
- repo: local
235+
hooks:
236+
- id: example
237+
name: example
238+
entry: example
239+
language: system
240+
stages: ["commit", "push", "merge-commit", "commit-msg"]
241+
'''
242+
expected = '''\
243+
repos:
244+
- repo: local
245+
hooks:
246+
- id: example
247+
name: example
248+
entry: example
249+
language: system
250+
stages: ["pre-commit", "pre-push", "pre-merge-commit", "commit-msg"]
251+
'''
252+
cfg = tmp_path.joinpath('cfg.yaml')
253+
cfg.write_text(src)
254+
assert migrate_config(str(cfg)) == 0
255+
assert cfg.read_text() == expected
256+
257+
216258
def test_migrate_config_invalid_yaml(tmpdir):
217259
contents = '['
218260
cfg = tmpdir.join(C.CONFIG_FILE)

0 commit comments

Comments
 (0)