grep: support -h (no header) with --count

Suppress printing the header (filename) with -h even if in -c/--count mode. GNU grep and OpenBSD's grep do the same. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 11, 2014 at 22:15 UTC f76d947ae13ca66790a305a58764ae50ea7442d9
2 files changed +17 -2
grep.c
+5 -2
@@ -1562,8 +1562,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1562 */
1563 if (opt->count && count) {
1564 char buf[32];
1565 - output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
1566 - output_sep(opt, ':');
1565 + if (opt->pathname) {
1566 + output_color(opt, gs->name, strlen(gs->name),
1567 + opt->color_filename);
1568 + output_sep(opt, ':');
1569 + }
1570 snprintf(buf, sizeof(buf), "%u\n", count);
1571 opt->output(opt, buf, strlen(buf));
1572 return 1;
t/t7810-grep.sh
+12
@@ -328,6 +328,18 @@ do
328 grep "a+b*c" $H ab >actual &&
329 test_cmp expected actual
330 '
331 +
332 + test_expect_success "grep --count $L" '
333 + echo ${HC}ab:3 >expected &&
334 + git grep --count -e b $H -- ab >actual &&
335 + test_cmp expected actual
336 + '
337 +
338 + test_expect_success "grep --count -h $L" '
339 + echo 3 >expected &&
340 + git grep --count -h -e b $H -- ab >actual &&
341 + test_cmp expected actual
342 + '
343 done
344
345 cat >expected <<EOF