Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types_or #1677

Merged
merged 1 commit into from Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are usually easier to demo with a local repo

I'd probably not write a full integration test for this feature as well but it looks like there maybe aren't great examples using the matcher

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback, but I'm not sure I understand - are there any existing tests I should look at as examples? If not, I'll try to figure this out, no worries

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