git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH] grep --no-index: allow to grep without git exclusions
@ 2011-07-20 12:50 Bert Wesarg
  2011-07-20 20:57 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Bert Wesarg @ 2011-07-20 12:50 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Bert Wesarg

It is currently not possible to grep in arbitrary files with --no-index,
because the standard git exclusions are active when using --no-index.

Suppress this when the new option --no-exclude-standard was provided.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---

The name comes from git-ls-files, and I think it is worthwhile to add the full
range of exclusion flags to git-grep. Namely --exclude=<patter>,
--exclude-from=<file>, and --exclude-per-directory=<file>. Which should 
only be honored when using --no-index, obviously.

 Documentation/git-grep.txt |    7 +++++
 builtin/grep.c             |   17 ++++++++++---
 t/t7810-grep.sh            |   55 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 07b3c6a..be9cf8b 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -24,6 +24,7 @@ SYNOPSIS
 	   [-f <file>] [-e] <pattern>
 	   [--and|--or|--not|(|)|-e <pattern>...]
 	   [--cached | --no-index | <tree>...]
+	   [--no-exclude-standard]
 	   [--] [<pathspec>...]
 
 DESCRIPTION
@@ -200,6 +201,12 @@ OPTIONS
 	Do not output matched lines; instead, exit with status 0 when
 	there is a match and with non-zero status when there isn't.
 
+--exclude-standard::
+--[no-]exclude-standard::
+	Use the standard git exclusions: .git/info/exclude, .gitignore
+	in each directory, and the user's global exclusion file when
+	searching in the work tree with `--no-index`.
+
 <tree>...::
 	Instead of searching tracked files in the working tree, search
 	blobs in the given trees.
diff --git a/builtin/grep.c b/builtin/grep.c
index cccf8da..f2fce73 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -637,13 +637,15 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 	return hit;
 }
 
-static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec)
+static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
+			  int exclude_standard)
 {
 	struct dir_struct dir;
 	int i, hit = 0;
 
 	memset(&dir, 0, sizeof(dir));
-	setup_standard_excludes(&dir);
+	if (exclude_standard)
+		setup_standard_excludes(&dir);
 
 	fill_directory(&dir, pathspec->raw);
 	for (i = 0; i < dir.nr; i++) {
@@ -761,7 +763,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	struct string_list path_list = STRING_LIST_INIT_NODUP;
 	int i;
 	int dummy;
-	int use_index = 1;
+	int use_index = 1, exclude_standard = 1;
 	enum {
 		pattern_type_unspecified = 0,
 		pattern_type_bre,
@@ -839,6 +841,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN('p', "show-function", &opt.funcname,
 			"show a line with the function name before matches"),
 		OPT_GROUP(""),
+		OPT_BOOLEAN(0, "exclude-standard", &exclude_standard,
+			"don't use standard excludes, needs --no-index"),
+		OPT_GROUP(""),
 		OPT_CALLBACK('f', NULL, &opt, "file",
 			"read patterns from file", file_callback),
 		{ OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
@@ -936,6 +941,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		break; /* nothing */
 	}
 
+	/* --no-exclude-standard needs --no-index */
+	if (use_index && !exclude_standard)
+		die(_("--no-exclude-standard does not make sense without --no-index."));
+
 	if (use_index && !startup_info->have_repository)
 		/* die the same way as if we did it at the beginning */
 		setup_git_directory();
@@ -1050,7 +1059,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			die(_("--cached cannot be used with --no-index."));
 		if (list.nr)
 			die(_("--no-index cannot be used with revs."));
-		hit = grep_directory(&opt, &pathspec);
+		hit = grep_directory(&opt, &pathspec, exclude_standard);
 	} else if (!list.nr) {
 		if (!cached)
 			setup_work_tree();
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index a29ae45..15eb52b 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -587,6 +587,61 @@ test_expect_success 'inside git repository but with --no-index' '
 	)
 '
 
+test_expect_success 'outside of git repository and --no-exclude-standard' '
+	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 "fi*" >>non/git/.gitignore &&
+	{
+		echo file1:hello &&
+		echo sub/file2:world
+	} >non/expect.full &&
+	: >non/expect.empty &&
+	echo file2:world >non/expect.sub &&
+	(
+		GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
+		export GIT_CEILING_DIRECTORIES &&
+		cd non/git &&
+		test_must_fail git grep l &&
+		git grep --no-index --no-exclude-standard l >../actual.full &&
+		test_cmp ../expect.full ../actual.full
+		cd sub &&
+		test_must_fail git grep l &&
+		git grep --no-index l >../../actual.sub &&
+		test_cmp ../../expect.sub ../../actual.sub
+	)
+'
+
+test_expect_success 'inside git repository but with --no-index and --no-exclude-standard' '
+	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 "fi*" >>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 l >../actual.empty1 &&
+		test_cmp ../expect.empty ../actual.empty1 &&
+		git grep --no-index --no-exclude-standard l >../actual.full &&
+		test_cmp ../expect.full ../actual.full &&
+		cd sub &&
+		test_must_fail git grep l >../../actual.sub &&
+		test_cmp ../../expect.empty ../../actual.sub &&
+		git grep --no-index --no-exclude-standard l >../../actual.sub &&
+		test_cmp ../../expect.sub ../../actual.sub
+	)
+'
+
 test_expect_success 'setup double-dash tests' '
 cat >double-dash <<EOF &&
 --
-- 
1.7.6.588.g8d735

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-07-21 17:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-20 12:50 [RFC/PATCH] grep --no-index: allow to grep without git exclusions Bert Wesarg
2011-07-20 20:57 ` Junio C Hamano
2011-07-21  3:47   ` Nguyen Thai Ngoc Duy
2011-07-21  7:11   ` Bert Wesarg
2011-07-21  7:25     ` Bert Wesarg
2011-07-21 16:22     ` Junio C Hamano
2011-07-21 17:23       ` Bert Wesarg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).