ref-filter: add option to match literal pattern
Since 'ref-filter' only has an option to match path names add an option for plain fnmatch pattern-matching. This is to support the pattern matching options which are used in `git tag -l` and `git branch -l` where we can match patterns like `git tag -l foo*` which would match all tags which has a "foo*" pattern. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karthik Nayak committed
Sep 10, 2015 at 21:18 UTC
bef0e12becd72b94d6bb9e48859742f55a4afb53
3 files changed
+40
-4
builtin/for-each-ref.c
+1
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
68
git_config(git_default_config, NULL);
69
70
filter.name_patterns = argv;
71
+ filter.match_as_path = 1;
72
filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
73
ref_array_sort(sorting, &array);
74
ref-filter.c
+37
-3
@@ -1159,11 +1159,35 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
1159
return is_descendant_of(commit, filter->with_commit);
1160
}
1161
1162
+/*
1163
+ * Return 1 if the refname matches one of the patterns, otherwise 0.
1164
+ * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1165
+ * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1166
+ * matches "refs/heads/mas*", too).
1167
+ */
1168
+static int match_pattern(const char **patterns, const char *refname)
1169
+{
1170
+ /*
1171
+ * When no '--format' option is given we need to skip the prefix
1172
+ * for matching refs of tags and branches.
1173
+ */
1174
+ (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1175
+ skip_prefix(refname, "refs/heads/", &refname) ||
1176
+ skip_prefix(refname, "refs/remotes/", &refname) ||
1177
+ skip_prefix(refname, "refs/", &refname));
1178
+
1179
+ for (; *patterns; patterns++) {
1180
+ if (!wildmatch(*patterns, refname, 0, NULL))
1181
+ return 1;
1182
+ }
1183
+ return 0;
1184
+}
1185
+
1186
/*
1187
* Return 1 if the refname matches one of the patterns, otherwise 0.
1188
* A pattern can be path prefix (e.g. a refname "refs/heads/master"
1165
- * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
1166
- * matches "refs/heads/m*",too).
1189
+ * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1190
+ * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1191
*/
1192
static int match_name_as_path(const char **pattern, const char *refname)
1193
{
@@ -1184,6 +1208,16 @@ static int match_name_as_path(const char **pattern, const char *refname)
1208
return 0;
1209
}
1210
1211
+/* Return 1 if the refname matches one of the patterns, otherwise 0. */
1212
+static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1213
+{
1214
+ if (!*filter->name_patterns)
1215
+ return 1; /* No pattern always matches */
1216
+ if (filter->match_as_path)
1217
+ return match_name_as_path(filter->name_patterns, refname);
1218
+ return match_pattern(filter->name_patterns, refname);
1219
+}
1220
+
1221
/*
1222
* Given a ref (sha1, refname), check if the ref belongs to the array
1223
* of sha1s. If the given ref is a tag, check if the given tag points
@@ -1286,7 +1320,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
1320
if (!(kind & filter->kind))
1321
return 0;
1322
1289
- if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
1323
+ if (!filter_pattern_match(filter, refname))
1324
return 0;
1325
1326
if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
ref-filter.h
+2
-1
@@ -59,7 +59,8 @@ struct ref_filter {
59
} merge;
60
struct commit *merge_commit;
61
62
- unsigned int with_commit_tag_algo : 1;
62
+ unsigned int with_commit_tag_algo : 1,
63
+ match_as_path : 1;
64
unsigned int kind,
65
lines;
66
};