git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/8] grep: --count over binary

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
May 22, 2010, 21:29 UTC
Message-ID
<4BF84CBF.5000109@lsrfire.ath.cx>
In-Reply-To
<4BF84B9E.7060009@lsrfire.ath.cx>

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>
---
 grep.c                 |    9 ++++-----
 t/t7008-grep-binary.sh |    6 ++++++
 2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/grep.c b/grep.c
index 2a8e879..35c18b7 100644
--- a/grep.c
+++ b/grep.c
@@ -873,6 +873,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 			count++;
 			if (opt->status_only)
 				return 1;
+			if (opt->count)
+				goto next_line;
 			if (binary_match_only) {
 				opt->output(opt, "Binary file ", 12);
 				output_color(opt, name, strlen(name),
@@ -886,16 +888,12 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 			}
 			/* Hit at this line.  If we haven't shown the
 			 * pre-context lines, we would need to show them.
-			 * When asked to do "count", this still show
-			 * the context which is nonsense, but the user
-			 * deserves to get that ;-).
 			 */
 			if (opt->pre_context)
 				show_pre_context(opt, name, buf, bol, lno);
 			else if (opt->funcname)
 				show_funcname_line(opt, name, buf, bol, lno);
-			if (!opt->count)
-				show_line(opt, bol, eol, name, lno, ':');
+			show_line(opt, bol, eol, name, lno, ':');
 			last_hit = lno;
 		}
 		else if (last_hit &&
@@ -939,6 +937,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 		output_sep(opt, ':');
 		snprintf(buf, sizeof(buf), "%u\n", count);
 		opt->output(opt, buf, strlen(buf));
+		return 1;
 	}
 	return !!last_hit;
 }
diff --git a/t/t7008-grep-binary.sh b/t/t7008-grep-binary.sh
index 2320e74..91970ea 100755
--- a/t/t7008-grep-binary.sh
+++ b/t/t7008-grep-binary.sh
@@ -27,6 +27,12 @@ test_expect_success 'git grep -I ina a' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git grep -c ina a' '
+	echo a:1 >expect &&
+	git grep -c ina a >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'git grep -L bar a' '
 	echo a >expect &&
 	git grep -L bar a >actual &&
-- 
1.7.1
Previous: René ScharfeNext: René Scharfe
Message 30 of 35 in “What's cooking extra”
  1. Junio C HamanoMay 19, 2010
  2. A Large Angry SCMMay 19, 2010
  3. Finn Arne GangstadMay 19, 2010
  4. Eyvind BernhardsenMay 19, 2010
  5. Clemens BuchacherMay 22, 2010
  6. Eyvind BernhardsenMay 22, 2010
  7. Clemens BuchacherMay 22, 2010
  8. Eyvind BernhardsenMay 23, 2010
  9. Clemens BuchacherMay 23, 2010
  10. Eyvind BernhardsenMay 23, 2010
  11. Ævar Arnfjörð BjarmasonMay 23, 2010
  12. Clemens BuchacherMay 24, 2010
  13. Dmitry PotapovMay 24, 2010
  14. Eyvind BernhardsenMay 24, 2010
  15. Clemens BuchacherMay 24, 2010
  16. Eyvind BernhardsenMay 24, 2010
  17. Eyvind BernhardsenMay 24, 2010
  18. Clemens BuchacherMay 24, 2010
  19. Eyvind BernhardsenMay 25, 2010
  20. Anthony YoungmanMay 25, 2010
  21. Eyvind BernhardsenJun 7, 2010
  22. Clemens BuchacherMay 25, 2010
  23. Dmitry PotapovMay 24, 2010
  24. Erik Faye-LundMay 24, 2010
  25. Dmitry PotapovMay 24, 2010
  26. Ævar Arnfjörð BjarmasonMay 21, 2010
  27. René ScharfeMay 22, 2010
  28. 1/8 grep: add test script for binary file handlingRené Scharfe, May 22, 2010
  29. 2/8 grep: grep: refactor handling of binary mode optionsRené Scharfe, May 22, 2010
  30. 3/8 grep: --count over binaryRené Scharfe, May 22, 2010
  31. 4/8 grep: --name-only over binaryRené Scharfe, May 22, 2010
  32. 5/8 grep: use memmem() for fixed string searchRené Scharfe, May 22, 2010
  33. 6/8 grep: continue case insensitive fixed string search after NUL charsRené Scharfe, May 22, 2010
  34. 7/8 grep: use REG_STARTEND for all matching if availableRené Scharfe, May 22, 2010
  35. 8/8 grep: support NUL chars in search strings for -FRené Scharfe, May 22, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.