From 30d00c395e3fb9f104a3f4a85f06a039c989cd8d Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sat, 12 Jun 2010 11:31:18 -0500 Subject: [PATCH] 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 Signed-off-by: Junio C Hamano --- builtin/grep.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index b194ea3cea..5b8879f7b3 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();