mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-16 10:31:46 +04:00
First pass at adding a hook selector for files that contain a given string
This commit is contained in:
parent
6872289f1a
commit
82e1768656
6 changed files with 38 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import argparse
|
|||
import contextlib
|
||||
import functools
|
||||
import logging
|
||||
import mmap
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
|
@ -100,6 +101,22 @@ class Classifier:
|
|||
ret.append(filename)
|
||||
return ret
|
||||
|
||||
def files_contain(
|
||||
self,
|
||||
names: Sequence[str],
|
||||
contains: str,
|
||||
) -> List[str]:
|
||||
if contains == '':
|
||||
return list(names)
|
||||
|
||||
ret = []
|
||||
for filename in names:
|
||||
with open(filename, 'r+') as f:
|
||||
with mmap.mmap(f.fileno(), 0) as mm:
|
||||
if mm.find(contains.encode()) >= 0:
|
||||
ret.append(filename)
|
||||
return ret
|
||||
|
||||
def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]:
|
||||
names = self.filenames
|
||||
names = filter_by_include_exclude(names, hook.files, hook.exclude)
|
||||
|
|
@ -109,6 +126,10 @@ class Classifier:
|
|||
hook.types_or,
|
||||
hook.exclude_types,
|
||||
)
|
||||
names = self.files_contain(
|
||||
names,
|
||||
hook.files_contain,
|
||||
)
|
||||
return tuple(names)
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue