Skip to content

Commit 7b24a17

Browse files
inosmeetgitster
authored andcommitted
refspec: relocate matching related functions
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be0905f commit 7b24a17

File tree

3 files changed

+139
-122
lines changed

3 files changed

+139
-122
lines changed

refspec.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "gettext.h"
66
#include "hash.h"
77
#include "hex.h"
8+
#include "string-list.h"
89
#include "strvec.h"
910
#include "refs.h"
1011
#include "refspec.h"
@@ -324,3 +325,125 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
324325
}
325326
return 0;
326327
}
328+
329+
static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
330+
{
331+
int i, matched_negative = 0;
332+
int find_src = !query->src;
333+
struct string_list reversed = STRING_LIST_INIT_DUP;
334+
const char *needle = find_src ? query->dst : query->src;
335+
336+
/*
337+
* Check whether the queried ref matches any negative refpsec. If so,
338+
* then we should ultimately treat this as not matching the query at
339+
* all.
340+
*
341+
* Note that negative refspecs always match the source, but the query
342+
* item uses the destination. To handle this, we apply pattern
343+
* refspecs in reverse to figure out if the query source matches any
344+
* of the negative refspecs.
345+
*
346+
* The first loop finds and expands all positive refspecs
347+
* matched by the queried ref.
348+
*
349+
* The second loop checks if any of the results of the first loop
350+
* match any negative refspec.
351+
*/
352+
for (i = 0; i < rs->nr; i++) {
353+
struct refspec_item *refspec = &rs->items[i];
354+
char *expn_name;
355+
356+
if (refspec->negative)
357+
continue;
358+
359+
/* Note the reversal of src and dst */
360+
if (refspec->pattern) {
361+
const char *key = refspec->dst ? refspec->dst : refspec->src;
362+
const char *value = refspec->src;
363+
364+
if (match_name_with_pattern(key, needle, value, &expn_name))
365+
string_list_append_nodup(&reversed, expn_name);
366+
} else if (refspec->matching) {
367+
/* For the special matching refspec, any query should match */
368+
string_list_append(&reversed, needle);
369+
} else if (!refspec->src) {
370+
BUG("refspec->src should not be null here");
371+
} else if (!strcmp(needle, refspec->src)) {
372+
string_list_append(&reversed, refspec->src);
373+
}
374+
}
375+
376+
for (i = 0; !matched_negative && i < reversed.nr; i++) {
377+
if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
378+
matched_negative = 1;
379+
}
380+
381+
string_list_clear(&reversed, 0);
382+
383+
return matched_negative;
384+
}
385+
386+
void refspec_find_all_matches(struct refspec *rs,
387+
struct refspec_item *query,
388+
struct string_list *results)
389+
{
390+
int i;
391+
int find_src = !query->src;
392+
393+
if (find_src && !query->dst)
394+
BUG("refspec_find_all_matches: need either src or dst");
395+
396+
if (refspec_find_negative_match(rs, query))
397+
return;
398+
399+
for (i = 0; i < rs->nr; i++) {
400+
struct refspec_item *refspec = &rs->items[i];
401+
const char *key = find_src ? refspec->dst : refspec->src;
402+
const char *value = find_src ? refspec->src : refspec->dst;
403+
const char *needle = find_src ? query->dst : query->src;
404+
char **result = find_src ? &query->src : &query->dst;
405+
406+
if (!refspec->dst || refspec->negative)
407+
continue;
408+
if (refspec->pattern) {
409+
if (match_name_with_pattern(key, needle, value, result))
410+
string_list_append_nodup(results, *result);
411+
} else if (!strcmp(needle, key)) {
412+
string_list_append(results, value);
413+
}
414+
}
415+
}
416+
417+
int refspec_find_match(struct refspec *rs, struct refspec_item *query)
418+
{
419+
int i;
420+
int find_src = !query->src;
421+
const char *needle = find_src ? query->dst : query->src;
422+
char **result = find_src ? &query->src : &query->dst;
423+
424+
if (find_src && !query->dst)
425+
BUG("refspec_find_match: need either src or dst");
426+
427+
if (refspec_find_negative_match(rs, query))
428+
return -1;
429+
430+
for (i = 0; i < rs->nr; i++) {
431+
struct refspec_item *refspec = &rs->items[i];
432+
const char *key = find_src ? refspec->dst : refspec->src;
433+
const char *value = find_src ? refspec->src : refspec->dst;
434+
435+
if (!refspec->dst || refspec->negative)
436+
continue;
437+
if (refspec->pattern) {
438+
if (match_name_with_pattern(key, needle, value, result)) {
439+
query->force = refspec->force;
440+
return 0;
441+
}
442+
} else if (!strcmp(needle, key)) {
443+
*result = xstrdup(value);
444+
query->force = refspec->force;
445+
return 0;
446+
}
447+
}
448+
return -1;
449+
}

refspec.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct refspec_item {
3030
char *raw;
3131
};
3232

33+
struct string_list;
34+
3335
#define REFSPEC_FETCH 1
3436
#define REFSPEC_PUSH 0
3537

@@ -80,4 +82,18 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
8082
int match_name_with_pattern(const char *key, const char *name,
8183
const char *value, char **result);
8284

85+
/*
86+
* Queries a refspec for a match and updates the query item.
87+
* Returns 0 on success, -1 if no match is found or negative refspec matches.
88+
*/
89+
int refspec_find_match(struct refspec *rs, struct refspec_item *query);
90+
91+
/*
92+
* Queries a refspec for all matches and appends results to the provided string
93+
* list.
94+
*/
95+
void refspec_find_all_matches(struct refspec *rs,
96+
struct refspec_item *query,
97+
struct string_list *results);
98+
8399
#endif /* REFSPEC_H */

remote.c

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -925,128 +925,6 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
925925
return ref_map;
926926
}
927927

928-
static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
929-
{
930-
int i, matched_negative = 0;
931-
int find_src = !query->src;
932-
struct string_list reversed = STRING_LIST_INIT_DUP;
933-
const char *needle = find_src ? query->dst : query->src;
934-
935-
/*
936-
* Check whether the queried ref matches any negative refpsec. If so,
937-
* then we should ultimately treat this as not matching the query at
938-
* all.
939-
*
940-
* Note that negative refspecs always match the source, but the query
941-
* item uses the destination. To handle this, we apply pattern
942-
* refspecs in reverse to figure out if the query source matches any
943-
* of the negative refspecs.
944-
*
945-
* The first loop finds and expands all positive refspecs
946-
* matched by the queried ref.
947-
*
948-
* The second loop checks if any of the results of the first loop
949-
* match any negative refspec.
950-
*/
951-
for (i = 0; i < rs->nr; i++) {
952-
struct refspec_item *refspec = &rs->items[i];
953-
char *expn_name;
954-
955-
if (refspec->negative)
956-
continue;
957-
958-
/* Note the reversal of src and dst */
959-
if (refspec->pattern) {
960-
const char *key = refspec->dst ? refspec->dst : refspec->src;
961-
const char *value = refspec->src;
962-
963-
if (match_name_with_pattern(key, needle, value, &expn_name))
964-
string_list_append_nodup(&reversed, expn_name);
965-
} else if (refspec->matching) {
966-
/* For the special matching refspec, any query should match */
967-
string_list_append(&reversed, needle);
968-
} else if (!refspec->src) {
969-
BUG("refspec->src should not be null here");
970-
} else if (!strcmp(needle, refspec->src)) {
971-
string_list_append(&reversed, refspec->src);
972-
}
973-
}
974-
975-
for (i = 0; !matched_negative && i < reversed.nr; i++) {
976-
if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
977-
matched_negative = 1;
978-
}
979-
980-
string_list_clear(&reversed, 0);
981-
982-
return matched_negative;
983-
}
984-
985-
static void refspec_find_all_matches(struct refspec *rs,
986-
struct refspec_item *query,
987-
struct string_list *results)
988-
{
989-
int i;
990-
int find_src = !query->src;
991-
992-
if (find_src && !query->dst)
993-
BUG("refspec_find_all_matches: need either src or dst");
994-
995-
if (refspec_find_negative_match(rs, query))
996-
return;
997-
998-
for (i = 0; i < rs->nr; i++) {
999-
struct refspec_item *refspec = &rs->items[i];
1000-
const char *key = find_src ? refspec->dst : refspec->src;
1001-
const char *value = find_src ? refspec->src : refspec->dst;
1002-
const char *needle = find_src ? query->dst : query->src;
1003-
char **result = find_src ? &query->src : &query->dst;
1004-
1005-
if (!refspec->dst || refspec->negative)
1006-
continue;
1007-
if (refspec->pattern) {
1008-
if (match_name_with_pattern(key, needle, value, result))
1009-
string_list_append_nodup(results, *result);
1010-
} else if (!strcmp(needle, key)) {
1011-
string_list_append(results, value);
1012-
}
1013-
}
1014-
}
1015-
1016-
int refspec_find_match(struct refspec *rs, struct refspec_item *query)
1017-
{
1018-
int i;
1019-
int find_src = !query->src;
1020-
const char *needle = find_src ? query->dst : query->src;
1021-
char **result = find_src ? &query->src : &query->dst;
1022-
1023-
if (find_src && !query->dst)
1024-
BUG("refspec_find_match: need either src or dst");
1025-
1026-
if (refspec_find_negative_match(rs, query))
1027-
return -1;
1028-
1029-
for (i = 0; i < rs->nr; i++) {
1030-
struct refspec_item *refspec = &rs->items[i];
1031-
const char *key = find_src ? refspec->dst : refspec->src;
1032-
const char *value = find_src ? refspec->src : refspec->dst;
1033-
1034-
if (!refspec->dst || refspec->negative)
1035-
continue;
1036-
if (refspec->pattern) {
1037-
if (match_name_with_pattern(key, needle, value, result)) {
1038-
query->force = refspec->force;
1039-
return 0;
1040-
}
1041-
} else if (!strcmp(needle, key)) {
1042-
*result = xstrdup(value);
1043-
query->force = refspec->force;
1044-
return 0;
1045-
}
1046-
}
1047-
return -1;
1048-
}
1049-
1050928
char *apply_refspecs(struct refspec *rs, const char *name)
1051929
{
1052930
struct refspec_item query;

0 commit comments

Comments
 (0)