Add branch --set-upstream
[gitweb.git] / builtin-grep.c
index 01be9bf7ff74731969384cc91dad2e20d08d81bc..529461fb8fdf6bdbee86b8cc46fa610696cb9382 100644 (file)
@@ -191,8 +191,6 @@ static int grep_file(struct grep_opt *opt, const char *filename)
                        error("'%s': %s", filename, strerror(errno));
                return 0;
        }
-       if (!st.st_size)
-               return 0; /* empty file -- no grep hit */
        if (!S_ISREG(st.st_mode))
                return 0;
        sz = xsize_t(st.st_size);
@@ -222,6 +220,7 @@ static int exec_grep(int argc, const char **argv)
        int status;
 
        argv[argc] = NULL;
+       trace_argv_printf(argv, "trace: grep:");
        pid = fork();
        if (pid < 0)
                return pid;
@@ -347,6 +346,21 @@ static void grep_add_color(struct strbuf *sb, const char *escape_seq)
                strbuf_setlen(sb, sb->len - 1);
 }
 
+static int has_skip_worktree_entry(struct grep_opt *opt, const char **paths)
+{
+       int nr;
+       for (nr = 0; nr < active_nr; nr++) {
+               struct cache_entry *ce = active_cache[nr];
+               if (!S_ISREG(ce->ce_mode))
+                       continue;
+               if (!pathspec_matches(paths, ce->name, opt->max_depth))
+                       continue;
+               if (ce_skip_worktree(ce))
+                       return 1;
+       }
+       return 0;
+}
+
 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
 {
        int i, nr, argc, hit, len, status;
@@ -355,7 +369,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
        char *argptr = randarg;
        struct grep_pat *p;
 
-       if (opt->extended || (opt->relative && opt->prefix_length))
+       if (opt->extended || (opt->relative && opt->prefix_length)
+           || has_skip_worktree_entry(opt, paths))
                return -1;
        len = nr = 0;
        push_arg("grep");
@@ -367,7 +382,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");
@@ -433,7 +448,11 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
 
                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;
@@ -508,7 +527,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
                 * are identical, even if worktree file has been modified, so use
                 * cache version instead
                 */
-               if (cached || (ce->ce_flags & CE_VALID)) {
+               if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
                        if (ce_stage(ce))
                                continue;
                        hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
@@ -706,8 +725,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,
@@ -837,6 +856,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);