ref-filter.c: parameterize match functions over patterns

`match_pattern()` and `match_name_as_path()` both take a `struct ref_filter *`, and then store a stack variable `patterns` pointing at `filter->patterns`. The subsequent patch will add a new array of patterns to match over (the excluded patterns, via a new `git for-each-ref --exclude` option), treating the return value of these functions differently depending on which patterns are being used to match. Tweak `match_pattern()` and `match_name_as_path()` to take an array of patterns to prepare for passing either in. Once we start passing either in, `match_pattern()` will have little to do with a particular `struct ref_filter *` instance. To clarify this, drop it from the argument list, and replace it with the only bit of the `ref_filter` that we care about (`filter->ignore_case`). Co-authored-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 10, 2023 at 17:12 UTC 284c55deb53921457929ca2afe231c48e6cada85
1 file changed +10 -8
ref-filter.c
+10 -8
@@ -2104,12 +2104,12 @@ static int get_ref_atom_value(struct ref_array_item *ref, int atom,
2104 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2105 * matches "refs/heads/mas*", too).
2106 */
2107 -static int match_pattern(const struct ref_filter *filter, const char *refname)
2107 +static int match_pattern(const char **patterns, const char *refname,
2108 + int ignore_case)
2109 {
2109 - const char **patterns = filter->name_patterns;
2110 unsigned flags = 0;
2111
2112 - if (filter->ignore_case)
2112 + if (ignore_case)
2113 flags |= WM_CASEFOLD;
2114
2115 /*
@@ -2134,13 +2134,13 @@ static int match_pattern(const struct ref_filter *filter, const char *refname)
2134 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2135 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
2136 */
2137 -static int match_name_as_path(const struct ref_filter *filter, const char *refname)
2137 +static int match_name_as_path(const char **pattern, const char *refname,
2138 + int ignore_case)
2139 {
2139 - const char **pattern = filter->name_patterns;
2140 int namelen = strlen(refname);
2141 unsigned flags = WM_PATHNAME;
2142
2143 - if (filter->ignore_case)
2143 + if (ignore_case)
2144 flags |= WM_CASEFOLD;
2145
2146 for (; *pattern; pattern++) {
@@ -2165,8 +2165,10 @@ static int filter_pattern_match(struct ref_filter *filter, const char *refname)
2165 if (!*filter->name_patterns)
2166 return 1; /* No pattern always matches */
2167 if (filter->match_as_path)
2168 - return match_name_as_path(filter, refname);
2169 - return match_pattern(filter, refname);
2168 + return match_name_as_path(filter->name_patterns, refname,
2169 + filter->ignore_case);
2170 + return match_pattern(filter->name_patterns, refname,
2171 + filter->ignore_case);
2172 }
2173
2174 /*