diff --git a/builtin/push.c b/builtin/push.c index 90de3746b5..92d530e5c4 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -78,7 +78,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref, .src = matched->name, }; - if (!query_refspecs(&remote->push, &query) && query.dst) { + if (!refspec_find_match(&remote->push, &query) && query.dst) { refspec_appendf(refspec, "%s%s:%s", query.force ? "+" : "", query.src, query.dst); diff --git a/builtin/remote.c b/builtin/remote.c index 71d84fb3cf..816d482340 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -383,7 +383,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat states->remote->fetch.items[i].raw); for (ref = fetch_map; ref; ref = ref->next) { - if (omit_name_by_refspec(ref->name, &states->remote->fetch)) + if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch)) string_list_append(&states->skipped, abbrev_branch(ref->name)); else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name)) string_list_append(&states->new_refs, abbrev_branch(ref->name)); diff --git a/refspec.c b/refspec.c index 6d86e04442..0dbbd1e799 100644 --- a/refspec.c +++ b/refspec.c @@ -5,9 +5,11 @@ #include "gettext.h" #include "hash.h" #include "hex.h" +#include "string-list.h" #include "strvec.h" #include "refs.h" #include "refspec.h" +#include "remote.h" #include "strbuf.h" /* @@ -276,3 +278,204 @@ void refspec_ref_prefixes(const struct refspec *rs, } } } + +int match_name_with_pattern(const char *key, const char *name, + const char *value, char **result) +{ + const char *kstar = strchr(key, '*'); + size_t klen; + size_t ksuffixlen; + size_t namelen; + int ret; + if (!kstar) + die(_("key '%s' of pattern had no '*'"), key); + klen = kstar - key; + ksuffixlen = strlen(kstar + 1); + namelen = strlen(name); + ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && + !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); + if (ret && value) { + struct strbuf sb = STRBUF_INIT; + const char *vstar = strchr(value, '*'); + if (!vstar) + die(_("value '%s' of pattern has no '*'"), value); + strbuf_add(&sb, value, vstar - value); + strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen); + strbuf_addstr(&sb, vstar + 1); + *result = strbuf_detach(&sb, NULL); + } + return ret; +} + +static int refspec_match(const struct refspec_item *refspec, + const char *name) +{ + if (refspec->pattern) + return match_name_with_pattern(refspec->src, name, NULL, NULL); + + return !strcmp(refspec->src, name); +} + +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs) +{ + int i; + + for (i = 0; i < rs->nr; i++) { + if (rs->items[i].negative && refspec_match(&rs->items[i], refname)) + return 1; + } + 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; +} + +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) +{ + struct ref **tail; + + for (tail = &ref_map; *tail; ) { + struct ref *ref = *tail; + + if (refname_matches_negative_refspec_item(ref->name, rs)) { + *tail = ref->next; + free(ref->peer_ref); + free(ref); + } else + tail = &ref->next; + } + + return ref_map; +} + +char *apply_refspecs(struct refspec *rs, const char *name) +{ + struct refspec_item query; + + memset(&query, 0, sizeof(struct refspec_item)); + query.src = (char *)name; + + if (refspec_find_match(rs, &query)) + return NULL; + + return query.dst; +} diff --git a/refspec.h b/refspec.h index 69d693c87d..2a28d043be 100644 --- a/refspec.h +++ b/refspec.h @@ -30,6 +30,8 @@ struct refspec_item { char *raw; }; +struct string_list; + #define REFSPEC_FETCH 1 #define REFSPEC_PUSH 0 @@ -71,4 +73,39 @@ struct strvec; void refspec_ref_prefixes(const struct refspec *rs, struct strvec *ref_prefixes); +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs); + +/* + * Checks whether a name matches a pattern and optionally generates a result. + * Returns 1 if the name matches the pattern, 0 otherwise. + */ +int match_name_with_pattern(const char *key, const char *name, + const char *value, char **result); + +/* + * Queries a refspec for a match and updates the query item. + * Returns 0 on success, -1 if no match is found or negative refspec matches. + */ +int refspec_find_match(struct refspec *rs, struct refspec_item *query); + +/* + * Queries a refspec for all matches and appends results to the provided string + * list. + */ +void refspec_find_all_matches(struct refspec *rs, + struct refspec_item *query, + struct string_list *results); + +/* + * Remove all entries in the input list which match any negative refspec in + * the refspec list. + */ +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs); + +/* + * Search for a refspec that matches the given name and return the + * corresponding destination (dst) if a match is found, NULL otherwise. + */ +char *apply_refspecs(struct refspec *rs, const char *name); + #endif /* REFSPEC_H */ diff --git a/remote.c b/remote.c index 1779f0e7bb..4bef6a1398 100644 --- a/remote.c +++ b/remote.c @@ -933,210 +933,9 @@ void ref_push_report_free(struct ref_push_report *report) } } -static int match_name_with_pattern(const char *key, const char *name, - const char *value, char **result) -{ - const char *kstar = strchr(key, '*'); - size_t klen; - size_t ksuffixlen; - size_t namelen; - int ret; - if (!kstar) - die(_("key '%s' of pattern had no '*'"), key); - klen = kstar - key; - ksuffixlen = strlen(kstar + 1); - namelen = strlen(name); - ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && - !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); - if (ret && value) { - struct strbuf sb = STRBUF_INIT; - const char *vstar = strchr(value, '*'); - if (!vstar) - die(_("value '%s' of pattern has no '*'"), value); - strbuf_add(&sb, value, vstar - value); - strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen); - strbuf_addstr(&sb, vstar + 1); - *result = strbuf_detach(&sb, NULL); - } - return ret; -} - -static int refspec_match(const struct refspec_item *refspec, - const char *name) -{ - if (refspec->pattern) - return match_name_with_pattern(refspec->src, name, NULL, NULL); - - return !strcmp(refspec->src, name); -} - -int omit_name_by_refspec(const char *name, struct refspec *rs) -{ - int i; - - for (i = 0; i < rs->nr; i++) { - if (rs->items[i].negative && refspec_match(&rs->items[i], name)) - return 1; - } - return 0; -} - -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) -{ - struct ref **tail; - - for (tail = &ref_map; *tail; ) { - struct ref *ref = *tail; - - if (omit_name_by_refspec(ref->name, rs)) { - *tail = ref->next; - free(ref->peer_ref); - free(ref); - } else - tail = &ref->next; - } - - return ref_map; -} - -static int query_matches_negative_refspec(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 (omit_name_by_refspec(reversed.items[i].string, rs)) - matched_negative = 1; - } - - string_list_clear(&reversed, 0); - - return matched_negative; -} - -static void query_refspecs_multiple(struct refspec *rs, - struct refspec_item *query, - struct string_list *results) -{ - int i; - int find_src = !query->src; - - if (find_src && !query->dst) - BUG("query_refspecs_multiple: need either src or dst"); - - if (query_matches_negative_refspec(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 query_refspecs(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("query_refspecs: need either src or dst"); - - if (query_matches_negative_refspec(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; -} - -char *apply_refspecs(struct refspec *rs, const char *name) -{ - struct refspec_item query; - - memset(&query, 0, sizeof(struct refspec_item)); - query.src = (char *)name; - - if (query_refspecs(rs, &query)) - return NULL; - - return query.dst; -} - int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) { - return query_refspecs(&remote->fetch, refspec); + return refspec_find_match(&remote->fetch, refspec); } static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, @@ -2561,7 +2360,7 @@ static int get_stale_heads_cb(const char *refname, const char *referent UNUSED, memset(&query, 0, sizeof(struct refspec_item)); query.dst = (char *)refname; - query_refspecs_multiple(info->rs, &query, &matches); + refspec_find_all_matches(info->rs, &query, &matches); if (matches.nr == 0) goto clean_exit; /* No matches */ diff --git a/remote.h b/remote.h index a19353f689..d3978cdb8d 100644 --- a/remote.h +++ b/remote.h @@ -263,21 +263,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list); */ struct ref *ref_remove_duplicates(struct ref *ref_map); -/* - * Check whether a name matches any negative refspec in rs. Returns 1 if the - * name matches at least one negative refspec, and 0 otherwise. - */ -int omit_name_by_refspec(const char *name, struct refspec *rs); - -/* - * Remove all entries in the input list which match any negative refspec in - * the refspec list. - */ -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs); - -int query_refspecs(struct refspec *rs, struct refspec_item *query); -char *apply_refspecs(struct refspec *rs, const char *name); - int check_push_refs(struct ref *src, struct refspec *rs); int match_push_refs(struct ref *src, struct ref **dst, struct refspec *rs, int flags);