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 (!ce_path_match (ce , pathspec , 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
@@ -805,6 +828,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
805
828
string_list_append (& path_list , show_in_pager );
806
829
use_threads = 0 ;
807
830
}
831
+ if ((opt .binary & GREP_BINARY_NOMATCH ))
832
+ use_threads = 0 ;
808
833
809
834
if (!opt .pattern_list )
810
835
die (_ ("no pattern given." ));
@@ -874,6 +899,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
874
899
if (len > 4 && is_dir_sep (pager [len - 5 ]))
875
900
pager += len - 4 ;
876
901
902
+ if (opt .ignore_case && !strcmp ("less" , pager ))
903
+ string_list_append (& path_list , "-i" );
904
+
877
905
if (!strcmp ("less" , pager ) || !strcmp ("vi" , pager )) {
878
906
struct strbuf buf = STRBUF_INIT ;
879
907
strbuf_addf (& buf , "+/%s%s" ,
0 commit comments