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 <meetsoni3017@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Meet Soni 2025-02-04 09:35:57 +05:30 committed by Junio C Hamano
parent be0905fed1
commit 7b24a170d2
3 changed files with 139 additions and 122 deletions

123
refspec.c
View File

@ -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;
}

View File

@ -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 */

122
remote.c
View File

@ -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;