|
14 | 14 | #include "userdiff.h" |
15 | 15 | #include "grep.h" |
16 | 16 | #include "quote.h" |
| 17 | +#include "dir.h" |
17 | 18 |
|
18 | 19 | #ifndef NO_PTHREADS |
19 | 20 | #include "thread-utils.h" |
@@ -645,6 +646,24 @@ static int grep_object(struct grep_opt *opt, const char **paths, |
645 | 646 | die("unable to grep from object of type %s", typename(obj->type)); |
646 | 647 | } |
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 | + |
648 | 667 | static int context_callback(const struct option *opt, const char *arg, |
649 | 668 | int unset) |
650 | 669 | { |
@@ -739,9 +758,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix) |
739 | 758 | const char **paths = NULL; |
740 | 759 | int i; |
741 | 760 | int dummy; |
| 761 | + int nongit = 0, use_index = 1; |
742 | 762 | struct option options[] = { |
743 | 763 | OPT_BOOLEAN(0, "cached", &cached, |
744 | 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"), |
745 | 767 | OPT_GROUP(""), |
746 | 768 | OPT_BOOLEAN('v', "invert-match", &opt.invert, |
747 | 769 | "show non-matching lines"), |
@@ -824,6 +846,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) |
824 | 846 | OPT_END() |
825 | 847 | }; |
826 | 848 |
|
| 849 | + prefix = setup_git_directory_gently(&nongit); |
| 850 | + |
827 | 851 | /* |
828 | 852 | * 'git grep -h', unlike 'git grep -h <pattern>', is a request |
829 | 853 | * to show usage information and exit. |
@@ -861,6 +885,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix) |
861 | 885 | PARSE_OPT_STOP_AT_NON_OPTION | |
862 | 886 | PARSE_OPT_NO_INTERNAL_HELP); |
863 | 887 |
|
| 888 | + if (use_index && nongit) |
| 889 | + /* die the same way as if we did it at the beginning */ |
| 890 | + setup_git_directory(); |
| 891 | + |
864 | 892 | /* First unrecognized non-option token */ |
865 | 893 | if (argc > 0 && !opt.pattern_list) { |
866 | 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) |
922 | 950 | paths[1] = NULL; |
923 | 951 | } |
924 | 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 | + |
925 | 965 | if (!list.nr) { |
926 | 966 | int hit; |
927 | 967 | if (!cached) |
|
0 commit comments