Skip to content

Commit 7c03353

Browse files
chriscoolgitster
authored andcommitted
promisor-remote: refactor how we parse advertised fields
In a follow up commit we are going to parse more fields, like a filter and a token, coming from the server when it advertises promisor remotes using the "promisor-remote" capability. To prepare for this, let's refactor the code that parses the advertised fields coming from the server into a new parse_one_advertised_remote() function that will populate a `struct promisor_info` with the content of the fields it parsed. While at it, let's also pass this `struct promisor_info` to the should_accept_remote() function, instead of passing it the parsed name and url. These changes will make it simpler to both parse more fields and access the content of these parsed fields in follow up commits. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c31bbc commit 7c03353

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

promisor-remote.c

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ enum accept_promisor {
538538
};
539539

540540
static int should_accept_remote(enum accept_promisor accept,
541-
const char *remote_name, const char *remote_url,
541+
struct promisor_info *advertised,
542542
struct promisor_info *config_info)
543543
{
544544
struct promisor_info *p;
545+
const char *remote_name = advertised->name;
546+
const char *remote_url = advertised->url;
545547

546548
if (accept == ACCEPT_ALL)
547549
return 1;
@@ -576,6 +578,46 @@ static int should_accept_remote(enum accept_promisor accept,
576578
return 0;
577579
}
578580

581+
static struct promisor_info *parse_one_advertised_remote(struct strbuf *remote_info)
582+
{
583+
struct promisor_info *info = xcalloc(1, sizeof(*info));
584+
struct strbuf **elems = strbuf_split(remote_info, ',');
585+
586+
for (size_t i = 0; elems[i]; i++) {
587+
char *elem = elems[i]->buf;
588+
char *value;
589+
char *p = strchr(elem, '=');
590+
591+
strbuf_strip_suffix(elems[i], ",");
592+
593+
if (!p) {
594+
warning(_("invalid element '%s' from remote info"), elem);
595+
continue;
596+
}
597+
598+
*p = '\0';
599+
value = url_percent_decode(p + 1);
600+
601+
if (!strcmp(elem, "name"))
602+
info->name = value;
603+
else if (!strcmp(elem, "url"))
604+
info->url = value;
605+
else
606+
free(value);
607+
}
608+
609+
strbuf_list_free(elems);
610+
611+
if (!info->name || !info->url) {
612+
warning(_("server advertised a promisor remote without a name or URL: %s"),
613+
remote_info->buf);
614+
promisor_info_list_free(info);
615+
return NULL;
616+
}
617+
618+
return info;
619+
}
620+
579621
static void filter_promisor_remote(struct repository *repo,
580622
struct strvec *accepted,
581623
const char *info)
@@ -610,32 +652,19 @@ static void filter_promisor_remote(struct repository *repo,
610652
remotes = strbuf_split_str(info, ';', 0);
611653

612654
for (size_t i = 0; remotes[i]; i++) {
613-
struct strbuf **elems;
614-
const char *remote_name = NULL;
615-
const char *remote_url = NULL;
616-
char *decoded_name = NULL;
617-
char *decoded_url = NULL;
655+
struct promisor_info *advertised;
618656

619657
strbuf_strip_suffix(remotes[i], ";");
620-
elems = strbuf_split(remotes[i], ',');
621658

622-
for (size_t j = 0; elems[j]; j++) {
623-
strbuf_strip_suffix(elems[j], ",");
624-
if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
625-
skip_prefix(elems[j]->buf, "url=", &remote_url);
626-
}
659+
advertised = parse_one_advertised_remote(remotes[i]);
627660

628-
if (remote_name)
629-
decoded_name = url_percent_decode(remote_name);
630-
if (remote_url)
631-
decoded_url = url_percent_decode(remote_url);
661+
if (!advertised)
662+
continue;
632663

633-
if (decoded_name && should_accept_remote(accept, decoded_name, decoded_url, config_info))
634-
strvec_push(accepted, decoded_name);
664+
if (should_accept_remote(accept, advertised, config_info))
665+
strvec_push(accepted, advertised->name);
635666

636-
strbuf_list_free(elems);
637-
free(decoded_name);
638-
free(decoded_url);
667+
promisor_info_list_free(advertised);
639668
}
640669

641670
promisor_info_list_free(config_info);

0 commit comments

Comments
 (0)