From: Christian Couder Date: Fri, 20 Nov 2026 16:48:14 GMT Subject: [PATCH v2 1/8] connect: pass capability lines to write_command_and_capabilities() Message-ID: <20261120164812.707946-11-christian.couder@gmail.com> In-Reply-To: <20261120164812.707946-10-christian.couder@gmail.com> There are three different functions that write capabilities on the client side: - get_remote_refs(), - send_capabilities(), and - write_command_and_capabilities(). They duplicate code and make things difficult to understand, fix and extend. To start rationalizing this, let's make the write_command_and_capabilities() function more generic, by replacing its `const struct string_list *server_options` argument with a `const struct strvec *capabilities` argument. This will make it possible for other functions to reuse this function in following commits. To allow callers to generate the "server-option=..." capability lines that can then be passed to write_command_and_capabilities() through its new `capabilities` argument, let's also add a new add_server_options() helper function. No change in behavior is intended. Signed-off-by: Christian Couder --- connect.c | 32 ++++++++++++++++++++++++++------ connect.h | 17 +++++++++++++++-- fetch-object-info.c | 6 +++++- fetch-pack.c | 11 +++++++++-- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/connect.c b/connect.c index 87d2e8bccd..5f73fd9e7e 100644 --- a/connect.c +++ b/connect.c @@ -723,9 +723,22 @@ int server_supports(const char *feature) return !!server_feature_value(feature, NULL); } +void add_server_options(struct strvec *capabilities, + const struct string_list *server_options) +{ + struct string_list_item *item; + + if (!server_options) + return; + + for_each_string_list_item(item, server_options) + strvec_pushf(capabilities, "server-option=%s", item->string); +} + void write_command_and_capabilities(struct strbuf *req_buf, const char *command, - const struct string_list *server_options) + const struct strvec *capabilities) { + struct strbuf key = STRBUF_INIT; const char *hash_name; int advertise_sid = 0; @@ -737,11 +750,16 @@ void write_command_and_capabilities(struct strbuf *req_buf, const char *command, packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); if (advertise_sid && server_supports_v2("session-id")) packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); - if (server_options && server_options->nr) { - ensure_server_supports_v2("server-option"); - for (size_t i = 0; i < server_options->nr; i++) - packet_buf_write(req_buf, "server-option=%s", - server_options->items[i].string); + + for (size_t i = 0; i < capabilities->nr; i++) { + const char *cap = capabilities->v[i]; + size_t key_len = strcspn(cap, "="); + + strbuf_add(&key, cap, key_len); + ensure_server_supports_v2(key.buf); + packet_buf_write(req_buf, "%s", cap); + + strbuf_reset(&key); } if (server_feature_v2("object-format", &hash_name)) { @@ -754,7 +772,9 @@ void write_command_and_capabilities(struct strbuf *req_buf, const char *command, die(_("the server does not support algorithm '%s'"), the_hash_algo->name); } + packet_buf_delim(req_buf); + strbuf_release(&key); } static const char *url_scheme_name(enum url_scheme scheme) diff --git a/connect.h b/connect.h index 957e5fe2b9..2d5f9e2b83 100644 --- a/connect.h +++ b/connect.h @@ -35,11 +35,24 @@ void check_stateless_delimiter(int stateless_rpc, const char *error); struct string_list; +struct strvec; + +/* + * Append a "server-option=" capability to `capabilities` for + * each item in `server_options`, which may be NULL. + */ +void add_server_options(struct strvec *capabilities, + const struct string_list *server_options); + /* * Write a protocol v2 command request, along with the capability - * advertisements, into req_buf. + * advertisements, into req_buf. The "agent", "session-id" and + * "object-format" capabilities are handled here, and must not be in + * `capabilities`. `capabilities` contains complete "key[=value]" + * lines and must not be NULL. The keys in `capabilities` must have + * been advertised by the server, otherwise we die(). */ void write_command_and_capabilities(struct strbuf *req_buf, const char *command, - const struct string_list *server_options); + const struct strvec *capabilities); #endif diff --git a/fetch-object-info.c b/fetch-object-info.c index 0a58308f9b..cd14105836 100644 --- a/fetch-object-info.c +++ b/fetch-object-info.c @@ -8,6 +8,7 @@ #include "odb.h" #include "fetch-object-info.h" #include "string-list.h" +#include "strvec.h" /* Sends object-info command and its arguments into the request buffer. */ static void send_object_info_request(const int fd_out, @@ -17,8 +18,10 @@ static void send_object_info_request(const int fd_out, unsigned ask_type) { struct strbuf req_buf = STRBUF_INIT; + struct strvec capabilities = STRVEC_INIT; - write_command_and_capabilities(&req_buf, "object-info", server_options); + add_server_options(&capabilities, server_options); + write_command_and_capabilities(&req_buf, "object-info", &capabilities); if (ask_size) packet_buf_write(&req_buf, "size"); @@ -35,6 +38,7 @@ static void send_object_info_request(const int fd_out, if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) die_errno(_("unable to write request to remote")); + strvec_clear(&capabilities); strbuf_release(&req_buf); } diff --git a/fetch-pack.c b/fetch-pack.c index 55d311c0c3..ecb384211c 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1385,8 +1385,10 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, int haves_added; int done_sent = 0; struct strbuf req_buf = STRBUF_INIT; + struct strvec capabilities = STRVEC_INIT; - write_command_and_capabilities(&req_buf, "fetch", args->server_options); + add_server_options(&capabilities, args->server_options); + write_command_and_capabilities(&req_buf, "fetch", &capabilities); if (args->use_thin_pack) packet_buf_write(&req_buf, "thin-pack"); @@ -1451,6 +1453,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) die_errno(_("unable to write request to remote")); + strvec_clear(&capabilities); strbuf_release(&req_buf); return done_sent; } @@ -2242,6 +2245,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips, int last_iteration = 0; int negotiation_round = 0; timestamp_t min_generation = GENERATION_NUMBER_INFINITY; + struct strvec capabilities = STRVEC_INIT; fetch_negotiator_init(the_repository, &negotiator); mark_tips(&negotiator, negotiation_restrict_tips); @@ -2257,6 +2261,8 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips, add_to_object_array, &nt_object_array); + add_server_options(&capabilities, server_options); + trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); while (!last_iteration) { int haves_added; @@ -2269,7 +2275,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips, the_repository, "%d", negotiation_round); strbuf_reset(&req_buf); - write_command_and_capabilities(&req_buf, "fetch", server_options); + write_command_and_capabilities(&req_buf, "fetch", &capabilities); packet_buf_write(&req_buf, "wait-for-done"); @@ -2324,6 +2330,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips, trace2_data_intmax("negotiate_using_fetch", the_repository, "total_rounds", negotiation_round); + strvec_clear(&capabilities); clear_common_flag(acked_commits); object_array_clear(&nt_object_array); oidset_clear(&negotiation_include_oids); -- 2.56.0