Skip to content

Commit 30d00c3

Browse files
jrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c42e39 commit 30d00c3

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

builtin/grep.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,25 @@ static int grep_object(struct grep_opt *opt, const char **paths,
675675
die("unable to grep from object of type %s", typename(obj->type));
676676
}
677677

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+
678697
static int grep_directory(struct grep_opt *opt, const char **paths)
679698
{
680699
struct dir_struct dir;
@@ -1024,16 +1043,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10241043

10251044
if (cached)
10261045
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);
10371047

10381048
if (use_threads)
10391049
hit |= wait_all();

0 commit comments

Comments
 (0)