add support for top-level map manifest

This commit is contained in:
Anthony Sottile 2025-11-19 16:14:54 -05:00 committed by anthony sottile
parent 9c7ea88ab9
commit 7cad9ec27f
10 changed files with 156 additions and 61 deletions

View file

@ -72,11 +72,20 @@ def transform_stage(stage: str) -> str:
return _STAGES.get(stage, stage)
MINIMAL_MANIFEST_SCHEMA = cfgv.Array(
cfgv.Map(
'Hook', 'id',
cfgv.Required('id', cfgv.check_string),
cfgv.Optional('stages', cfgv.check_array(cfgv.check_string), []),
_MINIMAL_MANIFEST_SCHEMA = cfgv.Map(
'Manifest', None,
cfgv.RequiredRecurse(
'hooks',
cfgv.KeyValueMap(
'Hooks',
cfgv.check_string,
cfgv.Map(
'Hook', None,
cfgv.Optional(
'stages', cfgv.check_array(cfgv.check_string), [],
),
),
),
),
)
@ -85,16 +94,16 @@ def warn_for_stages_on_repo_init(repo: str, directory: str) -> None:
try:
manifest = cfgv.load_from_filename(
os.path.join(directory, C.MANIFEST_FILE),
schema=MINIMAL_MANIFEST_SCHEMA,
load_strategy=yaml_load,
schema=_MINIMAL_MANIFEST_SCHEMA,
load_strategy=_load_manifest_backward_compat,
exc_tp=InvalidManifestError,
)
except InvalidManifestError:
return # they'll get a better error message when it actually loads!
legacy_stages = {} # sorted set
for hook in manifest:
for stage in hook.get('stages', ()):
for hook in manifest['hooks'].values():
for stage in hook['stages']:
if stage in _STAGES:
legacy_stages[stage] = True
@ -228,7 +237,7 @@ class LanguageMigrationRequired(LanguageMigration): # replace with Required
MANIFEST_HOOK_DICT = cfgv.Map(
'Hook', 'id',
'Hook', None,
# check first in case it uses some newer, incompatible feature
cfgv.Optional(
@ -237,7 +246,6 @@ MANIFEST_HOOK_DICT = cfgv.Map(
'0',
),
cfgv.Required('id', cfgv.check_string),
cfgv.Required('name', cfgv.check_string),
cfgv.Required('entry', cfgv.check_string),
LanguageMigrationRequired('language', cfgv.check_one_of(language_names)),
@ -263,18 +271,38 @@ MANIFEST_HOOK_DICT = cfgv.Map(
StagesMigration('stages', []),
cfgv.Optional('verbose', cfgv.check_bool, False),
)
MANIFEST_SCHEMA = cfgv.Array(MANIFEST_HOOK_DICT)
MANIFEST_SCHEMA = cfgv.Map(
'Manifest', None,
# check first in case it uses some newer, incompatible feature
cfgv.Optional(
'minimum_pre_commit_version',
cfgv.check_and(cfgv.check_string, check_min_version),
'0',
),
cfgv.RequiredRecurse(
'hooks',
cfgv.KeyValueMap('Hooks', cfgv.check_string, MANIFEST_HOOK_DICT),
),
)
class InvalidManifestError(FatalError):
pass
def _load_manifest_forward_compat(contents: str) -> object:
_MINIMAL_LEGACY_MANIFEST_SCHEMA = cfgv.Array(
cfgv.Map('Hook', 'id', cfgv.Required('id', cfgv.check_string)),
)
def _load_manifest_backward_compat(contents: str) -> object:
obj = yaml_load(contents)
if isinstance(obj, dict):
check_min_version('5')
raise AssertionError('unreachable')
if isinstance(obj, list):
cfgv.validate(obj, _MINIMAL_LEGACY_MANIFEST_SCHEMA)
return {'hooks': {hook.pop('id'): hook for hook in obj}}
else:
return obj
@ -282,7 +310,7 @@ def _load_manifest_forward_compat(contents: str) -> object:
load_manifest = functools.partial(
cfgv.load_from_filename,
schema=MANIFEST_SCHEMA,
load_strategy=_load_manifest_forward_compat,
load_strategy=_load_manifest_backward_compat,
exc_tp=InvalidManifestError,
)
@ -448,7 +476,6 @@ CONFIG_HOOK_DICT = cfgv.Map(
*(
cfgv.OptionalNoDefault(item.key, item.check_fn)
for item in MANIFEST_HOOK_DICT.items
if item.key != 'id'
if item.key != 'stages'
if item.key != 'language' # remove
),
@ -459,6 +486,7 @@ CONFIG_HOOK_DICT = cfgv.Map(
LOCAL_HOOK_DICT = cfgv.Map(
'Hook', 'id',
cfgv.Required('id', cfgv.check_string),
*MANIFEST_HOOK_DICT.items,
*_COMMON_HOOK_WARNINGS,
)

View file

@ -77,7 +77,7 @@ class RevInfo(NamedTuple):
except InvalidManifestError as e:
raise RepositoryCannotBeUpdatedError(f'[{self.repo}] {e}')
else:
hook_ids = frozenset(hook['id'] for hook in manifest)
hook_ids = frozenset(hook['id'] for hook in manifest['hooks'])
return self._replace(rev=rev, frozen=frozen, hook_ids=hook_ids)

View file

@ -43,7 +43,7 @@ def _mark_used_repos(
return
else:
unused_repos.discard(key)
by_id = {hook['id']: hook for hook in manifest}
by_id = manifest['hooks']
for hook in repo['hooks']:
if hook['id'] not in by_id:

View file

@ -58,8 +58,8 @@ def try_repo(args: argparse.Namespace) -> int:
else:
repo_path = store.clone(repo, ref)
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
manifest = sorted(manifest, key=lambda hook: hook['id'])
hooks = [{'id': hook['id']} for hook in manifest]
hook_ids = sorted(hook['id'] for hook in manifest['hooks'])
hooks = [{'id': hook_id} for hook_id in hook_ids]
config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]}
config_s = yaml_dump(config)

View file

@ -175,7 +175,7 @@ def _cloned_repository_hooks(
) -> tuple[Hook, ...]:
repo, rev = repo_config['repo'], repo_config['rev']
manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
by_id = {hook['id']: hook for hook in load_manifest(manifest_path)}
by_id = load_manifest(manifest_path)['hooks']
for hook in repo_config['hooks']:
if hook['id'] not in by_id: