Skip to content

Commit ff38d1a

Browse files
pcloudsgitster
authored andcommitted
grep: run setup_git_directory_gently() sooner
git grep already runs a repository search unconditionally, even when the --no-index option is supplied; running such a search earlier is not very risky. Just like with shortlog, without this change, the “[pager] grep” configuration is not respected at all. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 773b69b commit ff38d1a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

builtin/grep.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
839839
struct string_list path_list = { NULL, 0, 0, 0 };
840840
int i;
841841
int dummy;
842-
int nongit = 0, use_index = 1;
842+
int use_index = 1;
843843
struct option options[] = {
844844
OPT_BOOLEAN(0, "cached", &cached,
845845
"search in index instead of in the work tree"),
@@ -930,8 +930,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
930930
OPT_END()
931931
};
932932

933-
prefix = setup_git_directory_gently(&nongit);
934-
935933
/*
936934
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
937935
* to show usage information and exit.
@@ -976,7 +974,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
976974
PARSE_OPT_STOP_AT_NON_OPTION |
977975
PARSE_OPT_NO_INTERNAL_HELP);
978976

979-
if (use_index && nongit)
977+
if (use_index && !startup_info->have_repository)
980978
/* die the same way as if we did it at the beginning */
981979
setup_git_directory();
982980

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static void handle_internal_command(int argc, const char **argv)
336336
{ "fsck-objects", cmd_fsck, RUN_SETUP },
337337
{ "gc", cmd_gc, RUN_SETUP },
338338
{ "get-tar-commit-id", cmd_get_tar_commit_id },
339-
{ "grep", cmd_grep },
339+
{ "grep", cmd_grep, RUN_SETUP_GENTLY },
340340
{ "hash-object", cmd_hash_object },
341341
{ "help", cmd_help },
342342
{ "index-pack", cmd_index_pack },

t/t7006-pager.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ test_expect_success TTY 'no pager with --no-pager' '
106106
! test -e paginated.out
107107
'
108108

109+
test_expect_success TTY 'configuration can disable pager' '
110+
rm -f paginated.out &&
111+
test_might_fail git config --unset pager.grep &&
112+
test_terminal git grep initial &&
113+
test -e paginated.out &&
114+
115+
rm -f paginated.out &&
116+
git config pager.grep false &&
117+
test_when_finished "git config --unset pager.grep" &&
118+
test_terminal git grep initial &&
119+
! test -e paginated.out
120+
'
121+
109122
# A colored commit log will begin with an appropriate ANSI escape
110123
# for the first color; the text "commit" comes later.
111124
colorful() {

0 commit comments

Comments
 (0)