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

Re: [PATCH v5 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)'

From
Taylor Blau <me@ttaylorr.com>
Date
May 18, 2018, 21:50 UTC
Message-ID
<20180518215021.GA36543@syl.local>
In-Reply-To
<xmqqh8n54gwv.fsf@gitster-ct.c.googlers.com>

Thanks for your thoughtful response. I answered in detail below, but I think that we're in agreement about the semantics, with a few corrections on my part. I'd like to push forward with this series, including the proposed changes below, but feel that sending it as v7 would be asking too much of a reviewer. Would it be OK if I sent this a new series entirely and we abandon this thread?

On Fri, May 18, 2018 at 03:27:44PM +0900, Junio C Hamano wrote:
Show 8 quoted lines
> Taylor Blau <me@ttaylorr.com> writes:
>
> >   * `git grep --and -e foo -e bar`. This binary operation should recur
> >     on its sub-expressions and take the minimum of the starting offset
> >     and the maximum of the ending offset.
>
> We use infix notation, so the above is "git grep -e foo --and -e
> bar" actually ;-).
Thanks for catching that :-).
Show 6 quoted lines
> But you raise an interesting point.  A line with "hello foo bar baz"
> on it would match, so does a line with "goodbye bar baz foo", as
> both of them hits pattern "foo" *and* pattern "bar".  It is not
> quite clear what it means to "show the first hit on the line".  One
> interpretation would be to take the minimum span that makes both
> sides of "--and" happy (your "minimum of start, maximum of end").

It's funny you should mention: this was nearly the exact phrase I used when speaking with Peff.

Show 5 quoted lines
> Another might be to pick "foo" in the first and "bar" in the second
> line, as that is the "first hit" on the line, which is consistent
> with how "git grep -e foo" would say about "a foo b foo c foo" (I
> expect that the leftmost "foo" would be the first hit).  So there
> may be multiple, equally plausible answer to the question.

This is the largest fact in my mind pertaining to this discussion: there are probably many different interpretations of semantics for this, all equally valid in their own way. I am partial to the minimum substring interpretation (which follows naturally from the minimum-start, maximum-end idea), accepting the shortcoming that `--or` sometimes doesn't ``do the right thing.''

Show 6 quoted lines
> >     For inputs of the form "foobar" and "foo bar", it will do the right
> >     thing (give the starting and ending offset for "foobar" and give no
> >     match, respectively).
>
> I think I agree with "foobar", but I do not understand why there is
> no match for "foo bar".

Ah, I think this is my mistake -- when I wrote this note last night. The case of `-e foo --and -e bar` should clearly match both `foo bar` _and_ `foobar`.

Show 9 quoted lines
> >   * `git grep --or -e foo -e bar`. This is the most complicated case, in
> >     my opinion. In going with the min/max idea in the and case above, I
> >     think that `--or` should also min/max its sub-expressions, but in
> >     fact we short-circuit evaluating the second sub-expression when we
> >     find a match for the first.
>
> I am not sure I follow.  "git grep -e foo --or -e bar" is just a
> longhand for "git grep -e foo -e bar".  Shouldn't it highlight
> whichever between foo and bar that appears leftmost on the line?

I don't believe that the two are treated the same, but I think that this is another case where I was incorrect in my judgement of the implementation last night. In fact, the only time when we _don't_ recur on both sub-expressions of `--or` is when 'collect_hits' is zero. That's fine, I believe.

Show 16 quoted lines
> >     So, in cases like matching `--or -e foo -e bar` with "foo baz bar",
> >     we'll do the right thing, since `foo` is the first sub-expression
> >     and happens to be the left-most match. In other words, we __adhere
> >     to our answer with the left-most match first__ semantics, but only
> >     because __the first sub-expression is the left-most match__.
> >
> >     In the other case where we try and match the same expression against
> >     "bar baz foo", we'll return the starting offset of "foo", even
> >     though it isn't the left-most match, violating our semantics.
>
> I am not sure why you think your min-starting/max-ending would lead
> to such a conclusion.  'foo baz bar' would be covered in its
> entirety, 'bar baz foo' would also, as starting of hits with pattern
> 'foo' and pattern 'bar' would be 'b' in 'bar' on that three-word
> line, and ending of hits with these two patterns would be the last
> 'o' in 'foo' on the line.

Right, I think with the understanding in my last stanza of this response ("I don't believe that ...") this issue is resolved, and the min-starting/max-ending _will_ do the right thing.

> I'd expect that a line 'foo baz bar' matched against "-e foo --or -e
> bar" would say "among three words on me, 'f' in foo is the first
> location of the match", though.
I would, too.

Thanks, Taylor

Previous: Junio C HamanoNext: Taylor Blau
Message 85 of 108 in “grep.c: take regmatch_t as argument in match_line()”
  1. 1/6 grep.c: take regmatch_t as argument in match_line()Taylor Blau, Apr 21, 2018
  2. 0/6 Teach '--column-number' to 'git-grep(1)'Taylor Blau, Apr 22, 2018
  3. 1/6 grep.c: take regmatch_t as argument in match_line()Taylor Blau, Apr 22, 2018
  4. Eric SunshineApr 22, 2018
  5. Taylor BlauApr 22, 2018
  6. 2/6 grep.c: take column number as argument to show_line()Taylor Blau, Apr 22, 2018
  7. Eric SunshineApr 23, 2018
  8. Taylor BlauApr 23, 2018
  9. Eric SunshineApr 23, 2018
  10. Ævar Arnfjörð BjarmasonApr 23, 2018
  11. Eric SunshineApr 23, 2018
  12. Taylor BlauApr 24, 2018
  13. Ævar Arnfjörð BjarmasonApr 23, 2018
  14. Taylor BlauApr 24, 2018
  15. Junio C HamanoApr 24, 2018
  16. Taylor BlauApr 24, 2018
  17. 3/6 grep.[ch]: teach columnnum, color_columnno to grep_optTaylor Blau, Apr 22, 2018
  18. Ævar Arnfjörð BjarmasonApr 22, 2018
  19. Taylor BlauApr 22, 2018
  20. Eric SunshineApr 23, 2018
  21. Taylor BlauApr 23, 2018
  22. 4/6 grep.c: display column number of first matchTaylor Blau, Apr 22, 2018
  23. Eric SunshineApr 23, 2018
  24. Taylor BlauApr 23, 2018
  25. 5/6 builtin/grep.c: show column numbers via --column-numberTaylor Blau, Apr 22, 2018
  26. Ævar Arnfjörð BjarmasonApr 22, 2018
  27. Taylor BlauApr 22, 2018
  28. Eric SunshineApr 23, 2018
  29. Taylor BlauApr 23, 2018
  30. 6/6 contrib/git-jump/git-jump: use column number when grep-ingTaylor Blau, Apr 22, 2018
  31. Ævar Arnfjörð BjarmasonApr 22, 2018
  32. Taylor BlauApr 22, 2018
  33. Junio C HamanoApr 22, 2018
  34. Taylor BlauApr 22, 2018
  35. Junio C HamanoApr 23, 2018
  36. 0/7 Teach '--column-number' to 'git-grep(1)'Taylor Blau, Apr 24, 2018
  37. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, Apr 24, 2018
  38. 2/7 grep.c: expose matched column in match_line()Taylor Blau, Apr 24, 2018
  39. 4/7 grep.c: display column number of first matchTaylor Blau, Apr 24, 2018
  40. Eric SunshineApr 24, 2018
  41. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, Apr 24, 2018
  42. 7/7 contrib/git-jump/git-jump: jump to match column in addition to lineTaylor Blau, Apr 24, 2018
  43. Eric SunshineApr 24, 2018
  44. Taylor BlauApr 24, 2018
  45. 5/7 builtin/grep.c: add '--column-number' option to 'git-grep(1)'Taylor Blau, Apr 24, 2018
  46. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, Apr 24, 2018
  47. 0/7 Teach '--column' to 'git-grep(1)'Taylor Blau, May 5, 2018
  48. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, May 5, 2018
  49. 2/7 grep.c: expose matched column in match_line()Taylor Blau, May 5, 2018
  50. René ScharfeMay 8, 2018
  51. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, May 5, 2018
  52. 4/7 grep.c: display column number of first matchTaylor Blau, May 5, 2018
  53. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, May 5, 2018
  54. Duy NguyenMay 5, 2018
  55. Taylor BlauMay 7, 2018
  56. Phillip WoodMay 6, 2018
  57. Ævar Arnfjörð BjarmasonMay 6, 2018
  58. Taylor BlauMay 7, 2018
  59. Junio C HamanoMay 7, 2018
  60. Taylor BlauMay 8, 2018
  61. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, May 5, 2018
  62. 7/7 contrib/git-jump/git-jump: jump to match column in addition to lineTaylor Blau, May 5, 2018
  63. Martin ÅgrenMay 6, 2018
  64. Ævar Arnfjörð BjarmasonMay 6, 2018
  65. Taylor BlauMay 7, 2018
  66. 0/7 Teach '--column' to 'git-grep(1)'Taylor Blau, May 9, 2018
  67. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, May 9, 2018
  68. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, May 9, 2018
  69. 4/7 grep.c: display column number of first matchTaylor Blau, May 9, 2018
  70. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, May 9, 2018
  71. 7/7 contrib/git-jump/git-jump: jump to match column in addition to lineTaylor Blau, May 9, 2018
  72. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, May 9, 2018
  73. Phillip WoodMay 9, 2018
  74. Martin ÅgrenMay 9, 2018
  75. Taylor BlauMay 9, 2018
  76. Junio C HamanoMay 10, 2018
  77. René ScharfeMay 10, 2018
  78. Junio C HamanoMay 10, 2018
  79. Taylor BlauMay 12, 2018
  80. Junio C HamanoMay 12, 2018
  81. Taylor BlauMay 12, 2018
  82. Junio C HamanoMay 12, 2018
  83. Taylor BlauMay 18, 2018
  84. Junio C HamanoMay 18, 2018
  85. Taylor BlauMay 18, 2018
  86. Taylor BlauMay 19, 2018
  87. Taylor BlauMay 9, 2018
  88. Duy NguyenMay 9, 2018
  89. Taylor BlauMay 9, 2018
  90. 2/7 grep.c: expose matched column in match_line()Taylor Blau, May 9, 2018
  91. 0/7 Teach '--column' to 'git-grep(1)'Taylor Blau, May 12, 2018
  92. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, May 12, 2018
  93. 2/7 grep.c: expose matched column in match_line()Taylor Blau, May 12, 2018
  94. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, May 12, 2018
  95. 4/7 grep.c: display column number of first matchTaylor Blau, May 12, 2018
  96. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, May 12, 2018
  97. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, May 12, 2018
  98. 7/7 contrib/git-jump/git-jump: jump to match column in addition to lineTaylor Blau, May 12, 2018
  99. 2/6 grep.c: take column number as argument to show_line()Taylor Blau, Apr 21, 2018
  100. 3/6 grep.[ch]: teach columnnum, color_columnno to grep_optTaylor Blau, Apr 21, 2018
  101. Martin ÅgrenApr 21, 2018
  102. 4/6 grep.c: display column number of first matchTaylor Blau, Apr 21, 2018
  103. 5/6 builtin/grep.c: show column numbers via --column-numberTaylor Blau, Apr 21, 2018
  104. Junio C HamanoApr 21, 2018
  105. Junio C HamanoApr 21, 2018
  106. René ScharfeApr 21, 2018
  107. Martin ÅgrenApr 21, 2018
  108. 6/6 contrib/git-jump/git-jump: use column number when grep-ingTaylor Blau, Apr 21, 2018

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.