Teach -f <file> option to builtin-grep.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 8, 2006 at 13:28 UTC
aa8c79ad03264c9e6b184379fb5f4bc34aefe0d6
1 file changed
+42
-19
builtin-grep.c
+42
-19
@@ -82,6 +82,8 @@ static int pathspec_matches(const char **paths, const char *name)
82
83
struct grep_pat {
84
struct grep_pat *next;
85
+ const char *origin;
86
+ int no;
87
const char *pattern;
88
regex_t regexp;
89
};
@@ -105,10 +107,13 @@ struct grep_opt {
107
unsigned post_context;
108
};
109
108
-static void add_pattern(struct grep_opt *opt, const char *pat)
110
+static void add_pattern(struct grep_opt *opt, const char *pat,
111
+ const char *origin, int no)
112
{
113
struct grep_pat *p = xcalloc(1, sizeof(*p));
114
p->pattern = pat;
115
+ p->origin = origin;
116
+ p->no = no;
117
*opt->pattern_tail = p;
118
opt->pattern_tail = &p->next;
119
p->next = NULL;
@@ -121,9 +126,17 @@ static void compile_patterns(struct grep_opt *opt)
126
int err = regcomp(&p->regexp, p->pattern, opt->regflags);
127
if (err) {
128
char errbuf[1024];
129
+ char where[1024];
130
+ if (p->no)
131
+ sprintf(where, "In '%s' at %d, ",
132
+ p->origin, p->no);
133
+ else if (p->origin)
134
+ sprintf(where, "%s, ", p->origin);
135
+ else
136
+ where[0] = 0;
137
regerror(err, &p->regexp, errbuf, 1024);
138
regfree(&p->regexp);
126
- die("'%s': %s", p->pattern, errbuf);
139
+ die("%s'%s': %s", where, p->pattern, errbuf);
140
}
141
}
142
}
@@ -482,7 +495,6 @@ int cmd_grep(int argc, const char **argv, char **envp)
495
{
496
int hit = 0;
497
int no_more_flags = 0;
485
- int seen_noncommit = 0;
498
int cached = 0;
499
struct grep_opt opt;
500
struct object_list *list, **tail, *object_list = NULL;
@@ -598,9 +610,32 @@ int cmd_grep(int argc, const char **argv, char **envp)
610
}
611
continue;
612
}
613
+ if (!strcmp("-f", arg)) {
614
+ FILE *patterns;
615
+ int lno = 0;
616
+ char buf[1024];
617
+ if (argc <= 1)
618
+ usage(builtin_grep_usage);
619
+ patterns = fopen(argv[1], "r");
620
+ if (!patterns)
621
+ die("'%s': %s", strerror(errno));
622
+ while (fgets(buf, sizeof(buf), patterns)) {
623
+ int len = strlen(buf);
624
+ if (buf[len-1] == '\n')
625
+ buf[len-1] = 0;
626
+ /* ignore empty line like grep does */
627
+ if (!buf[0])
628
+ continue;
629
+ add_pattern(&opt, strdup(buf), argv[1], ++lno);
630
+ }
631
+ fclose(patterns);
632
+ argv++;
633
+ argc--;
634
+ continue;
635
+ }
636
if (!strcmp("-e", arg)) {
637
if (1 < argc) {
603
- add_pattern(&opt, argv[1]);
638
+ add_pattern(&opt, argv[1], "-e option", 0);
639
argv++;
640
argc--;
641
continue;
@@ -615,7 +650,7 @@ int cmd_grep(int argc, const char **argv, char **envp)
650
if (!no_more_flags && *arg == '-')
651
usage(builtin_grep_usage);
652
if (!opt.pattern_list) {
618
- add_pattern(&opt, arg);
653
+ add_pattern(&opt, arg, "command line", 0);
654
break;
655
}
656
else {
@@ -656,21 +691,9 @@ int cmd_grep(int argc, const char **argv, char **envp)
691
692
if (!object_list)
693
return !grep_cache(&opt, paths, cached);
659
- /*
660
- * Do not walk "grep -e foo master next pu -- Documentation/"
661
- * but do walk "grep -e foo master..next -- Documentation/".
662
- * Ranged request mixed with a blob or tree object, like
663
- * "grep -e foo v1.0.0:Documentation/ master..next"
664
- * so detect that and complain.
665
- */
666
- for (list = object_list; list; list = list->next) {
667
- struct object *real_obj;
668
- real_obj = deref_tag(list->item, NULL, 0);
669
- if (strcmp(real_obj->type, commit_type))
670
- seen_noncommit = 1;
671
- }
694
+
695
if (cached)
673
- die("both --cached and revisions given.");
696
+ die("both --cached and trees are given.");
697
698
for (list = object_list; list; list = list->next) {
699
struct object *real_obj;