summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2d7f98b)
raw | patch | inline | side by side (parent: 2d7f98b)
author | Junio C Hamano <gitster@pobox.com> | |
Mon, 25 Jan 2010 23:37:23 +0000 (15:37 -0800) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Mon, 25 Jan 2010 23:42:55 +0000 (15:42 -0800) |
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>
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 | patch | blob | history |
diff --git a/builtin-grep.c b/builtin-grep.c
index da854fa94f5cc159ea93156ba4c87973ee15cae9..64cdfefdd1f701cdb1dbb7921e82cf16f2440668 100644 (file)
--- a/builtin-grep.c
+++ b/builtin-grep.c
!strcmp(ce->name, active_cache[nr]->name));
nr--; /* compensate for loop control */
}
+ if (hit && opt->status_only)
+ break;
}
free_grep_patterns(opt);
return hit;
hit |= grep_tree(opt, paths, &sub, tree_name, down);
free(data);
}
+ if (hit && opt->status_only)
+ break;
}
strbuf_release(&pathbuf);
return hit;
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;
}
{ 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(""),
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;