threads / discuss / 42983

git grep -P is multiline for negative lookahead/behind

Subject: git grep -P is multiline for negative lookahead/behind

## tl;dr

4 messages between Aug 1, 2016 and Aug 4, 2016.

replies: 3people: 2as markdown or json

Michael Giuffrida· Aug 1, 2016, 09:09 UTC · lore

Negative lookahead/lookbehind with `git grep -P` considers the surrounding lines of an otherwise positive match. This differs from `grep -P` behavior. It does not repro for *positive* lookahead/lookbehind.

Example: $ echo -e 'Bar\nBar Baz\nBat' > test.txt && git add test.txt $ git grep -P 'Bar(?!\sB)' test.txt # Find 'Bar' not followed by '\sB'

With regular grep, this works:

$ grep -P 'Bar(?!\sB)' test.txt Bar

Because \s includes \n, the negative lookahead unexpectedly finds the B on line 2, and therefore line 1 fails to match the full expression.

Positive lookahead works like `grep -P`, in that it isn't unexpectedly multiline:

$ git grep -P 'Bar(?=\sB)' test.txt # Find 'Bar' followed by '\sB' test.txt:Bar Baz $ grep -P 'Bar(?=\sB)' test.txt Bar Baz

Is this expected behavior, and if so, why/where is this documented?

git: git version 2.8.0.rc3.226.g39d4020, also 2.9.2.517.gf8f7adc, both with libpcre grep: grep (GNU grep) 2.16

Junio C Hamano· Aug 1, 2016, 21:35 UTC · re: Michael Giuffrida · lore

Re: git grep -P is multiline for negative lookahead/behind

Michael Giuffrida <michaelpg@chromium.org> writes:
> Is this expected behavior, and if so, why/where is this documented?

I do not think "git grep" was designed to do multi-line anything, with or without lookahead. If you imagine that the implementation attempts its matches line-by-line, does that explain the observed symptom?

Michael Giuffrida· Aug 4, 2016, 18:54 UTC · re: Junio C Hamano · lore

Re: git grep -P is multiline for negative lookahead/behind

On Mon, Aug 1, 2016 at 2:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 8 quoted lines
> Michael Giuffrida <michaelpg@chromium.org> writes:
>
>> Is this expected behavior, and if so, why/where is this documented?
>
> I do not think "git grep" was designed to do multi-line anything,
> with or without lookahead.  If you imagine that the implementation
> attempts its matches line-by-line, does that explain the observed
> symptom?

No. If it worked line-by-line, it would produce more results. It is not producing the expected matches because it *is* considering the previous line in negative lookbehind, when I don't want or expect it to. Thus it throws out results that should match.

Junio C Hamano· Aug 4, 2016, 22:09 UTC · re: Michael Giuffrida · lore

Re: git grep -P is multiline for negative lookahead/behind

On Thu, Aug 4, 2016 at 11:54 AM, Michael Giuffrida <michaelpg@chromium.org> wrote:

Show 10 quoted lines
> On Mon, Aug 1, 2016 at 2:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> I do not think "git grep" was designed to do multi-line anything,
>> with or without lookahead.  If you imagine that the implementation
>> attempts its matches line-by-line, does that explain the observed
>> symptom?
>
> No. If it worked line-by-line, it would produce more results. It is
> not producing the expected matches because it *is* considering the
> previous line in negative lookbehind, when I don't want or expect it
> to. Thus it throws out results that should match.

If that is the case I do not know what is going on; perhaps somebody more familiar with the pcre codepath can help.

Sorry.

← back to recent threads