Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 5edf3c9

Browse files
committed
grep -I: do not bother to read known-binary files
Incidentally, this makes grep -I respect the "binary" attribute (actually, the "-text" attribute, but "binary" implies that). Since the attributes are not thread-safe, we now need to switch off threading if -I was passed. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a872d83 commit 5edf3c9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

builtin/grep.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "quote.h"
1919
#include "dir.h"
2020
#include "pathspec.h"
21+
#include "attr.h"
2122

2223
static char const * const grep_usage[] = {
2324
N_("git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]"),
@@ -163,6 +164,22 @@ static void work_done(struct work_item *w)
163164
grep_unlock();
164165
}
165166

167+
static int skip_binary(struct grep_opt *opt, const char *filename)
168+
{
169+
if ((opt->binary & GREP_BINARY_NOMATCH)) {
170+
static struct git_attr *attr_text;
171+
struct git_attr_check check;
172+
173+
if (!attr_text)
174+
attr_text = git_attr("text");
175+
memset(&check, 0, sizeof(check));
176+
check.attr = attr_text;
177+
return !git_check_attr(filename, 1, &check) &&
178+
ATTR_FALSE(check.value);
179+
}
180+
return 0;
181+
}
182+
166183
static void *run(void *arg)
167184
{
168185
int hit = 0;
@@ -173,6 +190,9 @@ static void *run(void *arg)
173190
if (!w)
174191
break;
175192

193+
if (skip_binary(opt, (const char *)w->source.identifier))
194+
continue;
195+
176196
opt->output_priv = w;
177197
hit |= grep_source(opt, &w->source);
178198
grep_source_clear_data(&w->source);
@@ -381,6 +401,9 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
381401
continue;
382402
if (!ce_path_match(ce, pathspec, NULL))
383403
continue;
404+
if (skip_binary(opt, ce->name))
405+
continue;
406+
384407
/*
385408
* If CE_VALID is on, we assume worktree file and its cache entry
386409
* are identical, even if worktree file has been modified, so use
@@ -805,6 +828,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
805828
string_list_append(&path_list, show_in_pager);
806829
use_threads = 0;
807830
}
831+
if ((opt.binary & GREP_BINARY_NOMATCH))
832+
use_threads = 0;
808833

809834
if (!opt.pattern_list)
810835
die(_("no pattern given."));

0 commit comments

Comments
 (0)