grep: Add the option '--exclude'
PATCH v4 1/1 grep: Add the option '--exclude'
grep: Add the option '--exclude'
---
hi Junio,
You're correct that adding an extra parameter to match_pathspec_depth() was unnecessary.
I added a "struct pathspec_set" as you suggested in your previous review. It had the side effect of forcing me to update a few more files than was previously necessary.
I understand that you want to keep the internals of a pathspec hidden from its users as much as possible. But if someone is already accessing its internals, I don't think it's a problem to change their usage, for example, from "ps->nr" to "ps->include.nr". Please correct me if I'm wrong.
There's one thing I did differently. You suggested moving "recursive" into "struct pathspec_set". I kept it in "struct pathspec" because it's usually modified alongside "max_depth".
btw, the last two parameters of match_pathspec_depth(), namely "prefix" and "seen", are not used anywhere. I understand their potential usefulness, but I wanted to point this out in case you want to further simplify calls to this function.
Looking forward to your feedback,
Albert Yale
Documentation/git-grep.txt | 5 ++ builtin/diff.c | 6 +- builtin/fast-export.c | 2 +- builtin/grep.c | 31 +++++++++++++- builtin/log.c | 2 +- builtin/ls-files.c | 2 +- builtin/ls-tree.c | 6 +- cache.h | 15 +++++-- dir.c | 100 +++++++++++++++++++++++++++++++++++--------- list-objects.c | 2 +- revision.c | 4 +- tree-diff.c | 2 +- tree-walk.c | 8 ++-- 13 files changed, 143 insertions(+), 42 deletions(-)
Documentation/git-grep.txt +5 -0
@@ -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,10 @@ OPTIONS Use fixed strings for patterns (don't interpret pattern as a regex). +-x <pattern>:: +--exclude <pattern>:: + Skip files matching pattern. + -n:: --line-number:: Prefix the line number to matching lines.
builtin/diff.c +3 -3
@@ -365,10 +365,10 @@ int cmd_diff(int argc, const char **argv, const char *prefix) }die (_ ("unhandled object '%s' given." ), name); } -if (rev.prune_data.nr) { +if (rev.prune_data.include.nr) {if (!path) - path = rev.prune_data.items[0 ].match; - paths += rev.prune_data.nr; + path = rev.prune_data.include.items[0 ].match; + paths += rev.prune_data.include.nr; }/*
builtin/fast-export.c +1 -1
@@ -674,7 +674,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)if (import_filename)import_marks (import_filename); -if (import_filename && revs.prune_data.nr) +if (import_filename && revs.prune_data.include.nr) full_tree =1 ;get_tags_and_duplicates (&revs.pending, &extra_refs);
builtin/grep.c +30 -1
@@ -566,6 +566,10 @@ 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, + entry.path,strlen (entry.path), +0 ,NULL )) +continue ;if (match != all_entries_interesting) { match =tree_entry_interesting (&entry, base, tn_len, pathspec);if (match == all_entries_not_interesting)
@@ -606,8 +610,16 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,static int grep_object (struct grep_opt *opt,const struct pathspec *pathspec,struct object *obj,const char *name) { -if (obj->type ==OBJ_BLOB ) +if (obj->type ==OBJ_BLOB ) { +const char *name_without_sha1 =strchr (name,':' ) +1 ; +if (!match_pathspec_depth (pathspec, + name_without_sha1, +strlen (name_without_sha1), +0 ,NULL )) +return 0 ; +return grep_sha1 (opt, obj->sha1, name,0 ); + }if (obj->type ==OBJ_COMMIT || obj->type ==OBJ_TREE ) {struct tree_desc tree;void *data;
@@ -764,6 +776,14 @@ static int pattern_callback(const struct option *opt, const char *arg,return 0 ; } +static int exclude_cb (const struct option *opt,const char *arg, +int unset) +{ +struct string_list *exclude_list = opt->value; +string_list_append (exclude_list, arg); +return 0 ; +} +static int help_callback (const struct option *opt,const char *arg,int unset) {return -1 ;
@@ -792,6 +812,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix) pattern_type_pcre, };int pattern_type = pattern_type_unspecified; +struct string_list exclude_list =STRING_LIST_INIT_NODUP ;struct option options[] = { OPT_BOOLEAN(0, "cached", &cached,
@@ -872,6 +893,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) "read patterns from file", file_callback), {OPTION_CALLBACK ,'e' ,NULL , &opt,"pattern" , "match <pattern>", PARSE_OPT_NONEG, pattern_callback }, + { OPTION_CALLBACK, 'x', "exclude", &exclude_list, "pattern", + "add <pattern> to ignore rules", PARSE_OPT_NONEG, exclude_cb }, { OPTION_CALLBACK, 0, "and", &opt, NULL,"combine patterns specified with -e" , PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
@@ -1054,6 +1077,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix) pathspec.max_depth = opt.max_depth; pathspec.recursive =1 ; +if (exclude_list.nr) { +init_pathspec_exclude_from_string_list (&pathspec, &exclude_list); + } +if (show_in_pager && (cached || list.nr))die (_ ("--open-files-in-pager only works on the worktree" ));
@@ -1103,5 +1130,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)if (hit && show_in_pager)run_pager (&opt, prefix);free_grep_patterns (&opt); +free_pathspec (&pathspec); +string_list_clear (&exclude_list,0 );return !hit; }
builtin/log.c +1 -1
@@ -125,7 +125,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix, rev->always_show_header =0 ;if (DIFF_OPT_TST (&rev->diffopt,FOLLOW_RENAMES )) { rev->always_show_header =0 ; -if (rev->diffopt.pathspec.nr !=1 ) +if (rev->diffopt.pathspec.include.nr !=1 )usage ("git logs can only follow renames on one pathname at a time" ); }
builtin/ls-files.c +1 -1
@@ -326,7 +326,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix) matchbuf[0 ] = prefix; matchbuf[1 ] =NULL ;init_pathspec (&pathspec, matchbuf); - pathspec.items[0 ].use_wildcard =0 ; + pathspec.include.items[0 ].use_wildcard =0 ; }else init_pathspec (&pathspec,NULL );if (read_tree (tree,1 , &pathspec))
builtin/ls-tree.c +3 -3
@@ -167,9 +167,9 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)die ("Not a valid object name %s" , argv[0 ]);init_pathspec (&pathspec,get_pathspec (prefix, argv +1 )); -for (i =0 ; i < pathspec.nr; i++) - pathspec.items[i].use_wildcard =0 ; - pathspec.has_wildcard =0 ; +for (i =0 ; i < pathspec.include.nr; i++) + pathspec.include.items[i].use_wildcard =0 ; + pathspec.include.has_wildcard =0 ; tree =parse_tree_indirect (sha1);if (!tree)die ("not a tree object" );
cache.h +11 -4
@@ -522,12 +522,9 @@ extern int index_name_is_other(const struct index_state *, const char *, int);extern int ie_match_stat (const struct index_state *,struct cache_entry *,struct stat *,unsigned int );extern int ie_modified (const struct index_state *,struct cache_entry *,struct stat *,unsigned int ); -struct pathspec { -const char **raw;/* get_pathspec() result, not freed by free_pathspec() */ +struct pathspec_set {int nr;unsigned int has_wildcard:1 ; -unsigned int recursive:1 ; -int max_depth;struct pathspec_item {const char *match;int len;
@@ -535,7 +532,17 @@ struct pathspec { } *items; }; +struct pathspec { +const char **raw;/* get_pathspec() result, not freed by free_pathspec() */ +unsigned int recursive:1 ; +int max_depth; + +struct pathspec_set include; +struct pathspec_set exclude; +}; +extern int init_pathspec (struct pathspec *,const char **); +extern int init_pathspec_exclude_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 +80 -20
159 lines not on this page · raw message
list-objects.c +1 -1
@@ -71,7 +71,7 @@ static void process_tree(struct rev_info *revs,struct tree_desc desc;struct name_entry entry;struct name_path me; -enum interesting match = revs->diffopt.pathspec.nr ==0 ? +enum interesting match = revs->diffopt.pathspec.include.nr ==0 ? all_entries_interesting: entry_not_interesting;int baselen = base->len;
revision.c +2 -2
@@ -389,7 +389,7 @@ static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct* tagged commit by specifying both --simplify-by-decoration * and pathspec. */ -if (!revs->prune_data.nr) +if (!revs->prune_data.include.nr)return REV_TREE_SAME ; }
@@ -1835,7 +1835,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct sif (revs->topo_order) revs->limited =1 ; -if (revs->prune_data.nr) { +if (revs->prune_data.include.nr) {diff_tree_setup_paths (revs->prune_data.raw, &revs->pruning);/* Can't prune commits with rename following: the paths change.. */ if (!DIFF_OPT_TST (&revs->diffopt,FOLLOW_RENAMES ))
tree-diff.c +1 -1
@@ -146,7 +146,7 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,for (;;) {if (diff_can_quit_early (opt))break ; -if (opt->pathspec.nr) { +if (opt->pathspec.include.nr) {skip_uninteresting (t1, &base, opt, &t1_match);skip_uninteresting (t2, &base, opt, &t2_match); }
tree-walk.c +4 -4
@@ -584,10 +584,10 @@ enum interesting tree_entry_interesting(const struct name_entry *entry, {int i;int pathlen, baselen = base->len - base_offset; -int never_interesting = ps->has_wildcard ? +int never_interesting = ps->include.has_wildcard ? entry_not_interesting : all_entries_not_interesting; -if (!ps->nr) { +if (!ps->include.nr) {if (!ps->recursive || ps->max_depth ==-1 )return all_entries_interesting; return within_depth(base->buf + base_offset, baselen,
@@ -598,8 +598,8 @@ enum interesting tree_entry_interesting(const struct name_entry *entry, pathlen =tree_entry_len (entry); -for (i = ps->nr -1 ; i >=0 ; i--) { -const struct pathspec_item *item = ps->items+i; +for (i = ps->include.nr -1 ; i >=0 ; i--) { +const struct pathspec_item *item = ps->include.items+i;const char *match = item->match;const char *base_str = base->buf + base_offset;int matchlen = item->len;
Signature · 1 line
1.7.8.3
Quoted 5 lines · Albert Yale
Documentation/git-grep.txt +0
-0@@ -22,6 +22,7 @@ SYNOPSIS
Quoted 7 lines
[--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>...]
I'd like to have a careful look at this but haven't found time/energy yet. So just a minor comment. Please use the term <pathspec> instead of <pattern>. Exclude pattern has complete different syntax than pathspec and --exclude followed by "pattern" may give wrong impression.
I'd also avoid reuse "-x" if it's common for exclude patterns, but finding a new letter may be difficult. Keep in mind this feature in the long run benefits more commands than just git-grep and ideally they should all use the same letter for short option name.
Signature · 1 line
Duy
Thank you for this feedback Nguyen. I'll make the change to git-grep.txt in my next revision.
Albert
Quoted 26 lines · Nguyen Thai Ngoc Duy
Quoted 5 lines · Albert Yale
Documentation/git-grep.txt +0
-0@@ -22,6 +22,7 @@ SYNOPSISQuoted 7 lines
[--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>...]I'd like to have a careful look at this but haven't found time/energy yet. So just a minor comment. Please use the term <pathspec> instead of <pattern>. Exclude pattern has complete different syntax than pathspec and --exclude followed by "pattern" may give wrong impression.
I'd also avoid reuse "-x" if it's common for exclude patterns, but finding a new letter may be difficult. Keep in mind this feature in the long run benefits more commands than just git-grep and ideally they should all use the same letter for short option name. --
Duy
Quoted 4 lines · Albert Yale
I added a "struct
pathspec_set" as you suggested
in your previous review. It had the side effect
of forcing me to update a few more files than was
previously necessary.
Please make it a separate patch, it's hard to follow an all-in-one patch. Although those changes might be unnecessary (see below).
builtin/grep.c static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec, · line 575
Quoted 13 lines
builtin/grep.c +4
-0@@ -566,6 +566,10 @@ 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, + entry.path, strlen(entry.path), + 0, NULL)) + continue; if (match != all_entries_interesting) { match = tree_entry_interesting(&entry, base, tn_len, pathspec); if (match == all_entries_not_interesting)
tree_entry_interesting() is equivalent to match_pathspec_depth(). The only difference is that the former is designed to match on trees why the latter a list. And tree_entry_interesting() is more efficient than match_pathspec_depth(). As you can see there's tree_entry_interesting() call above already. You should make the tree_entry_interesting() understand exclude pathspec instead of adding match_pathspec_depth() in.
Quoted 16 lines
builtin/grep.c +12
-0@@ -295,6 +298,25 @@ int match_pathspec_depth(const struct pathspec *ps, return retval; } +int match_pathspec_depth (const struct pathspec *ps, + const char *name, int namelen, + int prefix, char *seen) +{ + int retval = match_pathspec_set_depth(&ps->include, + ps->recursive, ps->max_depth, + name, namelen, prefix, seen); + + if (retval && ps->exclude.nr) + { + if (match_pathspec_set_depth(&ps->exclude, + ps->recursive, ps->max_depth,
builtin/grep.c int match_pathspec_depth(const struct pathspec *ps, · line 322
Quoted 10 lines
builtin/grep.c +7
-0+ name, namelen, prefix, seen)) + return 0; + } + + return retval; +} + static int no_wildcard(const char *string) { return string[strcspn(string, "*?[{\\")] == '\0';
It makes me wonder, why not add match_pathspec_with_exclusion(const struct pathspec *include_ps, const struct pathspec *exclude_ps,...), use the new function in grep.c and revert struct pathspec back to original? The same can be applied to tree_entry_interesting() (i.e. add a new one that takes two pathspec sets, which supports exclusion)
I think you may make less changes that way. I'm bad at naming. It's up to you to rename match_pathspec_with_exclusion to something meaningful and short enough.
Signature · 1 line
Duy
Quoted 5 lines · Nguyen Thai Ngoc Duy
It makes me wonder, why not add
match_pathspec_with_exclusion(const struct pathspec *include_ps, const struct pathspec *exclude_ps,...), use the new function ingrep.cand revert struct pathspec back to original? The same can be applied totree_entry_interesting()(i.e. add a new one that takes two pathspec sets, which supports exclusion)
Sorry, but I am the one to blame for this in:
http://thread.gmane.org/gmane.comp.version-control.git/189455/focus=189486
Quoted 1 line
I think you may make less changes that way.
The arrangement to use two pathspecs certainly is simpler in the short term, but I think it is a short-sighted hack that is wrong for two reasons.
- Doing it that way will not give you a solution where you only have to update the command line parsing of an existing command to stuff negative patterns to the pathspec structure you have been passing down to the existing codepath, and the command starts to understand negative patterns without any other change. You introduce a new pathspec that holds negative patterns, and pass that along with the positive one you have been passing down in the existing codepath for all commands that need to understand pathspec (otherwise we will be back to a similar situation we were in before you started looking at pathspec where we had three different implementations giving different semantics---some command knows how to handle this kind of pathspec but some others do not).
- Look at the fields in 'struct pathspec' and think about what those outside "items" list mean. The recursive and max_depth fields are about how the namespace is traversed [*1*], so if a codepath that used to know only about positive patterns learns to also care about negative ones, using different settings for these two fields in two separate 'struct pathspec' does not make sense. It is conceptually much cleaner to make 'struct pathspec' the data structure used by the updated logic to match paths to pathspec that can have positive and negative patterns.
[Footnote]
*1* The has_wildcard field is about optimizing the matching during the traversal so it is a bit different. Also it is our longer term goal to get rid of the "raw" field (which does not work well to substring match against the path when pathspec "magic" is involved), so we won't worry about it in this discussion.