summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2016-01-20 11:43:38 -0800
committerLibravatar Junio C Hamano <gitster@pobox.com>2016-01-20 11:43:39 -0800
commit52bae62f7888edf5e87ef85bbe04803474f67c10 (patch)
treea4f85d775b4dacc8356f99de3bbfd4d33cc3269f
parentMerge branch 'ho/gitweb-squelch-undef-warning' (diff)
parentbuiltin/grep: add grep.fallbackToNoIndex config (diff)
downloadtgif-52bae62f7888edf5e87ef85bbe04803474f67c10.tar.xz
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
-rw-r--r--Documentation/config.txt4
-rw-r--r--Documentation/git-grep.txt4
-rw-r--r--builtin/grep.c12
-rwxr-xr-xt/t7810-grep.sh49
4 files changed, 62 insertions, 7 deletions
diff --git a/Documentation/config.txt b/Documentation/config.txt
index beb18da143..07f7a3b771 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -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
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 35aada1447..cb0f6cf678 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -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
-------
diff --git a/builtin/grep.c b/builtin/grep.c
index 06ab68223a..5526fd7056 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -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
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 028ffe4a05..b540944408 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -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 &&