]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refspec: relocate matching related functions
authorMeet Soni <meetsoni3017@gmail.com>
Tue, 4 Feb 2025 04:05:57 +0000 (09:35 +0530)
committerJunio C Hamano <gitster@pobox.com>
Tue, 4 Feb 2025 17:51:41 +0000 (09:51 -0800)
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 <meetsoni3017@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refspec.c
refspec.h
remote.c

index b447768304bd83b031caab23b45b7b3432bdf790..cab0b0d1273ddd1a4ccc0f802e8c1b2d8db05963 100644 (file)
--- 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;
+}
index 584d9c9eb55f0d6ff9d298ec62aaf6a39bf3bf21..be20ba53ab7ca8cba0196d05df6a6b7c82eb272f 100644 (file)
--- 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 */
index b510809a56be6a3f455a2c3abd91fc2f18244f40..4c5940482f0ad9bf70eb7e1344da39e18b8cb9cd 100644 (file)
--- 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;