mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Add multiline mode to pygrep
This commit is contained in:
@@ -26,6 +26,15 @@ def _process_filename_by_line(pattern, filename):
|
||||
output.write_line(line.rstrip(b'\r\n'))
|
||||
return retv
|
||||
|
||||
def _process_filename_at_once(pattern, filename):
|
||||
retv = 0
|
||||
with open(filename, 'rb') as f:
|
||||
match = pattern.search(f.read())
|
||||
if match:
|
||||
retv = 1
|
||||
output.write('{}:'.format(filename))
|
||||
output.write_line(match.group())
|
||||
return retv
|
||||
|
||||
def run_hook(prefix, hook, file_args):
|
||||
exe = (sys.executable, '-m', __name__)
|
||||
@@ -42,6 +51,7 @@ def main(argv=None):
|
||||
),
|
||||
)
|
||||
parser.add_argument('-i', '--ignore-case', action='store_true')
|
||||
parser.add_argument('-z', '--null-data', action='store_true')
|
||||
parser.add_argument('pattern', help='python regex pattern.')
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
@@ -51,7 +61,10 @@ def main(argv=None):
|
||||
|
||||
retv = 0
|
||||
for filename in args.filenames:
|
||||
retv |= _process_filename_by_line(pattern, filename)
|
||||
if args.null_data:
|
||||
retv |= _process_filename_at_once(pattern, filename)
|
||||
else:
|
||||
retv |= _process_filename_by_line(pattern, filename)
|
||||
return retv
|
||||
|
||||
|
||||
|
||||
@@ -38,3 +38,9 @@ def test_ignore_case(some_files, cap_out):
|
||||
out = cap_out.get()
|
||||
assert ret == 1
|
||||
assert out == 'f2:1:[INFO] hi\n'
|
||||
|
||||
def test_null_data(some_files, cap_out):
|
||||
ret = pygrep.main(('--null-data', r'foo.*bar', 'f1', 'f2', 'f3'))
|
||||
out = cap_out.get()
|
||||
assert ret == 1
|
||||
assert out == 'f1:foobar\n'
|
||||
|
||||
Reference in New Issue
Block a user