@matheusbsbs / matheus-bagliano / commits / 1e2398d7fa
Fork 0

builtin-grep: use external grep when we can take advantage of it

It's not perfect, but it gets the "git grep some-random-string" down to the good old half-a-second range for the kernel. It should convert more of the argument flags for "grep", that should be trivial to expand (I did a few just as an example). It should also bother to try to return the right "hit" value (which it doesn't, right now - the code is kind of there, but I didn't actually bother to do it _right_). Also, right now it _just_ limits by number of arguments, but it should also strictly speaking limit by total argument size (ie add up the length of the filenames, and do the "exec_grep()" flush call if it's bigger than some random value like 32kB). But I think that it's _conceptually_ doing all the right things, and it seems to work. So maybe somebody else can do some of the final polish. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 14, 2006 at 20:49 UTC 1e2398d7fad308dcc6019709244b40303b51b54d
1 file changed +79
builtin-grep.c
+79
@@ -12,6 +12,7 @@
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;
@@ -409,12 +410,90 @@ static int grep_file(struct grep_opt *opt, const char *filename)
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)))