12
#include "builtin.h"
13
#include <regex.h>
14
#include <fnmatch.h>
15
+#include <sys/wait.h>
16
17
/*
18
* git grep pathspecs are somewhat different from diff-tree pathspecs;
410
return i;
411
}
412
413
+static int exec_grep(int argc, const char **argv)
414
+{
415
+ pid_t pid;
416
+ int status;
417
+
418
+ argv[argc] = NULL;
419
+ pid = fork();
420
+ if (pid < 0)
421
+ return pid;
422
+ if (!pid) {
423
+ execvp("grep", (char **) argv);
424
+ exit(255);
425
+ }
426
+ while (waitpid(pid, &status, 0) < 0) {
427
+ if (errno == EINTR)
428
+ continue;
429
+ return -1;
430
+ }
431
+ if (WIFEXITED(status)) {
432
+ if (!WEXITSTATUS(status))
433
+ return 1;
434
+ return 0;
435
+ }
436
+ return -1;
437
+}
438
+
439
+#define MAXARGS 1000
440
+
441
+static int external_grep(struct grep_opt *opt, const char **paths, int cached)
442
+{
443
+ int i, nr, argc, hit;
444
+ const char *argv[MAXARGS+1];
445
+ struct grep_pat *p;
446
+
447
+ nr = 0;
448
+ argv[nr++] = "grep";
449
+ if (opt->word_regexp)
450
+ argv[nr++] = "-w";
451
+ if (opt->name_only)
452
+ argv[nr++] = "-l";
453
+ for (p = opt->pattern_list; p; p = p->next) {
454
+ argv[nr++] = "-e";
455
+ argv[nr++] = p->pattern;
456
+ }
457
+ argv[nr++] = "--";
458
+
459
+ hit = 0;
460
+ argc = nr;
461
+ for (i = 0; i < active_nr; i++) {
462
+ struct cache_entry *ce = active_cache[i];
463
+ if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
464
+ continue;
465
+ if (!pathspec_matches(paths, ce->name))
466
+ continue;
467
+ argv[argc++] = ce->name;
468
+ if (argc < MAXARGS)
469
+ continue;
470
+ hit += exec_grep(argc, argv);
471
+ argc = nr;
472
+ }
473
+ if (argc > nr)
474
+ hit += exec_grep(argc, argv);
475
+ return 0;
476
+}
477
+
478
static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
479
{
480
int hit = 0;
481
int nr;
482
read_cache();
483
484
+#ifdef __unix__
485
+ /*
486
+ * Use the external "grep" command for the case where
487
+ * we grep through the checked-out files. It tends to
488
+ * be a lot more optimized
489
+ */
490
+ if (!cached) {
491
+ hit = external_grep(opt, paths, cached);
492
+ if (hit >= 0)
493
+ return hit;
494
+ }
495
+#endif
496
+
497
for (nr = 0; nr < active_nr; nr++) {
498
struct cache_entry *ce = active_cache[nr];
499
if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))