66import subprocess
77import sys
88
9+ from identify .identify import tags_from_path
10+
911from pre_commit import color
1012from pre_commit import git
1113from pre_commit import output
1214from pre_commit .output import get_hook_message
1315from pre_commit .staged_files_only import staged_files_only
1416from pre_commit .util import cmd_output
17+ from pre_commit .util import memoize_by_cwd
1518from pre_commit .util import noop_context
1619
1720
1821logger = logging .getLogger ('pre_commit' )
1922
2023
24+ tags_from_path = memoize_by_cwd (tags_from_path )
25+
26+
2127def _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+
4054def 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
6074def _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 ),
0 commit comments