Merge branch 'bc/grep-i-F'
authorJunio C Hamano <gitster@pobox.com>
Mon, 23 Nov 2009 00:28:29 +0000 (16:28 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Nov 2009 00:28:29 +0000 (16:28 -0800)
* bc/grep-i-F:
grep: Allow case insensitive search of fixed-strings

1  2 
builtin-grep.c
t/t7002-grep.sh
diff --combined builtin-grep.c
index 9a9e3fccd58adb9c984707e9398fa194ad7284a7,b41ad1e43a1e4bc4bec8d4d6b8c0fd68aa6aa92f..a5b6719a1af014497399ea6ff4f1a6f3852a0570
@@@ -367,7 -367,7 +367,7 @@@ static int external_grep(struct grep_op
                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");
  
                if (opt->color_external && strlen(opt->color_external) > 0)
                        push_arg(opt->color_external);
 +      } else {
 +              unsetenv("GREP_COLOR");
 +              unsetenv("GREP_COLORS");
        }
 +      unsetenv("GREP_OPTIONS");
  
        hit = 0;
        argc = nr;
@@@ -635,7 -631,7 +635,7 @@@ static int file_callback(const struct o
        struct grep_opt *grep_opt = opt->value;
        FILE *patterns;
        int lno = 0;
 -      struct strbuf sb;
 +      struct strbuf sb = STRBUF_INIT;
  
        patterns = fopen(arg, "r");
        if (!patterns)
@@@ -710,8 -706,8 +710,8 @@@ int cmd_grep(int argc, const char **arg
                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,
                OPT_END()
        };
  
 +      /*
 +       * 'git grep -h', unlike 'git grep -h <pattern>', is a request
 +       * to show usage information and exit.
 +       */
 +      if (argc == 2 && !strcmp(argv[1], "-h"))
 +              usage_with_options(grep_usage, options);
 +
        memset(&opt, 0, sizeof(opt));
        opt.prefix = prefix;
        opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
                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 --combined t/t7002-grep.sh
index dd0da6c0bf45524b75de9dbaf67bbbef724fdf4e,35a1e7a5d430a8b8565f3b767e31810df4463485..abd14bf819f5c60fc1b9dc758c04974bd24b10a4
@@@ -14,6 -14,7 +14,7 @@@ int main(int argc, const char **argv
  {
        printf("Hello world.\n");
        return 0;
+       /* char ?? */
  }
  EOF
  
@@@ -213,77 -214,6 +214,77 @@@ test_expect_success 'grep -e A --and --
        test_cmp expected actual
  '
  
 +test_expect_success 'grep should ignore GREP_OPTIONS' '
 +      GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 +      test_cmp expected actual
 +'
 +
 +test_expect_success 'grep -f, non-existent file' '
 +      test_must_fail git grep -f patterns
 +'
 +
 +cat >expected <<EOF
 +file:foo mmap bar
 +file:foo_mmap bar
 +file:foo_mmap bar mmap
 +file:foo mmap bar_mmap
 +file:foo_mmap bar mmap baz
 +EOF
 +
 +cat >pattern <<EOF
 +mmap
 +EOF
 +
 +test_expect_success 'grep -f, one pattern' '
 +      git grep -f pattern >actual &&
 +      test_cmp expected actual
 +'
 +
 +cat >expected <<EOF
 +file:foo mmap bar
 +file:foo_mmap bar
 +file:foo_mmap bar mmap
 +file:foo mmap bar_mmap
 +file:foo_mmap bar mmap baz
 +t/a/v:vvv
 +t/v:vvv
 +v:vvv
 +EOF
 +
 +cat >patterns <<EOF
 +mmap
 +vvv
 +EOF
 +
 +test_expect_success 'grep -f, multiple patterns' '
 +      git grep -f patterns >actual &&
 +      test_cmp expected actual
 +'
 +
 +cat >expected <<EOF
 +file:foo mmap bar
 +file:foo_mmap bar
 +file:foo_mmap bar mmap
 +file:foo mmap bar_mmap
 +file:foo_mmap bar mmap baz
 +t/a/v:vvv
 +t/v:vvv
 +v:vvv
 +EOF
 +
 +cat >patterns <<EOF
 +
 +mmap
 +
 +vvv
 +
 +EOF
 +
 +test_expect_success 'grep -f, ignore empty lines' '
 +      git grep -f patterns >actual &&
 +      test_cmp expected actual
 +'
 +
  cat >expected <<EOF
  y:y yy
  --
@@@ -416,4 -346,13 +417,13 @@@ test_expect_success 'grep from a subdir
        )
  '
  
+ 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