Skip to content

Commit 5651c66

Browse files
committed
Migrate sha -> rev
1 parent 184e22e commit 5651c66

19 files changed

+215
-107
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ repos:
1313
- id: requirements-txt-fixer
1414
- id: flake8
1515
- repo: https://github.com/pre-commit/pre-commit.git
16-
sha: v0.16.3
16+
rev: v0.16.3
1717
hooks:
1818
- id: validate_manifest
1919
- repo: https://github.com/asottile/reorder_python_imports.git
20-
sha: v0.3.5
20+
rev: v0.3.5
2121
hooks:
2222
- id: reorder-python-imports
2323
language_version: python2.7
2424
- repo: https://github.com/asottile/add-trailing-comma
25-
sha: v0.6.4
25+
rev: v0.6.4
2626
hooks:
2727
- id: add-trailing-comma
2828
- repo: meta

pre_commit/clientlib.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ def validate_manifest_main(argv=None):
9393
_LOCAL_SENTINEL = 'local'
9494
_META_SENTINEL = 'meta'
9595

96+
97+
class MigrateShaToRev(object):
98+
@staticmethod
99+
def _cond(key):
100+
return cfgv.Conditional(
101+
key, cfgv.check_string,
102+
condition_key='repo',
103+
condition_value=cfgv.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
104+
ensure_absent=True,
105+
)
106+
107+
def check(self, dct):
108+
if dct.get('repo') in {_LOCAL_SENTINEL, _META_SENTINEL}:
109+
self._cond('rev').check(dct)
110+
self._cond('sha').check(dct)
111+
elif 'sha' in dct and 'rev' in dct:
112+
raise cfgv.ValidationError('Cannot specify both sha and rev')
113+
elif 'sha' in dct:
114+
self._cond('sha').check(dct)
115+
else:
116+
self._cond('rev').check(dct)
117+
118+
def apply_default(self, dct):
119+
if 'sha' in dct:
120+
dct['rev'] = dct.pop('sha')
121+
122+
def remove_default(self, dct):
123+
pass
124+
125+
96126
CONFIG_HOOK_DICT = cfgv.Map(
97127
'Hook', 'id',
98128

@@ -114,12 +144,7 @@ def validate_manifest_main(argv=None):
114144
cfgv.Required('repo', cfgv.check_string),
115145
cfgv.RequiredRecurse('hooks', cfgv.Array(CONFIG_HOOK_DICT)),
116146

117-
cfgv.Conditional(
118-
'sha', cfgv.check_string,
119-
condition_key='repo',
120-
condition_value=cfgv.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
121-
ensure_absent=True,
122-
),
147+
MigrateShaToRev(),
123148
)
124149
CONFIG_SCHEMA = cfgv.Map(
125150
'Config', None,

pre_commit/commands/autoupdate.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _update_repo(repo_config, runner, tags_only):
3232
Args:
3333
repo_config - A config for a repository
3434
"""
35-
repo_path = runner.store.clone(repo_config['repo'], repo_config['sha'])
35+
repo_path = runner.store.clone(repo_config['repo'], repo_config['rev'])
3636

3737
cmd_output('git', '-C', repo_path, 'fetch')
3838
tag_cmd = ('git', '-C', repo_path, 'describe', 'origin/master', '--tags')
@@ -46,13 +46,13 @@ def _update_repo(repo_config, runner, tags_only):
4646
tag_cmd = ('git', '-C', repo_path, 'rev-parse', 'origin/master')
4747
rev = cmd_output(*tag_cmd)[1].strip()
4848

49-
# Don't bother trying to update if our sha is the same
50-
if rev == repo_config['sha']:
49+
# Don't bother trying to update if our rev is the same
50+
if rev == repo_config['rev']:
5151
return repo_config
5252

53-
# Construct a new config with the head sha
53+
# Construct a new config with the head rev
5454
new_config = OrderedDict(repo_config)
55-
new_config['sha'] = rev
55+
new_config['rev'] = rev
5656
new_repo = Repository.create(new_config, runner.store)
5757

5858
# See if any of our hooks were deleted with the new commits
@@ -67,8 +67,8 @@ def _update_repo(repo_config, runner, tags_only):
6767
return new_config
6868

6969

70-
SHA_LINE_RE = re.compile(r'^(\s+)sha:(\s*)([^\s#]+)(.*)$', re.DOTALL)
71-
SHA_LINE_FMT = '{}sha:{}{}{}'
70+
REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([^\s#]+)(.*)$', re.DOTALL)
71+
REV_LINE_FMT = '{}rev:{}{}{}'
7272

7373

7474
def _write_new_config_file(path, output):
@@ -77,25 +77,25 @@ def _write_new_config_file(path, output):
7777
new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS)
7878

7979
lines = original_contents.splitlines(True)
80-
sha_line_indices_rev = list(reversed([
81-
i for i, line in enumerate(lines) if SHA_LINE_RE.match(line)
80+
rev_line_indices_reversed = list(reversed([
81+
i for i, line in enumerate(lines) if REV_LINE_RE.match(line)
8282
]))
8383

8484
for line in new_contents.splitlines(True):
85-
if SHA_LINE_RE.match(line):
86-
# It's possible we didn't identify the sha lines in the original
87-
if not sha_line_indices_rev:
85+
if REV_LINE_RE.match(line):
86+
# It's possible we didn't identify the rev lines in the original
87+
if not rev_line_indices_reversed:
8888
break
89-
line_index = sha_line_indices_rev.pop()
89+
line_index = rev_line_indices_reversed.pop()
9090
original_line = lines[line_index]
91-
orig_match = SHA_LINE_RE.match(original_line)
92-
new_match = SHA_LINE_RE.match(line)
93-
lines[line_index] = SHA_LINE_FMT.format(
91+
orig_match = REV_LINE_RE.match(original_line)
92+
new_match = REV_LINE_RE.match(line)
93+
lines[line_index] = REV_LINE_FMT.format(
9494
orig_match.group(1), orig_match.group(2),
9595
new_match.group(3), orig_match.group(4),
9696
)
9797

98-
# If we failed to intelligently rewrite the sha lines, fall back to the
98+
# If we failed to intelligently rewrite the rev lines, fall back to the
9999
# pretty-formatted yaml output
100100
to_write = ''.join(lines)
101101
if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output:
@@ -132,10 +132,10 @@ def autoupdate(runner, tags_only, repos=()):
132132
retv = 1
133133
continue
134134

135-
if new_repo_config['sha'] != repo_config['sha']:
135+
if new_repo_config['rev'] != repo_config['rev']:
136136
changed = True
137137
output.write_line('updating {} -> {}.'.format(
138-
repo_config['sha'], new_repo_config['sha'],
138+
repo_config['rev'], new_repo_config['rev'],
139139
))
140140
output_repos.append(new_repo_config)
141141
else:

pre_commit/commands/migrate_config.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import io
5+
import re
56

67
import yaml
78
from aspy.yaml import ordered_load
@@ -16,10 +17,7 @@ def _is_header_line(line):
1617
return (line.startswith(('#', '---')) or not line.strip())
1718

1819

19-
def migrate_config(runner, quiet=False):
20-
with io.open(runner.config_file_path) as f:
21-
contents = f.read()
22-
20+
def _migrate_map(contents):
2321
# Find the first non-header line
2422
lines = contents.splitlines(True)
2523
i = 0
@@ -39,6 +37,22 @@ def migrate_config(runner, quiet=False):
3937
except yaml.YAMLError:
4038
contents = header + 'repos:\n' + _indent(rest)
4139

40+
return contents
41+
42+
43+
def _migrate_sha_to_rev(contents):
44+
reg = re.compile(r'(\n\s+)sha:')
45+
return reg.sub(r'\1rev:', contents)
46+
47+
48+
def migrate_config(runner, quiet=False):
49+
with io.open(runner.config_file_path) as f:
50+
orig_contents = contents = f.read()
51+
52+
contents = _migrate_map(contents)
53+
contents = _migrate_sha_to_rev(contents)
54+
55+
if contents != orig_contents:
4256
with io.open(runner.config_file_path, 'w') as f:
4357
f.write(contents)
4458

pre_commit/commands/sample_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See https://pre-commit.com/hooks.html for more hooks
1313
repos:
1414
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
sha: v0.9.2
15+
rev: v1.2.1-1
1616
hooks:
1717
- id: trailing-whitespace
1818
- id: end-of-file-fixer

pre_commit/commands/try_repo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
def try_repo(args):
20-
ref = args.ref or git.head_sha(args.repo)
20+
ref = args.ref or git.head_rev(args.repo)
2121

2222
with tmpdir() as tempdir:
2323
if args.hook:
@@ -28,7 +28,7 @@ def try_repo(args):
2828
manifest = sorted(manifest, key=lambda hook: hook['id'])
2929
hooks = [{'id': hook['id']} for hook in manifest]
3030

31-
items = (('repo', args.repo), ('sha', ref), ('hooks', hooks))
31+
items = (('repo', args.repo), ('rev', ref), ('hooks', hooks))
3232
config = {'repos': [collections.OrderedDict(items)]}
3333
config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS)
3434

pre_commit/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_changed_files(new, old):
9797
)[1])
9898

9999

100-
def head_sha(remote):
100+
def head_rev(remote):
101101
_, out, _ = cmd_output('git', 'ls-remote', '--exit-code', remote, 'HEAD')
102102
return out.split()[0]
103103

pre_commit/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ def main(argv=None):
200200
'repo', help='Repository to source hooks from.',
201201
)
202202
try_repo_parser.add_argument(
203-
'--ref',
203+
'--ref', '--rev',
204204
help=(
205-
'Manually select a ref to run against, otherwise the `HEAD` '
205+
'Manually select a rev to run against, otherwise the `HEAD` '
206206
'revision will be used.'
207207
),
208208
)

pre_commit/repository.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ def create(cls, config, store):
150150

151151
@cached_property
152152
def manifest_hooks(self):
153-
repo, sha = self.repo_config['repo'], self.repo_config['sha']
154-
repo_path = self.store.clone(repo, sha)
153+
repo, rev = self.repo_config['repo'], self.repo_config['rev']
154+
repo_path = self.store.clone(repo, rev)
155155
manifest_path = os.path.join(repo_path, C.MANIFEST_FILE)
156156
return {hook['id']: hook for hook in load_manifest(manifest_path)}
157157

@@ -174,8 +174,8 @@ def hooks(self):
174174
)
175175

176176
def _prefix_from_deps(self, language_name, deps):
177-
repo, sha = self.repo_config['repo'], self.repo_config['sha']
178-
return Prefix(self.store.clone(repo, sha, deps))
177+
repo, rev = self.repo_config['repo'], self.repo_config['rev']
178+
return Prefix(self.store.clone(repo, rev, deps))
179179

180180
def _venvs(self):
181181
ret = []

testing/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ def config_with_local_hooks():
7878
))
7979

8080

81-
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
81+
def make_config_from_repo(repo_path, rev=None, hooks=None, check=True):
8282
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
8383
config = OrderedDict((
8484
('repo', 'file://{}'.format(repo_path)),
85-
('sha', sha or git.head_sha(repo_path)),
85+
('rev', rev or git.head_rev(repo_path)),
8686
(
8787
'hooks',
8888
hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],

0 commit comments

Comments
 (0)