Skip to contentSkip to conversation

grep: add result grouping, ack style

PATCH In review René Scharfe 2 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover grep: add result grouping, ack style

ack (http://betterthangrep.com/) has a nice way to display results from multiple files by writing the file name once at the top instead of at the start of each matching line. This short series starts with a fix that is needed anyway and then adds the basic options that enable this display mode.

[PATCH 1/3] grep: fix coloring of hunk marks between files [PATCH 2/3] grep: add --break
[PATCH 3/3] grep: add --heading

 Documentation/git-grep.txt |    7 +++
 builtin/grep.c             |   29 ++++++++++----
 grep.c                     |   26 ++++++++++--
 grep.h                     |    2 +
 t/t7810-grep.sh            |   95 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+), 13 deletions(-)
#1 lore raw

PATCH 1/3 grep: fix coloring of hunk marks between files

Commit 431d6e7b (grep: enable threading for context line printing) split the printing of the "--\n" mark between results from different files out into two places: show_line() in grep.c for the non-threaded case and work_done() in builtin/grep.c for the threaded case. Commit 55f638bd (grep: Colorize filename, line number, and separator) updated the former, but not the latter, so the separators between files are not colored if threads are used.

This patch merges the two. In the threaded case, hunk marks are now printed by show_line() for every file, including the first one, and the very first mark is simply skipped in work_done(). This ensures that the output is properly colored and works just as well.

---
 builtin/grep.c  |   23 ++++++++++++++++-------
 grep.c          |   15 ++++++++++++---
 t/t7810-grep.sh |   30 ++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 10 deletions(-)
builtin/grep.c +16 -7
index 871afaa..0d5a90b 100644
@@ -93,8 +93,7 @@ static pthread_cond_t cond_write;
 /* Signalled when we are finished with everything. */
 static pthread_cond_t cond_result;
 
-static int print_hunk_marks_between_files;
-static int printed_something;
+static int skip_first_line;
 
 static void add_work(enum work_type type, char *name, void *id)
 {
@@ -160,10 +159,20 @@ static void work_done(struct work_item *w)
 	    todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
 		w = &todo[todo_done];
 		if (w->out.len) {
-			if (print_hunk_marks_between_files && printed_something)
-				write_or_die(1, "--\n", 3);
-			write_or_die(1, w->out.buf, w->out.len);
-			printed_something = 1;
+			const char *p = w->out.buf;
+			size_t len = w->out.len;
+
+			/* Skip the leading hunk mark of the first file. */
+			if (skip_first_line) {
+				while (len) {
+					len--;
+					if (*p++ == '\n')
+						break;
+				}
+				skip_first_line = 0;
+			}
+
+			write_or_die(1, p, len);
 		}
 		free(w->name);
 		free(w->identifier);
@@ -968,7 +977,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
 	if (use_threads) {
 		if (opt.pre_context || opt.post_context)
-			print_hunk_marks_between_files = 1;
+			skip_first_line = 1;
 		start_threads(&opt);
 	}
 #else
grep.c +12 -3
index d03d9e2..3f15085 100644
@@ -941,9 +941,18 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 	if (!opt->output)
 		opt->output = std_output;
 
-	if (opt->last_shown && (opt->pre_context || opt->post_context) &&
-	    opt->output == std_output)
-		opt->show_hunk_mark = 1;
+	if (opt->pre_context || opt->post_context) {
+		/* Show hunk marks, except for the first file. */
+		if (opt->last_shown)
+			opt->show_hunk_mark = 1;
+		/*
+		 * If we're using threads then we can't easily identify
+		 * the first file.  Always put hunk marks in that case
+		 * and skip the very first one later in work_done().
+		 */
+		if (opt->output != std_output)
+			opt->show_hunk_mark = 1;
+	}
 	opt->last_shown = 0;
 
 	switch (opt->binary) {
t/t7810-grep.sh +30 -0
index 69bd576..539a8fe 100755
@@ -716,4 +716,34 @@ test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
 	test_cmp expected actual
 '
 
+test_config() {
+	git config "$1" "$2" &&
+	test_when_finished "git config --unset $1"
+}
+
+cat >expected <<EOF
+hello.c<RED>:<RESET>int main(int argc, const char **argv)
+hello.c<RED>-<RESET>{
+<RED>--<RESET>
+hello.c<RED>:<RESET>	/* char ?? */
+hello.c<RED>-<RESET>}
+<RED>--<RESET>
+hello_world<RED>:<RESET>Hello_world
+hello_world<RED>-<RESET>HeLLo_world
+EOF
+
+test_expect_success 'grep --color, separator' '
+	test_config color.grep.context		normal &&
+	test_config color.grep.filename		normal &&
+	test_config color.grep.function		normal &&
+	test_config color.grep.linenumber	normal &&
+	test_config color.grep.match		normal &&
+	test_config color.grep.selected		normal &&
+	test_config color.grep.separator	red &&
+
+	git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
+	test_decode_color >actual &&
+	test_cmp expected actual
+'
+
 test_done
Signature · 1 line
1.7.6.rc0.3.ga805ac
#2 lore raw

PATCH 2/3 grep: add --break

In reply to René Scharfe

With --break, an empty line is printed between matches from different files, increasing readability. This option is taken from ack
(http://betterthangrep.com/).

---
 Documentation/git-grep.txt |    3 +++
 builtin/grep.c             |    4 +++-
 grep.c                     |    7 +++++--
 grep.h                     |    1 +
 t/t7810-grep.sh            |   28 ++++++++++++++++++++++++++++
 5 files changed, 40 insertions(+), 3 deletions(-)
Documentation/git-grep.txt +3 -0
index e150c77..dea7cad 100644
@@ -148,6 +148,9 @@ OPTIONS
 	gives the default to color output.
 	Same as `--color=never`.
 
+--break::
+	Print an empty line between matches from different files.
+
 -[ABC] <context>::
 	Show `context` trailing (`A` -- after), or leading (`B`
 	-- before), or both (`C` -- context) lines, and place a
builtin/grep.c +3 -1
index 0d5a90b..42bb87f 100644
@@ -822,6 +822,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN('c', "count", &opt.count,
 			"show the number of matches instead of matching lines"),
 		OPT__COLOR(&opt.color, "highlight matches"),
+		OPT_BOOLEAN(0, "break", &opt.file_break,
+			"print empty line between matches from different files"),
 		OPT_GROUP(""),
 		OPT_CALLBACK('C', NULL, &opt, "n",
 			"show <n> context lines before and after matches",
@@ -976,7 +978,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		use_threads = 0;
 
 	if (use_threads) {
-		if (opt.pre_context || opt.post_context)
+		if (opt.pre_context || opt.post_context || opt.file_break)
 			skip_first_line = 1;
 		start_threads(&opt);
 	}
grep.c +5 -2
index 3f15085..b0b860a 100644
@@ -721,7 +721,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 	int rest = eol - bol;
 	char *line_color = NULL;
 
-	if (opt->pre_context || opt->post_context) {
+	if (opt->file_break && opt->last_shown == 0) {
+		if (opt->show_hunk_mark)
+			opt->output(opt, "\n", 1);
+	} else if (opt->pre_context || opt->post_context) {
 		if (opt->last_shown == 0) {
 			if (opt->show_hunk_mark) {
 				output_color(opt, "--", 2, opt->color_sep);
@@ -941,7 +944,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 	if (!opt->output)
 		opt->output = std_output;
 
-	if (opt->pre_context || opt->post_context) {
+	if (opt->pre_context || opt->post_context || opt->file_break) {
 		/* Show hunk marks, except for the first file. */
 		if (opt->last_shown)
 			opt->show_hunk_mark = 1;
grep.h +1 -0
index cd055cd..638bee8 100644
@@ -110,6 +110,7 @@ struct grep_opt {
 	unsigned post_context;
 	unsigned last_shown;
 	int show_hunk_mark;
+	int file_break;
 	void *priv;
 
 	void (*output)(struct grep_opt *opt, const void *data, size_t size);
t/t7810-grep.sh +28 -0
index 539a8fe..f55793e 100755
@@ -746,4 +746,32 @@ test_expect_success 'grep --color, separator' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+hello.c:	/* char ?? */
+
+hello_world:Hello_world
+EOF
+
+test_expect_success 'grep --break' '
+	git grep --break -e char -e lo_w hello.c hello_world >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+hello.c-{
+--
+hello.c:	/* char ?? */
+hello.c-}
+
+hello_world:Hello_world
+hello_world-HeLLo_world
+EOF
+
+test_expect_success 'grep --break with context' '
+	git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
+	test_cmp expected actual
+'
+
 test_done
Signature · 1 line
1.7.6.rc0.3.ga805ac
#3 lore raw

PATCH 3/3 grep: add --heading

In reply to René Scharfe

With --heading, the filename is printed once before matches from that file instead of at the start of each line, giving more screen space to the actual search results.

This option is taken from ack (http://betterthangrep.com/). And now git grep can dress up like it:

	$ git config alias.ack "grep --break --heading --line-number"
	$ git ack -e --heading
	Documentation/git-grep.txt
	154:--heading::
	t/t7810-grep.sh
	785:test_expect_success 'grep --heading' '
	786:    git grep --heading -e char -e lo_w hello.c hello_world >actual &&
	808:    git grep --break --heading -n --color \
---
 Documentation/git-grep.txt |    4 ++++
 builtin/grep.c             |    2 ++
 grep.c                     |    6 +++++-
 grep.h                     |    1 +
 t/t7810-grep.sh            |   37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 49 insertions(+), 1 deletions(-)
Documentation/git-grep.txt +4 -0
index dea7cad..07b3c6a 100644
@@ -151,6 +151,10 @@ OPTIONS
 --break::
 	Print an empty line between matches from different files.
 
+--heading::
+	Show the filename above the matches in that file instead of
+	at the start of each shown line.
+
 -[ABC] <context>::
 	Show `context` trailing (`A` -- after), or leading (`B`
 	-- before), or both (`C` -- context) lines, and place a
builtin/grep.c +2 -0
index 42bb87f..cccf8da 100644
@@ -824,6 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT__COLOR(&opt.color, "highlight matches"),
 		OPT_BOOLEAN(0, "break", &opt.file_break,
 			"print empty line between matches from different files"),
+		OPT_BOOLEAN(0, "heading", &opt.heading,
+			"show filename only once above matches from same file"),
 		OPT_GROUP(""),
 		OPT_CALLBACK('C', NULL, &opt, "n",
 			"show <n> context lines before and after matches",
grep.c +5 -1
index b0b860a..04e9ba4 100644
@@ -735,9 +735,13 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 			opt->output(opt, "\n", 1);
 		}
 	}
+	if (opt->heading && opt->last_shown == 0) {
+		output_color(opt, name, strlen(name), opt->color_filename);
+		opt->output(opt, "\n", 1);
+	}
 	opt->last_shown = lno;
 
-	if (opt->pathname) {
+	if (!opt->heading && opt->pathname) {
 		output_color(opt, name, strlen(name), opt->color_filename);
 		output_sep(opt, sign);
 	}
grep.h +1 -0
index 638bee8..c568297 100644
@@ -111,6 +111,7 @@ struct grep_opt {
 	unsigned last_shown;
 	int show_hunk_mark;
 	int file_break;
+	int heading;
 	void *priv;
 
 	void (*output)(struct grep_opt *opt, const void *data, size_t size);
t/t7810-grep.sh +37 -0
index f55793e..1227fa6 100755
@@ -774,4 +774,41 @@ test_expect_success 'grep --break with context' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+hello.c
+int main(int argc, const char **argv)
+	/* char ?? */
+hello_world
+Hello_world
+EOF
+
+test_expect_success 'grep --heading' '
+	git grep --heading -e char -e lo_w hello.c hello_world >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<EOF
+<BOLD;GREEN>hello.c<RESET>
+2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
+6:	/* <BLACK;BYELLOW>char<RESET> ?? */
+
+<BOLD;GREEN>hello_world<RESET>
+3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
+EOF
+
+test_expect_success 'mimic ack-grep --group' '
+	test_config color.grep.context		normal &&
+	test_config color.grep.filename		"bold green" &&
+	test_config color.grep.function		normal &&
+	test_config color.grep.linenumber	normal &&
+	test_config color.grep.match		"black yellow" &&
+	test_config color.grep.selected		normal &&
+	test_config color.grep.separator	normal &&
+
+	git grep --break --heading -n --color \
+		-e char -e lo_w hello.c hello_world |
+	test_decode_color >actual &&
+	test_cmp expected actual
+'
+
 test_done
Signature · 1 line
1.7.6.rc0.3.ga805ac
#4 lore raw

Re: [PATCH 3/3] grep: add --heading

Quoted 6 lines · René Scharfe <rene.scharfe <at> lsrfire.ath.cx> writes:

This option is taken from ack (http://betterthangrep.com/). And now git grep can dress up like it:

	$ git config alias.ack "grep --break --heading --line-number"
Quoted 9 lines
	$ git ack -e --heading
	Documentation/git-grep.txt
	154:--heading::
	t/t7810-grep.sh
	785:test_expect_success 'grep --heading' '
	786:    git grep --heading -e char -e lo_w hello.c hello_world >actual &&
	808:    git grep --break --heading -n --color \

Nice! I really missed that.

You can also add --perl-regexp (since 1.7.6-rc0) to alias.ack to achive more compability with ack (although it uses Perl regexes, not PCRE).