Implement exclude_types · precommit/pre-commit@05a108e · GitHub
Skip to content

Commit 05a108e

Browse files
committed
Implement exclude_types
1 parent f956f42 commit 05a108e

6 files changed

Lines changed: 46 additions & 13 deletions

File tree

pre_commit/clientlib.py

Lines changed: 6 additions & 5 deletions

pre_commit/commands/run.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ def get_changed_files(new, old):
4343
)[1].splitlines()
4444

4545

46-
def filter_filenames_by_types(filenames, types):
47-
types = frozenset(types)
48-
return tuple(
49-
filename for filename in filenames
50-
if tags_from_path(filename) >= types
51-
)
46+
def filter_filenames_by_types(filenames, types, exclude_types):
47+
types, exclude_types = frozenset(types), frozenset(exclude_types)
48+
ret = []
49+
for filename in filenames:
50+
tags = tags_from_path(filename)
51+
if tags >= types and not tags & exclude_types:
52+
ret.append(filename)
53+
return tuple(ret)
5254

5355

5456
def get_filenames(args, include_expr, exclude_expr):
@@ -73,7 +75,9 @@ def get_filenames(args, include_expr, exclude_expr):
7375

7476
def _run_single_hook(hook, repo, args, skips, cols):
7577
filenames = get_filenames(args, hook['files'], hook['exclude'])
76-
filenames = filter_filenames_by_types(filenames, hook['types'])
78+
filenames = filter_filenames_by_types(
79+
filenames, hook['types'], hook['exclude_types'],
80+
)
7781
if hook['id'] in skips:
7882
output.write(get_hook_message(
7983
_hook_msg_start(hook, args.verbose),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- id: python-files
2+
name: Python files
3+
entry: bin/hook.sh
4+
language: script
5+
types: [python]
6+
exclude_types: [python3]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
echo $@
3+
exit 1

tests/commands/run_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pre_commit.runner import Runner
2121
from pre_commit.util import cmd_output
2222
from pre_commit.util import cwd
23+
from pre_commit.util import make_executable
2324
from testing.auto_namedtuple import auto_namedtuple
2425
from testing.fixtures import add_config_to_repo
2526
from testing.fixtures import make_consuming_repo
@@ -43,7 +44,7 @@ def repo_with_failing_hook(tempdir_factory):
4344

4445

4546
def stage_a_file(filename='foo.py'):
46-
cmd_output('touch', filename)
47+
open(filename, 'a').close()
4748
cmd_output('git', 'add', filename)
4849

4950

@@ -166,6 +167,22 @@ def test_types_hook_repository(
166167
assert b'bar.notpy' not in printed
167168

168169

170+
def test_exclude_types_hook_repository(
171+
cap_out, tempdir_factory, mock_out_store_directory,
172+
):
173+
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
174+
with cwd(git_path):
175+
with io.open('exe', 'w') as exe:
176+
exe.write('#!/usr/bin/env python3\n')
177+
make_executable('exe')
178+
cmd_output('git', 'add', 'exe')
179+
stage_a_file('bar.py')
180+
ret, printed = _do_run(cap_out, git_path, _get_opts())
181+
assert ret == 1
182+
assert b'bar.py' in printed
183+
assert b'exe' not in printed
184+
185+
169186
def test_show_diff_on_failure(
170187
capfd, cap_out, tempdir_factory, mock_out_store_directory,
171188
):

tests/manifest_test.py

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)