git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] RFC Allow case insensitive search flag with git-grep for fixed-strings

From
Brian Collins <bricollins@gmail.com>
Date
Oct 30, 2009, 01:21 UTC
Message-ID
<B7C4E16C-B15D-4A7B-873A-B6BD0FDAD8C8@gmail.com>

You will have to excuse me, this is my first patch and I don't know if this is the right place to post this. Apologies in advance if I'm in the wrong place.

git-grep currently throws 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.

---
  builtin-grep.c |    8 +++++---
  grep.c         |   16 ++++++++++++----
  grep.h         |    2 ++
  3 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index 761799d..c73f05b 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->caseless)
  		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.caseless,
+			"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.caseless)
+		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..d8f14be 100644
--- a/grep.c
+++ b/grep.c
@@ -41,7 +41,8 @@ static void compile_regexp(struct grep_pat *p,  
struct grep_opt *opt)
  	int err;

  	p->word_regexp = opt->word_regexp;
-
+	p->caseless = opt->caseless;
+	
  	if (opt->fixed || is_fixed(p->pattern))
  		p->fixed = 1;
  	if (opt->regflags & REG_ICASE)
@@ -262,9 +263,16 @@ 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 caseless,  
regmatch_t *match)
  {
-	char *hit = strstr(line, pattern);
+	char *hit;
+	if (caseless) {
+		hit = strcasestr(line, pattern);
+	} else {
+		hit = strstr(line, pattern);
+	}
+	
  	if (!hit) {
  		match->rm_so = match->rm_eo = -1;
  		return REG_NOMATCH;
@@ -326,7 +334,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->caseless, pmatch);
  	else
  		hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);

diff --git a/grep.h b/grep.h
index f6eecc6..24b7d44 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 caseless:1;
  	unsigned word_regexp:1;
  };

@@ -64,6 +65,7 @@ struct grep_opt {
  	regex_t regexp;
  	int linenum;
  	int invert;
+	int caseless;
  	int status_only;
  	int name_only;
  	int unmatch_name_only;
-- 
1.6.4.4
Next: Jeff King
Message 1 of 13 in “RFC Allow case insensitive search flag with git-grep for fixed-strings”
  1. RFC Allow case insensitive search flag with git-grep for fixed-stringsBrian Collins, Oct 30, 2009
  2. Jeff KingNov 6, 2009
  3. Brian CollinsNov 6, 2009
  4. Junio C HamanoNov 6, 2009
  5. Jeff KingNov 6, 2009
  6. Junio C HamanoNov 7, 2009
  7. Nanako ShiraishiNov 16, 2009
  8. Jeff KingNov 16, 2009
  9. Brian CollinsNov 16, 2009
  10. Brandon CaseyNov 16, 2009
  11. Junio C HamanoNov 16, 2009
  12. Junio C HamanoNov 17, 2009
  13. Jeff KingNov 17, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.