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

[PATCH] grep --no-index: allow use of "git grep" outside a git repository

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

Just like some people wanted diff features that are not found in other people's diff implementations outside of a git repository and added --no-index mode to the command, this adds --no-index mode to the "git grep" command.

Also, inside a git repository, --no-index mode allows you to grep in untracked (but not ignored) files.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This is much easier than "git diff --no-index" because it does not have
   to worry about corner cases like "git diff --no-index file1 file2".
 builtin-grep.c  |   26 ++++++++++++++++++++++++++
 t/t7002-grep.sh |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 0 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index 229555d..1283373 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -14,6 +14,7 @@
 #include "userdiff.h"
 #include "grep.h"
 #include "quote.h"
+#include "dir.h"
 
 static char const * const grep_usage[] = {
 	"git grep [options] [-e] <pattern> [<rev>...] [[--] path...]",
@@ -320,6 +321,21 @@ static int grep_object(struct grep_opt *opt, const char **paths,
 	die("unable to grep from object of type %s", typename(obj->type));
 }
 
+static int grep_directory(struct grep_opt *opt, const char **paths)
+{
+	struct dir_struct dir;
+	int i, hit = 0;
+
+	memset(&dir, 0, sizeof(dir));
+	setup_standard_excludes(&dir);
+
+	fill_directory(&dir, paths);
+	for (i = 0; i < dir.nr; i++)
+		hit |= grep_file(opt, dir.entries[i]->name);
+	free_grep_patterns(opt);
+	return hit;
+}
+
 static int context_callback(const struct option *opt, const char *arg,
 			    int unset)
 {
@@ -418,6 +434,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		OPT_BOOLEAN(0, "cached", &cached,
 			"search in index instead of in the work tree"),
+		OPT_BOOLEAN(0, "index", &use_index,
+			"--no-index finds in contents not managed by git"),
 		OPT_GROUP(""),
 		OPT_BOOLEAN('v', "invert-match", &opt.invert,
 			"show non-matching lines"),
@@ -591,6 +609,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		paths[1] = NULL;
 	}
 
+	if (!use_index) {
+		if (cached)
+			die("--cached cannot be used with --no-index.");
+		if (list.nr)
+			die("--no-index cannot be used with revs.");
+		return !grep_directory(&opt, paths);
+	}
+
 	if (!list.nr) {
 		if (!cached)
 			setup_work_tree();
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index c369cdb..7eceb08 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -426,4 +426,56 @@ test_expect_success 'grep -Fi' '
 	test_cmp expected actual
 '
 
+test_expect_success 'outside of git repository' '
+	rm -fr non &&
+	mkdir -p non/git/sub &&
+	echo hello >non/git/file1 &&
+	echo world >non/git/sub/file2 &&
+	echo ".*o*" >non/git/.gitignore &&
+	{
+		echo file1:hello &&
+		echo sub/file2:world
+	} >non/expect.full &&
+	echo file2:world >non/expect.sub
+	(
+		GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
+		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
+		cd sub &&
+		test_must_fail git grep o &&
+		git grep --no-index o >../../actual.sub &&
+		test_cmp ../../expect.sub ../../actual.sub
+	)
+'
+
+test_expect_success 'inside git repository but with --no-index' '
+	rm -fr is &&
+	mkdir -p is/git/sub &&
+	echo hello >is/git/file1 &&
+	echo world >is/git/sub/file2 &&
+	echo ".*o*" >is/git/.gitignore &&
+	{
+		echo file1:hello &&
+		echo sub/file2:world
+	} >is/expect.full &&
+	: >is/expect.empty &&
+	echo file2:world >is/expect.sub
+	(
+		cd is/git &&
+		git init &&
+		test_must_fail git grep o >../actual.full &&
+		test_cmp ../expect.empty ../actual.full &&
+		git grep --no-index o >../actual.full &&
+		test_cmp ../expect.full ../actual.full &&
+		cd sub &&
+		test_must_fail git grep o >../../actual.sub &&
+		test_cmp ../../expect.empty ../../actual.sub &&
+		git grep --no-index o >../../actual.sub &&
+		test_cmp ../../expect.sub ../../actual.sub
+	)
+'
+
 test_done
-- 
1.6.6.324.g20f8f4
Previous: Junio C HamanoNext: Jeff King
Message 7 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.