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);
int status;
argv[argc] = NULL;
+ trace_argv_printf(argv, "trace: grep:");
pid = fork();
if (pid < 0)
return pid;
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;
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");
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;
* 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);
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;