Skip to content

Commit ecd9ba6

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f5101a commit ecd9ba6

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,10 @@ grep.extendedRegexp::
13991399
option is ignored when the 'grep.patternType' option is set to a value
14001400
other than 'default'.
14011401

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+
14021406
gpg.program::
14031407
Use this custom program instead of "gpg" found on $PATH when
14041408
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
@@ -56,6 +56,10 @@ grep.extendedRegexp::
5656
grep.fullName::
5757
If set to true, enable '--full-name' option by default.
5858

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+
5963

6064
OPTIONS
6165
-------

builtin/grep.c

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

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+
}
776782

777783
/*
778784
* skip a -- separator; we know it cannot be

t/t7810-grep.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,47 @@ test_expect_success 'outside of git repository' '
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)