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

Re: [PATCH v2 2/2] grep: use '/' delimiter for paths

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 7, 2017, 20:24 UTC
Message-ID
<xmqq8tphzr41.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170207150414.GD8583@stefanha-x1.localdomain>
Stefan Hajnoczi <stefanha@redhat.com> writes:
> Perhaps it's better to leave this than to merge code that doesn't work
> correctly 100% of the time.

I am not sure if you are shooting for is "work correctly" to begin with, to be honest. The current code always shows the "correct" output which is "the tree-ish object name (expressed in a way easier to understand by the humans), followed by a colon, followed by the path in the tree-ish the hit lies". You are making it "incorrect but often more convenient", and sometimes that is a worth goal, but for the particular use cases you presented, i.e.

    $ git grep -e "$pattern" "$commit:path"

a more natural way to express "I want to find this pattern in the commit under that path" exists:

    $ git grep -e "$pattern" "$commit" -- path

and because of that, I do not think the former form of the query should happen _less_ often in the first place, which would make it "incorrect but more convenient if the user gives an unusual query".

So I am not sure if the change to "grep" is worth it.

Having said that, I actually think "make it more convenient" without making anything incorrect would be to teach the revision parser to understand

    <any-expression-to-name-a-tree-ish:<path>

as an extended SHA-1 expression to name the blob or the tree at that path in the tree-ish, e.g. if we can make the revision parser to take this

    master:Documentation:git.txt

as the name of the blob object, then the current output is both correct and more convenient. After all, this sample string starts at "master:Documentation" (which is an extended SHA-1 expression to name a tree-ish), followed by a colon, then followed by the path "git.txt" in it, and "grep -e pattern master:Documentation" would show hits in that blob prefixed with it.

I.e.
	T=$(git rev-parse master:Documentation)
	git cat-file blob $T:git.txt

would give you the contents of the source to the Git manual. It is not all that unreasonable to expect

	git cat-file blob master:Documentation:git.txt

to be able to show the same thing as well. You'd need to backtrack the parsing (e.g. attempt to find "Documentation:git.txt" in "master", fail to find any, then fall back to find "git.txt" in "master:Documentation", find one, and be happy, or something like that), and define how to resolve potential ambiguity (e.g. there may indeed be "Documentation:git.txt" and "Documentation/git.txt" in the tree-ish "master"), though.

Previous: Jeff KingNext: Junio C Hamano
Message 9 of 15 in “grep: make output consistent with revision syntax”
  1. 0/2 grep: make output consistent with revision syntaxStefan Hajnoczi, Jan 20, 2017
  2. 1/2 grep: only add delimiter if there isn't one alreadyStefan Hajnoczi, Jan 20, 2017
  3. Junio C HamanoJan 20, 2017
  4. 2/2 grep: use '/' delimiter for pathsStefan Hajnoczi, Jan 20, 2017
  5. Junio C HamanoJan 20, 2017
  6. Brandon WilliamsJan 20, 2017
  7. Stefan HajnocziFeb 7, 2017
  8. Jeff KingFeb 7, 2017
  9. Junio C HamanoFeb 7, 2017
  10. Junio C HamanoFeb 7, 2017
  11. Jeff KingFeb 9, 2017
  12. Junio C HamanoFeb 9, 2017
  13. Jeff KingFeb 9, 2017
  14. Stefan HajnocziFeb 14, 2017
  15. Junio C HamanoJan 20, 2017

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.