Merge pull request #2657 from pre-commit/remove-sha-support

remove support for sha to specify rev
This commit is contained in:
Anthony Sottile 2022-12-27 12:13:26 -05:00 committed by GitHub
commit def3fa3929
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 82 deletions

View file

@ -114,8 +114,7 @@ LOCAL = 'local'
META = 'meta' META = 'meta'
# should inherit from cfgv.Conditional if sha support is dropped class WarnMutableRev(cfgv.Conditional):
class WarnMutableRev(cfgv.ConditionalOptional):
def check(self, dct: dict[str, Any]) -> None: def check(self, dct: dict[str, Any]) -> None:
super().check(dct) super().check(dct)
@ -171,36 +170,6 @@ class OptionalSensibleRegexAtTop(cfgv.OptionalNoDefault):
) )
class MigrateShaToRev:
key = 'rev'
@staticmethod
def _cond(key: str) -> cfgv.Conditional:
return cfgv.Conditional(
key, cfgv.check_string,
condition_key='repo',
condition_value=cfgv.NotIn(LOCAL, META),
ensure_absent=True,
)
def check(self, dct: dict[str, Any]) -> None:
if dct.get('repo') in {LOCAL, META}:
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: dict[str, Any]) -> None:
if 'sha' in dct:
dct['rev'] = dct.pop('sha')
remove_default = cfgv.Required.remove_default
def _entry(modname: str) -> str: def _entry(modname: str) -> str:
"""the hook `entry` is passed through `shlex.split()` by the command """the hook `entry` is passed through `shlex.split()` by the command
runner, so to prevent issues with spaces and backslashes (on Windows) runner, so to prevent issues with spaces and backslashes (on Windows)
@ -324,14 +293,11 @@ CONFIG_REPO_DICT = cfgv.Map(
'repo', META, 'repo', META,
), ),
MigrateShaToRev(),
WarnMutableRev( WarnMutableRev(
'rev', 'rev', cfgv.check_string,
cfgv.check_string, condition_key='repo',
'', condition_value=cfgv.NotIn(LOCAL, META),
'repo', ensure_absent=True,
cfgv.NotIn(LOCAL, META),
True,
), ),
cfgv.WarnAdditionalKeys(('repo', 'rev', 'hooks'), warn_unknown_keys_repo), cfgv.WarnAdditionalKeys(('repo', 'rev', 'hooks'), warn_unknown_keys_repo),
) )

View file

@ -14,7 +14,6 @@ from pre_commit.clientlib import CONFIG_SCHEMA
from pre_commit.clientlib import DEFAULT_LANGUAGE_VERSION from pre_commit.clientlib import DEFAULT_LANGUAGE_VERSION
from pre_commit.clientlib import MANIFEST_SCHEMA from pre_commit.clientlib import MANIFEST_SCHEMA
from pre_commit.clientlib import META_HOOK_DICT from pre_commit.clientlib import META_HOOK_DICT
from pre_commit.clientlib import MigrateShaToRev
from pre_commit.clientlib import OptionalSensibleRegexAtHook from pre_commit.clientlib import OptionalSensibleRegexAtHook
from pre_commit.clientlib import OptionalSensibleRegexAtTop from pre_commit.clientlib import OptionalSensibleRegexAtTop
from pre_commit.clientlib import validate_config_main from pre_commit.clientlib import validate_config_main
@ -417,48 +416,6 @@ def test_valid_manifests(manifest_obj, expected):
assert ret is expected 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'}
@pytest.mark.parametrize( @pytest.mark.parametrize(
'config_repo', 'config_repo',
( (