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

[PATCH v3 0/7] grep.c: teach --column to 'git-grep(1)'

From
Taylor Blau <me@ttaylorr.com>
Date
Jun 22, 2018, 15:49 UTC
Message-ID
<cover.1529682173.git.me@ttaylorr.com>
In-Reply-To
<cover.1529365072.git.me@ttaylorr.com>
Hi,

Attached is my third--anticipate the final--re-roll of my series to teach 'git grep --column'.

Since the last time, only a couple of things have changed at Peff's suggestions in [1]. The changes are summarized here, and an inter-diff is available below:

  - Change "%zu" to PRIuMAX (and an appropriate cast into uintmax_t). I
    plan to send a follow-up patch to convert this back to "%zu" to see
    how people feel about it, but I wanted to keep that out of the
    present series in order to not hold things up.
  - Don't short-circuit AND when given --column, since an earlier NOT
    higher in the tree may cause an AND to be converted into an OR via
    de Morgan's Law, in which case the problem is reduced to the OR case
    (and should not have been short-circuited in the first place).
  - Add a test in t7810 to cover this behavior (i.e., '--not \( -e x
    --and -e y \)').

Thanks, Taylor

[1]: https://public-inbox.org/git/20180621115302.GB15293@sigill.intra.peff.net/
Taylor Blau (7):
  Documentation/config.txt: camel-case lineNumber for consistency
  grep.c: expose {,inverted} match column in match_line()
  grep.[ch]: extend grep_opt to allow showing matched column
  grep.c: display column number of first match
  builtin/grep.c: add '--column' option to 'git-grep(1)'
  grep.c: add configuration variables to show matched option
  contrib/git-jump/git-jump: jump to exact location
 Documentation/config.txt   |   7 +-
 Documentation/git-grep.txt |   9 ++-
 builtin/grep.c             |   1 +
 contrib/git-jump/README    |  12 +++-
 contrib/git-jump/git-jump  |   2 +-
 grep.c                     | 134 +++++++++++++++++++++++++++++--------
 grep.h                     |   2 +
 t/t7810-grep.sh            |  95 ++++++++++++++++++++++++++
 8 files changed, 228 insertions(+), 34 deletions(-)
Inter-diff (since: v2)
diff --git a/grep.c b/grep.c
index 08d3df2855..992673fe7e 100644
--- a/grep.c
+++ b/grep.c
@@ -1286,11 +1286,17 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
 				     0);
 		break;
 	case GREP_NODE_AND:
-		if (!match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
-				     icol, 0))
-			return 0;
-		h = match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
+		h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
 				    icol, 0);
+		if (h || opt->columnnum) {
+			/*
+			 * Don't short-circuit AND when given --column, since a
+			 * NOT earlier in the tree may turn this into an OR. In
+			 * this case, see the below comment.
+			 */
+			h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
+					     ctx, col, icol, 0);
+		}
 		break;
 	case GREP_NODE_OR:
 		if (!(collect_hits || opt->columnnum)) {
@@ -1447,7 +1453,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 	 */
 	if (opt->columnnum && cno) {
 		char buf[32];
-		xsnprintf(buf, sizeof(buf), "%zu", cno);
+		xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
 		output_color(opt, buf, strlen(buf), opt->color_columnno);
 		output_sep(opt, sign);
 	}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index bf0b572dab..9312c8daf5 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -110,7 +110,7 @@ do
 		test_cmp expected actual
 	'

-	test_expect_success "grep -w $L (with --column, extended)" '
+	test_expect_success "grep -w $L (with --column, extended OR)" '
 		{
 			echo ${HC}file:14:foo_mmap bar mmap
 			echo ${HC}file:19:foo_mmap bar mmap baz
@@ -130,7 +130,7 @@ do
 		test_cmp expected actual
 	'

-	test_expect_success "grep $L (with --column, --invert, extended)" '
+	test_expect_success "grep $L (with --column, --invert, extended OR)" '
 		{
 			echo ${HC}hello_world:6:HeLLo_world
 		} >expected &&
@@ -139,6 +139,17 @@ do
 		test_cmp expected actual
 	'

+	test_expect_success "grep $L (with --column, --invert, extended AND)" '
+		{
+			echo ${HC}hello_world:3:Hello world
+			echo ${HC}hello_world:3:Hello_world
+			echo ${HC}hello_world:6:HeLLo_world
+		} >expected &&
+		git grep --column --invert --not -e _ --and --not -e ll $H -- hello_world \
+			>actual &&
+		test_cmp expected actual
+	'
+
 	test_expect_success "grep $L (with --column, double-negation)" '
 		{
 			echo ${HC}file:1:foo_mmap bar mmap baz

--
2.18.0
Previous: Jeff KingNext: Taylor Blau
Message 46 of 56 in “grep.c: teach --column to 'git-grep(1)'”
  1. 0/7 grep.c: teach --column to 'git-grep(1)'Taylor Blau, Jun 18, 2018
  2. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, Jun 18, 2018
  3. 2/7 grep.c: expose {,inverted} match column in match_line()Taylor Blau, Jun 18, 2018
  4. Junio C HamanoJun 19, 2018
  5. Taylor BlauJun 19, 2018
  6. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, Jun 18, 2018
  7. 4/7 grep.c: display column number of first matchTaylor Blau, Jun 18, 2018
  8. Jeff KingJun 19, 2018
  9. Taylor BlauJun 19, 2018
  10. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, Jun 18, 2018
  11. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, Jun 18, 2018
  12. 7/7 contrib/git-jump/git-jump: jump to exact locationTaylor Blau, Jun 18, 2018
  13. Jeff KingJun 19, 2018
  14. René ScharfeJun 19, 2018
  15. Taylor BlauJun 19, 2018
  16. René ScharfeJun 19, 2018
  17. René ScharfeJun 19, 2018
  18. Jeff KingJun 19, 2018
  19. Taylor BlauJun 19, 2018
  20. Junio C HamanoJun 19, 2018
  21. Taylor BlauJun 19, 2018
  22. Jeff KingJun 19, 2018
  23. Junio C HamanoJun 19, 2018
  24. René ScharfeJun 19, 2018
  25. Jeff KingJun 19, 2018
  26. René ScharfeJun 19, 2018
  27. Junio C HamanoJun 19, 2018
  28. Junio C HamanoJun 19, 2018
  29. Taylor BlauJun 19, 2018
  30. Taylor BlauJun 19, 2018
  31. 0/7 grep.c: teach --column to 'git-grep(1)'Taylor Blau, Jun 20, 2018
  32. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, Jun 20, 2018
  33. 2/7 grep.c: expose {,inverted} match column in match_line()Taylor Blau, Jun 20, 2018
  34. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, Jun 20, 2018
  35. 4/7 grep.c: display column number of first matchTaylor Blau, Jun 20, 2018
  36. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, Jun 20, 2018
  37. 7/7 contrib/git-jump/git-jump: jump to exact locationTaylor Blau, Jun 20, 2018
  38. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, Jun 20, 2018
  39. Jeff KingJun 21, 2018
  40. Jeff KingJun 21, 2018
  41. Johannes SchindelinJun 22, 2018
  42. Jeff KingJun 22, 2018
  43. Junio C HamanoJun 21, 2018
  44. Taylor BlauJun 21, 2018
  45. Jeff KingJun 22, 2018
  46. 0/7 grep.c: teach --column to 'git-grep(1)'Taylor Blau, Jun 22, 2018
  47. 1/7 Documentation/config.txt: camel-case lineNumber for consistencyTaylor Blau, Jun 22, 2018
  48. 2/7 grep.c: expose {,inverted} match column in match_line()Taylor Blau, Jun 22, 2018
  49. 3/7 grep.[ch]: extend grep_opt to allow showing matched columnTaylor Blau, Jun 22, 2018
  50. 4/7 grep.c: display column number of first matchTaylor Blau, Jun 22, 2018
  51. 5/7 builtin/grep.c: add '--column' option to 'git-grep(1)'Taylor Blau, Jun 22, 2018
  52. 7/7 contrib/git-jump/git-jump: jump to exact locationTaylor Blau, Jun 22, 2018
  53. 6/7 grep.c: add configuration variables to show matched optionTaylor Blau, Jun 22, 2018
  54. Jeff KingJun 25, 2018
  55. Taylor BlauJun 25, 2018
  56. Junio C HamanoJun 26, 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.