From 7b24a170d2c36c83c3669d194af46a09ccdeec43 Mon Sep 17 00:00:00 2001 From: Meet Soni Date: Tue, 4 Feb 2025 09:35:57 +0530 Subject: refspec: relocate matching related functions Move the functions `refspec_find_match()`, `refspec_find_all_matches()` and `refspec_find_negative_match()` from `remote.c` to `refspec.c`. These functions focus on matching refspecs, so centralizing them in `refspec.c` improves code organization by keeping refspec-related logic in one place. Signed-off-by: Meet Soni Signed-off-by: Junio C Hamano --- refspec.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) (limited to 'refspec.c') diff --git a/refspec.c b/refspec.c index b447768304..cab0b0d127 100644 --- a/refspec.c +++ b/refspec.c @@ -5,6 +5,7 @@ #include "gettext.h" #include "hash.h" #include "hex.h" +#include "string-list.h" #include "strvec.h" #include "refs.h" #include "refspec.h" @@ -324,3 +325,125 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r } return 0; } + +static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query) +{ + int i, matched_negative = 0; + int find_src = !query->src; + struct string_list reversed = STRING_LIST_INIT_DUP; + const char *needle = find_src ? query->dst : query->src; + + /* + * Check whether the queried ref matches any negative refpsec. If so, + * then we should ultimately treat this as not matching the query at + * all. + * + * Note that negative refspecs always match the source, but the query + * item uses the destination. To handle this, we apply pattern + * refspecs in reverse to figure out if the query source matches any + * of the negative refspecs. + * + * The first loop finds and expands all positive refspecs + * matched by the queried ref. + * + * The second loop checks if any of the results of the first loop + * match any negative refspec. + */ + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + char *expn_name; + + if (refspec->negative) + continue; + + /* Note the reversal of src and dst */ + if (refspec->pattern) { + const char *key = refspec->dst ? refspec->dst : refspec->src; + const char *value = refspec->src; + + if (match_name_with_pattern(key, needle, value, &expn_name)) + string_list_append_nodup(&reversed, expn_name); + } else if (refspec->matching) { + /* For the special matching refspec, any query should match */ + string_list_append(&reversed, needle); + } else if (!refspec->src) { + BUG("refspec->src should not be null here"); + } else if (!strcmp(needle, refspec->src)) { + string_list_append(&reversed, refspec->src); + } + } + + for (i = 0; !matched_negative && i < reversed.nr; i++) { + if (refname_matches_negative_refspec_item(reversed.items[i].string, rs)) + matched_negative = 1; + } + + string_list_clear(&reversed, 0); + + return matched_negative; +} + +void refspec_find_all_matches(struct refspec *rs, + struct refspec_item *query, + struct string_list *results) +{ + int i; + int find_src = !query->src; + + if (find_src && !query->dst) + BUG("refspec_find_all_matches: need either src or dst"); + + if (refspec_find_negative_match(rs, query)) + return; + + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + const char *key = find_src ? refspec->dst : refspec->src; + const char *value = find_src ? refspec->src : refspec->dst; + const char *needle = find_src ? query->dst : query->src; + char **result = find_src ? &query->src : &query->dst; + + if (!refspec->dst || refspec->negative) + continue; + if (refspec->pattern) { + if (match_name_with_pattern(key, needle, value, result)) + string_list_append_nodup(results, *result); + } else if (!strcmp(needle, key)) { + string_list_append(results, value); + } + } +} + +int refspec_find_match(struct refspec *rs, struct refspec_item *query) +{ + int i; + int find_src = !query->src; + const char *needle = find_src ? query->dst : query->src; + char **result = find_src ? &query->src : &query->dst; + + if (find_src && !query->dst) + BUG("refspec_find_match: need either src or dst"); + + if (refspec_find_negative_match(rs, query)) + return -1; + + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + const char *key = find_src ? refspec->dst : refspec->src; + const char *value = find_src ? refspec->src : refspec->dst; + + if (!refspec->dst || refspec->negative) + continue; + if (refspec->pattern) { + if (match_name_with_pattern(key, needle, value, result)) { + query->force = refspec->force; + return 0; + } + } else if (!strcmp(needle, key)) { + *result = xstrdup(value); + query->force = refspec->force; + return 0; + } + } + return -1; +} -- cgit v1.2.3