git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 29/31] Remove match_pathspec() in favor of match_pathspec_depth()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 13, 2013, 12:35 UTC
Message-ID
<1358080539-17436-30-git-send-email-pclouds@gmail.com>
In-Reply-To
<1358080539-17436-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/add.c |  46 ++++++++++++---------------
 dir.c         | 100 ----------------------------------------------------------
 dir.h         |   1 -
 3 files changed, 20 insertions(+), 127 deletions(-)
diff --git a/builtin/add.c b/builtin/add.c
index 89ae67d..9edab95 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -99,7 +99,7 @@ int add_files_to_cache(const char *prefix,
 	return !!data.add_errors;
 }
 
-static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
+static void fill_pathspec_matches(struct pathspec *pathspec, char *seen)
 {
 	int num_unmatched = 0, i;
 
@@ -109,49 +109,43 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
 	 * mistakenly think that the user gave a pathspec that did not match
 	 * anything.
 	 */
-	for (i = 0; i < specs; i++)
+	for (i = 0; i < pathspec->nr; i++)
 		if (!seen[i])
 			num_unmatched++;
 	if (!num_unmatched)
 		return;
 	for (i = 0; i < active_nr; i++) {
 		struct cache_entry *ce = active_cache[i];
-		match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
+		match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen);
 	}
 }
 
-static char *find_used_pathspec(const char **pathspec)
+static char *find_used_pathspec(struct pathspec *pathspec)
 {
 	char *seen;
-	int i;
-
-	for (i = 0; pathspec[i];  i++)
-		; /* just counting */
-	seen = xcalloc(i, 1);
-	fill_pathspec_matches(pathspec, seen, i);
+	seen = xcalloc(pathspec->nr, 1);
+	fill_pathspec_matches(pathspec, seen);
 	return seen;
 }
 
-static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
+static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
 {
 	char *seen;
-	int i, specs;
+	int i;
 	struct dir_entry **src, **dst;
 
-	for (specs = 0; pathspec[specs];  specs++)
-		/* nothing */;
-	seen = xcalloc(specs, 1);
+	seen = xcalloc(pathspec->nr, 1);
 
 	src = dst = dir->entries;
 	i = dir->nr;
 	while (--i >= 0) {
 		struct dir_entry *entry = *src++;
-		if (match_pathspec(pathspec, entry->name, entry->len,
-				   prefix, seen))
+		if (match_pathspec_depth(pathspec, entry->name, entry->len,
+					 prefix, seen))
 			*dst++ = entry;
 	}
 	dir->nr = dst - dir->entries;
-	fill_pathspec_matches(pathspec, seen, specs);
+	fill_pathspec_matches(pathspec, seen);
 	return seen;
 }
 
@@ -401,7 +395,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		/* This picks up the paths that are not tracked */
 		baselen = fill_directory(&dir, &pathspec);
 		if (pathspec.nr)
-			seen = prune_directory(&dir, pathspec.raw, baselen);
+			seen = prune_directory(&dir, &pathspec, baselen);
 	}
 
 	if (refresh_only) {
@@ -415,23 +409,23 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
 		path_exclude_check_init(&check, &dir);
 		if (!seen)
-			seen = find_used_pathspec(pathspec.raw);
+			seen = find_used_pathspec(&pathspec);
 
 		/*
 		 * file_exists() assumes exact match
 		 */
 		GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);
 
-		for (i = 0; pathspec.raw[i]; i++) {
-			if (!seen[i] && pathspec.raw[i][0]
-			    && !file_exists(pathspec.raw[i])) {
+		for (i = 0; i < pathspec.nr; i++) {
+			const char *path = pathspec.items[i].match;
+			if (!seen[i] && !file_exists(path)) {
 				if (ignore_missing) {
 					int dtype = DT_UNKNOWN;
-					if (is_path_excluded(&check, pathspec.raw[i], -1, &dtype))
-						dir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));
+					if (is_path_excluded(&check, path, -1, &dtype))
+						dir_add_ignored(&dir, path, pathspec.items[i].len);
 				} else
 					die(_("pathspec '%s' did not match any files"),
-					    pathspec.raw[i]);
+					    pathspec.items[i].original);
 			}
 		}
 		free(seen);
diff --git a/dir.c b/dir.c
index 76b267e..2a1bcb8 100644
--- a/dir.c
+++ b/dir.c
@@ -139,106 +139,6 @@ int within_depth(const char *name, int namelen,
  *
  * It returns 0 when there is no match.
  */
-static int match_one(const char *match, const char *name, int namelen)
-{
-	int matchlen;
-	int literal = limit_pathspec_to_literal();
-
-	/* If the match was just the prefix, we matched */
-	if (!*match)
-		return MATCHED_RECURSIVELY;
-
-	if (ignore_case) {
-		for (;;) {
-			unsigned char c1 = tolower(*match);
-			unsigned char c2 = tolower(*name);
-			if (c1 == '\0' || (!literal && is_glob_special(c1)))
-				break;
-			if (c1 != c2)
-				return 0;
-			match++;
-			name++;
-			namelen--;
-		}
-	} else {
-		for (;;) {
-			unsigned char c1 = *match;
-			unsigned char c2 = *name;
-			if (c1 == '\0' || (!literal && is_glob_special(c1)))
-				break;
-			if (c1 != c2)
-				return 0;
-			match++;
-			name++;
-			namelen--;
-		}
-	}
-
-	/*
-	 * If we don't match the matchstring exactly,
-	 * we need to match by fnmatch
-	 */
-	matchlen = strlen(match);
-	if (strncmp_icase(match, name, matchlen)) {
-		if (literal)
-			return 0;
-		return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
-	}
-
-	if (namelen == matchlen)
-		return MATCHED_EXACTLY;
-	if (match[matchlen-1] == '/' || name[matchlen] == '/')
-		return MATCHED_RECURSIVELY;
-	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(const char **pathspec, const char *name, int namelen,
-		int prefix, char *seen)
-{
-	int i, retval = 0;
-
-	if (!pathspec)
-		return 1;
-
-	name += prefix;
-	namelen -= prefix;
-
-	for (i = 0; pathspec[i] != NULL; i++) {
-		int how;
-		const char *match = pathspec[i] + prefix;
-		if (seen && seen[i] == MATCHED_EXACTLY)
-			continue;
-		how = match_one(match, name, namelen);
-		if (how) {
-			if (retval < how)
-				retval = how;
-			if (seen && seen[i] < how)
-				seen[i] = how;
-		}
-	}
-	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)
 {
diff --git a/dir.h b/dir.h
index ff5ada5..a03af80 100644
--- a/dir.h
+++ b/dir.h
@@ -91,7 +91,6 @@ struct dir_struct {
 extern int simple_length(const char *match);
 extern int no_wildcard(const char *string);
 extern char *common_prefix(const struct pathspec *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);
-- 
1.8.0.rc2.23.g1fb49df
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 34 of 37 in “nd/parse-pathspec”
  1. 00/31 nd/parse-pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  2. 01/31 clean: remove unused variable "seen"Nguyễn Thái Ngọc Duy, Jan 13, 2013
  3. 02/31 Add copy_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  4. 03/31 Add parse_pathspec() that converts cmdline args to struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  5. Martin von ZweigbergkJan 14, 2013
  6. Duy NguyenJan 14, 2013
  7. 04/31 parse_pathspec: save original pathspec for reportingNguyễn Thái Ngọc Duy, Jan 13, 2013
  8. 05/31 Export parse_pathspec() and convert some get_pathspec() callsNguyễn Thái Ngọc Duy, Jan 13, 2013
  9. 06/31 Guard against new pathspec magic in pathspec matching codeNguyễn Thái Ngọc Duy, Jan 13, 2013
  10. 07/31 clean: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  11. 08/31 parse_pathspec: add PATHSPEC_EMPTY_MATCH_ALLNguyễn Thái Ngọc Duy, Jan 13, 2013
  12. Martin von ZweigbergkJan 21, 2013
  13. Duy NguyenJan 22, 2013
  14. 09/31 commit: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  15. 10/31 status: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  16. 11/31 rerere: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  17. 12/31 checkout: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  18. 13/31 rm: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  19. 14/31 parse_pathspec: support stripping submodule trailing slashesNguyễn Thái Ngọc Duy, Jan 13, 2013
  20. 15/31 ls-files: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  21. 16/31 archive: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  22. 17/31 parse_pathspec: support stripping/checking submodule pathsNguyễn Thái Ngọc Duy, Jan 13, 2013
  23. 18/31 add: convert to use parse_pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  24. 19/31 Convert read_cache_preload() to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  25. 20/31 Convert unmerge_cache to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  26. 21/31 checkout: convert read_tree_some to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  27. 22/31 Convert report_path_error to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  28. 23/31 Convert refresh_index to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  29. 24/31 Convert {read,fill}_directory to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  30. 25/31 Convert add_files_to_cache to take struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  31. 26/31 Convert common_prefix() to use struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  32. 27/31 Remove diff_tree_{setup,release}_pathsNguyễn Thái Ngọc Duy, Jan 13, 2013
  33. 28/31 Remove init_pathspec() in favor of parse_pathspec()Nguyễn Thái Ngọc Duy, Jan 13, 2013
  34. 29/31 Remove match_pathspec() in favor of match_pathspec_depth()Nguyễn Thái Ngọc Duy, Jan 13, 2013
  35. 30/31 tree-diff: remove the use of pathspec's raw[] in follow-rename codepathNguyễn Thái Ngọc Duy, Jan 13, 2013
  36. 31/31 Rename field "raw" to "_raw" in struct pathspecNguyễn Thái Ngọc Duy, Jan 13, 2013
  37. Junio C HamanoJan 13, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.