mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-06 08:19:06 -06:00
add types_or
This commit is contained in:
@@ -61,6 +61,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
|
||||
cfgv.Optional('files', check_string_regex, ''),
|
||||
cfgv.Optional('exclude', check_string_regex, '^$'),
|
||||
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']),
|
||||
cfgv.Optional('types_or', cfgv.check_array(check_type_tag), ['file']),
|
||||
cfgv.Optional('exclude_types', cfgv.check_array(check_type_tag), []),
|
||||
|
||||
cfgv.Optional(
|
||||
|
||||
@@ -83,20 +83,28 @@ class Classifier:
|
||||
self,
|
||||
names: Sequence[str],
|
||||
types: Collection[str],
|
||||
types_or: Collection[str],
|
||||
exclude_types: Collection[str],
|
||||
) -> List[str]:
|
||||
types, exclude_types = frozenset(types), frozenset(exclude_types)
|
||||
types = frozenset(types)
|
||||
types_or = frozenset(types_or)
|
||||
exclude_types = frozenset(exclude_types)
|
||||
ret = []
|
||||
for filename in names:
|
||||
tags = self._types_for_file(filename)
|
||||
if tags >= types and not tags & exclude_types:
|
||||
if tags >= types and tags & types_or and not tags & exclude_types:
|
||||
ret.append(filename)
|
||||
return ret
|
||||
|
||||
def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]:
|
||||
names = self.filenames
|
||||
names = filter_by_include_exclude(names, hook.files, hook.exclude)
|
||||
names = self.by_types(names, hook.types, hook.exclude_types)
|
||||
names = self.by_types(
|
||||
names,
|
||||
hook.types,
|
||||
hook.types_or,
|
||||
hook.exclude_types,
|
||||
)
|
||||
return tuple(names)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -22,6 +22,7 @@ class Hook(NamedTuple):
|
||||
files: str
|
||||
exclude: str
|
||||
types: Sequence[str]
|
||||
types_or: Sequence[str]
|
||||
exclude_types: Sequence[str]
|
||||
additional_dependencies: Sequence[str]
|
||||
args: Sequence[str]
|
||||
|
||||
@@ -47,8 +47,10 @@ def check_useless_excludes(config_file: str) -> int:
|
||||
# the defaults applied during runtime
|
||||
hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
|
||||
names = classifier.filenames
|
||||
types, exclude_types = hook['types'], hook['exclude_types']
|
||||
names = classifier.by_types(names, types, exclude_types)
|
||||
types = hook['types']
|
||||
types_or = hook['types_or']
|
||||
exclude_types = hook['exclude_types']
|
||||
names = classifier.by_types(names, types, types_or, exclude_types)
|
||||
include, exclude = hook['files'], hook['exclude']
|
||||
if not exclude_matches_any(names, include, exclude):
|
||||
print(
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
echo $@
|
||||
echo "$@"
|
||||
exit 1
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
echo 'Fail'
|
||||
echo $@
|
||||
echo "$@"
|
||||
exit 1
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
echo $@
|
||||
echo "$@"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo $@
|
||||
echo "$@"
|
||||
echo 'Hello World'
|
||||
|
||||
6
testing/resources/types_or_repo/.pre-commit-hooks.yaml
Normal file
6
testing/resources/types_or_repo/.pre-commit-hooks.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
- id: python-cython-files
|
||||
name: Python and Cython files
|
||||
entry: bin/hook.sh
|
||||
language: script
|
||||
types: [file]
|
||||
types_or: [python, cython]
|
||||
3
testing/resources/types_or_repo/bin/hook.sh
Executable file
3
testing/resources/types_or_repo/bin/hook.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
echo "$@"
|
||||
exit 1
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
echo $@
|
||||
echo "$@"
|
||||
exit 1
|
||||
|
||||
@@ -219,6 +219,19 @@ def test_types_hook_repository(cap_out, store, tempdir_factory):
|
||||
assert b'bar.notpy' not in printed
|
||||
|
||||
|
||||
def test_types_or_hook_repository(cap_out, store, tempdir_factory):
|
||||
git_path = make_consuming_repo(tempdir_factory, 'types_or_repo')
|
||||
with cwd(git_path):
|
||||
stage_a_file('bar.notpy')
|
||||
stage_a_file('bar.pxd')
|
||||
stage_a_file('bar.py')
|
||||
ret, printed = _do_run(cap_out, store, git_path, run_opts())
|
||||
assert ret == 1
|
||||
assert b'bar.notpy' not in printed
|
||||
assert b'bar.pxd' in printed
|
||||
assert b'bar.py' in printed
|
||||
|
||||
|
||||
def test_exclude_types_hook_repository(cap_out, store, tempdir_factory):
|
||||
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
|
||||
with cwd(git_path):
|
||||
|
||||
@@ -901,6 +901,7 @@ def test_manifest_hooks(tempdir_factory, store):
|
||||
'post-commit', 'manual', 'post-checkout', 'push',
|
||||
),
|
||||
types=['file'],
|
||||
types_or=['file'],
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user