git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 13/21] pathspec: add match_pathspec_depth()
Date: Wed, 15 Dec 2010 22:02:48 +0700	[thread overview]
Message-ID: <1292425376-14550-14-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1292425376-14550-1-git-send-email-pclouds@gmail.com>

match_pathspec_depth() is a clone of match_pathspec() except that it
can take depth limit. Computation is a bit lighter compared to
match_pathspec() because it's usually precomputed and stored in struct
pathspec.

In long term, match_pathspec() and match_one() should be removed in
favor of this function.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 dir.c |   89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 dir.h |    3 ++
 2 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/dir.c b/dir.c
index 66c163f..b1407a5 100644
--- a/dir.c
+++ b/dir.c
@@ -169,6 +169,95 @@ int match_pathspec(const char **pathspec, const char *name, int namelen,
 	return retval;
 }
 
+/*
+ * Does 'match' match the given name?
+ * A match is found if
+ *
+ * (1) the 'match' string is leading directory of 'name', or
+ * (2) the 'match' string is a wildcard and matches 'name', or
+ * (3) the 'match' string is exactly the same as 'name'.
+ *
+ * and the return value tells which case it was.
+ *
+ * It returns 0 when there is no match.
+ */
+static int match_pathspec_item(const struct pathspec_item *item, int prefix,
+			       const char *name, int namelen)
+{
+	/* name/namelen has prefix cut off by caller */
+	const char *match = item->match + prefix;
+	int matchlen = item->len - prefix;
+
+	/* If the match was just the prefix, we matched */
+	if (!*match)
+		return MATCHED_RECURSIVELY;
+
+	if (matchlen <= namelen && !strncmp(match, name, matchlen)) {
+		if (matchlen == namelen)
+			return MATCHED_EXACTLY;
+
+		if (match[matchlen-1] == '/' || name[matchlen] == '/')
+			return MATCHED_RECURSIVELY;
+	}
+
+	if (item->has_wildcard && !fnmatch(match, name, 0))
+		return MATCHED_FNMATCH;
+
+	return 0;
+}
+
+/*
+ * Given a name and a list of pathspecs, see if the name matches
+ * any of the pathspecs.  The caller is also interested in seeing
+ * all pathspec matches some names it calls this function with
+ * (otherwise the user could have mistyped the unmatched pathspec),
+ * and a mark is left in seen[] array for pathspec element that
+ * actually matched anything.
+ */
+int match_pathspec_depth(const struct pathspec *ps,
+			 const char *name, int namelen,
+			 int prefix, char *seen)
+{
+	int i, retval = 0;
+
+	if (!ps->nr) {
+		if (!ps->recursive || ps->max_depth == -1)
+			return MATCHED_RECURSIVELY;
+
+		if (within_depth(name, namelen, 0, ps->max_depth))
+			return MATCHED_EXACTLY;
+		else
+			return 0;
+	}
+
+	name += prefix;
+	namelen -= prefix;
+
+	for (i = ps->nr - 1; i >= 0; i--) {
+		int how;
+		if (seen && seen[i] == MATCHED_EXACTLY)
+			continue;
+		how = match_pathspec_item(ps->items+i, prefix, name, namelen);
+		if (ps->recursive && ps->max_depth != -1 &&
+		    how && how != MATCHED_FNMATCH) {
+			int len = ps->items[i].len;
+			if (name[len] == '/')
+				len++;
+			if (within_depth(name+len, namelen-len, 0, ps->max_depth))
+				how = MATCHED_EXACTLY;
+			else
+				how = 0;
+		}
+		if (how) {
+			if (retval < how)
+				retval = how;
+			if (seen && seen[i] < how)
+				seen[i] = how;
+		}
+	}
+	return retval;
+}
+
 static int no_wildcard(const char *string)
 {
 	return string[strcspn(string, "*?[{\\")] == '\0';
diff --git a/dir.h b/dir.h
index c71de08..ddb4147 100644
--- a/dir.h
+++ b/dir.h
@@ -65,6 +65,9 @@ struct dir_struct {
 #define MATCHED_FNMATCH 2
 #define MATCHED_EXACTLY 3
 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);
 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);
-- 
1.7.3.3.476.g10a82

  parent reply	other threads:[~2010-12-15 15:05 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-15 15:02 [PATCH 00/21] nd/struct-pathspec v2 Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 01/21] Add struct pathspec Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 02/21] diff-no-index: use diff_tree_setup_paths() Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 03/21] Convert struct diff_options to use struct pathspec Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 04/21] tree_entry_interesting(): remove dependency on struct diff_options Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 05/21] Move tree_entry_interesting() to tree-walk.c and export it Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 06/21] glossary: define pathspec Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 07/21] diff-tree: convert base+baselen to writable strbuf Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 08/21] tree_entry_interesting(): refactor into separate smaller functions Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 09/21] tree_entry_interesting(): support depth limit Nguyễn Thái Ngọc Duy
2011-01-28 20:40   ` Junio C Hamano
2011-01-29  3:13     ` Nguyen Thai Ngoc Duy
2011-01-31 20:21       ` [PATCH] tree_entry_interesting(): with no pathspecs, everything will match Junio C Hamano
2010-12-15 15:02 ` [PATCH 10/21] tree_entry_interesting(): fix depth limit with overlapping pathspecs Nguyễn Thái Ngọc Duy
2010-12-16 23:31   ` Junio C Hamano
2010-12-17 10:05     ` Nguyen Thai Ngoc Duy
2010-12-17 20:02       ` Junio C Hamano
2010-12-18  3:37     ` Nguyen Thai Ngoc Duy
2010-12-15 15:02 ` [PATCH 11/21] tree_entry_interesting(): support wildcard matching Nguyễn Thái Ngọc Duy
2019-02-04 10:36   ` [PATCH] diff-tree doc: correct & remove wrong documentation Ævar Arnfjörð Bjarmason
2019-02-04 10:42     ` Duy Nguyen
2019-02-04 21:10     ` Junio C Hamano
2019-02-04 21:49       ` Ævar Arnfjörð Bjarmason
2010-12-15 15:02 ` [PATCH 12/21] tree_entry_interesting(): optimize wildcard matching when base is matched Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` Nguyễn Thái Ngọc Duy [this message]
2010-12-15 15:02 ` [PATCH 14/21] Convert ce_path_match() to use struct pathspec Nguyễn Thái Ngọc Duy
2010-12-17  0:02   ` Junio C Hamano
2010-12-17  9:59     ` Nguyen Thai Ngoc Duy
2010-12-17 12:43       ` [PATCH 14/21] struct rev_info: convert prune_data to " Nguyễn Thái Ngọc Duy
2010-12-17 12:43         ` [PATCH 15/21] Convert ce_path_match() to use " Nguyễn Thái Ngọc Duy
2010-12-17 15:09       ` [PATCH 14/21] " Junio C Hamano
2010-12-17 15:11         ` Nguyen Thai Ngoc Duy
2010-12-17 20:29           ` Junio C Hamano
2010-12-15 15:02 ` [PATCH 15/21] Convert ce_path_match() to use match_pathspec_depth() Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 16/21] grep: convert to use struct pathspec Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 17/21] grep: use match_pathspec_depth() for cache/worktree grepping Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 18/21] strbuf: allow "buf" to point to the middle of the allocated buffer Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 19/21] grep: use writable strbuf from caller in grep_tree() Nguyễn Thái Ngọc Duy
2010-12-17  0:15   ` Junio C Hamano
2010-12-17  9:56     ` Nguyen Thai Ngoc Duy
2010-12-17 12:44       ` [PATCH 19/21] grep: use writable strbuf from caller for grep_tree() Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 20/21] grep: drop pathspec_matches() in favor of tree_entry_interesting() Nguyễn Thái Ngọc Duy
2010-12-17 12:45   ` Nguyễn Thái Ngọc Duy
2010-12-15 15:02 ` [PATCH 21/21] t7810: overlapping pathspecs and depth limit Nguyễn Thái Ngọc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1292425376-14550-14-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).