Skip to content

Commit bef0e12

Browse files
KarthikNayakgitster
authored andcommitted
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>
1 parent 90c0040 commit bef0e12

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

builtin/for-each-ref.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
6868
git_config(git_default_config, NULL);
6969

7070
filter.name_patterns = argv;
71+
filter.match_as_path = 1;
7172
filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
7273
ref_array_sort(sorting, &array);
7374

ref-filter.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,11 +1159,35 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
11591159
return is_descendant_of(commit, filter->with_commit);
11601160
}
11611161

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+
11621186
/*
11631187
* Return 1 if the refname matches one of the patterns, otherwise 0.
11641188
* 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).
11671191
*/
11681192
static int match_name_as_path(const char **pattern, const char *refname)
11691193
{
@@ -1184,6 +1208,16 @@ static int match_name_as_path(const char **pattern, const char *refname)
11841208
return 0;
11851209
}
11861210

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+
11871221
/*
11881222
* Given a ref (sha1, refname), check if the ref belongs to the array
11891223
* 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,
12861320
if (!(kind & filter->kind))
12871321
return 0;
12881322

1289-
if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
1323+
if (!filter_pattern_match(filter, refname))
12901324
return 0;
12911325

12921326
if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))

ref-filter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ struct ref_filter {
5959
} merge;
6060
struct commit *merge_commit;
6161

62-
unsigned int with_commit_tag_algo : 1;
62+
unsigned int with_commit_tag_algo : 1,
63+
match_as_path : 1;
6364
unsigned int kind,
6465
lines;
6566
};

0 commit comments

Comments
 (0)