promisor-remote: pass config entry to all_fields_match() directly

The `in_list == 0` path of all_fields_match() looks up the remote in `config_info` by `advertised->name` repeatedly, even though every caller in should_accept_remote() has already performed this lookup and holds the result in `p`. To avoid this useless work, let's replace the `int in_list` parameter with a `struct promisor_info *config_entry` pointer: - When NULL (ACCEPT_ALL mode): scan the whole `config_info` list, as the old `in_list == 1` path did. - When non-NULL: match against that single config entry directly, avoiding the redundant string_list_lookup() call. This removes the hidden dependency on `advertised->name` inside all_fields_match(), which would be wrong if in the future auto-configured remotes are implemented, as the local config name may differ from the server's advertised name. While at it, let's also add a comment before all_fields_match() and match_field_against_config() to help understand how things work and help avoid similar issues. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Apr 7, 2026 at 13:52 UTC 720b7c26c82ef212852897bedb0d38eee78cb531
1 file changed +24 -12
promisor-remote.c
+24 -12
@@ -575,6 +575,12 @@ enum accept_promisor {
575 ACCEPT_ALL
576 };
577
578 +/*
579 + * Check if a specific field and its advertised value match the local
580 + * configuration of a given promisor remote.
581 + *
582 + * Returns 1 if they match, 0 otherwise.
583 + */
584 static int match_field_against_config(const char *field, const char *value,
585 struct promisor_info *config_info)
586 {
@@ -586,9 +592,18 @@ static int match_field_against_config(const char *field, const char *value,
592 return 0;
593 }
594
595 +/*
596 + * Check that the advertised fields match the local configuration.
597 + *
598 + * When 'config_entry' is NULL (ACCEPT_ALL mode), every checked field
599 + * must match at least one remote in 'config_info'.
600 + *
601 + * When 'config_entry' points to a specific remote's config, the
602 + * checked fields are compared against that single remote only.
603 + */
604 static int all_fields_match(struct promisor_info *advertised,
605 struct string_list *config_info,
591 - int in_list)
606 + struct promisor_info *config_entry)
607 {
608 struct string_list *fields = fields_checked();
609 struct string_list_item *item_checked;
@@ -597,7 +612,6 @@ static int all_fields_match(struct promisor_info *advertised,
612 int match = 0;
613 const char *field = item_checked->string;
614 const char *value = NULL;
600 - struct string_list_item *item;
615
616 if (!strcasecmp(field, promisor_field_filter))
617 value = advertised->filter;
@@ -607,7 +621,11 @@ static int all_fields_match(struct promisor_info *advertised,
621 if (!value)
622 return 0;
623
610 - if (in_list) {
624 + if (config_entry) {
625 + match = match_field_against_config(field, value,
626 + config_entry);
627 + } else {
628 + struct string_list_item *item;
629 for_each_string_list_item(item, config_info) {
630 struct promisor_info *p = item->util;
631 if (match_field_against_config(field, value, p)) {
@@ -615,12 +633,6 @@ static int all_fields_match(struct promisor_info *advertised,
633 break;
634 }
635 }
618 - } else {
619 - item = string_list_lookup(config_info, advertised->name);
620 - if (item) {
621 - struct promisor_info *p = item->util;
622 - match = match_field_against_config(field, value, p);
623 - }
636 }
637
638 if (!match)
@@ -640,7 +652,7 @@ static int should_accept_remote(enum accept_promisor accept,
652 const char *remote_url = advertised->url;
653
654 if (accept == ACCEPT_ALL)
643 - return all_fields_match(advertised, config_info, 1);
655 + return all_fields_match(advertised, config_info, NULL);
656
657 /* Get config info for that promisor remote */
658 item = string_list_lookup(config_info, remote_name);
@@ -652,7 +664,7 @@ static int should_accept_remote(enum accept_promisor accept,
664 p = item->util;
665
666 if (accept == ACCEPT_KNOWN_NAME)
655 - return all_fields_match(advertised, config_info, 0);
667 + return all_fields_match(advertised, config_info, p);
668
669 if (accept != ACCEPT_KNOWN_URL)
670 BUG("Unhandled 'enum accept_promisor' value '%d'", accept);
@@ -663,7 +675,7 @@ static int should_accept_remote(enum accept_promisor accept,
675 }
676
677 if (!strcmp(p->url, remote_url))
666 - return all_fields_match(advertised, config_info, 0);
678 + return all_fields_match(advertised, config_info, p);
679
680 warning(_("known remote named '%s' but with URL '%s' instead of '%s'"),
681 remote_name, p->url, remote_url);