builtin/grep: add grep.fallbackToNoIndex config
Currently when git grep is used outside of a git repository without the --no-index option git simply dies. For convenience, add a grep.fallbackToNoIndex configuration variable. If set to true, git grep behaves like git grep --no-index if it is run outside of a git repository. It defaults to false, preserving the current behavior. Helped-by: Jeff King <peff@peff.net> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Gummerer committed
Jan 12, 2016 at 11:40 UTC
ecd9ba61778e436dde12f08781a12ad1149d5ebf
4 files changed
+58
-3
Documentation/config.txt
+4
@@ -1399,6 +1399,10 @@ grep.extendedRegexp::
1399
option is ignored when the 'grep.patternType' option is set to a value
1400
other than 'default'.
1401
1402
+grep.fallbackToNoIndex::
1403
+ If set to true, fall back to git grep --no-index if git grep
1404
+ is executed outside of a git repository. Defaults to false.
1405
+
1406
gpg.program::
1407
Use this custom program instead of "gpg" found on $PATH when
1408
making or verifying a PGP signature. The program must support the
Documentation/git-grep.txt
+4
@@ -56,6 +56,10 @@ grep.extendedRegexp::
56
grep.fullName::
57
If set to true, enable '--full-name' option by default.
58
59
+grep.fallbackToNoIndex::
60
+ If set to true, fall back to git grep --no-index if git grep
61
+ is executed outside of a git repository. Defaults to false.
62
+
63
64
OPTIONS
65
-------
builtin/grep.c
+9
-3
@@ -770,9 +770,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
770
PARSE_OPT_NO_INTERNAL_HELP);
771
grep_commit_pattern_type(pattern_type_arg, &opt);
772
773
- if (use_index && !startup_info->have_repository)
774
- /* die the same way as if we did it at the beginning */
775
- setup_git_directory();
773
+ if (use_index && !startup_info->have_repository) {
774
+ int fallback = 0;
775
+ git_config_get_bool("grep.fallbacktonoindex", &fallback);
776
+ if (fallback)
777
+ use_index = 0;
778
+ else
779
+ /* die the same way as if we did it at the beginning */
780
+ setup_git_directory();
781
+ }
782
783
/*
784
* skip a -- separator; we know it cannot be
t/t7810-grep.sh
+41
@@ -821,6 +821,47 @@ test_expect_success 'outside of git repository' '
821
)
822
'
823
824
+test_expect_success 'outside of git repository with fallbackToNoIndex' '
825
+ rm -fr non &&
826
+ mkdir -p non/git/sub &&
827
+ echo hello >non/git/file1 &&
828
+ echo world >non/git/sub/file2 &&
829
+ cat <<-\EOF >non/expect.full &&
830
+ file1:hello
831
+ sub/file2:world
832
+ EOF
833
+ echo file2:world >non/expect.sub &&
834
+ (
835
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
836
+ export GIT_CEILING_DIRECTORIES &&
837
+ cd non/git &&
838
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
839
+ git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
840
+ test_cmp ../expect.full ../actual.full &&
841
+ cd sub &&
842
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
843
+ git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
844
+ test_cmp ../../expect.sub ../../actual.sub
845
+ ) &&
846
+
847
+ echo ".*o*" >non/git/.gitignore &&
848
+ (
849
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
850
+ export GIT_CEILING_DIRECTORIES &&
851
+ cd non/git &&
852
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
853
+ git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
854
+ test_cmp ../expect.full ../actual.full &&
855
+
856
+ {
857
+ echo ".gitignore:.*o*" &&
858
+ cat ../expect.full
859
+ } >../expect.with.ignored &&
860
+ git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
861
+ test_cmp ../expect.with.ignored ../actual.full
862
+ )
863
+'
864
+
865
test_expect_success 'inside git repository but with --no-index' '
866
rm -fr is &&
867
mkdir -p is/git/sub &&