refspec: relocate refname_matches_negative_refspec_item

Move the functions `refname_matches_negative_refspec_item()`, `refspec_match()`, and `match_name_with_pattern()` from `remote.c` to `refspec.c`. These functions focus on refspec matching, so placing them in `refspec.c` aligns with the separation of concerns. Keep refspec-related logic in `refspec.c` and remote-specific logic in `remote.c` for better code organization. Signed-off-by: Meet Soni <meetsoni3017@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Meet Soni committed Feb 4, 2025 at 09:35 UTC 230d022fe3ba67465e867ac1fa4da3bcf0d31630
3 files changed +57 -48
refspec.c
+48
@@ -276,3 +276,51 @@ void refspec_ref_prefixes(const struct refspec *rs,
276 }
277 }
278 }
279 +
280 +int match_name_with_pattern(const char *key, const char *name,
281 + const char *value, char **result)
282 +{
283 + const char *kstar = strchr(key, '*');
284 + size_t klen;
285 + size_t ksuffixlen;
286 + size_t namelen;
287 + int ret;
288 + if (!kstar)
289 + die(_("key '%s' of pattern had no '*'"), key);
290 + klen = kstar - key;
291 + ksuffixlen = strlen(kstar + 1);
292 + namelen = strlen(name);
293 + ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
294 + !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
295 + if (ret && value) {
296 + struct strbuf sb = STRBUF_INIT;
297 + const char *vstar = strchr(value, '*');
298 + if (!vstar)
299 + die(_("value '%s' of pattern has no '*'"), value);
300 + strbuf_add(&sb, value, vstar - value);
301 + strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
302 + strbuf_addstr(&sb, vstar + 1);
303 + *result = strbuf_detach(&sb, NULL);
304 + }
305 + return ret;
306 +}
307 +
308 +static int refspec_match(const struct refspec_item *refspec,
309 + const char *name)
310 +{
311 + if (refspec->pattern)
312 + return match_name_with_pattern(refspec->src, name, NULL, NULL);
313 +
314 + return !strcmp(refspec->src, name);
315 +}
316 +
317 +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
318 +{
319 + int i;
320 +
321 + for (i = 0; i < rs->nr; i++) {
322 + if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
323 + return 1;
324 + }
325 + return 0;
326 +}
refspec.h
+9
@@ -71,4 +71,13 @@ struct strvec;
71 void refspec_ref_prefixes(const struct refspec *rs,
72 struct strvec *ref_prefixes);
73
74 +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
75 +
76 +/*
77 + * Checks whether a name matches a pattern and optionally generates a result.
78 + * Returns 1 if the name matches the pattern, 0 otherwise.
79 + */
80 +int match_name_with_pattern(const char *key, const char *name,
81 + const char *value, char **result);
82 +
83 #endif /* REFSPEC_H */
remote.c
-48
@@ -907,54 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
907 }
908 }
909
910 -static int match_name_with_pattern(const char *key, const char *name,
911 - const char *value, char **result)
912 -{
913 - const char *kstar = strchr(key, '*');
914 - size_t klen;
915 - size_t ksuffixlen;
916 - size_t namelen;
917 - int ret;
918 - if (!kstar)
919 - die(_("key '%s' of pattern had no '*'"), key);
920 - klen = kstar - key;
921 - ksuffixlen = strlen(kstar + 1);
922 - namelen = strlen(name);
923 - ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
924 - !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
925 - if (ret && value) {
926 - struct strbuf sb = STRBUF_INIT;
927 - const char *vstar = strchr(value, '*');
928 - if (!vstar)
929 - die(_("value '%s' of pattern has no '*'"), value);
930 - strbuf_add(&sb, value, vstar - value);
931 - strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
932 - strbuf_addstr(&sb, vstar + 1);
933 - *result = strbuf_detach(&sb, NULL);
934 - }
935 - return ret;
936 -}
937 -
938 -static int refspec_match(const struct refspec_item *refspec,
939 - const char *name)
940 -{
941 - if (refspec->pattern)
942 - return match_name_with_pattern(refspec->src, name, NULL, NULL);
943 -
944 - return !strcmp(refspec->src, name);
945 -}
946 -
947 -int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
948 -{
949 - int i;
950 -
951 - for (i = 0; i < rs->nr; i++) {
952 - if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
953 - return 1;
954 - }
955 - return 0;
956 -}
957 -
910 struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
911 {
912 struct ref **tail;