mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Migrate sha -> rev
This commit is contained in:
parent
184e22e81f
commit
5651c66995
19 changed files with 215 additions and 107 deletions
|
|
@ -93,6 +93,36 @@ def validate_manifest_main(argv=None):
|
|||
_LOCAL_SENTINEL = 'local'
|
||||
_META_SENTINEL = 'meta'
|
||||
|
||||
|
||||
class MigrateShaToRev(object):
|
||||
@staticmethod
|
||||
def _cond(key):
|
||||
return cfgv.Conditional(
|
||||
key, cfgv.check_string,
|
||||
condition_key='repo',
|
||||
condition_value=cfgv.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
|
||||
ensure_absent=True,
|
||||
)
|
||||
|
||||
def check(self, dct):
|
||||
if dct.get('repo') in {_LOCAL_SENTINEL, _META_SENTINEL}:
|
||||
self._cond('rev').check(dct)
|
||||
self._cond('sha').check(dct)
|
||||
elif 'sha' in dct and 'rev' in dct:
|
||||
raise cfgv.ValidationError('Cannot specify both sha and rev')
|
||||
elif 'sha' in dct:
|
||||
self._cond('sha').check(dct)
|
||||
else:
|
||||
self._cond('rev').check(dct)
|
||||
|
||||
def apply_default(self, dct):
|
||||
if 'sha' in dct:
|
||||
dct['rev'] = dct.pop('sha')
|
||||
|
||||
def remove_default(self, dct):
|
||||
pass
|
||||
|
||||
|
||||
CONFIG_HOOK_DICT = cfgv.Map(
|
||||
'Hook', 'id',
|
||||
|
||||
|
|
@ -114,12 +144,7 @@ CONFIG_REPO_DICT = cfgv.Map(
|
|||
cfgv.Required('repo', cfgv.check_string),
|
||||
cfgv.RequiredRecurse('hooks', cfgv.Array(CONFIG_HOOK_DICT)),
|
||||
|
||||
cfgv.Conditional(
|
||||
'sha', cfgv.check_string,
|
||||
condition_key='repo',
|
||||
condition_value=cfgv.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
|
||||
ensure_absent=True,
|
||||
),
|
||||
MigrateShaToRev(),
|
||||
)
|
||||
CONFIG_SCHEMA = cfgv.Map(
|
||||
'Config', None,
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ def _update_repo(repo_config, runner, tags_only):
|
|||
Args:
|
||||
repo_config - A config for a repository
|
||||
"""
|
||||
repo_path = runner.store.clone(repo_config['repo'], repo_config['sha'])
|
||||
repo_path = runner.store.clone(repo_config['repo'], repo_config['rev'])
|
||||
|
||||
cmd_output('git', '-C', repo_path, 'fetch')
|
||||
tag_cmd = ('git', '-C', repo_path, 'describe', 'origin/master', '--tags')
|
||||
|
|
@ -46,13 +46,13 @@ def _update_repo(repo_config, runner, tags_only):
|
|||
tag_cmd = ('git', '-C', repo_path, 'rev-parse', 'origin/master')
|
||||
rev = cmd_output(*tag_cmd)[1].strip()
|
||||
|
||||
# Don't bother trying to update if our sha is the same
|
||||
if rev == repo_config['sha']:
|
||||
# Don't bother trying to update if our rev is the same
|
||||
if rev == repo_config['rev']:
|
||||
return repo_config
|
||||
|
||||
# Construct a new config with the head sha
|
||||
# Construct a new config with the head rev
|
||||
new_config = OrderedDict(repo_config)
|
||||
new_config['sha'] = rev
|
||||
new_config['rev'] = rev
|
||||
new_repo = Repository.create(new_config, runner.store)
|
||||
|
||||
# See if any of our hooks were deleted with the new commits
|
||||
|
|
@ -67,8 +67,8 @@ def _update_repo(repo_config, runner, tags_only):
|
|||
return new_config
|
||||
|
||||
|
||||
SHA_LINE_RE = re.compile(r'^(\s+)sha:(\s*)([^\s#]+)(.*)$', re.DOTALL)
|
||||
SHA_LINE_FMT = '{}sha:{}{}{}'
|
||||
REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([^\s#]+)(.*)$', re.DOTALL)
|
||||
REV_LINE_FMT = '{}rev:{}{}{}'
|
||||
|
||||
|
||||
def _write_new_config_file(path, output):
|
||||
|
|
@ -77,25 +77,25 @@ def _write_new_config_file(path, output):
|
|||
new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS)
|
||||
|
||||
lines = original_contents.splitlines(True)
|
||||
sha_line_indices_rev = list(reversed([
|
||||
i for i, line in enumerate(lines) if SHA_LINE_RE.match(line)
|
||||
rev_line_indices_reversed = list(reversed([
|
||||
i for i, line in enumerate(lines) if REV_LINE_RE.match(line)
|
||||
]))
|
||||
|
||||
for line in new_contents.splitlines(True):
|
||||
if SHA_LINE_RE.match(line):
|
||||
# It's possible we didn't identify the sha lines in the original
|
||||
if not sha_line_indices_rev:
|
||||
if REV_LINE_RE.match(line):
|
||||
# It's possible we didn't identify the rev lines in the original
|
||||
if not rev_line_indices_reversed:
|
||||
break
|
||||
line_index = sha_line_indices_rev.pop()
|
||||
line_index = rev_line_indices_reversed.pop()
|
||||
original_line = lines[line_index]
|
||||
orig_match = SHA_LINE_RE.match(original_line)
|
||||
new_match = SHA_LINE_RE.match(line)
|
||||
lines[line_index] = SHA_LINE_FMT.format(
|
||||
orig_match = REV_LINE_RE.match(original_line)
|
||||
new_match = REV_LINE_RE.match(line)
|
||||
lines[line_index] = REV_LINE_FMT.format(
|
||||
orig_match.group(1), orig_match.group(2),
|
||||
new_match.group(3), orig_match.group(4),
|
||||
)
|
||||
|
||||
# If we failed to intelligently rewrite the sha lines, fall back to the
|
||||
# If we failed to intelligently rewrite the rev lines, fall back to the
|
||||
# pretty-formatted yaml output
|
||||
to_write = ''.join(lines)
|
||||
if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output:
|
||||
|
|
@ -132,10 +132,10 @@ def autoupdate(runner, tags_only, repos=()):
|
|||
retv = 1
|
||||
continue
|
||||
|
||||
if new_repo_config['sha'] != repo_config['sha']:
|
||||
if new_repo_config['rev'] != repo_config['rev']:
|
||||
changed = True
|
||||
output.write_line('updating {} -> {}.'.format(
|
||||
repo_config['sha'], new_repo_config['sha'],
|
||||
repo_config['rev'], new_repo_config['rev'],
|
||||
))
|
||||
output_repos.append(new_repo_config)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from __future__ import print_function
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import io
|
||||
import re
|
||||
|
||||
import yaml
|
||||
from aspy.yaml import ordered_load
|
||||
|
|
@ -16,10 +17,7 @@ def _is_header_line(line):
|
|||
return (line.startswith(('#', '---')) or not line.strip())
|
||||
|
||||
|
||||
def migrate_config(runner, quiet=False):
|
||||
with io.open(runner.config_file_path) as f:
|
||||
contents = f.read()
|
||||
|
||||
def _migrate_map(contents):
|
||||
# Find the first non-header line
|
||||
lines = contents.splitlines(True)
|
||||
i = 0
|
||||
|
|
@ -39,6 +37,22 @@ def migrate_config(runner, quiet=False):
|
|||
except yaml.YAMLError:
|
||||
contents = header + 'repos:\n' + _indent(rest)
|
||||
|
||||
return contents
|
||||
|
||||
|
||||
def _migrate_sha_to_rev(contents):
|
||||
reg = re.compile(r'(\n\s+)sha:')
|
||||
return reg.sub(r'\1rev:', contents)
|
||||
|
||||
|
||||
def migrate_config(runner, quiet=False):
|
||||
with io.open(runner.config_file_path) as f:
|
||||
orig_contents = contents = f.read()
|
||||
|
||||
contents = _migrate_map(contents)
|
||||
contents = _migrate_sha_to_rev(contents)
|
||||
|
||||
if contents != orig_contents:
|
||||
with io.open(runner.config_file_path, 'w') as f:
|
||||
f.write(contents)
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ SAMPLE_CONFIG = '''\
|
|||
# See https://pre-commit.com/hooks.html for more hooks
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
sha: v0.9.2
|
||||
rev: v1.2.1-1
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ from pre_commit.util import tmpdir
|
|||
|
||||
|
||||
def try_repo(args):
|
||||
ref = args.ref or git.head_sha(args.repo)
|
||||
ref = args.ref or git.head_rev(args.repo)
|
||||
|
||||
with tmpdir() as tempdir:
|
||||
if args.hook:
|
||||
|
|
@ -28,7 +28,7 @@ def try_repo(args):
|
|||
manifest = sorted(manifest, key=lambda hook: hook['id'])
|
||||
hooks = [{'id': hook['id']} for hook in manifest]
|
||||
|
||||
items = (('repo', args.repo), ('sha', ref), ('hooks', hooks))
|
||||
items = (('repo', args.repo), ('rev', ref), ('hooks', hooks))
|
||||
config = {'repos': [collections.OrderedDict(items)]}
|
||||
config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS)
|
||||
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ def get_changed_files(new, old):
|
|||
)[1])
|
||||
|
||||
|
||||
def head_sha(remote):
|
||||
def head_rev(remote):
|
||||
_, out, _ = cmd_output('git', 'ls-remote', '--exit-code', remote, 'HEAD')
|
||||
return out.split()[0]
|
||||
|
||||
|
|
|
|||
|
|
@ -200,9 +200,9 @@ def main(argv=None):
|
|||
'repo', help='Repository to source hooks from.',
|
||||
)
|
||||
try_repo_parser.add_argument(
|
||||
'--ref',
|
||||
'--ref', '--rev',
|
||||
help=(
|
||||
'Manually select a ref to run against, otherwise the `HEAD` '
|
||||
'Manually select a rev to run against, otherwise the `HEAD` '
|
||||
'revision will be used.'
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -150,8 +150,8 @@ class Repository(object):
|
|||
|
||||
@cached_property
|
||||
def manifest_hooks(self):
|
||||
repo, sha = self.repo_config['repo'], self.repo_config['sha']
|
||||
repo_path = self.store.clone(repo, sha)
|
||||
repo, rev = self.repo_config['repo'], self.repo_config['rev']
|
||||
repo_path = self.store.clone(repo, rev)
|
||||
manifest_path = os.path.join(repo_path, C.MANIFEST_FILE)
|
||||
return {hook['id']: hook for hook in load_manifest(manifest_path)}
|
||||
|
||||
|
|
@ -174,8 +174,8 @@ class Repository(object):
|
|||
)
|
||||
|
||||
def _prefix_from_deps(self, language_name, deps):
|
||||
repo, sha = self.repo_config['repo'], self.repo_config['sha']
|
||||
return Prefix(self.store.clone(repo, sha, deps))
|
||||
repo, rev = self.repo_config['repo'], self.repo_config['rev']
|
||||
return Prefix(self.store.clone(repo, rev, deps))
|
||||
|
||||
def _venvs(self):
|
||||
ret = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue