Skip to content

Commit

Permalink
Merge pull request #1677 from MarcoGorelli/types_or
Browse files Browse the repository at this point in the history
Add types_or
  • Loading branch information
asottile committed Nov 3, 2020
2 parents 3112e08 + 62f668f commit 3fa9d3d
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions pre_commit/clientlib.py
Expand Up @@ -61,6 +61,7 @@ def _make_argparser(filenames_help: str) -> argparse.ArgumentParser:
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(
Expand Down
14 changes: 11 additions & 3 deletions pre_commit/commands/run.py
Expand Up @@ -83,20 +83,28 @@ def by_types(
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
Expand Down
1 change: 1 addition & 0 deletions pre_commit/hook.py
Expand Up @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions pre_commit/meta_hooks/check_useless_excludes.py
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion testing/resources/exclude_types_repo/bin/hook.sh
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo $@
echo "$@"
exit 1
2 changes: 1 addition & 1 deletion testing/resources/failing_hook_repo/bin/hook.sh
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
echo 'Fail'
echo $@
echo "$@"
exit 1
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
echo $@
echo "$@"
2 changes: 1 addition & 1 deletion testing/resources/script_hooks_repo/bin/hook.sh
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

echo $@
echo "$@"
echo 'Hello World'
6 changes: 6 additions & 0 deletions testing/resources/types_or_repo/.pre-commit-hooks.yaml
@@ -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 changes: 3 additions & 0 deletions testing/resources/types_or_repo/bin/hook.sh
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "$@"
exit 1
2 changes: 1 addition & 1 deletion testing/resources/types_repo/bin/hook.sh
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo $@
echo "$@"
exit 1
13 changes: 13 additions & 0 deletions tests/commands/run_test.py
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/repository_test.py
Expand Up @@ -901,6 +901,7 @@ def test_manifest_hooks(tempdir_factory, store):
'post-commit', 'manual', 'post-checkout', 'push',
),
types=['file'],
types_or=['file'],
verbose=False,
)

Expand Down

0 comments on commit 3fa9d3d

Please sign in to comment.