[PATCH v1 10/45] parse_pathspec: a special flag for max_depth feature

2013-03-15 Thread Nguyễn Thái Ngọc Duy
match_pathspec_depth() and tree_entry_interesting() check max_depth
field in order to support git grep --max-depth. The feature
activation is tied to recursive field, which led to some unwated
activation, e.g. 5c8eeb8 (diff-index: enable recursive pathspec
matching in unpack_trees - 2012-01-15).

This patch decouples the activation from recursive field, puts it in
magic field instead. This makes sure that only git grep can
activate this feature. And because parse_pathspec knows when the
feature is not used, it does not need to sort pathspec (required for
max_depth to work correctly). A small win for non-grep cases.

Even though a new magic flag is introduced, no magic syntax is. The
magic can be only enabled by parse_pathspec() caller. We might someday
want to support :(maxdepth:10)src. It all depends on actual use
cases.

max_depth feature cannot be enabled via init_pathspec() anymore. But
that's ok because init_pathspec() is on its way to /dev/null.

Signed-off-by: Nguyễn Thái Ngọc Duy pclo...@gmail.com
---
 builtin/grep.c | 3 ++-
 diff-lib.c | 1 -
 dir.c  | 8 ++--
 pathspec.c | 8 ++--
 pathspec.h | 6 +-
 tree-diff.c| 1 -
 tree-walk.c| 8 ++--
 7 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 54b089e..1c7397a 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -860,7 +860,8 @@ int cmd_grep(int argc, const char **argv, const char 
*prefix)
}
 
parse_pathspec(pathspec, 0,
-  PATHSPEC_PREFER_CWD,
+  PATHSPEC_PREFER_CWD |
+  (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0),
   prefix, argv + i);
pathspec.max_depth = opt.max_depth;
pathspec.recursive = 1;
diff --git a/diff-lib.c b/diff-lib.c
index f35de0f..4729157 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -474,7 +474,6 @@ static int diff_cache(struct rev_info *revs,
opts.dst_index = NULL;
opts.pathspec = revs-diffopt.pathspec;
opts.pathspec-recursive = 1;
-   opts.pathspec-max_depth = -1;
 
init_tree_desc(t, tree-buffer, tree-size);
return unpack_trees(1, t, opts);
diff --git a/dir.c b/dir.c
index 8541ea7..1e9db41 100644
--- a/dir.c
+++ b/dir.c
@@ -298,7 +298,9 @@ int match_pathspec_depth(const struct pathspec *ps,
int i, retval = 0;
 
if (!ps-nr) {
-   if (!ps-recursive || ps-max_depth == -1)
+   if (!ps-recursive ||
+   !(ps-magic  PATHSPEC_MAXDEPTH) ||
+   ps-max_depth == -1)
return MATCHED_RECURSIVELY;
 
if (within_depth(name, namelen, 0, ps-max_depth))
@@ -315,7 +317,9 @@ int match_pathspec_depth(const struct pathspec *ps,
if (seen  seen[i] == MATCHED_EXACTLY)
continue;
how = match_pathspec_item(ps-items+i, prefix, name, namelen);
-   if (ps-recursive  ps-max_depth != -1 
+   if (ps-recursive 
+   (ps-magic  PATHSPEC_MAXDEPTH) 
+   ps-max_depth != -1 
how  how != MATCHED_FNMATCH) {
int len = ps-items[i].len;
if (name[len] == '/')
diff --git a/pathspec.c b/pathspec.c
index 6dd944a..b2446c3 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -266,6 +266,9 @@ void parse_pathspec(struct pathspec *pathspec,
 
memset(pathspec, 0, sizeof(*pathspec));
 
+   if (flags  PATHSPEC_MAXDEPTH_VALID)
+   pathspec-magic |= PATHSPEC_MAXDEPTH;
+
/* No arguments, no prefix - no pathspec */
if (!entry  !prefix)
return;
@@ -320,8 +323,9 @@ void parse_pathspec(struct pathspec *pathspec,
pathspec-magic |= item[i].magic;
}
 
-   qsort(pathspec-items, pathspec-nr,
- sizeof(struct pathspec_item), pathspec_item_cmp);
+   if (pathspec-magic  PATHSPEC_MAXDEPTH)
+   qsort(pathspec-items, pathspec-nr,
+ sizeof(struct pathspec_item), pathspec_item_cmp);
 }
 
 /*
diff --git a/pathspec.h b/pathspec.h
index d630e8b..aa98597 100644
--- a/pathspec.h
+++ b/pathspec.h
@@ -3,7 +3,10 @@
 
 /* Pathspec magic */
 #define PATHSPEC_FROMTOP   (10)
-#define PATHSPEC_ALL_MAGIC PATHSPEC_FROMTOP
+#define PATHSPEC_MAXDEPTH  (11)
+#define PATHSPEC_ALL_MAGIC   \
+   (PATHSPEC_FROMTOP   | \
+PATHSPEC_MAXDEPTH)
 
 #define PATHSPEC_ONESTAR 1 /* the pathspec pattern sastisfies GFNM_ONESTAR 
*/
 
@@ -27,6 +30,7 @@ struct pathspec {
 /* parse_pathspec flags */
 #define PATHSPEC_PREFER_CWD (10) /* No args means match cwd */
 #define PATHSPEC_PREFER_FULL (11) /* No args means match everything */
+#define PATHSPEC_MAXDEPTH_VALID (12) /* max_depth field is valid */
 
 extern int init_pathspec(struct pathspec *, const char **);
 extern void parse_pathspec(struct pathspec *pathspec,
diff --git a/tree-diff.c b/tree-diff.c

Re: [PATCH v1 10/45] parse_pathspec: a special flag for max_depth feature

2013-03-15 Thread Eric Sunshine
On Fri, Mar 15, 2013 at 2:06 AM, Nguyễn Thái Ngọc Duy pclo...@gmail.com wrote:
 match_pathspec_depth() and tree_entry_interesting() check max_depth
 field in order to support git grep --max-depth. The feature
 activation is tied to recursive field, which led to some unwated

s/unwated/unwanted/

 activation, e.g. 5c8eeb8 (diff-index: enable recursive pathspec
 matching in unpack_trees - 2012-01-15).
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html