builtin-grep: -F (--fixed-strings)
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 9, 2006 at 18:28 UTC
07ea91d84f3c7bb075d4716ee40096e3b12a4c86
2 files changed
+33
-5
Documentation/git-grep.txt
+1
-1
@@ -12,7 +12,7 @@ SYNOPSIS
12
'git-grep' [--cached]
13
[-a | --text] [-I] [-i | --ignore-case] [-w | --word-regexp]
14
[-v | --invert-match]
15
- [-E | --extended-regexp] [-G | --basic-regexp]
15
+ [-E | --extended-regexp] [-G | --basic-regexp] [-F | --fixed-strings]
16
[-n] [-l | --files-with-matches] [-L | --files-without-match]
17
[-c | --count]
18
[-A <post-context>] [-B <pre-context>] [-C <context>]
builtin-grep.c
+32
-4
@@ -98,6 +98,7 @@ struct grep_opt {
98
unsigned unmatch_name_only:1;
99
unsigned count:1;
100
unsigned word_regexp:1;
101
+ unsigned fixed:1;
102
#define GREP_BINARY_DEFAULT 0
103
#define GREP_BINARY_NOMATCH 1
104
#define GREP_BINARY_TEXT 2
@@ -179,6 +180,20 @@ static int buffer_is_binary(const char *ptr, unsigned long size)
180
return 0;
181
}
182
183
+static int fixmatch(const char *pattern, char *line, regmatch_t *match)
184
+{
185
+ char *hit = strstr(line, pattern);
186
+ if (!hit) {
187
+ match->rm_so = match->rm_eo = -1;
188
+ return REG_NOMATCH;
189
+ }
190
+ else {
191
+ match->rm_so = hit - line;
192
+ match->rm_eo = match->rm_so + strlen(pattern);
193
+ return 0;
194
+ }
195
+}
196
+
197
static int grep_buffer(struct grep_opt *opt, const char *name,
198
char *buf, unsigned long size)
199
{
@@ -224,9 +239,14 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
239
*eol = 0;
240
241
for (p = opt->pattern_list; p; p = p->next) {
227
- regex_t *exp = &p->regexp;
228
- hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
229
- pmatch, 0);
242
+ if (!opt->fixed) {
243
+ regex_t *exp = &p->regexp;
244
+ hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
245
+ pmatch, 0);
246
+ }
247
+ else {
248
+ hit = !fixmatch(p->pattern, bol, pmatch);
249
+ }
250
251
if (hit && opt->word_regexp) {
252
/* Match beginning must be either
@@ -549,6 +569,11 @@ int cmd_grep(int argc, const char **argv, char **envp)
569
opt.regflags |= REG_EXTENDED;
570
continue;
571
}
572
+ if (!strcmp("-F", arg) ||
573
+ !strcmp("--fixed-strings", arg)) {
574
+ opt.fixed = 1;
575
+ continue;
576
+ }
577
if (!strcmp("-G", arg) ||
578
!strcmp("--basic-regexp", arg)) {
579
opt.regflags &= ~REG_EXTENDED;
@@ -674,7 +699,10 @@ int cmd_grep(int argc, const char **argv, char **envp)
699
700
if (!opt.pattern_list)
701
die("no pattern given.");
677
- compile_patterns(&opt);
702
+ if ((opt.regflags != REG_NEWLINE) && opt.fixed)
703
+ die("cannot mix --fixed-strings and regexp");
704
+ if (!opt.fixed)
705
+ compile_patterns(&opt);
706
707
/* Check revs and then paths */
708
for (i = 1; i < argc; i++) {