Commit 943700ecbbc2be4ef49c5c31d8e5c49353fd3d84

yorah 2013-01-18T16:37:13

Return the matched pathspec pattern in `git_pathspec_match_path` Instead of returning directly the pattern as the return value, I used an out parameter, because the function also tests if the passed pathspecs vector is empty. If yes, it considers that the path "matches", but in that case there is no matched pattern per se.

diff --git a/src/checkout.c b/src/checkout.c
index 40f5732..0ce283b 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -224,7 +224,7 @@ static int checkout_action_wd_only(
 	if (!git_pathspec_match_path(
 			pathspec, wd->path,
 			(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
-			git_iterator_ignore_case(workdir)))
+			git_iterator_ignore_case(workdir), NULL))
 		return 0;
 
 	/* check if item is tracked in the index but not in the checkout diff */
diff --git a/src/diff.c b/src/diff.c
index 4b60935..3e80285 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -47,6 +47,7 @@ static int diff_delta__from_one(
 	const git_index_entry *entry)
 {
 	git_diff_delta *delta;
+	const char *matched_pathspec;
 
 	if (status == GIT_DELTA_IGNORED &&
 		(diff->opts.flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
@@ -59,7 +60,7 @@ static int diff_delta__from_one(
 	if (!git_pathspec_match_path(
 			&diff->pathspec, entry->path,
 			(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
-			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
+			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
 		return 0;
 
 	delta = diff_delta__alloc(diff, status, entry->path);
@@ -419,13 +420,14 @@ static int maybe_modified(
 	unsigned int omode = oitem->mode;
 	unsigned int nmode = nitem->mode;
 	bool new_is_workdir = (new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
+	const char *matched_pathspec;
 
 	GIT_UNUSED(old_iter);
 
 	if (!git_pathspec_match_path(
 			&diff->pathspec, oitem->path,
 			(diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
-			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0))
+			(diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
 		return 0;
 
 	/* on platforms with no symlinks, preserve mode of existing symlinks */
diff --git a/src/index.c b/src/index.c
index 1e00dd3..f5bf954 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1710,7 +1710,8 @@ int git_index_read_tree_match(
 		goto cleanup;
 
 	while (entry != NULL) {
-		if (git_pathspec_match_path(&pathspec, entry->path, false, false) &&
+		if (git_pathspec_match_path(
+				&pathspec, entry->path, false, false, NULL) &&
 			(error = git_index_add(index, entry)) < 0)
 			goto cleanup;
 
diff --git a/src/pathspec.c b/src/pathspec.c
index 2bde3ba..7321802 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -106,14 +106,21 @@ void git_pathspec_free(git_vector *vspec)
 
 /* match a path against the vectorized pathspec */
 bool git_pathspec_match_path(
-	git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold)
+	git_vector *vspec,
+	const char *path,
+	bool disable_fnmatch,
+	bool casefold,
+	const char **matched_pathspec)
 {
-	unsigned int i;
+	size_t i;
 	git_attr_fnmatch *match;
 	int fnmatch_flags = 0;
 	int (*use_strcmp)(const char *, const char *);
 	int (*use_strncmp)(const char *, const char *, size_t);
 
+	if (matched_pathspec)
+		*matched_pathspec = NULL;
+
 	if (!vspec || !vspec->length)
 		return true;
 
@@ -143,8 +150,12 @@ bool git_pathspec_match_path(
 			path[match->length] == '/')
 			result = 0;
 
-		if (result == 0)
+		if (result == 0) {
+			if (matched_pathspec)
+				*matched_pathspec = match->pattern;
+
 			return (match->flags & GIT_ATTR_FNMATCH_NEGATIVE) ? false : true;
+		}
 	}
 
 	return false;
diff --git a/src/pathspec.h b/src/pathspec.h
index dde63c7..c445615 100644
--- a/src/pathspec.h
+++ b/src/pathspec.h
@@ -25,8 +25,16 @@ extern int git_pathspec_init(
 /* free data from the pathspec vector */
 extern void git_pathspec_free(git_vector *vspec);
 
-/* match a path against the vectorized pathspec */
+/*
+ * Match a path against the vectorized pathspec.
+ * The matched pathspec is passed back into the `matched_pathspec` parameter,
+ * unless it is passed as NULL by the caller.
+ */
 extern bool git_pathspec_match_path(
-	git_vector *vspec, const char *path, bool disable_fnmatch, bool casefold);
+	git_vector *vspec,
+	const char *path,
+	bool disable_fnmatch,
+	bool casefold,
+	const char **matched_pathspec);
 
 #endif