@matheusbsbs / matheus-bagliano / commits / c30c10cff1
Fork 0

grep: --count over binary

The intent of showing the message "Binary file xyz matches" for binary files is to avoid annoying users by potentially messing up their terminals by printing control characters. In --count mode, this precaution isn't necessary. Display counts of matches if -c/--count was specified, even if -a was not given. GNU grep does the same. Moving the check for ->count before the code for handling binary file also avoids printing context lines if --count and -[ABC] were used together, so we can remove the part of the comment that mentions this behaviour. Again, GNU grep does the same. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed May 22, 2010 at 23:29 UTC c30c10cff1d640ce119596b907c10cc11f83358d
2 files changed +10 -5
grep.c
+4 -5
@@ -873,6 +873,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
873 count++;
874 if (opt->status_only)
875 return 1;
876 + if (opt->count)
877 + goto next_line;
878 if (binary_match_only) {
879 opt->output(opt, "Binary file ", 12);
880 output_color(opt, name, strlen(name),
@@ -886,16 +888,12 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
888 }
889 /* Hit at this line. If we haven't shown the
890 * pre-context lines, we would need to show them.
889 - * When asked to do "count", this still show
890 - * the context which is nonsense, but the user
891 - * deserves to get that ;-).
891 */
892 if (opt->pre_context)
893 show_pre_context(opt, name, buf, bol, lno);
894 else if (opt->funcname)
895 show_funcname_line(opt, name, buf, bol, lno);
897 - if (!opt->count)
898 - show_line(opt, bol, eol, name, lno, ':');
896 + show_line(opt, bol, eol, name, lno, ':');
897 last_hit = lno;
898 }
899 else if (last_hit &&
@@ -939,6 +937,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
937 output_sep(opt, ':');
938 snprintf(buf, sizeof(buf), "%u\n", count);
939 opt->output(opt, buf, strlen(buf));
940 + return 1;
941 }
942 return !!last_hit;
943 }
t/t7008-grep-binary.sh
+6
@@ -27,6 +27,12 @@ test_expect_success 'git grep -I ina a' '
27 test_cmp expect actual
28 '
29
30 +test_expect_success 'git grep -c ina a' '
31 + echo a:1 >expect &&
32 + git grep -c ina a >actual &&
33 + test_cmp expect actual
34 +'
35 +
36 test_expect_success 'git grep -L bar a' '
37 echo a >expect &&
38 git grep -L bar a >actual &&