mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 01:51:46 +04:00
Add multiline mode to pygrep
This commit is contained in:
parent
2e6cd2a44c
commit
69333fa227
2 changed files with 20 additions and 1 deletions
|
|
@ -26,6 +26,15 @@ def _process_filename_by_line(pattern, filename):
|
||||||
output.write_line(line.rstrip(b'\r\n'))
|
output.write_line(line.rstrip(b'\r\n'))
|
||||||
return retv
|
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):
|
def run_hook(prefix, hook, file_args):
|
||||||
exe = (sys.executable, '-m', __name__)
|
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('-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('pattern', help='python regex pattern.')
|
||||||
parser.add_argument('filenames', nargs='*')
|
parser.add_argument('filenames', nargs='*')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
@ -51,7 +61,10 @@ def main(argv=None):
|
||||||
|
|
||||||
retv = 0
|
retv = 0
|
||||||
for filename in args.filenames:
|
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
|
return retv
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,3 +38,9 @@ def test_ignore_case(some_files, cap_out):
|
||||||
out = cap_out.get()
|
out = cap_out.get()
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert out == 'f2:1:[INFO] hi\n'
|
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'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue