From: Christian Couder Date: Fri, 20 Nov 2026 16:48:16 GMT Subject: [PATCH v2 3/8] connect: pass a reader to write_command_and_capabilities() Message-ID: <20261120164812.707946-13-christian.couder@gmail.com> In-Reply-To: <20261120164812.707946-10-christian.couder@gmail.com> send_capabilities(), used for `ls-refs` and `bundle-uri`, sets `reader->hash_algo` to the object format advertised by the server, as the client may not know its own yet (for example in `git clone`). On the other hand, write_command_and_capabilities(), used for `fetch` and `object-info`, checks the server's object format against `the_hash_algo`. To enable write_command_and_capabilities() to replace send_capabilities() in a following commit, let's add a `struct packet_reader *reader` argument to it. When that `reader` is NULL, write_command_and_capabilities() behaves as it used to, but when it is not NULL, it behaves as send_capabilities() does, and sets `reader->hash_algo` to the server's algorithm. No behavior change is intended in this patch as callers always pass a NULL `reader` to write_command_and_capabilities(). Signed-off-by: Christian Couder --- connect.c | 16 +++++++++++++--- connect.h | 9 +++++++-- fetch-object-info.c | 3 ++- fetch-pack.c | 6 ++++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/connect.c b/connect.c index 40cd279290..71d9e98a4b 100644 --- a/connect.c +++ b/connect.c @@ -742,7 +742,8 @@ void add_server_options(struct strvec *capabilities, } void write_command_and_capabilities(struct strbuf *req_buf, const char *command, - const struct strvec *capabilities) + const struct strvec *capabilities, + struct packet_reader *reader) { struct strbuf key = STRBUF_INIT; const char *hash_name; @@ -770,10 +771,19 @@ void write_command_and_capabilities(struct strbuf *req_buf, const char *command, if (server_feature_v2("object-format", &hash_name)) { const unsigned int hash_algo = hash_algo_by_name(hash_name); - if (hash_algo_by_ptr(the_hash_algo) != hash_algo) + if (reader) { + if (hash_algo == GIT_HASH_UNKNOWN) + die(_("unknown object format '%s' specified by server"), + hash_name); + reader->hash_algo = &hash_algos[hash_algo]; + } else if (hash_algo_by_ptr(the_hash_algo) != hash_algo) { die(_("mismatched algorithms: client %s; server %s"), the_hash_algo->name, hash_name); - packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); + } + packet_buf_write(req_buf, "object-format=%s", + hash_algos[hash_algo].name); + } else if (reader) { + reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY]; } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) { die(_("the server does not support algorithm '%s'"), the_hash_algo->name); diff --git a/connect.h b/connect.h index 2d5f9e2b83..cff987ae38 100644 --- a/connect.h +++ b/connect.h @@ -50,9 +50,14 @@ void add_server_options(struct strvec *capabilities, * "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(). + * been advertised by the server, otherwise we die(). If `reader` is + * not NULL, its `hash_algo` member is set from the "object-format" + * capability sent by the server (or to SHA-1 if the server didn't + * send one). Otherwise that capability is checked against + * `the_hash_algo`, and we die() if they don't match. */ void write_command_and_capabilities(struct strbuf *req_buf, const char *command, - const struct strvec *capabilities); + const struct strvec *capabilities, + struct packet_reader *reader); #endif diff --git a/fetch-object-info.c b/fetch-object-info.c index cd14105836..b7a5996233 100644 --- a/fetch-object-info.c +++ b/fetch-object-info.c @@ -21,7 +21,8 @@ static void send_object_info_request(const int fd_out, struct strvec capabilities = STRVEC_INIT; add_server_options(&capabilities, server_options); - write_command_and_capabilities(&req_buf, "object-info", &capabilities); + write_command_and_capabilities(&req_buf, "object-info", + &capabilities, NULL); if (ask_size) packet_buf_write(&req_buf, "size"); diff --git a/fetch-pack.c b/fetch-pack.c index ecb384211c..176d625936 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -1388,7 +1388,8 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, struct strvec capabilities = STRVEC_INIT; add_server_options(&capabilities, args->server_options); - write_command_and_capabilities(&req_buf, "fetch", &capabilities); + write_command_and_capabilities(&req_buf, "fetch", + &capabilities, NULL); if (args->use_thin_pack) packet_buf_write(&req_buf, "thin-pack"); @@ -2275,7 +2276,8 @@ 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", &capabilities); + write_command_and_capabilities(&req_buf, "fetch", + &capabilities, NULL); packet_buf_write(&req_buf, "wait-for-done"); -- 2.56.0