summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--refspec.c123
-rw-r--r--refspec.h16
-rw-r--r--remote.c122
3 files changed, 139 insertions, 122 deletions
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;
+}
diff --git a/refspec.h b/refspec.h
index 584d9c9eb5..be20ba53ab 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
@@ -80,4 +82,18 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
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);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index b510809a56..4c5940482f 100644
--- a/remote.c
+++ b/remote.c
@@ -925,128 +925,6 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
return ref_map;
}
-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;
-}
-
-static 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;
-}
-
char *apply_refspecs(struct refspec *rs, const char *name)
{
struct refspec_item query;