Skip to content

Commit 05a108e

Browse files
committed
Implement exclude_types
1 parent f956f42 commit 05a108e

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

pre_commit/clientlib.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ def _make_argparser(filenames_help):
4949
'files', schema.check_and(schema.check_string, schema.check_regex),
5050
'',
5151
),
52+
schema.Optional(
53+
'exclude',
54+
schema.check_and(schema.check_string, schema.check_regex),
55+
'^$',
56+
),
5257
schema.Optional('types', schema.check_array(check_type_tag), ['file']),
58+
schema.Optional('exclude_types', schema.check_array(check_type_tag), []),
5359

5460
schema.Optional(
5561
'additional_dependencies', schema.check_array(schema.check_string), [],
@@ -58,11 +64,6 @@ def _make_argparser(filenames_help):
5864
schema.Optional('always_run', schema.check_bool, False),
5965
schema.Optional('pass_filenames', schema.check_bool, True),
6066
schema.Optional('description', schema.check_string, ''),
61-
schema.Optional(
62-
'exclude',
63-
schema.check_and(schema.check_string, schema.check_regex),
64-
'^$',
65-
),
6667
schema.Optional('language_version', schema.check_string, 'default'),
6768
schema.Optional('log_file', schema.check_string, ''),
6869
schema.Optional('minimum_pre_commit_version', schema.check_string, '0'),

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
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_manifest_contents(manifest):
3535
'pass_filenames': True,
3636
'stages': [],
3737
'types': ['file'],
38+
'exclude_types': [],
3839
}]
3940

4041

@@ -56,6 +57,7 @@ def test_hooks(manifest):
5657
'pass_filenames': True,
5758
'stages': [],
5859
'types': ['file'],
60+
'exclude_types': [],
5961
}
6062

6163

0 commit comments

Comments
 (0)