Skip to content

Commit 7e62265

Browse files
committed
grep: prepare to run outside of a work tree
This moves the call to setup_git_directory() for running "grep" from the "git" wrapper to the implementation of the "grep" subcommand. A new variable "use_index" is always true at this stage in the series, and when it is on, we require that we are in a directory that is under git control. To make sure we die the same way, we make a second call into setup_git_directory() when we detect this situation. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 885d211 commit 7e62265

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

builtin-grep.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
414414
const char **paths = NULL;
415415
int i;
416416
int dummy;
417+
int nongit = 0, use_index = 1;
417418
struct option options[] = {
418419
OPT_BOOLEAN(0, "cached", &cached,
419420
"search in index instead of in the work tree"),
@@ -497,6 +498,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
497498
OPT_END()
498499
};
499500

501+
prefix = setup_git_directory_gently(&nongit);
502+
500503
/*
501504
* 'git grep -h', unlike 'git grep -h <pattern>', is a request
502505
* to show usage information and exit.
@@ -534,6 +537,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
534537
PARSE_OPT_STOP_AT_NON_OPTION |
535538
PARSE_OPT_NO_INTERNAL_HELP);
536539

540+
if (use_index && nongit)
541+
/* die the same way as if we did it at the beginning */
542+
setup_git_directory();
543+
537544
/* First unrecognized non-option token */
538545
if (argc > 0 && !opt.pattern_list) {
539546
append_grep_pattern(&opt, argv[0], "command line", 0,

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
317317
{ "fsck-objects", cmd_fsck, RUN_SETUP },
318318
{ "gc", cmd_gc, RUN_SETUP },
319319
{ "get-tar-commit-id", cmd_get_tar_commit_id },
320-
{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
320+
{ "grep", cmd_grep, USE_PAGER },
321321
{ "help", cmd_help },
322322
{ "init", cmd_init_db },
323323
{ "init-db", cmd_init_db },

0 commit comments

Comments
 (0)