grep: convert to use struct pathspec
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 15, 2010 at 22:02 UTC
f34bbc15ce0749a3e9f749f0fef6c486e1482719
1 file changed
+16
-14
builtin/grep.c
+16
-14
@@ -581,7 +581,7 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
581
free(argv);
582
}
583
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)
585
{
586
int hit = 0;
587
int nr;
@@ -591,7 +591,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
591
struct cache_entry *ce = active_cache[nr];
592
if (!S_ISREG(ce->ce_mode))
593
continue;
594
- if (!pathspec_matches(paths, ce->name, opt->max_depth))
594
+ if (!pathspec_matches(pathspec->raw, ce->name, opt->max_depth))
595
continue;
596
/*
597
* 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)
618
return hit;
619
}
620
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,
622
struct tree_desc *tree,
623
const char *tree_name, const char *base)
624
{
@@ -652,7 +652,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
652
strbuf_addch(&pathbuf, '/');
653
654
down = pathbuf.buf + tn_len;
655
- if (!pathspec_matches(paths, down, opt->max_depth))
655
+ if (!pathspec_matches(pathspec->raw, down, opt->max_depth))
656
;
657
else if (S_ISREG(entry.mode))
658
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,
667
die("unable to read tree (%s)",
668
sha1_to_hex(entry.sha1));
669
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);
671
free(data);
672
}
673
if (hit && opt->status_only)
@@ -677,7 +677,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
677
return hit;
678
}
679
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,
681
struct object *obj, const char *name)
682
{
683
if (obj->type == OBJ_BLOB)
@@ -692,14 +692,14 @@ static int grep_object(struct grep_opt *opt, const char **paths,
692
if (!data)
693
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
694
init_tree_desc(&tree, data, size);
695
- hit = grep_tree(opt, paths, &tree, name, "");
695
+ hit = grep_tree(opt, pathspec, &tree, name, "");
696
free(data);
697
return hit;
698
}
699
die("unable to grep from object of type %s", typename(obj->type));
700
}
701
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,
703
const struct object_array *list)
704
{
705
unsigned int i;
@@ -709,7 +709,7 @@ static int grep_objects(struct grep_opt *opt, const char **paths,
709
for (i = 0; i < nr; i++) {
710
struct object *real_obj;
711
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)) {
713
hit = 1;
714
if (opt->status_only)
715
break;
@@ -718,7 +718,7 @@ static int grep_objects(struct grep_opt *opt, const char **paths,
718
return hit;
719
}
720
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)
722
{
723
struct dir_struct dir;
724
int i, hit = 0;
@@ -726,7 +726,7 @@ static int grep_directory(struct grep_opt *opt, const char **paths)
726
memset(&dir, 0, sizeof(dir));
727
setup_standard_excludes(&dir);
728
729
- fill_directory(&dir, paths);
729
+ fill_directory(&dir, pathspec->raw);
730
for (i = 0; i < dir.nr; i++) {
731
hit |= grep_file(opt, dir.entries[i]->name);
732
if (hit && opt->status_only)
@@ -832,6 +832,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
832
struct grep_opt opt;
833
struct object_array list = OBJECT_ARRAY_INIT;
834
const char **paths = NULL;
835
+ struct pathspec pathspec;
836
struct string_list path_list = STRING_LIST_INIT_NODUP;
837
int i;
838
int dummy;
@@ -1059,6 +1060,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1060
paths[0] = prefix;
1061
paths[1] = NULL;
1062
}
1063
+ init_pathspec(&pathspec, paths);
1064
1065
if (show_in_pager && (cached || list.nr))
1066
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)
1091
die("--cached cannot be used with --no-index.");
1092
if (list.nr)
1093
die("--no-index cannot be used with revs.");
1092
- hit = grep_directory(&opt, paths);
1094
+ hit = grep_directory(&opt, &pathspec);
1095
} else if (!list.nr) {
1096
if (!cached)
1097
setup_work_tree();
1098
1097
- hit = grep_cache(&opt, paths, cached);
1099
+ hit = grep_cache(&opt, &pathspec, cached);
1100
} else {
1101
if (cached)
1102
die("both --cached and trees are given.");
1101
- hit = grep_objects(&opt, paths, &list);
1103
+ hit = grep_objects(&opt, &pathspec, &list);
1104
}
1105
1106
if (use_threads)