aboutsummaryrefslogtreecommitdiff
path: root/builtin/grep.c
diff options
context:
space:
mode:
authorJonathan Nieder <jrnieder@gmail.com>2010-06-12 11:31:18 -0500
committerJunio C Hamano <gitster@pobox.com>2010-06-13 09:15:09 -0700
commit30d00c395e3fb9f104a3f4a85f06a039c989cd8d (patch)
tree8c587e5e49d288a58f2b5fe95a93e1df3bcf9be0 /builtin/grep.c
parent7c42e390a37a11b1f7a77f5f07c261a8f930663a (diff)
downloadgit-30d00c395e3fb9f104a3f4a85f06a039c989cd8d.tar.gz
git-30d00c395e3fb9f104a3f4a85f06a039c989cd8d.tar.xz
grep: refactor grep_objects loop into its own function
Simplify cmd_grep by splitting off the loop that finds matches in a list of trees. So now the main part of cmd_grep looks like: if (!use_index) { int hit = grep_directory(&opt, paths); if (use_threads) hit |= wait_all(); return !hit; } if (!list.nr) { if (!cached) setup_work_tree(); int hit = grep_cache(&opt, paths, cached); if (use_threads) hit |= wait_all; return !hit; } hit = grep_objects(&opt, path, &list); if (use_threads) hit |= wait_all(); return !hit; and is ripe for further refactoring. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/grep.c')
-rw-r--r--builtin/grep.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index b194ea3ce..5b8879f7b 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -675,6 +675,25 @@ static int grep_object(struct grep_opt *opt, const char **paths,
die("unable to grep from object of type %s", typename(obj->type));
}
+static int grep_objects(struct grep_opt *opt, const char **paths,
+ const struct object_array *list)
+{
+ unsigned int i;
+ int hit = 0;
+ const unsigned int nr = list->nr;
+
+ for (i = 0; i < 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)) {
+ hit = 1;
+ if (opt->status_only)
+ break;
+ }
+ }
+ return hit;
+}
+
static int grep_directory(struct grep_opt *opt, const char **paths)
{
struct dir_struct dir;
@@ -1024,16 +1043,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
if (cached)
die("both --cached and trees are given.");
-
- 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)) {
- hit = 1;
- if (opt.status_only)
- break;
- }
- }
+ hit = grep_objects(&opt, paths, &list);
if (use_threads)
hit |= wait_all();