summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Collins <bricollins@gmail.com>2009-11-06 09:22:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2009-11-17 00:06:46 (GMT)
commit5183bf67278ce5a0da9779d74f05169beac219b8 (patch)
tree3ff2119ec35588228c723069b1f21b061ac1c18d
parent78d553b7d7b269bb22ebd8b1198657c37484a3a0 (diff)
downloadgit-5183bf67278ce5a0da9779d74f05169beac219b8.zip
git-5183bf67278ce5a0da9779d74f05169beac219b8.tar.gz
git-5183bf67278ce5a0da9779d74f05169beac219b8.tar.bz2
grep: Allow case insensitive search of fixed-strings
"git grep" currently an error when you combine the -F and -i flags. This isn't in line with how GNU grep handles it. This patch allows the simultaneous use of those flags. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Brian Collins <bricollins@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin-grep.c8
-rw-r--r--grep.c13
-rw-r--r--grep.h2
-rwxr-xr-xt/t7002-grep.sh10
4 files changed, 27 insertions, 6 deletions
diff --git a/builtin-grep.c b/builtin-grep.c
index 761799d..b41ad1e 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -367,7 +367,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
push_arg("-h");
if (opt->regflags & REG_EXTENDED)
push_arg("-E");
- if (opt->regflags & REG_ICASE)
+ if (opt->ignore_case)
push_arg("-i");
if (opt->binary == GREP_BINARY_NOMATCH)
push_arg("-I");
@@ -706,8 +706,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
OPT_GROUP(""),
OPT_BOOLEAN('v', "invert-match", &opt.invert,
"show non-matching lines"),
- OPT_BIT('i', "ignore-case", &opt.regflags,
- "case insensitive matching", REG_ICASE),
+ OPT_BOOLEAN('i', "ignore-case", &opt.ignore_case,
+ "case insensitive matching"),
OPT_BOOLEAN('w', "word-regexp", &opt.word_regexp,
"match patterns only at word boundaries"),
OPT_SET_INT('a', "text", &opt.binary,
@@ -830,6 +830,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
external_grep_allowed = 0;
if (!opt.pattern_list)
die("no pattern given.");
+ if (!opt.fixed && opt.ignore_case)
+ opt.regflags |= REG_ICASE;
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
die("cannot mix --fixed-strings and regexp");
compile_grep_patterns(&opt);
diff --git a/grep.c b/grep.c
index 5d162da..bdadf2c 100644
--- a/grep.c
+++ b/grep.c
@@ -41,6 +41,7 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
int err;
p->word_regexp = opt->word_regexp;
+ p->ignore_case = opt->ignore_case;
if (opt->fixed || is_fixed(p->pattern))
p->fixed = 1;
@@ -262,9 +263,15 @@ static void show_name(struct grep_opt *opt, const char *name)
printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
}
-static int fixmatch(const char *pattern, char *line, regmatch_t *match)
+
+static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
{
- char *hit = strstr(line, pattern);
+ char *hit;
+ if (ignore_case)
+ hit = strcasestr(line, pattern);
+ else
+ hit = strstr(line, pattern);
+
if (!hit) {
match->rm_so = match->rm_eo = -1;
return REG_NOMATCH;
@@ -326,7 +333,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
again:
if (p->fixed)
- hit = !fixmatch(p->pattern, bol, pmatch);
+ hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
else
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
diff --git a/grep.h b/grep.h
index f6eecc6..75370f6 100644
--- a/grep.h
+++ b/grep.h
@@ -32,6 +32,7 @@ struct grep_pat {
enum grep_header_field field;
regex_t regexp;
unsigned fixed:1;
+ unsigned ignore_case:1;
unsigned word_regexp:1;
};
@@ -64,6 +65,7 @@ struct grep_opt {
regex_t regexp;
int linenum;
int invert;
+ int ignore_case;
int status_only;
int name_only;
int unmatch_name_only;
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index ae56a36..35a1e7a 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -14,6 +14,7 @@ int main(int argc, const char **argv)
{
printf("Hello world.\n");
return 0;
+ /* char ?? */
}
EOF
@@ -345,4 +346,13 @@ test_expect_success 'grep from a subdirectory to search wider area (2)' '
)
'
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+EOF
+
+test_expect_success 'grep -Fi' '
+ git grep -Fi "CHAR *" >actual &&
+ test_cmp expected actual
+'
+
test_done