git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] grep: prepare to run outside of a work tree

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 15, 2010, 20:50 UTC
Message-ID
<7v4omnw0r5.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7vska71br0.fsf@alter.siamese.dyndns.org>

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>
---
 Junio C Hamano <gitster@pobox.com> writes:
 > Nanako Shiraishi <nanako3@lavabit.com> writes:
 >
 >> Is it possible to give --no-index option to "git grep", please?
 >
 > Surely.  And "grep" is much easier to do than "diff".  Will send a
 > patch perhaps during my lunch break.
 This is merely a preparatory step.
 builtin-grep.c |    7 +++++++
 git.c          |    2 +-
 2 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index 3d6ebb5..229555d 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -414,6 +414,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	const char **paths = NULL;
 	int i;
 	int dummy;
+	int nongit = 0, use_index = 1;
 	struct option options[] = {
 		OPT_BOOLEAN(0, "cached", &cached,
 			"search in index instead of in the work tree"),
@@ -497,6 +498,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
+	prefix = setup_git_directory_gently(&nongit);
+
 	/*
 	 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
 	 * to show usage information and exit.
@@ -534,6 +537,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			     PARSE_OPT_STOP_AT_NON_OPTION |
 			     PARSE_OPT_NO_INTERNAL_HELP);
 
+	if (use_index && nongit)
+		/* die the same way as if we did it at the beginning */
+		setup_git_directory();
+
 	/* First unrecognized non-option token */
 	if (argc > 0 && !opt.pattern_list) {
 		append_grep_pattern(&opt, argv[0], "command line", 0,
diff --git a/git.c b/git.c
index 11544cd..ad07473 100644
--- a/git.c
+++ b/git.c
@@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "fsck-objects", cmd_fsck, RUN_SETUP },
 		{ "gc", cmd_gc, RUN_SETUP },
 		{ "get-tar-commit-id", cmd_get_tar_commit_id },
-		{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
+		{ "grep", cmd_grep, USE_PAGER },
 		{ "help", cmd_help },
 		{ "init", cmd_init_db },
 		{ "init-db", cmd_init_db },
-- 
1.6.6.324.g20f8f4.dirty
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 20 in “Filenames and prefixes in extended diffs”
  1. Andreas GruenbacherJan 13, 2010
  2. Junio C HamanoJan 13, 2010
  3. Junio C HamanoJan 14, 2010
  4. Nanako ShiraishiJan 15, 2010
  5. Junio C HamanoJan 15, 2010
  6. grep: prepare to run outside of a work treeJunio C Hamano, Jan 15, 2010
  7. grep --no-index: allow use of "git grep" outside a git repositoryJunio C Hamano, Jan 15, 2010
  8. Jeff KingJan 15, 2010
  9. Junio C HamanoJan 16, 2010
  10. Jeff KingJan 16, 2010
  11. Junio C HamanoJan 16, 2010
  12. David AguilarJan 16, 2010
  13. Junio C HamanoJan 16, 2010
  14. Jeff KingJan 18, 2010
  15. Junio C HamanoJan 18, 2010
  16. Junio C HamanoJan 18, 2010
  17. Jeff KingJan 18, 2010
  18. Junio C HamanoJan 18, 2010
  19. Jeff KingJan 18, 2010
  20. Andreas GruenbacherJan 18, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.