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

[PATCH 3/3] implement pattern matching in ce_path_match

From
Clemens Buchacher <drizzd@aon.at>
Date
Jan 14, 2009, 14:54 UTC
Message-ID
<1231944876-29930-4-git-send-email-drizzd@aon.at>
In-Reply-To
<1231944876-29930-3-git-send-email-drizzd@aon.at>

For tracked files, matching globbing pattern in "git add" was broken by 1e5f764c (builtin-add.c: optimize -A option and "git add ."), which uses ce_path_match instead of match_pathspec for tracked files. But ce_path_match does not implement pattern matching.

With this patch ce_path_match uses match_pathspec in order to perform pattern matching.

This also implies pattern matching for many other git commands, such as add -u, blame, log, etc. For "git log --follow", we have to disallow globbing pattern, because they potentially match more than one file.

Reported-by: Johannes Schneider <johannes@familieschneider.info>
Signed-off-by: Clemens Buchacher <drizzd@aon.at>
---
 builtin-log.c         |   13 ++++++++++++-
 read-cache.c          |   23 +----------------------
 t/t2200-add-update.sh |   10 ++++++++++
 3 files changed, 23 insertions(+), 23 deletions(-)
diff --git a/builtin-log.c b/builtin-log.c
index c7aa48e..16e9207 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -25,6 +25,17 @@ static int default_show_root = 1;
 static const char *fmt_patch_subject_prefix = "PATCH";
 static const char *fmt_pretty;
 
+static int has_special(const char *p)
+{
+	int x;
+
+	while ((x = *p++) != '\0')
+		if (isspecial(x))
+			return 1;
+
+	return 0;
+}
+
 static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		      struct rev_info *rev)
 {
@@ -49,7 +60,7 @@ static void cmd_log_init(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.nr_paths != 1)
+		if (rev->diffopt.nr_paths != 1 || has_special(rev->diffopt.paths[0]))
 			usage("git logs can only follow renames on one pathname at a time");
 	}
 	for (i = 1; i < argc; i++) {
diff --git a/read-cache.c b/read-cache.c
index b1475ff..8a5f306 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -640,28 +640,7 @@ int ce_same_name(struct cache_entry *a, struct cache_entry *b)
 
 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
 {
-	const char *match, *name;
-	int len;
-
-	if (!pathspec)
-		return 1;
-
-	len = ce_namelen(ce);
-	name = ce->name;
-	while ((match = *pathspec++) != NULL) {
-		int matchlen = strlen(match);
-		if (matchlen > len)
-			continue;
-		if (memcmp(name, match, matchlen))
-			continue;
-		if (matchlen && name[matchlen-1] == '/')
-			return 1;
-		if (name[matchlen] == '/' || !name[matchlen])
-			return 1;
-		if (!matchlen)
-			return 1;
-	}
-	return 0;
+	return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL);
 }
 
 /*
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
index cd9231c..6d99461 100755
--- a/t/t2200-add-update.sh
+++ b/t/t2200-add-update.sh
@@ -128,4 +128,14 @@ test_expect_success 'add -n -u should not add but just report' '
 
 '
 
+test_expect_success 'wildcard update' '
+
+	: >a.x &&
+	git add "*.x" &&
+	echo asdf >a.x &&
+	git add -u "*.x" &&
+	test -z "`git ls-files -m a.x`"
+
+'
+
 test_done
-- 
1.6.1
Previous: Clemens BuchacherNext: Clemens Buchacher
Message 4 of 16 in “fix "git add" pattern matching”
  1. 0/3 fix "git add" pattern matchingClemens Buchacher, Jan 14, 2009
  2. 1/3 clean up pathspec matchingClemens Buchacher, Jan 14, 2009
  3. 2/3 remove pathspec_match, use match_pathspec insteadClemens Buchacher, Jan 14, 2009
  4. 3/3 implement pattern matching in ce_path_matchClemens Buchacher, Jan 14, 2009
  5. Clemens BuchacherJan 14, 2009
  6. Johannes SchindelinJan 14, 2009
  7. Sverre RabbelierJan 14, 2009
  8. Samuel TardieuJan 14, 2009
  9. Jeff KingJan 14, 2009
  10. Junio C HamanoJan 14, 2009
  11. Clemens BuchacherJan 14, 2009
  12. Junio C HamanoJan 14, 2009
  13. Clemens BuchacherJan 15, 2009
  14. Junio C HamanoJan 16, 2009
  15. Johannes SchindelinJan 14, 2009
  16. Clemens BuchacherJan 14, 2009

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.