Skip to content

Commit

Permalink
grep: support -h (no header) with --count
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Mar 11, 2014
1 parent 9afad7a commit f76d947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
*/
if (opt->count && count) {
char buf[32];
output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
output_sep(opt, ':');
if (opt->pathname) {
output_color(opt, gs->name, strlen(gs->name),
opt->color_filename);
output_sep(opt, ':');
}
snprintf(buf, sizeof(buf), "%u\n", count);
opt->output(opt, buf, strlen(buf));
return 1;
Expand Down
12 changes: 12 additions & 0 deletions t/t7810-grep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ do
grep "a+b*c" $H ab >actual &&
test_cmp expected actual
'

test_expect_success "grep --count $L" '
echo ${HC}ab:3 >expected &&
git grep --count -e b $H -- ab >actual &&
test_cmp expected actual
'

test_expect_success "grep --count -h $L" '
echo 3 >expected &&
git grep --count -h -e b $H -- ab >actual &&
test_cmp expected actual
'
done

cat >expected <<EOF
Expand Down

0 comments on commit f76d947

Please sign in to comment.