git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/8] connect: pass a reader to write_command_and_capabilities()

From
Christian Couder <christian.couder@gmail.com>
Date
Nov 20, 2026, 16:48 UTC
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 <christian.couder@gmail.com>
---
 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
Previous: Christian CouderNext: Christian Couder
Message 13 of 18 in “Fix promisor-remote reply missing from fetch”
  1. 0/3 Fix promisor-remote reply missing from fetchChristian Couder, Nov 12, 2026
  2. 1/3 connect: let write_command_and_capabilities() send a promisor-remote replyChristian Couder, Nov 12, 2026
  3. Junio C HamanoNov 12, 2026
  4. Junio C HamanoNov 12, 2026
  5. Christian CouderNov 13, 2026
  6. Junio C HamanoNov 13, 2026
  7. Christian CouderNov 20, 2026
  8. 2/3 fetch-pack: ensure fetch sends a promisor-remote replyChristian Couder, Nov 12, 2026
  9. 3/3 doc: gitprotocol-v2: send the promisor-remote reply with fetchChristian Couder, Nov 12, 2026
  10. 0/8 Fix promisor-remote reply missing from fetchChristian Couder, Nov 20, 2026
  11. 1/8 connect: pass capability lines to write_command_and_capabilities()Christian Couder, Nov 20, 2026
  12. 2/8 connect: send session-id with ls-refs and bundle-uriChristian Couder, Nov 20, 2026
  13. 3/8 connect: pass a reader to write_command_and_capabilities()Christian Couder, Nov 20, 2026
  14. 4/8 connect: buffer the ls-refs and bundle-uri requestsChristian Couder, Nov 20, 2026
  15. 5/8 connect: use write_command_and_capabilities() in get_remote_refs()Christian Couder, Nov 20, 2026
  16. 6/8 connect: use write_command_and_capabilities() in get_remote_bundle_uri()Christian Couder, Nov 20, 2026
  17. 7/8 fetch-pack: ensure fetch sends a promisor-remote replyChristian Couder, Nov 20, 2026
  18. 8/8 doc: gitprotocol-v2: send the promisor-remote reply with fetchChristian Couder, Nov 20, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.