mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-16 02:21:46 +04:00
Allow local config only hooks
If a repo does not have a manifest, go with local repo config only.
This commit is contained in:
parent
0670e0b287
commit
ebe3e59e35
2 changed files with 41 additions and 9 deletions
|
|
@ -5,8 +5,10 @@ import shlex
|
||||||
import sys
|
import sys
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
from typing import Mapping
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
import cfgv
|
import cfgv
|
||||||
from identify.identify import ALL_TAGS
|
from identify.identify import ALL_TAGS
|
||||||
|
|
@ -89,6 +91,18 @@ load_manifest = functools.partial(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
HOOK_CONFIG_T = TypeVar('HOOK_CONFIG_T', bound=Mapping[str, Any])
|
||||||
|
|
||||||
|
|
||||||
|
def validate_manifest(data: HOOK_CONFIG_T) -> HOOK_CONFIG_T:
|
||||||
|
return cfgv.validate(data, MANIFEST_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_manifest_defaults(data: Sequence[HOOK_CONFIG_T]) \
|
||||||
|
-> Sequence[HOOK_CONFIG_T]:
|
||||||
|
return cfgv.apply_defaults(data, MANIFEST_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int:
|
def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int:
|
||||||
parser = _make_argparser('Manifest filenames.')
|
parser = _make_argparser('Manifest filenames.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,11 @@ from typing import Set
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
|
from pre_commit.clientlib import apply_manifest_defaults
|
||||||
from pre_commit.clientlib import load_manifest
|
from pre_commit.clientlib import load_manifest
|
||||||
from pre_commit.clientlib import LOCAL
|
from pre_commit.clientlib import LOCAL
|
||||||
from pre_commit.clientlib import META
|
from pre_commit.clientlib import META
|
||||||
|
from pre_commit.clientlib import validate_manifest
|
||||||
from pre_commit.hook import Hook
|
from pre_commit.hook import Hook
|
||||||
from pre_commit.languages.all import languages
|
from pre_commit.languages.all import languages
|
||||||
from pre_commit.languages.helpers import environment_dir
|
from pre_commit.languages.helpers import environment_dir
|
||||||
|
|
@ -146,21 +148,37 @@ def _cloned_repository_hooks(
|
||||||
) -> Tuple[Hook, ...]:
|
) -> Tuple[Hook, ...]:
|
||||||
repo, rev = repo_config['repo'], repo_config['rev']
|
repo, rev = repo_config['repo'], repo_config['rev']
|
||||||
manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
|
manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
|
||||||
by_id = {hook['id']: hook for hook in load_manifest(manifest_path)}
|
if os.path.isfile(manifest_path):
|
||||||
|
by_id = {hook['id']: hook for hook in load_manifest(manifest_path)}
|
||||||
|
|
||||||
for hook in repo_config['hooks']:
|
for hook in repo_config['hooks']:
|
||||||
if hook['id'] not in by_id:
|
if hook['id'] not in by_id:
|
||||||
logger.error(
|
logger.error(
|
||||||
f'`{hook["id"]}` is not present in repository {repo}. '
|
f'`{hook["id"]}` is not present in repository {repo}. '
|
||||||
f'Typo? Perhaps it is introduced in a newer version? '
|
f'Typo? Perhaps it is introduced in a newer version? '
|
||||||
f'Often `pre-commit autoupdate` fixes this.',
|
f'Often `pre-commit autoupdate` fixes this.',
|
||||||
)
|
)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
f'No {C.MANIFEST_FILE} found in repository {repo}. '
|
||||||
|
'Proceeding with local repo config only.',
|
||||||
|
)
|
||||||
|
by_id = {
|
||||||
|
hook['id']: hook
|
||||||
|
for hook in apply_manifest_defaults([
|
||||||
|
{'id': repo_hook['id']}
|
||||||
|
for repo_hook in repo_config['hooks']
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
hook_dcts = [
|
hook_dcts = [
|
||||||
_hook(by_id[hook['id']], hook, root_config=root_config)
|
_hook(by_id[hook['id']], hook, root_config=root_config)
|
||||||
for hook in repo_config['hooks']
|
for hook in repo_config['hooks']
|
||||||
]
|
]
|
||||||
|
if not by_id:
|
||||||
|
for hook_dct in hook_dcts:
|
||||||
|
validate_manifest(hook_dct)
|
||||||
return tuple(
|
return tuple(
|
||||||
Hook.create(
|
Hook.create(
|
||||||
repo_config['repo'],
|
repo_config['repo'],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue