refspec: relocate apply_refspecs and related funtions

Move the functions `apply_refspecs()` and `apply_negative_refspecs()` from `remote.c` to `refspec.c`. These functions focus on applying 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>

Meet Soni committed Feb 4, 2025 at 09:35 UTC d549b6c9ff44d3ccb32b9bfe1816d3cfb1d7052a
4 files changed +44 -42
refspec.c
+32
@@ -9,6 +9,7 @@
9 #include "strvec.h"
10 #include "refs.h"
11 #include "refspec.h"
12 +#include "remote.h"
13 #include "strbuf.h"
14
15 /*
@@ -447,3 +448,34 @@ int refspec_find_match(struct refspec *rs, struct refspec_item *query)
448 }
449 return -1;
450 }
451 +
452 +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
453 +{
454 + struct ref **tail;
455 +
456 + for (tail = &ref_map; *tail; ) {
457 + struct ref *ref = *tail;
458 +
459 + if (refname_matches_negative_refspec_item(ref->name, rs)) {
460 + *tail = ref->next;
461 + free(ref->peer_ref);
462 + free(ref);
463 + } else
464 + tail = &ref->next;
465 + }
466 +
467 + return ref_map;
468 +}
469 +
470 +char *apply_refspecs(struct refspec *rs, const char *name)
471 +{
472 + struct refspec_item query;
473 +
474 + memset(&query, 0, sizeof(struct refspec_item));
475 + query.src = (char *)name;
476 +
477 + if (refspec_find_match(rs, &query))
478 + return NULL;
479 +
480 + return query.dst;
481 +}
refspec.h
+12
@@ -96,4 +96,16 @@ void refspec_find_all_matches(struct refspec *rs,
96 struct refspec_item *query,
97 struct string_list *results);
98
99 +/*
100 + * Remove all entries in the input list which match any negative refspec in
101 + * the refspec list.
102 + */
103 +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
104 +
105 +/*
106 + * Search for a refspec that matches the given name and return the
107 + * corresponding destination (dst) if a match is found, NULL otherwise.
108 + */
109 +char *apply_refspecs(struct refspec *rs, const char *name);
110 +
111 #endif /* REFSPEC_H */
remote.c
-31
@@ -907,37 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
907 }
908 }
909
910 -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
911 -{
912 - struct ref **tail;
913 -
914 - for (tail = &ref_map; *tail; ) {
915 - struct ref *ref = *tail;
916 -
917 - if (refname_matches_negative_refspec_item(ref->name, rs)) {
918 - *tail = ref->next;
919 - free(ref->peer_ref);
920 - free(ref);
921 - } else
922 - tail = &ref->next;
923 - }
924 -
925 - return ref_map;
926 -}
927 -
928 -char *apply_refspecs(struct refspec *rs, const char *name)
929 -{
930 - struct refspec_item query;
931 -
932 - memset(&query, 0, sizeof(struct refspec_item));
933 - query.src = (char *)name;
934 -
935 - if (refspec_find_match(rs, &query))
936 - return NULL;
937 -
938 - return query.dst;
939 -}
940 -
910 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
911 {
912 return refspec_find_match(&remote->fetch, refspec);
remote.h
-11
@@ -261,17 +261,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
261 */
262 struct ref *ref_remove_duplicates(struct ref *ref_map);
263
264 -int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
265 -
266 -/*
267 - * Remove all entries in the input list which match any negative refspec in
268 - * the refspec list.
269 - */
270 -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
271 -
272 -int refspec_find_match(struct refspec *rs, struct refspec_item *query);
273 -char *apply_refspecs(struct refspec *rs, const char *name);
274 -
264 int check_push_refs(struct ref *src, struct refspec *rs);
265 int match_push_refs(struct ref *src, struct ref **dst,
266 struct refspec *rs, int flags);