Skip to content

Commit 55c74c1

Browse files
committed
Rename option to and improve output
1 parent 1907537 commit 55c74c1

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

pre_commit/languages/pygrep.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ def _process_filename_by_line(pattern, filename):
3030
def _process_filename_at_once(pattern, filename):
3131
retv = 0
3232
with open(filename, 'rb') as f:
33-
match = pattern.search(f.read())
33+
contents = f.read()
34+
match = pattern.search(contents)
3435
if match:
3536
retv = 1
37+
line_no = len(
38+
re.compile('\n'.encode()).findall(contents, 0, match.start()),
39+
)
3640
output.write(
37-
'{}:{}-{}:'.format(filename, match.start(), match.end()),
41+
'{}:{}:'.format(filename, line_no + 1),
3842
)
39-
output.write_line(match.group())
43+
44+
matched_lines = match.group().split('\n')
45+
matched_lines[0] = contents.split('\n')[line_no]
46+
47+
output.write_line('\n'.join(matched_lines))
4048
return retv
4149

4250

@@ -55,20 +63,20 @@ def main(argv=None):
5563
),
5664
)
5765
parser.add_argument('-i', '--ignore-case', action='store_true')
58-
parser.add_argument('-z', '--null-data', action='store_true')
66+
parser.add_argument('--multiline', action='store_true')
5967
parser.add_argument('pattern', help='python regex pattern.')
6068
parser.add_argument('filenames', nargs='*')
6169
args = parser.parse_args(argv)
6270

6371
flags = re.IGNORECASE if args.ignore_case else 0
64-
if args.null_data:
72+
if args.multiline:
6573
flags = flags | re.MULTILINE | re.DOTALL
6674

6775
pattern = re.compile(args.pattern.encode(), flags)
6876

6977
retv = 0
7078
for filename in args.filenames:
71-
if args.null_data:
79+
if args.multiline:
7280
retv |= _process_filename_at_once(pattern, filename)
7381
else:
7482
retv |= _process_filename_by_line(pattern, filename)

tests/languages/pygrep_test.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,29 @@ def test_ignore_case(some_files, cap_out):
4040
assert out == 'f2:1:[INFO] hi\n'
4141

4242

43-
def test_null_data(some_files, cap_out):
44-
ret = pygrep.main(('--null-data', r'foo\nbar', 'f1', 'f2', 'f3'))
43+
def test_multiline(some_files, cap_out):
44+
ret = pygrep.main(('--multiline', r'foo\nbar', 'f1', 'f2', 'f3'))
4545
out = cap_out.get()
4646
assert ret == 1
47-
assert out == 'f1:0-7:foo\nbar\n'
47+
assert out == 'f1:1:foo\nbar\n'
4848

4949

50-
def test_null_data_dotall_flag_is_enabled(some_files, cap_out):
51-
ret = pygrep.main(('--null-data', r'o.*bar', 'f1', 'f2', 'f3'))
50+
def test_multiline_line_number(some_files, cap_out):
51+
ret = pygrep.main(('--multiline', r'ar', 'f1', 'f2', 'f3'))
5252
out = cap_out.get()
5353
assert ret == 1
54-
assert out == 'f1:1-7:oo\nbar\n'
54+
assert out == 'f1:2:bar\n'
5555

5656

57-
def test_null_data_multiline_flag_is_enabled(some_files, cap_out):
58-
ret = pygrep.main(('--null-data', r'foo$.*bar', 'f1', 'f2', 'f3'))
57+
def test_multiline_dotall_flag_is_enabled(some_files, cap_out):
58+
ret = pygrep.main(('--multiline', r'o.*bar', 'f1', 'f2', 'f3'))
5959
out = cap_out.get()
6060
assert ret == 1
61-
assert out == 'f1:0-7:foo\nbar\n'
61+
assert out == 'f1:1:foo\nbar\n'
62+
63+
64+
def test_multiline_multiline_flag_is_enabled(some_files, cap_out):
65+
ret = pygrep.main(('--multiline', r'foo$.*bar', 'f1', 'f2', 'f3'))
66+
out = cap_out.get()
67+
assert ret == 1
68+
assert out == 'f1:1:foo\nbar\n'

0 commit comments

Comments
 (0)