Migrate sha -> rev

This commit is contained in:
Anthony Sottile 2018-02-24 18:42:51 -08:00
parent 184e22e81f
commit 5651c66995
19 changed files with 215 additions and 107 deletions

View file

@ -8,6 +8,7 @@ from pre_commit.clientlib import CONFIG_HOOK_DICT
from pre_commit.clientlib import CONFIG_SCHEMA
from pre_commit.clientlib import is_local_repo
from pre_commit.clientlib import MANIFEST_SCHEMA
from pre_commit.clientlib import MigrateShaToRev
from pre_commit.clientlib import validate_config_main
from pre_commit.clientlib import validate_manifest_main
from testing.util import get_resource_path
@ -49,7 +50,7 @@ def test_validate_config_main(args, expected_output):
(
{'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'rev': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [{'id': 'pyflakes', 'files': '\\.py$'}],
}]},
True,
@ -57,7 +58,7 @@ def test_validate_config_main(args, expected_output):
(
{'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'rev': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [
{
'id': 'pyflakes',
@ -71,7 +72,7 @@ def test_validate_config_main(args, expected_output):
(
{'repos': [{
'repo': 'git@github.com:pre-commit/pre-commit-hooks',
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'rev': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
'hooks': [
{
'id': 'pyflakes',
@ -94,7 +95,7 @@ def test_config_valid(config_obj, expected):
def test_config_with_local_hooks_definition_fails():
config_obj = {'repos': [{
'repo': 'local',
'sha': 'foo',
'rev': 'foo',
'hooks': [{
'id': 'do_not_commit',
'name': 'Block if "DO NOT COMMIT" is found',
@ -201,3 +202,45 @@ def test_validate_manifest_main(args, expected_output):
def test_valid_manifests(manifest_obj, expected):
ret = is_valid_according_to_schema(manifest_obj, MANIFEST_SCHEMA)
assert ret is expected
@pytest.mark.parametrize(
'dct',
(
{'repo': 'local'}, {'repo': 'meta'},
{'repo': 'wat', 'sha': 'wat'}, {'repo': 'wat', 'rev': 'wat'},
),
)
def test_migrate_sha_to_rev_ok(dct):
MigrateShaToRev().check(dct)
def test_migrate_sha_to_rev_dont_specify_both():
with pytest.raises(cfgv.ValidationError) as excinfo:
MigrateShaToRev().check({'repo': 'a', 'sha': 'b', 'rev': 'c'})
msg, = excinfo.value.args
assert msg == 'Cannot specify both sha and rev'
@pytest.mark.parametrize(
'dct',
(
{'repo': 'a'},
{'repo': 'meta', 'sha': 'a'}, {'repo': 'meta', 'rev': 'a'},
),
)
def test_migrate_sha_to_rev_conditional_check_failures(dct):
with pytest.raises(cfgv.ValidationError):
MigrateShaToRev().check(dct)
def test_migrate_to_sha_apply_default():
dct = {'repo': 'a', 'sha': 'b'}
MigrateShaToRev().apply_default(dct)
assert dct == {'repo': 'a', 'rev': 'b'}
def test_migrate_to_sha_ok():
dct = {'repo': 'a', 'rev': 'b'}
MigrateShaToRev().apply_default(dct)
assert dct == {'repo': 'a', 'rev': 'b'}

View file

@ -32,9 +32,9 @@ def up_to_date_repo(tempdir_factory):
def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
config = make_config_from_repo(up_to_date_repo)
input_sha = config['sha']
input_rev = config['rev']
ret = _update_repo(config, runner_with_mocked_store, tags_only=False)
assert ret['sha'] == input_sha
assert ret['rev'] == input_rev
def test_autoupdate_up_to_date_repo(
@ -65,12 +65,12 @@ def test_autoupdate_old_revision_broken(
cmd_output('git', '-C', path, 'mv', C.MANIFEST_FILE, 'nope.yaml')
cmd_output('git', '-C', path, 'commit', '-m', 'simulate old repo')
# Assume this is the revision the user's old repository was at
rev = git.head_sha(path)
rev = git.head_rev(path)
cmd_output('git', '-C', path, 'mv', 'nope.yaml', C.MANIFEST_FILE)
cmd_output('git', '-C', path, 'commit', '-m', 'move hooks file')
update_rev = git.head_sha(path)
update_rev = git.head_rev(path)
config['sha'] = rev
config['rev'] = rev
write_config('.', config)
before = open(C.CONFIG_FILE).read()
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
@ -83,24 +83,24 @@ def test_autoupdate_old_revision_broken(
@pytest.fixture
def out_of_date_repo(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_sha = git.head_sha(path)
original_rev = git.head_rev(path)
# Make a commit
cmd_output('git', '-C', path, 'commit', '--allow-empty', '-m', 'foo')
head_sha = git.head_sha(path)
head_rev = git.head_rev(path)
yield auto_namedtuple(
path=path, original_sha=original_sha, head_sha=head_sha,
path=path, original_rev=original_rev, head_rev=head_rev,
)
def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
out_of_date_repo.path, rev=out_of_date_repo.original_rev,
)
ret = _update_repo(config, runner_with_mocked_store, tags_only=False)
assert ret['sha'] != out_of_date_repo.original_sha
assert ret['sha'] == out_of_date_repo.head_sha
assert ret['rev'] != out_of_date_repo.original_rev
assert ret['rev'] == out_of_date_repo.head_rev
def test_autoupdate_out_of_date_repo(
@ -108,7 +108,7 @@ def test_autoupdate_out_of_date_repo(
):
# Write out the config
config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
)
write_config('.', config)
@ -119,14 +119,14 @@ def test_autoupdate_out_of_date_repo(
assert before != after
# Make sure we don't add defaults
assert 'exclude' not in after
assert out_of_date_repo.head_sha in after
assert out_of_date_repo.head_rev in after
def test_autoupdate_out_of_date_repo_with_correct_repo_name(
out_of_date_repo, in_tmpdir, mock_out_store_directory,
):
stale_config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
)
local_config = config_with_local_hooks()
config = {'repos': [stale_config, local_config]}
@ -140,7 +140,7 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name(
after = open(C.CONFIG_FILE).read()
assert ret == 0
assert before != after
assert out_of_date_repo.head_sha in after
assert out_of_date_repo.head_rev in after
assert local_config['repo'] in after
@ -149,7 +149,7 @@ def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
):
# Write out the config
config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
)
write_config('.', config)
@ -168,40 +168,40 @@ def test_does_not_reformat(
fmt = (
'repos:\n'
'- repo: {}\n'
' sha: {} # definitely the version I want!\n'
' rev: {} # definitely the version I want!\n'
' hooks:\n'
' - id: foo\n'
' # These args are because reasons!\n'
' args: [foo, bar, baz]\n'
)
config = fmt.format(out_of_date_repo.path, out_of_date_repo.original_sha)
config = fmt.format(out_of_date_repo.path, out_of_date_repo.original_rev)
with open(C.CONFIG_FILE, 'w') as f:
f.write(config)
autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
after = open(C.CONFIG_FILE).read()
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_sha)
expected = fmt.format(out_of_date_repo.path, out_of_date_repo.head_rev)
assert after == expected
def test_loses_formatting_when_not_detectable(
out_of_date_repo, mock_out_store_directory, in_tmpdir,
):
"""A best-effort attempt is made at updating sha without rewriting
"""A best-effort attempt is made at updating rev without rewriting
formatting. When the original formatting cannot be detected, this
is abandoned.
"""
config = (
'repos: [\n'
' {{\n'
' repo: {}, sha: {},\n'
' repo: {}, rev: {},\n'
' hooks: [\n'
' # A comment!\n'
' {{id: foo}},\n'
' ],\n'
' }}\n'
']\n'.format(
pipes.quote(out_of_date_repo.path), out_of_date_repo.original_sha,
pipes.quote(out_of_date_repo.path), out_of_date_repo.original_rev,
)
)
with open(C.CONFIG_FILE, 'w') as f:
@ -212,10 +212,10 @@ def test_loses_formatting_when_not_detectable(
expected = (
'repos:\n'
'- repo: {}\n'
' sha: {}\n'
' rev: {}\n'
' hooks:\n'
' - id: foo\n'
).format(out_of_date_repo.path, out_of_date_repo.head_sha)
).format(out_of_date_repo.path, out_of_date_repo.head_rev)
assert after == expected
@ -229,7 +229,7 @@ def test_autoupdate_tagged_repo(
tagged_repo, in_tmpdir, mock_out_store_directory,
):
config = make_config_from_repo(
tagged_repo.path, sha=tagged_repo.original_sha,
tagged_repo.path, rev=tagged_repo.original_rev,
)
write_config('.', config)
@ -250,7 +250,7 @@ def test_autoupdate_tags_only(
):
config = make_config_from_repo(
tagged_repo_with_more_commits.path,
sha=tagged_repo_with_more_commits.original_sha,
rev=tagged_repo_with_more_commits.original_rev,
)
write_config('.', config)
@ -262,7 +262,7 @@ def test_autoupdate_tags_only(
@pytest.fixture
def hook_disappearing_repo(tempdir_factory):
path = make_repo(tempdir_factory, 'python_hooks_repo')
original_sha = git.head_sha(path)
original_rev = git.head_rev(path)
shutil.copy(
get_resource_path('manifest_without_foo.yaml'),
@ -271,7 +271,7 @@ def hook_disappearing_repo(tempdir_factory):
cmd_output('git', '-C', path, 'add', '.')
cmd_output('git', '-C', path, 'commit', '-m', 'Remove foo')
yield auto_namedtuple(path=path, original_sha=original_sha)
yield auto_namedtuple(path=path, original_rev=original_rev)
def test_hook_disppearing_repo_raises(
@ -279,7 +279,7 @@ def test_hook_disppearing_repo_raises(
):
config = make_config_from_repo(
hook_disappearing_repo.path,
sha=hook_disappearing_repo.original_sha,
rev=hook_disappearing_repo.original_rev,
hooks=[OrderedDict((('id', 'foo'),))],
)
with pytest.raises(RepositoryCannotBeUpdatedError):
@ -291,7 +291,7 @@ def test_autoupdate_hook_disappearing_repo(
):
config = make_config_from_repo(
hook_disappearing_repo.path,
sha=hook_disappearing_repo.original_sha,
rev=hook_disappearing_repo.original_rev,
hooks=[OrderedDict((('id', 'foo'),))],
check=False,
)
@ -319,7 +319,7 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
out_of_date_repo, in_tmpdir, mock_out_store_directory,
):
stale_config = make_config_from_repo(
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
)
local_config = config_with_local_hooks()
config = {'repos': [local_config, stale_config]}

View file

@ -118,3 +118,30 @@ def test_already_migrated_configuration_noop(tmpdir, capsys):
out, _ = capsys.readouterr()
assert out == 'Configuration is already migrated.\n'
assert cfg.read() == contents
def test_migrate_config_sha_to_rev(tmpdir):
contents = (
'repos:\n'
'- repo: https://github.com/pre-commit/pre-commit-hooks\n'
' sha: v1.2.0\n'
' hooks: []\n'
'repos:\n'
'- repo: https://github.com/pre-commit/pre-commit-hooks\n'
' sha: v1.2.0\n'
' hooks: []\n'
)
cfg = tmpdir.join(C.CONFIG_FILE)
cfg.write(contents)
assert not migrate_config(Runner(tmpdir.strpath, C.CONFIG_FILE))
contents = cfg.read()
assert contents == (
'repos:\n'
'- repo: https://github.com/pre-commit/pre-commit-hooks\n'
' rev: v1.2.0\n'
' hooks: []\n'
'repos:\n'
'- repo: https://github.com/pre-commit/pre-commit-hooks\n'
' rev: v1.2.0\n'
' hooks: []\n'
)

View file

@ -13,7 +13,7 @@ def test_sample_config(capsys):
# 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

View file

@ -39,7 +39,7 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
assert re.match(
'^repos:\n'
'- repo: .+\n'
' sha: .+\n'
' rev: .+\n'
' hooks:\n'
' - id: bash_hook\n'
' - id: bash_hook2\n'
@ -63,7 +63,7 @@ def test_try_repo_with_specific_hook(cap_out, tempdir_factory):
assert re.match(
'^repos:\n'
'- repo: .+\n'
' sha: .+\n'
' rev: .+\n'
' hooks:\n'
' - id: bash_hook\n$',
config,

View file

@ -19,8 +19,8 @@ def test_make_archive(tempdir_factory):
open(os.path.join(git_path, 'foo'), 'a').close()
cmd_output('git', '-C', git_path, 'add', '.')
cmd_output('git', '-C', git_path, 'commit', '-m', 'foo')
# We'll use this sha
head_sha = git.head_sha(git_path)
# We'll use this rev
head_rev = git.head_rev(git_path)
# And check that this file doesn't exist
open(os.path.join(git_path, 'bar'), 'a').close()
cmd_output('git', '-C', git_path, 'add', '.')
@ -28,7 +28,7 @@ def test_make_archive(tempdir_factory):
# Do the thing
archive_path = make_archives.make_archive(
'foo', git_path, head_sha, output_dir,
'foo', git_path, head_rev, output_dir,
)
assert archive_path == os.path.join(output_dir, 'foo.tar.gz')

View file

@ -660,14 +660,14 @@ def test_tags_on_repositories(in_tmpdir, tempdir_factory, store):
)
repo_1 = Repository.create(
make_config_from_repo(git_dir_1, sha=tag), store,
make_config_from_repo(git_dir_1, rev=tag), store,
)
ret = repo_1.run_hook(repo_1.hooks[0][1], ['-L'])
assert ret[0] == 0
assert ret[1].strip() == _norm_pwd(in_tmpdir)
repo_2 = Repository.create(
make_config_from_repo(git_dir_2, sha=tag), store,
make_config_from_repo(git_dir_2, rev=tag), store,
)
ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar'])
assert ret[0] == 0

View file

@ -192,15 +192,14 @@ def submodule_with_commits(tempdir_factory):
path = git_dir(tempdir_factory)
with cwd(path):
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
sha1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
cmd_output('git', 'commit', '--allow-empty', '-m', 'bar')
sha2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
yield auto_namedtuple(path=path, sha1=sha1, sha2=sha2)
rev2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
yield auto_namedtuple(path=path, rev1=rev1, rev2=rev2)
def checkout_submodule(sha):
with cwd('sub'):
cmd_output('git', 'checkout', sha)
def checkout_submodule(rev):
cmd_output('git', '-C', 'sub', 'checkout', rev)
@pytest.fixture
@ -210,7 +209,7 @@ def sub_staged(submodule_with_commits, tempdir_factory):
cmd_output(
'git', 'submodule', 'add', submodule_with_commits.path, 'sub',
)
checkout_submodule(submodule_with_commits.sha1)
checkout_submodule(submodule_with_commits.rev1)
cmd_output('git', 'add', 'sub')
yield auto_namedtuple(
path=path,
@ -219,11 +218,11 @@ def sub_staged(submodule_with_commits, tempdir_factory):
)
def _test_sub_state(path, sha='sha1', status='A'):
def _test_sub_state(path, rev='rev1', status='A'):
assert os.path.exists(path.sub_path)
with cwd(path.sub_path):
actual_sha = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
assert actual_sha == getattr(path.submodule, sha)
actual_rev = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
assert actual_rev == getattr(path.submodule, rev)
actual_status = get_short_git_status()['sub']
assert actual_status == status
@ -239,15 +238,15 @@ def test_sub_nothing_unstaged(sub_staged, patch_dir):
def test_sub_something_unstaged(sub_staged, patch_dir):
checkout_submodule(sub_staged.submodule.sha2)
checkout_submodule(sub_staged.submodule.rev2)
_test_sub_state(sub_staged, 'sha2', 'AM')
_test_sub_state(sub_staged, 'rev2', 'AM')
with staged_files_only(patch_dir):
# This is different from others, we don't want to touch subs
_test_sub_state(sub_staged, 'sha2', 'AM')
_test_sub_state(sub_staged, 'rev2', 'AM')
_test_sub_state(sub_staged, 'sha2', 'AM')
_test_sub_state(sub_staged, 'rev2', 'AM')
def test_stage_utf8_changes(foo_staged, patch_dir):

View file

@ -91,10 +91,10 @@ def test_clone(store, tempdir_factory, log_info_mock):
path = git_dir(tempdir_factory)
with cwd(path):
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
sha = git.head_sha(path)
rev = git.head_rev(path)
cmd_output('git', 'commit', '--allow-empty', '-m', 'bar')
ret = store.clone(path, sha)
ret = store.clone(path, rev)
# Should have printed some stuff
assert log_info_mock.call_args_list[0][0][0].startswith(
'Initializing environment for ',
@ -106,14 +106,14 @@ def test_clone(store, tempdir_factory, log_info_mock):
# Directory should start with `repo`
_, dirname = os.path.split(ret)
assert dirname.startswith('repo')
# Should be checked out to the sha we specified
assert git.head_sha(ret) == sha
# Should be checked out to the rev we specified
assert git.head_rev(ret) == rev
# Assert there's an entry in the sqlite db for this
with sqlite3.connect(store.db_path) as db:
path, = db.execute(
'SELECT path from repos WHERE repo = ? and ref = ?',
[path, sha],
(path, rev),
).fetchone()
assert path == ret
@ -122,7 +122,7 @@ def test_clone_cleans_up_on_checkout_failure(store):
try:
# This raises an exception because you can't clone something that
# doesn't exist!
store.clone('/i_dont_exist_lol', 'fake_sha')
store.clone('/i_dont_exist_lol', 'fake_rev')
except Exception as e:
assert '/i_dont_exist_lol' in six.text_type(e)