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>
Jonathan Nieder committed
Jun 12, 2010 at 11:31 UTC
30d00c395e3fb9f104a3f4a85f06a039c989cd8d
1 file changed
+20
-10
builtin/grep.c
+20
-10
@@ -675,6 +675,25 @@ static int grep_object(struct grep_opt *opt, const char **paths,
675
die("unable to grep from object of type %s", typename(obj->type));
676
}
677
678
+static int grep_objects(struct grep_opt *opt, const char **paths,
679
+ const struct object_array *list)
680
+{
681
+ unsigned int i;
682
+ int hit = 0;
683
+ const unsigned int nr = list->nr;
684
+
685
+ for (i = 0; i < nr; i++) {
686
+ struct object *real_obj;
687
+ real_obj = deref_tag(list->objects[i].item, NULL, 0);
688
+ if (grep_object(opt, paths, real_obj, list->objects[i].name)) {
689
+ hit = 1;
690
+ if (opt->status_only)
691
+ break;
692
+ }
693
+ }
694
+ return hit;
695
+}
696
+
697
static int grep_directory(struct grep_opt *opt, const char **paths)
698
{
699
struct dir_struct dir;
@@ -1024,16 +1043,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1043
1044
if (cached)
1045
die("both --cached and trees are given.");
1027
-
1028
- for (i = 0; i < list.nr; i++) {
1029
- struct object *real_obj;
1030
- real_obj = deref_tag(list.objects[i].item, NULL, 0);
1031
- if (grep_object(&opt, paths, real_obj, list.objects[i].name)) {
1032
- hit = 1;
1033
- if (opt.status_only)
1034
- break;
1035
- }
1036
- }
1046
+ hit = grep_objects(&opt, paths, &list);
1047
1048
if (use_threads)
1049
hit |= wait_all();