2020#endif
2121#endif
2222
23+ static int builtin_grep ;
24+
2325/*
2426 * git grep pathspecs are somewhat different from diff-tree pathspecs;
2527 * pathname wildcards are allowed.
@@ -389,7 +391,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
389391 * we grep through the checked-out files. It tends to
390392 * be a lot more optimized
391393 */
392- if (!cached ) {
394+ if (!cached && ! builtin_grep ) {
393395 hit = external_grep (opt , paths , cached );
394396 if (hit >= 0 )
395397 return hit ;
@@ -402,7 +404,12 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
402404 continue ;
403405 if (!pathspec_matches (paths , ce -> name ))
404406 continue ;
405- if (cached ) {
407+ /*
408+ * If CE_VALID is on, we assume worktree file and its cache entry
409+ * are identical, even if worktree file has been modified, so use
410+ * cache version instead
411+ */
412+ if (cached || (ce -> ce_flags & CE_VALID )) {
406413 if (ce_stage (ce ))
407414 continue ;
408415 hit |= grep_sha1 (opt , ce -> sha1 , ce -> name , 0 );
@@ -545,6 +552,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
545552 cached = 1 ;
546553 continue ;
547554 }
555+ if (!strcmp ("--no-ext-grep" , arg )) {
556+ builtin_grep = 1 ;
557+ continue ;
558+ }
548559 if (!strcmp ("-a" , arg ) ||
549560 !strcmp ("--text" , arg )) {
550561 opt .binary = GREP_BINARY_TEXT ;
0 commit comments