18
18
#include "quote.h"
19
19
#include "dir.h"
20
20
#include "pathspec.h"
21
+ #include "attr.h"
21
22
22
23
static char const * const grep_usage [] = {
23
24
N_ ("git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]" ),
@@ -163,6 +164,22 @@ static void work_done(struct work_item *w)
163
164
grep_unlock ();
164
165
}
165
166
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
+
166
183
static void * run (void * arg )
167
184
{
168
185
int hit = 0 ;
@@ -173,6 +190,9 @@ static void *run(void *arg)
173
190
if (!w )
174
191
break ;
175
192
193
+ if (skip_binary (opt , (const char * )w -> source .identifier ))
194
+ continue ;
195
+
176
196
opt -> output_priv = w ;
177
197
hit |= grep_source (opt , & w -> source );
178
198
grep_source_clear_data (& w -> source );
@@ -381,6 +401,9 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
381
401
continue ;
382
402
if (!match_pathspec_depth (pathspec , ce -> name , ce_namelen (ce ), 0 , NULL ))
383
403
continue ;
404
+ if (skip_binary (opt , ce -> name ))
405
+ continue ;
406
+
384
407
/*
385
408
* If CE_VALID is on, we assume worktree file and its cache entry
386
409
* are identical, even if worktree file has been modified, so use
@@ -807,6 +830,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
807
830
string_list_append (& path_list , show_in_pager );
808
831
use_threads = 0 ;
809
832
}
833
+ if ((opt .binary & GREP_BINARY_NOMATCH ))
834
+ use_threads = 0 ;
810
835
811
836
if (!opt .pattern_list )
812
837
die (_ ("no pattern given." ));
@@ -876,6 +901,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
876
901
if (len > 4 && is_dir_sep (pager [len - 5 ]))
877
902
pager += len - 4 ;
878
903
904
+ if (opt .ignore_case && !strcmp ("less" , pager ))
905
+ string_list_append (& path_list , "-i" );
906
+
879
907
if (!strcmp ("less" , pager ) || !strcmp ("vi" , pager )) {
880
908
struct strbuf buf = STRBUF_INIT ;
881
909
strbuf_addf (& buf , "+/%s%s" ,
0 commit comments