Subject: [PATCH v3 0/7] Teach '--column-number' to 'git-grep(1)'

From: Taylor Blau <[email-masked]>
Date: Mon, 23 Apr 2018 22:07:34 -0700

Hi,

Attached is v3 of my series to teach '--column-numbers' to 'git-grep(1)'.

Since last time, I have:

  * Removed '-m' in a few places that I forgot to during v2 [1] [2].

  * Expanded upon the definition of '--column-number' in 'git grep
    --help'. [3]

  * Initialized some new fields in grep_opt to 0 that would be avoided
    by memset(). [4]

  * Block comment to indicate the desired use of 'cno' in grep.c's
    'show_line()' [5], [6].

  * Annotated the new tests with the permutation that they are testing.

The most notable change since last time is reorganizing the series to
match Eric's suggestion, which I hope makes the reviewing experience a
little easier. I will strive to apply these lessons in future series.

Little has changed on the implementation front since v2, so I think that
the most outstanding further discussion will be centered around --column
vs --column-number. I have posted some thoughts about that in [7].

Thanks as always for your review :-).

Thanks,
Taylor

[1]: https://public-inbox.org/git/[email-masked]
[2]: https://public-inbox.org/git/[email-masked]
[3]: https://public-inbox.org/git/[email-masked]
[4]: https://public-inbox.org/git/[email-masked]
[5]: https://public-inbox.org/git/CAPig+cQ2+wTTXE0mhnGnp2pZug=[email-masked]
[6]: https://public-inbox.org/git/[email-masked]
[7]: https://public-inbox.org/git/[email-masked]

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-number' 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 |  8 +++++++-
 builtin/grep.c             |  1 +
 contrib/git-jump/git-jump  |  2 +-
 grep.c                     | 39 +++++++++++++++++++++++++++++---------
 grep.h                     |  2 ++
 t/t7810-grep.sh            | 22 +++++++++++++++++++++
 7 files changed, 69 insertions(+), 12 deletions(-)

Inter-diff (since v2):

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 1645fcf2ae..8a2893d1e1 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1711,7 +1711,7 @@ grep.lineNumber::
 	If set to true, enable `-n` option by default.

 grep.columnNumber::
-	If set to true, enable `-m` option by default.
+	If set to true, enable the `--column-number` option by default.

 grep.patternType::
 	Set the default matching behavior. Using a value of 'basic', 'extended',
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index b75a039768..c5c4d712e6 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -45,7 +45,7 @@ grep.lineNumber::
 	If set to true, enable `-n` option by default.

 grep.columnNumber::
-	If set to true, enable `-m` option by default.
+	If set to true, enable the `--column-number` option by default.

 grep.patternType::
 	Set the default matching behavior. Using a value of 'basic', 'extended',
diff --git a/builtin/grep.c b/builtin/grep.c
index 23ce97f998..512f60c591 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -829,7 +829,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			    GREP_PATTERN_TYPE_PCRE),
 		OPT_GROUP(""),
 		OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")),
-		OPT_BOOL(0, "column-number", &opt.columnnum, N_("show column numbers")),
+		OPT_BOOL(0, "column-number", &opt.columnnum, N_("show column number of first match")),
 		OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
 		OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
 		OPT_NEGBIT(0, "full-name", &opt.relative,
diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
index 2706963690..8bc57ea0f8 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -52,7 +52,7 @@ mode_merge() {
 # editor shows them to us in the status bar.
 mode_grep() {
 	cmd=$(git config jump.grepCmd)
-	test -n "$cmd" || cmd="git grep -n -m"
+	test -n "$cmd" || cmd="git grep -n --column-number"
 	$cmd "$@" |
 	perl -pe '
 	s/[ \t]+/ /g;
diff --git a/grep.c b/grep.c
index 23250e60d0..7284dec155 100644
--- a/grep.c
+++ b/grep.c
@@ -46,6 +46,7 @@ void init_grep_defaults(void)
 	color_set(opt->color_filename, "");
 	color_set(opt->color_function, "");
 	color_set(opt->color_lineno, "");
+	color_set(opt->color_columnno, "");
 	color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
 	color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
 	color_set(opt->color_selected, "");
@@ -1404,6 +1405,11 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		output_color(opt, buf, strlen(buf), opt->color_lineno);
 		output_sep(opt, sign);
 	}
+	/**
+	 * 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.
+	 */
 	if (opt->columnnum && cno) {
 		char buf[32];
 		xsnprintf(buf, sizeof(buf), "%d", cno);
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 7349c7fadc..bbce57c8b1 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -99,7 +99,7 @@ do
 		test_cmp expected actual
 	'

-	test_expect_success "grep -w $L" '
+	test_expect_success "grep -w $L (with --column-number)" '
 		{
 			echo ${HC}file:5:foo mmap bar
 			echo ${HC}file:14:foo_mmap bar mmap
@@ -110,7 +110,7 @@ do
 		test_cmp expected actual
 	'

-	test_expect_success "grep -w $L" '
+	test_expect_success "grep -w $L (with --{line,column}-number)" '
 		{
 			echo ${HC}file:1:5:foo mmap bar
 			echo ${HC}file:3:14:foo_mmap bar mmap

--
2.17.0