Skip to content

Commit f2c0ca4

Browse files
committed
Merge branch 'bc/grep-i-F' into maint
* bc/grep-i-F: grep: Allow case insensitive search of fixed-strings
2 parents 9a6b9cd + 5183bf6 commit f2c0ca4

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

builtin-grep.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
367367
push_arg("-h");
368368
if (opt->regflags & REG_EXTENDED)
369369
push_arg("-E");
370-
if (opt->regflags & REG_ICASE)
370+
if (opt->ignore_case)
371371
push_arg("-i");
372372
if (opt->binary == GREP_BINARY_NOMATCH)
373373
push_arg("-I");
@@ -706,8 +706,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
706706
OPT_GROUP(""),
707707
OPT_BOOLEAN('v', "invert-match", &opt.invert,
708708
"show non-matching lines"),
709-
OPT_BIT('i', "ignore-case", &opt.regflags,
710-
"case insensitive matching", REG_ICASE),
709+
OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case,
710+
"case insensitive matching"),
711711
OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
712712
"match patterns only at word boundaries"),
713713
OPT_SET_INT('a', "text", &opt.binary,
@@ -830,6 +830,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
830830
external_grep_allowed = 0;
831831
if (!opt.pattern_list)
832832
die("no pattern given.");
833+
if (!opt.fixed && opt.ignore_case)
834+
opt.regflags |= REG_ICASE;
833835
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
834836
die("cannot mix --fixed-strings and regexp");
835837
compile_grep_patterns(&opt);

grep.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
4141
int err;
4242

4343
p->word_regexp = opt->word_regexp;
44+
p->ignore_case = opt->ignore_case;
4445

4546
if (opt->fixed || is_fixed(p->pattern))
4647
p->fixed = 1;
@@ -262,9 +263,15 @@ static void show_name(struct grep_opt *opt, const char *name)
262263
printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
263264
}
264265

265-
static int fixmatch(const char *pattern, char *line, regmatch_t *match)
266+
267+
static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
266268
{
267-
char *hit = strstr(line, pattern);
269+
char *hit;
270+
if (ignore_case)
271+
hit = strcasestr(line, pattern);
272+
else
273+
hit = strstr(line, pattern);
274+
268275
if (!hit) {
269276
match->rm_so = match->rm_eo = -1;
270277
return REG_NOMATCH;
@@ -326,7 +333,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
326333

327334
again:
328335
if (p->fixed)
329-
hit = !fixmatch(p->pattern, bol, pmatch);
336+
hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
330337
else
331338
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
332339

grep.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct grep_pat {
3232
enum grep_header_field field;
3333
regex_t regexp;
3434
unsigned fixed:1;
35+
unsigned ignore_case:1;
3536
unsigned word_regexp:1;
3637
};
3738

@@ -64,6 +65,7 @@ struct grep_opt {
6465
regex_t regexp;
6566
int linenum;
6667
int invert;
68+
int ignore_case;
6769
int status_only;
6870
int name_only;
6971
int unmatch_name_only;

t/t7002-grep.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int main(int argc, const char **argv)
1414
{
1515
printf("Hello world.\n");
1616
return 0;
17+
/* char ?? */
1718
}
1819
EOF
1920

@@ -411,4 +412,13 @@ test_expect_success 'grep from a subdirectory to search wider area (2)' '
411412
)
412413
'
413414

415+
cat >expected <<EOF
416+
hello.c:int main(int argc, const char **argv)
417+
EOF
418+
419+
test_expect_success 'grep -Fi' '
420+
git grep -Fi "CHAR *" >actual &&
421+
test_cmp expected actual
422+
'
423+
414424
test_done

0 commit comments

Comments
 (0)