{"thread":{"id":"42983","subject":"git grep -P is multiline for negative lookahead/behind","startedAt":"2016-08-01T09:09:32Z","lastAt":"2016-08-04T22:09:47Z","messageCount":4,"participants":["Michael Giuffrida","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"292709","messageId":"CACi5S_0QGEgnijGyaBeZxOSobdwfA+d-wa-jrHs64Va097mnRQ@mail.gmail.com","threadId":"42983","inReplyTo":null,"subject":"git grep -P is multiline for negative lookahead/behind","fromName":"Michael Giuffrida","fromEmail":"michaelpg@chromium.org","sentAt":"2016-08-01T09:09:07Z","receivedAt":"2016-08-01T09:09:32Z","isPatch":false,"sender":{"key":"michaelpg@chromium.org","avatar":null},"body":"Negative lookahead/lookbehind with `git grep -P` considers the\nsurrounding lines of an otherwise positive match. This differs from\n`grep -P` behavior. It does not repro for *positive*\nlookahead/lookbehind.\n\nExample:\n$ echo -e 'Bar\\nBar Baz\\nBat' > test.txt && git add test.txt\n$ git grep -P 'Bar(?!\\sB)' test.txt  # Find 'Bar' not followed by '\\sB'\n\nWith regular grep, this works:\n\n$ grep -P 'Bar(?!\\sB)' test.txt\nBar\n\nBecause \\s includes \\n, the negative lookahead unexpectedly finds the\nB on line 2, and therefore line 1 fails to match the full expression.\n\nPositive lookahead works like `grep -P`, in that it isn't unexpectedly\nmultiline:\n\n$ git grep -P 'Bar(?=\\sB)' test.txt  # Find 'Bar' followed by '\\sB'\ntest.txt:Bar Baz\n$ grep -P 'Bar(?=\\sB)' test.txt\nBar Baz\n\nIs this expected behavior, and if so, why/where is this documented?\n\ngit: git version 2.8.0.rc3.226.g39d4020, also 2.9.2.517.gf8f7adc, both\nwith libpcre\ngrep: grep (GNU grep) 2.16\n"},{"id":"292800","messageId":"xmqqr3a8goka.fsf@gitster.mtv.corp.google.com","threadId":"42983","inReplyTo":"CACi5S_0QGEgnijGyaBeZxOSobdwfA+d-wa-jrHs64Va097mnRQ@mail.gmail.com","subject":"Re: git grep -P is multiline for negative lookahead/behind","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-01T21:35:33Z","receivedAt":"2016-08-01T21:42:29Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Giuffrida <michaelpg@chromium.org> writes:\n\n> Is this expected behavior, and if so, why/where is this documented?\n\nI do not think \"git grep\" was designed to do multi-line anything,\nwith or without lookahead.  If you imagine that the implementation\nattempts its matches line-by-line, does that explain the observed\nsymptom?\n"},{"id":"293119","messageId":"CACi5S_2j+PXFwE755CSiE01=sYALx-17Hk1k8zJOR9_Fj7y9Pg@mail.gmail.com","threadId":"42983","inReplyTo":"xmqqr3a8goka.fsf@gitster.mtv.corp.google.com","subject":"Re: git grep -P is multiline for negative lookahead/behind","fromName":"Michael Giuffrida","fromEmail":"michaelpg@chromium.org","sentAt":"2016-08-04T18:54:32Z","receivedAt":"2016-08-04T18:54:58Z","isPatch":false,"sender":{"key":"michaelpg@chromium.org","avatar":null},"body":"On Mon, Aug 1, 2016 at 2:35 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Michael Giuffrida <michaelpg@chromium.org> writes:\n>\n>> Is this expected behavior, and if so, why/where is this documented?\n>\n> I do not think \"git grep\" was designed to do multi-line anything,\n> with or without lookahead.  If you imagine that the implementation\n> attempts its matches line-by-line, does that explain the observed\n> symptom?\n\nNo. If it worked line-by-line, it would produce more results. It is\nnot producing the expected matches because it *is* considering the\nprevious line in negative lookbehind, when I don't want or expect it\nto. Thus it throws out results that should match.\n"},{"id":"293145","messageId":"CAPc5daVPpeCTLbo4t7gRbKbP=BMnhHHtE68uXjanQDS2nKc4dw@mail.gmail.com","threadId":"42983","inReplyTo":"CACi5S_2j+PXFwE755CSiE01=sYALx-17Hk1k8zJOR9_Fj7y9Pg@mail.gmail.com","subject":"Re: git grep -P is multiline for negative lookahead/behind","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-04T22:09:21Z","receivedAt":"2016-08-04T22:09:47Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"On Thu, Aug 4, 2016 at 11:54 AM, Michael Giuffrida\n<michaelpg@chromium.org> wrote:\n> On Mon, Aug 1, 2016 at 2:35 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>> I do not think \"git grep\" was designed to do multi-line anything,\n>> with or without lookahead.  If you imagine that the implementation\n>> attempts its matches line-by-line, does that explain the observed\n>> symptom?\n>\n> No. If it worked line-by-line, it would produce more results. It is\n> not producing the expected matches because it *is* considering the\n> previous line in negative lookbehind, when I don't want or expect it\n> to. Thus it throws out results that should match.\n\nIf that is the case I do not know what is going on; perhaps\nsomebody more familiar with the pcre codepath can help.\n\nSorry.\n"}]}