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
This commit is contained in:
Junio C Hamano 2016-01-20 11:43:38 -08:00
commit 52bae62f78
4 changed files with 62 additions and 7 deletions

View file

@ -1454,6 +1454,10 @@ grep.threads::
Number of grep worker threads to use.
See `grep.threads` in linkgit:git-grep[1] for more information.
grep.fallbackToNoIndex::
If set to true, fall back to git grep --no-index if git grep
is executed outside of a git repository. Defaults to false.
gpg.program::
Use this custom program instead of "gpg" found on $PATH when
making or verifying a PGP signature. The program must support the

View file

@ -61,6 +61,10 @@ grep.threads::
grep.fullName::
If set to true, enable '--full-name' option by default.
grep.fallbackToNoIndex::
If set to true, fall back to git grep --no-index if git grep
is executed outside of a git repository. Defaults to false.
OPTIONS
-------

View file

@ -768,9 +768,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
PARSE_OPT_STOP_AT_NON_OPTION);
grep_commit_pattern_type(pattern_type_arg, &opt);
if (use_index && !startup_info->have_repository)
/* die the same way as if we did it at the beginning */
setup_git_directory();
if (use_index && !startup_info->have_repository) {
int fallback = 0;
git_config_get_bool("grep.fallbacktonoindex", &fallback);
if (fallback)
use_index = 0;
else
/* die the same way as if we did it at the beginning */
setup_git_directory();
}
/*
* skip a -- separator; we know it cannot be

View file

@ -791,12 +791,12 @@ test_expect_success 'outside of git repository' '
} >non/expect.full &&
echo file2:world >non/expect.sub &&
(
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
test_must_fail git grep o &&
git grep --no-index o >../actual.full &&
test_cmp ../expect.full ../actual.full
test_cmp ../expect.full ../actual.full &&
cd sub &&
test_must_fail git grep o &&
git grep --no-index o >../../actual.sub &&
@ -805,7 +805,7 @@ test_expect_success 'outside of git repository' '
echo ".*o*" >non/git/.gitignore &&
(
GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
test_must_fail git grep o &&
@ -813,7 +813,7 @@ test_expect_success 'outside of git repository' '
test_cmp ../expect.full ../actual.full &&
{
echo ".gitignore:.*o*"
echo ".gitignore:.*o*" &&
cat ../expect.full
} >../expect.with.ignored &&
git grep --no-index --no-exclude o >../actual.full &&
@ -821,6 +821,47 @@ test_expect_success 'outside of git repository' '
)
'
test_expect_success 'outside of git repository with fallbackToNoIndex' '
rm -fr non &&
mkdir -p non/git/sub &&
echo hello >non/git/file1 &&
echo world >non/git/sub/file2 &&
cat <<-\EOF >non/expect.full &&
file1:hello
sub/file2:world
EOF
echo file2:world >non/expect.sub &&
(
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
test_cmp ../expect.full ../actual.full &&
cd sub &&
test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
test_cmp ../../expect.sub ../../actual.sub
) &&
echo ".*o*" >non/git/.gitignore &&
(
GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
export GIT_CEILING_DIRECTORIES &&
cd non/git &&
test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
test_cmp ../expect.full ../actual.full &&
{
echo ".gitignore:.*o*" &&
cat ../expect.full
} >../expect.with.ignored &&
git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
test_cmp ../expect.with.ignored ../actual.full
)
'
test_expect_success 'inside git repository but with --no-index' '
rm -fr is &&
mkdir -p is/git/sub &&