Skip to content

Commit f34bbc1

Browse files
pcloudsgitster
authored andcommitted
grep: convert to use struct pathspec
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898bbd9 commit f34bbc1

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

builtin/grep.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
581581
free(argv);
582582
}
583583

584-
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
584+
static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int cached)
585585
{
586586
int hit = 0;
587587
int nr;
@@ -591,7 +591,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
591591
struct cache_entry *ce = active_cache[nr];
592592
if (!S_ISREG(ce->ce_mode))
593593
continue;
594-
if (!pathspec_matches(paths, ce->name, opt->max_depth))
594+
if (!pathspec_matches(pathspec->raw, ce->name, opt->max_depth))
595595
continue;
596596
/*
597597
* If CE_VALID is on, we assume worktree file and its cache entry
@@ -618,7 +618,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
618618
return hit;
619619
}
620620

621-
static int grep_tree(struct grep_opt *opt, const char **paths,
621+
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
622622
struct tree_desc *tree,
623623
const char *tree_name, const char *base)
624624
{
@@ -652,7 +652,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
652652
strbuf_addch(&pathbuf, '/');
653653

654654
down = pathbuf.buf + tn_len;
655-
if (!pathspec_matches(paths, down, opt->max_depth))
655+
if (!pathspec_matches(pathspec->raw, down, opt->max_depth))
656656
;
657657
else if (S_ISREG(entry.mode))
658658
hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
@@ -667,7 +667,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
667667
die("unable to read tree (%s)",
668668
sha1_to_hex(entry.sha1));
669669
init_tree_desc(&sub, data, size);
670-
hit |= grep_tree(opt, paths, &sub, tree_name, down);
670+
hit |= grep_tree(opt, pathspec, &sub, tree_name, down);
671671
free(data);
672672
}
673673
if (hit && opt->status_only)
@@ -677,7 +677,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
677677
return hit;
678678
}
679679

680-
static int grep_object(struct grep_opt *opt, const char **paths,
680+
static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
681681
struct object *obj, const char *name)
682682
{
683683
if (obj->type == OBJ_BLOB)
@@ -692,14 +692,14 @@ static int grep_object(struct grep_opt *opt, const char **paths,
692692
if (!data)
693693
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
694694
init_tree_desc(&tree, data, size);
695-
hit = grep_tree(opt, paths, &tree, name, "");
695+
hit = grep_tree(opt, pathspec, &tree, name, "");
696696
free(data);
697697
return hit;
698698
}
699699
die("unable to grep from object of type %s", typename(obj->type));
700700
}
701701

702-
static int grep_objects(struct grep_opt *opt, const char **paths,
702+
static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
703703
const struct object_array *list)
704704
{
705705
unsigned int i;
@@ -709,7 +709,7 @@ static int grep_objects(struct grep_opt *opt, const char **paths,
709709
for (i = 0; i < nr; i++) {
710710
struct object *real_obj;
711711
real_obj = deref_tag(list->objects[i].item, NULL, 0);
712-
if (grep_object(opt, paths, real_obj, list->objects[i].name)) {
712+
if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) {
713713
hit = 1;
714714
if (opt->status_only)
715715
break;
@@ -718,15 +718,15 @@ static int grep_objects(struct grep_opt *opt, const char **paths,
718718
return hit;
719719
}
720720

721-
static int grep_directory(struct grep_opt *opt, const char **paths)
721+
static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec)
722722
{
723723
struct dir_struct dir;
724724
int i, hit = 0;
725725

726726
memset(&dir, 0, sizeof(dir));
727727
setup_standard_excludes(&dir);
728728

729-
fill_directory(&dir, paths);
729+
fill_directory(&dir, pathspec->raw);
730730
for (i = 0; i < dir.nr; i++) {
731731
hit |= grep_file(opt, dir.entries[i]->name);
732732
if (hit && opt->status_only)
@@ -832,6 +832,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
832832
struct grep_opt opt;
833833
struct object_array list = OBJECT_ARRAY_INIT;
834834
const char **paths = NULL;
835+
struct pathspec pathspec;
835836
struct string_list path_list = STRING_LIST_INIT_NODUP;
836837
int i;
837838
int dummy;
@@ -1059,6 +1060,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10591060
paths[0] = prefix;
10601061
paths[1] = NULL;
10611062
}
1063+
init_pathspec(&pathspec, paths);
10621064

10631065
if (show_in_pager && (cached || list.nr))
10641066
die("--open-files-in-pager only works on the worktree");
@@ -1089,16 +1091,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10891091
die("--cached cannot be used with --no-index.");
10901092
if (list.nr)
10911093
die("--no-index cannot be used with revs.");
1092-
hit = grep_directory(&opt, paths);
1094+
hit = grep_directory(&opt, &pathspec);
10931095
} else if (!list.nr) {
10941096
if (!cached)
10951097
setup_work_tree();
10961098

1097-
hit = grep_cache(&opt, paths, cached);
1099+
hit = grep_cache(&opt, &pathspec, cached);
10981100
} else {
10991101
if (cached)
11001102
die("both --cached and trees are given.");
1101-
hit = grep_objects(&opt, paths, &list);
1103+
hit = grep_objects(&opt, &pathspec, &list);
11021104
}
11031105

11041106
if (use_threads)

0 commit comments

Comments
 (0)