Skip to content

Commit c500352

Browse files
carlosmngitster
authored andcommitted
remote: separate out the remote_find_tracking logic into query_refspecs
Move the body of remote_find_tracking() to a new helper query_refspecs() that finds a refspec that matches and applies the transformation, but explicitly takes the list of refspecs, and make remote_find_tracking() a thin wrapper of it. Make apply_refspecs() also use query_refspecs(). Signed-off-by: Carlos Martín Nieto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43a8a04 commit c500352

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

remote.c

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -828,59 +828,56 @@ static int match_name_with_pattern(const char *key, const char *name,
828828
return ret;
829829
}
830830

831-
char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
832-
const char *name)
831+
static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query)
833832
{
834833
int i;
835-
char *ret = NULL;
836-
for (i = 0; i < nr_refspec; i++) {
837-
struct refspec *refspec = refspecs + i;
838-
if (refspec->pattern) {
839-
if (match_name_with_pattern(refspec->src, name,
840-
refspec->dst, &ret))
841-
return ret;
842-
} else if (!strcmp(refspec->src, name))
843-
return strdup(refspec->dst);
844-
}
845-
return NULL;
846-
}
834+
int find_src = !query->src;
847835

848-
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
849-
{
850-
int find_src = refspec->src == NULL;
851-
char *needle, **result;
852-
int i;
836+
if (find_src && !query->dst)
837+
return error("query_refspecs: need either src or dst");
853838

854-
if (find_src) {
855-
if (!refspec->dst)
856-
return error("find_tracking: need either src or dst");
857-
needle = refspec->dst;
858-
result = &refspec->src;
859-
} else {
860-
needle = refspec->src;
861-
result = &refspec->dst;
862-
}
839+
for (i = 0; i < ref_count; i++) {
840+
struct refspec *refspec = &refs[i];
841+
const char *key = find_src ? refspec->dst : refspec->src;
842+
const char *value = find_src ? refspec->src : refspec->dst;
843+
const char *needle = find_src ? query->dst : query->src;
844+
char **result = find_src ? &query->src : &query->dst;
863845

864-
for (i = 0; i < remote->fetch_refspec_nr; i++) {
865-
struct refspec *fetch = &remote->fetch[i];
866-
const char *key = find_src ? fetch->dst : fetch->src;
867-
const char *value = find_src ? fetch->src : fetch->dst;
868-
if (!fetch->dst)
846+
if (!refspec->dst)
869847
continue;
870-
if (fetch->pattern) {
848+
if (refspec->pattern) {
871849
if (match_name_with_pattern(key, needle, value, result)) {
872-
refspec->force = fetch->force;
850+
query->force = refspec->force;
873851
return 0;
874852
}
875853
} else if (!strcmp(needle, key)) {
876854
*result = xstrdup(value);
877-
refspec->force = fetch->force;
855+
query->force = refspec->force;
878856
return 0;
879857
}
880858
}
881859
return -1;
882860
}
883861

862+
char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
863+
const char *name)
864+
{
865+
struct refspec query;
866+
867+
memset(&query, 0, sizeof(struct refspec));
868+
query.src = (char *)name;
869+
870+
if (query_refspecs(refspecs, nr_refspec, &query))
871+
return NULL;
872+
873+
return query.dst;
874+
}
875+
876+
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
877+
{
878+
return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
879+
}
880+
884881
static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
885882
const char *name)
886883
{

0 commit comments

Comments
 (0)