@matheusbsbs / matheus-bagliano / commits / 59332d13b2
Fork 0

Resurrect "git grep --no-index"

This reverts commit 3c8f6c8 (Revert 30816237 and 7e62265, 2010-02-05) as the issue has been sorted out.

Junio C Hamano committed Feb 6, 2010 at 10:40 UTC 59332d13b2b23840452180368914921bffe9bfbc
3 files changed +93 -1
builtin-grep.c
+40
@@ -14,6 +14,7 @@
14 #include "userdiff.h"
15 #include "grep.h"
16 #include "quote.h"
17 +#include "dir.h"
18
19 #ifndef NO_PTHREADS
20 #include "thread-utils.h"
@@ -645,6 +646,24 @@ static int grep_object(struct grep_opt *opt, const char **paths,
646 die("unable to grep from object of type %s", typename(obj->type));
647 }
648
649 +static int grep_directory(struct grep_opt *opt, const char **paths)
650 +{
651 + struct dir_struct dir;
652 + int i, hit = 0;
653 +
654 + memset(&dir, 0, sizeof(dir));
655 + setup_standard_excludes(&dir);
656 +
657 + fill_directory(&dir, paths);
658 + for (i = 0; i < dir.nr; i++) {
659 + hit |= grep_file(opt, dir.entries[i]->name);
660 + if (hit && opt->status_only)
661 + break;
662 + }
663 + free_grep_patterns(opt);
664 + return hit;
665 +}
666 +
667 static int context_callback(const struct option *opt, const char *arg,
668 int unset)
669 {
@@ -739,9 +758,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
758 const char **paths = NULL;
759 int i;
760 int dummy;
761 + int nongit = 0, use_index = 1;
762 struct option options[] = {
763 OPT_BOOLEAN(0, "cached", &cached,
764 "search in index instead of in the work tree"),
765 + OPT_BOOLEAN(0, "index", &use_index,
766 + "--no-index finds in contents not managed by git"),
767 OPT_GROUP(""),
768 OPT_BOOLEAN('v', "invert-match", &opt.invert,
769 "show non-matching lines"),
@@ -824,6 +846,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
846 OPT_END()
847 };
848
849 + prefix = setup_git_directory_gently(&nongit);
850 +
851 /*
852 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
853 * to show usage information and exit.
@@ -861,6 +885,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
885 PARSE_OPT_STOP_AT_NON_OPTION |
886 PARSE_OPT_NO_INTERNAL_HELP);
887
888 + if (use_index && nongit)
889 + /* die the same way as if we did it at the beginning */
890 + setup_git_directory();
891 +
892 /* First unrecognized non-option token */
893 if (argc > 0 && !opt.pattern_list) {
894 append_grep_pattern(&opt, argv[0], "command line", 0,
@@ -922,6 +950,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
950 paths[1] = NULL;
951 }
952
953 + if (!use_index) {
954 + int hit;
955 + if (cached)
956 + die("--cached cannot be used with --no-index.");
957 + if (list.nr)
958 + die("--no-index cannot be used with revs.");
959 + hit = grep_directory(&opt, paths);
960 + if (use_threads)
961 + hit |= wait_all();
962 + return !hit;
963 + }
964 +
965 if (!list.nr) {
966 int hit;
967 if (!cached)
git.c
+1 -1
@@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
317 { "fsck-objects", cmd_fsck, RUN_SETUP },
318 { "gc", cmd_gc, RUN_SETUP },
319 { "get-tar-commit-id", cmd_get_tar_commit_id },
320 - { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
320 + { "grep", cmd_grep, USE_PAGER },
321 { "hash-object", cmd_hash_object },
322 { "help", cmd_help },
323 { "index-pack", cmd_index_pack },
t/t7002-grep.sh
+52
@@ -434,4 +434,56 @@ test_expect_success 'grep -Fi' '
434 test_cmp expected actual
435 '
436
437 +test_expect_success 'outside of git repository' '
438 + rm -fr non &&
439 + mkdir -p non/git/sub &&
440 + echo hello >non/git/file1 &&
441 + echo world >non/git/sub/file2 &&
442 + echo ".*o*" >non/git/.gitignore &&
443 + {
444 + echo file1:hello &&
445 + echo sub/file2:world
446 + } >non/expect.full &&
447 + echo file2:world >non/expect.sub
448 + (
449 + GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
450 + export GIT_CEILING_DIRECTORIES &&
451 + cd non/git &&
452 + test_must_fail git grep o &&
453 + git grep --no-index o >../actual.full &&
454 + test_cmp ../expect.full ../actual.full
455 + cd sub &&
456 + test_must_fail git grep o &&
457 + git grep --no-index o >../../actual.sub &&
458 + test_cmp ../../expect.sub ../../actual.sub
459 + )
460 +'
461 +
462 +test_expect_success 'inside git repository but with --no-index' '
463 + rm -fr is &&
464 + mkdir -p is/git/sub &&
465 + echo hello >is/git/file1 &&
466 + echo world >is/git/sub/file2 &&
467 + echo ".*o*" >is/git/.gitignore &&
468 + {
469 + echo file1:hello &&
470 + echo sub/file2:world
471 + } >is/expect.full &&
472 + : >is/expect.empty &&
473 + echo file2:world >is/expect.sub
474 + (
475 + cd is/git &&
476 + git init &&
477 + test_must_fail git grep o >../actual.full &&
478 + test_cmp ../expect.empty ../actual.full &&
479 + git grep --no-index o >../actual.full &&
480 + test_cmp ../expect.full ../actual.full &&
481 + cd sub &&
482 + test_must_fail git grep o >../../actual.sub &&
483 + test_cmp ../../expect.empty ../../actual.sub &&
484 + git grep --no-index o >../../actual.sub &&
485 + test_cmp ../../expect.sub ../../actual.sub
486 + )
487 +'
488 +
489 test_done