git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git-grep: option parsing conflicts with prefix-dash searches

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 5, 2010, 23:31 UTC
Message-ID
<7vsk9fs1j9.fsf@alter.siamese.dyndns.org>
In-Reply-To
<alpine.LSU.2.01.1002052351060.30204@obet.zrqbmnf.qr>
Jan Engelhardt <jengelh@medozas.de> writes:
Show 10 quoted lines
> Just about now I wanted to grep for accesses of a particular struct 
> member. Needless to say that it was not a very amusing experience.
> I would expect that (1) probably fails:
>
> (1)	$ git grep '->cnt' net/ipv4/netfilter/
> 	error: unknown switch `>'
>
> So far so good, seems reasonable and matches what I would expect from 
> most other userspace tools. So let's add -- to terminate the option 
> list:

Also you can say "grep -e '->cnt'". Not just "git grep" but regular grep understands this, too.

> (2)	$ git grep -- '->cnt' net/ipv4/netfilter/
> 	fatal: bad flag '->cnt' used after filename
>
> *bzzt*.

This indeed is bzzt, especially if you had a file called "./->cnt" in the work tree. That would mean that you cannot tell the command to look for a pattern in the work tree.

But because you are not giving anything before "--", that "git grep" is not looking for anything. Indeed, (2) is a user error. If you try this:

        $ git grep a -- '->cnt' net/ipv4/netfilter/

does do what the command line specifies: Look for a pattern "a" in files whose names match given pathspecs ('->cnt' or 'net/ipv4/netfilter/').

> What works is (3).
>
> (3)	$ git grep -- -- '->cnt' net/ipv4/netfilter/
Huh?  Now I am lost.  Weren't you looking for a pattern "->cnt"?

And if this command looks for and finds the string '->cnt' in files whose path match net/ipv4/netfilter/ pathspec, I would say it _is_ a bug.

The command line looks for "--" (the first one) as a pattern, and interprets the second "--" as your attempt to tell git that '->cnt' is not an option but is a pathspec. So it looks for a pattern "--" in files whose names match given pathspecs( again '->cnt' or 'net/ipv4/netfilter/').

> But it almost looks like Morse code.

Indeed. But did (3) really work? I tried it myself in a copy of the kernel repository, and it found lines that contain '--' in files whose names match net/ipv4/netfilter/ pathspec, as my copy of the kernel source does not have a file '->cnt' at all.

Previous: Junio C HamanoNext: Jeff King
Message 5 of 12 in “git-grep: option parsing conflicts with prefix-dash searches”
  1. Jan EngelhardtFeb 5, 2010
  2. Jan EngelhardtFeb 5, 2010
  3. Santi BéjarFeb 5, 2010
  4. Junio C HamanoFeb 5, 2010
  5. Junio C HamanoFeb 5, 2010
  6. Jeff KingFeb 6, 2010
  7. Junio C HamanoFeb 6, 2010
  8. Miles BaderFeb 6, 2010
  9. Jeff KingFeb 6, 2010
  10. Junio C HamanoFeb 6, 2010
  11. Jeff KingFeb 7, 2010
  12. Junio C HamanoFeb 8, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.