Skip to content

Commit 52bae62

Browse files
committed
Merge branch 'tg/grep-no-index-fallback'
"git grep" by default does not fall back to its "--no-index" behaviour outside a directory under Git's control (otherwise the user may by mistake end up running a huge recursive search); with a new configuration (set in $HOME/.gitconfig--by definition this cannot be set in the config file per project), this safety can be disabled. * tg/grep-no-index-fallback: builtin/grep: add grep.fallbackToNoIndex config t7810: correct --no-index test
2 parents 569ff48 + ecd9ba6 commit 52bae62

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,10 @@ grep.threads::
14541454
Number of grep worker threads to use.
14551455
See `grep.threads` in linkgit:git-grep[1] for more information.
14561456

1457+
grep.fallbackToNoIndex::
1458+
If set to true, fall back to git grep --no-index if git grep
1459+
is executed outside of a git repository. Defaults to false.
1460+
14571461
gpg.program::
14581462
Use this custom program instead of "gpg" found on $PATH when
14591463
making or verifying a PGP signature. The program must support the

Documentation/git-grep.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ grep.threads::
6161
grep.fullName::
6262
If set to true, enable '--full-name' option by default.
6363

64+
grep.fallbackToNoIndex::
65+
If set to true, fall back to git grep --no-index if git grep
66+
is executed outside of a git repository. Defaults to false.
67+
6468

6569
OPTIONS
6670
-------

builtin/grep.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
768768
PARSE_OPT_STOP_AT_NON_OPTION);
769769
grep_commit_pattern_type(pattern_type_arg, &opt);
770770

771-
if (use_index && !startup_info->have_repository)
772-
/* die the same way as if we did it at the beginning */
773-
setup_git_directory();
771+
if (use_index && !startup_info->have_repository) {
772+
int fallback = 0;
773+
git_config_get_bool("grep.fallbacktonoindex", &fallback);
774+
if (fallback)
775+
use_index = 0;
776+
else
777+
/* die the same way as if we did it at the beginning */
778+
setup_git_directory();
779+
}
774780

775781
/*
776782
* skip a -- separator; we know it cannot be

t/t7810-grep.sh

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,12 @@ test_expect_success 'outside of git repository' '
791791
} >non/expect.full &&
792792
echo file2:world >non/expect.sub &&
793793
(
794-
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
794+
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
795795
export GIT_CEILING_DIRECTORIES &&
796796
cd non/git &&
797797
test_must_fail git grep o &&
798798
git grep --no-index o >../actual.full &&
799-
test_cmp ../expect.full ../actual.full
799+
test_cmp ../expect.full ../actual.full &&
800800
cd sub &&
801801
test_must_fail git grep o &&
802802
git grep --no-index o >../../actual.sub &&
@@ -805,22 +805,63 @@ test_expect_success 'outside of git repository' '
805805
806806
echo ".*o*" >non/git/.gitignore &&
807807
(
808-
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
808+
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
809809
export GIT_CEILING_DIRECTORIES &&
810810
cd non/git &&
811811
test_must_fail git grep o &&
812812
git grep --no-index --exclude-standard o >../actual.full &&
813813
test_cmp ../expect.full ../actual.full &&
814814
815815
{
816-
echo ".gitignore:.*o*"
816+
echo ".gitignore:.*o*" &&
817817
cat ../expect.full
818818
} >../expect.with.ignored &&
819819
git grep --no-index --no-exclude o >../actual.full &&
820820
test_cmp ../expect.with.ignored ../actual.full
821821
)
822822
'
823823

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+
824865
test_expect_success 'inside git repository but with --no-index' '
825866
rm -fr is &&
826867
mkdir -p is/git/sub &&

0 commit comments

Comments
 (0)