threads / bug / 41332

git grep argument parser bug

Subject: git grep argument parser bug

## tl;dr

3 messages between Feb 4, 2016 and Feb 5, 2016.

replies: 2people: 3as markdown or json

Dylan Grafmyre· Feb 4, 2016, 22:02 UTC · lore
In both ubuntu versions of git 1.9.1 and 2.7.0
    git grep '-test'
    git grep '--help'

Or any other expressions literal leading with a single dash or double dash get interpreted as argument flags and not as search expressions.

What I expect is grep results for the literal strings "-test" and "--help" What i get is git help output informing of wrong argument usage, or accidentally turning on flags I didn't expect.

Work around; for afflicted users terminating argument parsing with ` -- ` works as it should.

    git grep -- '-test'

Confirmed on two Ubuntu [based] systems, Ubuntu Server 14.03 and LinuxMint 17.3 [Cinnamon]

-- 
Dylan Grafmyre
Associate System Administrator

Email: DylanG@conversica.com
Office: +1 (888) 778 1004
Web:   www.conversica.com
Junio C Hamano· Feb 4, 2016, 22:18 UTC · re: Dylan Grafmyre · lore

Re: git grep argument parser bug

Dylan Grafmyre <dylang@conversica.com> writes:
Show 9 quoted lines
> In both ubuntu versions of git 1.9.1 and 2.7.0
>
>     git grep '-test'
>     git grep '--help'
>
> Or any other expressions literal leading with a single dash or double
> dash get interpreted as argument flags and not as search expressions.
> ...
> What I expect is grep results for the literal strings "-test" and "--help"
I think you'd need to adjust your expectations, then ;-).
This is how "grep" (not "git grep") works, and you use "-e", i.e.
	grep -e "-anything that begins with a dash" FILES...

to disambiguate. If you are scripting, if you do not know what you have in a variable, and if you are looking for the value in that variable, you would always do

	grep -e "$variable" FILES...
not
        grep "$variable" FILES...

I think all of the above is pretty much the standard practice and "git grep" merely follows suit, i.e.

	git grep -e "-help"
is designed to work that way to match users' expectations.
Duy Nguyen· Feb 5, 2016, 00:22 UTC · re: Dylan Grafmyre · lore

Re: git grep argument parser bug

On Fri, Feb 5, 2016 at 5:02 AM, Dylan Grafmyre <dylang@conversica.com> wrote:
> In both ubuntu versions of git 1.9.1 and 2.7.0
>
>     git grep '-test'
>     git grep '--help'

(Un)Quoting is done by shell and stripped out before "git" is executed. We just don't see them.

Show 11 quoted lines
> Or any other expressions literal leading with a single dash or double
> dash get interpreted as argument flags and not as search expressions.
>
> What I expect is grep results for the literal strings "-test" and "--help"
> What i get is git help output informing of wrong argument usage, or
> accidentally turning on flags I didn't expect.
>
> Work around; for afflicted users terminating argument parsing with `
> -- ` works as it should.
>
>     git grep -- '-test'

Or you can do "git grep -e --test". UNIX grep faces the same problem, and also has -e to deal with it.

> Confirmed on two Ubuntu [based] systems, Ubuntu Server 14.03 and
> LinuxMint 17.3 [Cinnamon]
-- 
Duy

← back to recent threads