Skip to content

Commit f76d947

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9afad7a commit f76d947

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

grep.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,8 +1562,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
15621562
*/
15631563
if (opt->count && count) {
15641564
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+
}
15671570
snprintf(buf, sizeof(buf), "%u\n", count);
15681571
opt->output(opt, buf, strlen(buf));
15691572
return 1;

t/t7810-grep.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,18 @@ do
328328
grep "a+b*c" $H ab >actual &&
329329
test_cmp expected actual
330330
'
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+
'
331343
done
332344

333345
cat >expected <<EOF

0 commit comments

Comments
 (0)