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

[PATCH] grep: expose "status-only" feature via -q

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 25, 2010, 23:37 UTC
Message-ID
<7vk4v53gek.fsf@alter.siamese.dyndns.org>

Teach "git grep" a new "-q" option to report the presense of a match via its exit status without showing any output, similar to how "grep -q" works. Internally "grep" engine already knew this "status-only" mode of operation because it needed to grep inside log message to filter commits when called from the "git log" machinery, and this patch only exposes it to the command line tool.

A somewhat unfair benchmark in the Linux kernel directory shows a dramatic improvement:

    (with patch)
    $ time ../git.git/git grep -q linux HEAD ; echo $?
    real    0m0.030s
    user    0m0.004s
    sys     0m0.004s
    0
    (without patch)
    $ time git grep linux HEAD >/dev/null; echo $?
    real    0m4.432s
    user    0m4.272s
    sys     0m0.076s
    0

This is "somewhat unfair" because I knew a file with such a string comes very early in the tree traversal (namely, ".gitignore").

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-grep.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index da854fa..64cdfef 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -236,6 +236,8 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 				 !strcmp(ce->name, active_cache[nr]->name));
 			nr--; /* compensate for loop control */
 		}
+		if (hit && opt->status_only)
+			break;
 	}
 	free_grep_patterns(opt);
 	return hit;
@@ -293,6 +295,8 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
 			hit |= grep_tree(opt, paths, &sub, tree_name, down);
 			free(data);
 		}
+		if (hit && opt->status_only)
+			break;
 	}
 	strbuf_release(&pathbuf);
 	return hit;
@@ -329,8 +333,11 @@ static int grep_directory(struct grep_opt *opt, const char **paths)
 	setup_standard_excludes(&dir);
 
 	fill_directory(&dir, paths);
-	for (i = 0; i < dir.nr; i++)
+	for (i = 0; i < dir.nr; i++) {
 		hit |= grep_file(opt, dir.entries[i]->name);
+		if (hit && opt->status_only)
+			break;
+	}
 	free_grep_patterns(opt);
 	return hit;
 }
@@ -505,6 +512,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		{ OPTION_CALLBACK, ')', NULL, &opt, NULL, "",
 		  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,
 		  close_callback },
+		OPT_BOOLEAN('q', "quick", &opt.status_only,
+			    "indicate hit with exit status without output"),
 		OPT_BOOLEAN(0, "all-match", &opt.all_match,
 			"show only matches from files that match all patterns"),
 		OPT_GROUP(""),
@@ -628,8 +637,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	for (i = 0; i < list.nr; i++) {
 		struct object *real_obj;
 		real_obj = deref_tag(list.objects[i].item, NULL, 0);
-		if (grep_object(&opt, paths, real_obj, list.objects[i].name))
+		if (grep_object(&opt, paths, real_obj, list.objects[i].name)) {
 			hit = 1;
+			if (opt.status_only)
+				break;
+		}
 	}
 	free_grep_patterns(&opt);
 	return !hit;
Next: Stephen Boyd
Message 1 of 3 in “grep: expose "status-only" feature via -q”
  1. grep: expose "status-only" feature via -qJunio C Hamano, Jan 25, 2010
  2. Stephen BoydJan 28, 2010
  3. Junio C HamanoJan 28, 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.