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

[PATCH v6 0/7] Teach '--column' to 'git-grep(1)'

From
Taylor Blau <me@ttaylorr.com>
Date
May 12, 2018, 03:10 UTC
Message-ID
<cover.1526094383.git.me@ttaylorr.com>
In-Reply-To
<20180421034530.GB24606@syl.local>
Hi,

Attached is my sixth re-roll of a series to add '--column' to 'git grep'.

The main change since v5 is supporting --column with queries containing --and, --or, or --not. Previously, I had chosen to die() in this case since there isn't always a good answer to "what is the first column of <complicated expression>?" but have gone back on this for two reasons:

  1. It is important not to regress calls to git-jump/contrib/git-jump
  that contain --and, --or, or --not.
  2. It is not that hard to detect the absence of column data in scripts.
     Likewise, git-jump will happily accept lines with or without
     columnar information, and Vim will accept it as-is.

So, let's support --column and only die() when also given --invert-match. When we don't have a good answer, print nothing.

Thanks, Taylor

Taylor Blau (7):
  Documentation/config.txt: camel-case lineNumber for consistency
  grep.c: expose matched 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 match column in addition to line
 Documentation/config.txt   |  7 ++++++-
 Documentation/git-grep.txt | 10 +++++++++-
 builtin/grep.c             |  4 ++++
 contrib/git-jump/README    | 12 ++++++++++--
 contrib/git-jump/git-jump  |  2 +-
 grep.c                     | 40 +++++++++++++++++++++++++++++---------
 grep.h                     |  2 ++
 t/t7810-grep.sh            | 39 +++++++++++++++++++++++++++++++++++++
 8 files changed, 102 insertions(+), 14 deletions(-)
Inter-diff (since v5):
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index dc8f76ce99..c48a578cb1 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -173,8 +173,9 @@ providing this option will cause it to die.
 	Prefix the line number to matching lines.

 --column::
-	Prefix the 1-indexed byte-offset of the first match on non-context lines. This
-	option is incompatible with '--invert-match', and extended expressions.
+	Prefix the 1-indexed byte-offset of the first match from the start of the
+	matching line. This option is incompatible with '--invert-match', and
+	ignored with expressions using '--and', '--or', '--not'.

 -l::
 --files-with-matches::
diff --git a/grep.c b/grep.c
index 5d904810ad..5ba1b05526 100644
--- a/grep.c
+++ b/grep.c
@@ -1001,9 +1001,6 @@ static void compile_grep_patterns_real(struct grep_opt *opt)
 	else if (!opt->extended && !opt->debug)
 		return;

-	if (opt->columnnum && opt->extended)
-		die(_("--column and extended expressions cannot be combined"));
-
 	p = opt->pattern_list;
 	if (p)
 		opt->pattern_expression = compile_pattern_expr(&p);
@@ -1411,9 +1408,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 	/*
 	 * Treat 'cno' as the 1-indexed offset from the start of a non-context
 	 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
-	 * being called with a context line.
+	 * being called with a context line, or we are --extended, and cannot
+	 * always show an answer.
 	 */
-	if (opt->columnnum && cno) {
+	if (opt->columnnum && sign == ':' && !opt->extended) {
 		char buf[32];
 		xsnprintf(buf, sizeof(buf), "%d", cno);
 		output_color(opt, buf, strlen(buf), opt->color_columnno);
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index aa56b21ed9..491b2e044a 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -110,6 +110,18 @@ do
 		test_cmp expected actual
 	'

+	test_expect_success "grep -w $L (with --column, -C)" '
+		{
+			echo ${HC}file:5:foo mmap bar
+			echo ${HC}file-foo_mmap bar
+			echo ${HC}file:14:foo_mmap bar mmap
+			echo ${HC}file:5:foo mmap bar_mmap
+			echo ${HC}file:14:foo_mmap bar mmap baz
+		} >expected &&
+		git grep --column -w -C1 -e mmap $H >actual &&
+		test_cmp expected actual
+	'
+
 	test_expect_success "grep -w $L (with --line-number, --column)" '
 		{
 			echo ${HC}file:1:5:foo mmap bar
@@ -1617,9 +1629,4 @@ test_expect_success 'grep does not allow --column, --invert-match' '
 	test_i18ngrep "\-\-column and \-\-invert-match cannot be combined" err
 '

-test_expect_success 'grep does not allow --column, extended' '
-	test_must_fail git grep --column --not -e pat 2>err &&
-	test_i18ngrep "\-\-column and extended expressions cannot be combined" err
-'
-
 test_done

--
2.17.0
Previous: Taylor BlauNext: Taylor Blau
Message 91 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.