Skip to contentSkip to conversation

grep: Add the option '--exclude'

PATCH Superseded Albert Yale 2 participants 2 messages Jun 15, 2016 Documentation

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 grep: Add the option '--exclude'

[PATCH/RFC v3] grep: Add the option '--exclude'

---
Here's my latest progress towards a cleaner implementation.

Albert Yale

 Documentation/git-grep.txt |    7 +++
 builtin/grep.c             |   41 +++++++++++++--
 cache.h                    |    3 +
 dir.c                      |  126 +++++++++++++++++++++++++++++++++++++------
 dir.h                      |    3 +-
 read-cache.c               |    2 +-
 6 files changed, 158 insertions(+), 24 deletions(-)
Documentation/git-grep.txt +7 -0
index 6a8b1e3..67a83aa 100644
@@ -22,6 +22,7 @@ SYNOPSIS
 	   [--color[=<when>] | --no-color]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-f <file>] [-e] <pattern>
+	   [-x <pattern>|--exclude <pattern>]
 	   [--and|--or|--not|(|)|-e <pattern>...]
 	   [ [--exclude-standard] [--cached | --no-index | --untracked] | <tree>...]
 	   [--] [<pathspec>...]
@@ -124,6 +125,12 @@ OPTIONS
 	Use fixed strings for patterns (don't interpret pattern
 	as a regex).
 
+-x <pattern>::
+--exclude <pattern>::
+	In addition to those found in .gitignore (per directory) and
+	$GIT_DIR/info/exclude, also consider these patterns to be in the
+	set of the ignore rules in effect.
+
 -n::
 --line-number::
 	Prefix the line number to matching lines.
builtin/grep.c +37 -4

95 lines not on this page · raw message

cache.h +3 -0
index 9bd8c2d..683458a 100644
@@ -533,9 +533,12 @@ struct pathspec {
 		int len;
 		unsigned int use_wildcard:1;
 	} *items;
+
+	struct pathspec *exclude; /* This is never NULL. */
 };
 
 extern int init_pathspec(struct pathspec *, const char **);
+extern int add_pathspec_item_from_string_list(struct pathspec *, const struct string_list *);
 extern void free_pathspec(struct pathspec *);
 extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec);
 
dir.c +108 -18

183 lines not on this page · raw message

dir.h +2 -1
index dd6947e..2b9870d 100644
@@ -68,7 +68,8 @@ extern char *common_prefix(const char **pathspec);
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
 extern int match_pathspec_depth(const struct pathspec *pathspec,
 				const char *name, int namelen,
-				int prefix, char *seen);
+				int prefix, char *seen,
+				int empty_pathspec_can_match);
 extern int within_depth(const char *name, int namelen, int depth, int max_depth);
 
 extern int fill_directory(struct dir_struct *dir, const char **pathspec);
read-cache.c +1 -1
index a51bba1..9783c4f 100644
@@ -708,7 +708,7 @@ int ce_same_name(struct cache_entry *a, struct cache_entry *b)
 
 int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec)
 {
-	return match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL);
+	return match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL, 1);
 }
 
 /*
Signature · 1 line
1.7.8.3
#1 lore raw

Documentation/git-grep.txt OPTIONS · line 132

Quoted 9 lines · Albert Yale
Documentation/git-grep.txt +5 -0
@@ -124,6 +125,12 @@ OPTIONS
 	Use fixed strings for patterns (don't interpret pattern
 	as a regex).
 
+-x <pattern>::
+--exclude <pattern>::
+	In addition to those found in .gitignore (per directory) and
+	$GIT_DIR/info/exclude, also consider these patterns to be in the
+	set of the ignore rules in effect.

That makes it sound as if "git grep" will not produce hits for a path that was forcibly added despite it matches a pattern in .gitignore file, which is not true at all.

Quoted 18 lines
Documentation/git-grep.txt +0 -0
 -n::
 --line-number::
 	Prefix the line number to matching lines.
builtin/grep.c +2 -1
index 9ce064a..e9e1ac1 100644
@@ -528,7 +528,8 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
 		struct cache_entry *ce = active_cache[nr];
 		if (!S_ISREG(ce->ce_mode))
 			continue;
-		if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
+		if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce),
+					  0, NULL, 1))
 			continue;
 		/*
 		 * If CE_VALID is on, we assume worktree file and its cache entry

builtin/grep.c static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, · line 574

Quoted 8 lines
builtin/grep.c +5 -0
@@ -566,6 +567,11 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 	while (tree_entry(tree, &entry)) {
 		int te_len = tree_entry_len(&entry);
 
+		if (match_pathspec_depth(pathspec->exclude,
+					 entry.path, strlen(entry.path),
+					 0, NULL, 0))
+			continue;
+

Why???

IOW, why isn't this

	if (!match_pathspec_depth(pathspec, ...))
		continue;	

My point of the two previous review messages was that it would be nice if we can make it so that nobody outside match_pathspec_depth() should even need to know existence of pathspec->exclude. Either that was ignored, or perhaps you found a compelling reason why that is not a good idea, but if so I'd like to know why.

I suspect that you do not need to add the extra 0 at the end (it makes the caller even harder to read) if you go that route. The extra parameter defeats the whole point of encapsulating the new "negative match" feature behind match_pathspec_depth() implementation.

builtin/grep.c static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, · line 622

Quoted 8 lines
builtin/grep.c +8 -0
+	if (obj->type == OBJ_BLOB) {
+		const char *name_without_sha1 = strchr(name, ':') + 1;
+
+		if (match_pathspec_depth(pathspec->exclude,
+					 name_without_sha1,
+					 strlen(name_without_sha1),
+					 0, NULL, 0))
+			return 0;

Likewise.

cache.h struct pathspec { · line 538

Quoted 11 lines
cache.h +2 -0
index 9bd8c2d..683458a 100644
@@ -533,9 +533,12 @@ struct pathspec {
 		int len;
 		unsigned int use_wildcard:1;
 	} *items;
+
+	struct pathspec *exclude; /* This is never NULL. */
 };

"This is never NULL" is blatantly wrong, as pathspec->exclude->exclude will very likely be NULL.

I've been assuming that the resulting structure would be more like this:

 struct pathspec_set {
         int nr;
         unsigned int has_wildcard:1;
         unsigned int recursive:1;
         struct pathspec_item {
                 const char *match;
                 int len;
                 unsigned int use_wildcard:1;
         } *items;

};

 struct pathspec {
         const char **raw; /* get_pathspec() result, not freed by free_pathspec() */
         int max_depth;
         struct pathspec_set include;
         struct pathspec_set exclude;

};

I am not including "raw" in the include/exclude because its use should be deprecated away over time.

The various helpers used in match_pathspec_depth() that know _only_ about matching and not about excluding will be changed to take a pointer to "struct pathspec_set" (and possibly max_depth). The top-level callers would not have to know any of these changes if we structure the API that way, no?

I think something like that was more or less the structure we discussed when Nguyễn brought up his negative pathspec work the last time.