Skip to content

Commit a58d99a

Browse files
committed
Implement types filtering
1 parent a68c1ab commit a58d99a

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

pre_commit/commands/run.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
import subprocess
77
import sys
88

9+
from identify.identify import tags_from_path
10+
911
from pre_commit import color
1012
from pre_commit import git
1113
from pre_commit import output
1214
from pre_commit.output import get_hook_message
1315
from pre_commit.staged_files_only import staged_files_only
1416
from pre_commit.util import cmd_output
17+
from pre_commit.util import memoize_by_cwd
1518
from pre_commit.util import noop_context
1619

1720

1821
logger = logging.getLogger('pre_commit')
1922

2023

24+
tags_from_path = memoize_by_cwd(tags_from_path)
25+
26+
2127
def _get_skips(environ):
2228
skips = environ.get('SKIP', '')
2329
return {skip.strip() for skip in skips.split(',') if skip.strip()}
@@ -37,6 +43,14 @@ def get_changed_files(new, old):
3743
)[1].splitlines()
3844

3945

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+
)
52+
53+
4054
def get_filenames(args, include_expr, exclude_expr):
4155
if args.origin and args.source:
4256
getter = git.get_files_matching(
@@ -59,6 +73,7 @@ def get_filenames(args, include_expr, exclude_expr):
5973

6074
def _run_single_hook(hook, repo, args, skips, cols):
6175
filenames = get_filenames(args, hook['files'], hook['exclude'])
76+
filenames = filter_filenames_by_types(filenames, hook['types'])
6277
if hook['id'] in skips:
6378
output.write(get_hook_message(
6479
_hook_msg_start(hook, args.verbose),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: python-files
2+
name: Python files
3+
entry: bin/hook.sh
4+
language: script
5+
types: [python]
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ def test_hook_that_modifies_but_returns_zero(
153153
)
154154

155155

156+
def test_types_hook_repository(
157+
cap_out, tempdir_factory, mock_out_store_directory,
158+
):
159+
git_path = make_consuming_repo(tempdir_factory, 'types_repo')
160+
with cwd(git_path):
161+
stage_a_file('bar.py')
162+
stage_a_file('bar.notpy')
163+
ret, printed = _do_run(cap_out, git_path, _get_opts())
164+
assert ret == 1
165+
assert b'bar.py' in printed
166+
assert b'bar.notpy' not in printed
167+
168+
156169
def test_show_diff_on_failure(
157170
capfd, cap_out, tempdir_factory, mock_out_store_directory,
158171
):

0 commit comments

Comments
 (0)