# git grep -P is multiline for negative lookahead/behind

4 messages from 2016-08-01 to 2016-08-04. Participants: Michael Giuffrida, Junio C Hamano.
Thread: https://gitlist.dev/t/42983

## Michael Giuffrida, 2016-08-01 09:09

Subject: git grep -P is multiline for negative lookahead/behind
Message-ID: <CACi5S_0QGEgnijGyaBeZxOSobdwfA+d-wa-jrHs64Va097mnRQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACi5S_0QGEgnijGyaBeZxOSobdwfA%2Bd-wa-jrHs64Va097mnRQ%40mail.gmail.com

```
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, 2016-08-01 21:35

Subject: Re: git grep -P is multiline for negative lookahead/behind
Message-ID: <xmqqr3a8goka.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqr3a8goka.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CACi5S_0QGEgnijGyaBeZxOSobdwfA+d-wa-jrHs64Va097mnRQ@mail.gmail.com>

```
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, 2016-08-04 18:54

Subject: Re: git grep -P is multiline for negative lookahead/behind
Message-ID: <CACi5S_2j+PXFwE755CSiE01=sYALx-17Hk1k8zJOR9_Fj7y9Pg@mail.gmail.com>
URL: https://gitlist.dev/e/CACi5S_2j%2BPXFwE755CSiE01%3DsYALx-17Hk1k8zJOR9_Fj7y9Pg%40mail.gmail.com
In-Reply-To: <xmqqr3a8goka.fsf@gitster.mtv.corp.google.com>

```
On Mon, Aug 1, 2016 at 2:35 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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, 2016-08-04 22:09

Subject: Re: git grep -P is multiline for negative lookahead/behind
Message-ID: <CAPc5daVPpeCTLbo4t7gRbKbP=BMnhHHtE68uXjanQDS2nKc4dw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPc5daVPpeCTLbo4t7gRbKbP%3DBMnhHHtE68uXjanQDS2nKc4dw%40mail.gmail.com
In-Reply-To: <CACi5S_2j+PXFwE755CSiE01=sYALx-17Hk1k8zJOR9_Fj7y9Pg@mail.gmail.com>

```
On Thu, Aug 4, 2016 at 11:54 AM, Michael Giuffrida
<michaelpg@chromium.org> wrote:
> 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.

```
