Skip to contentSkip to conversation

grep: Add the option '--exclude'

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

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

---
This is a revision to my previous patch.
The exclusion information is now held
inside the pathspec.

Feedback would again be appreciated,

Albert Yale

 Documentation/git-grep.txt |    7 +++++
 builtin/grep.c             |   43 ++++++++++++++++++++++++++++++++-
 cache.h                    |    3 ++
 dir.c                      |   55 ++++++++++++++++++++++++++++++++++++++++----
 4 files changed, 101 insertions(+), 7 deletions(-)
Documentation/git-grep.txt +7 -0
index 6a8b1e3..db143e3 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 +41 -2

97 lines not on this page · raw message

cache.h +3 -0
index 10afd71..882a390 100644
@@ -533,9 +533,12 @@ struct pathspec {
 		int len;
 		unsigned int use_wildcard:1;
 	} *items;
+
+	struct pathspec *exclude;
 };
 
 extern int init_pathspec(struct pathspec *, const char **);
+extern int create_pathspec_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 +50 -5

77 lines not on this page · raw message

Signature · 1 line
1.7.8.3
#1 lore raw
Quoted 1 line · Albert Yale

Feedback would again be appreciated,

Hmm.

Documentation/git-grep.txt SYNOPSIS · line 25

Quoted 2 lines
Documentation/git-grep.txt +1 -0
 	   [-f <file>] [-e] <pattern>
+	   [-x<pattern>|--exclude<pattern>]

Compare the above two lines and notice some funny in the added one?

Documentation/git-grep.txt OPTIONS · line 133

Quoted 10 lines
Documentation/git-grep.txt +6 -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.
+

Likewise.

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

Quoted 12 lines
builtin/grep.c +4 -0
index 9ce064a..9772fa4 100644
@@ -530,6 +530,10 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
 			continue;
 		if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
 			continue;
+		if (pathspec->exclude &&
+			pathspec->exclude->nr &&
+			match_pathspec_depth(pathspec->exclude, ce->name, ce_namelen(ce), 0, NULL))
+			continue;

Why isn't this just

	if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))

that is, *no change whatsoever* on the existing codepaths that call match_pathspec_depth()? Can't the "even if one of the positive pathspec matched, if one of the excluded one matches, declare that the path does *not* match" logic live in match_pathspec_depth() itself?

Exactly the same comment applies to all the other additions that calls match_pathspec_depth() with pathspec->exclude as its first parameter in this patch.

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 1092

Quoted 10 lines
builtin/grep.c +6 -0
@@ -1053,6 +1084,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	pathspec.max_depth = opt.max_depth;
 	pathspec.recursive = 1;
 
+	if( exclude_list.nr ) {
+		create_pathspec_from_string_list(&pathspec.exclude, &exclude_list);
+		pathspec.exclude->max_depth = opt.max_depth;
+		pathspec.exclude->recursive = 1;
+	}
+

Style. Notice where SPs should be near parentheses on "if" statement in our codebase.

#2 lore raw

Thank you for your feedback Junio.
I'll revise my patch next weekend.

Albert Yale

Quoted 64 lines · Junio C Hamano
Quoted 1 line · Albert Yale

Feedback would again be appreciated,

Hmm.

Quoted 2 lines

         [-f <file>] [-e] <pattern>
+        [-x<pattern>|--exclude<pattern>]

Compare the above two lines and notice some funny in the added one?

Quoted 10 lines
(quoted diff) +0 -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.
+

Likewise.

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

Quoted 12 lines
builtin/grep.c +4 -0
index 9ce064a..9772fa4 100644
@@ -530,6 +530,10 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
                       continue;
               if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))
                       continue;
+             if (pathspec->exclude &&
+                     pathspec->exclude->nr &&
+                     match_pathspec_depth(pathspec->exclude, ce->name, ce_namelen(ce), 0, NULL))
+                     continue;

Why isn't this just

       if (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))

that is, *no change whatsoever* on the existing codepaths that call match_pathspec_depth()?  Can't the "even if one of the positive pathspec matched, if one of the excluded one matches, declare that the path does *not* match" logic live in match_pathspec_depth() itself?

Exactly the same comment applies to all the other additions that calls match_pathspec_depth() with pathspec->exclude as its first parameter in this patch.

builtin/grep.c int cmd_grep(int argc, const char **argv, const char *prefix) · line 1092

Quoted 10 lines
builtin/grep.c +6 -0
@@ -1053,6 +1084,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
       pathspec.max_depth = opt.max_depth;
       pathspec.recursive = 1;
 
+     if( exclude_list.nr ) {
+             create_pathspec_from_string_list(&pathspec.exclude, &exclude_list);
+             pathspec.exclude->max_depth = opt.max_depth;
+             pathspec.exclude->recursive = 1;
+     }
+

Style. Notice where SPs should be near parentheses on "if" statement in our codebase.