mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
Implement types filtering
This commit is contained in:
@@ -6,18 +6,24 @@ import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from identify.identify import tags_from_path
|
||||
|
||||
from pre_commit import color
|
||||
from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.output import get_hook_message
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import memoize_by_cwd
|
||||
from pre_commit.util import noop_context
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
tags_from_path = memoize_by_cwd(tags_from_path)
|
||||
|
||||
|
||||
def _get_skips(environ):
|
||||
skips = environ.get('SKIP', '')
|
||||
return {skip.strip() for skip in skips.split(',') if skip.strip()}
|
||||
@@ -37,6 +43,14 @@ def get_changed_files(new, old):
|
||||
)[1].splitlines()
|
||||
|
||||
|
||||
def filter_filenames_by_types(filenames, types):
|
||||
types = frozenset(types)
|
||||
return tuple(
|
||||
filename for filename in filenames
|
||||
if tags_from_path(filename) >= types
|
||||
)
|
||||
|
||||
|
||||
def get_filenames(args, include_expr, exclude_expr):
|
||||
if args.origin and args.source:
|
||||
getter = git.get_files_matching(
|
||||
@@ -59,6 +73,7 @@ NO_FILES = '(no files to check)'
|
||||
|
||||
def _run_single_hook(hook, repo, args, skips, cols):
|
||||
filenames = get_filenames(args, hook['files'], hook['exclude'])
|
||||
filenames = filter_filenames_by_types(filenames, hook['types'])
|
||||
if hook['id'] in skips:
|
||||
output.write(get_hook_message(
|
||||
_hook_msg_start(hook, args.verbose),
|
||||
|
||||
Reference in New Issue
Block a user