Skip to content

Commit b307d47

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 dedf3a5 commit b307d47

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

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 "thread-utils.h"
21+
#include "attr.h"
2122

2223
static char const * const grep_usage[] = {
2324
"git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]",
@@ -178,6 +179,22 @@ static void work_done(struct work_item *w)
178179
grep_unlock();
179180
}
180181

182+
static int skip_binary(struct grep_opt *opt, const char *filename)
183+
{
184+
if ((opt->binary & GREP_BINARY_NOMATCH)) {
185+
static struct git_attr *attr_text;
186+
struct git_attr_check check;
187+
188+
if (!attr_text)
189+
attr_text = git_attr("text");
190+
memset(&check, 0, sizeof(check));
191+
check.attr = attr_text;
192+
return !git_checkattr(filename, 1, &check) &&
193+
ATTR_FALSE(check.value);
194+
}
195+
return 0;
196+
}
197+
181198
static void *run(void *arg)
182199
{
183200
int hit = 0;
@@ -188,6 +205,9 @@ static void *run(void *arg)
188205
if (!w)
189206
break;
190207

208+
if (skip_binary(opt, (const char *)w->identifier))
209+
continue;
210+
191211
opt->output_priv = w;
192212
if (w->type == WORK_SHA1) {
193213
unsigned long sz;
@@ -505,6 +525,9 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
505525
continue;
506526
if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
507527
continue;
528+
if (skip_binary(opt, ce->name))
529+
continue;
530+
508531
/*
509532
* If CE_VALID is on, we assume worktree file and its cache entry
510533
* are identical, even if worktree file has been modified, so use
@@ -956,6 +979,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
956979
string_list_append(&path_list, show_in_pager);
957980
use_threads = 0;
958981
}
982+
if ((opt.binary & GREP_BINARY_NOMATCH))
983+
use_threads = 0;
959984

960985
if (!opt.pattern_list)
961986
die(_("no pattern given."));

0 commit comments

Comments
 (0)