Git development
 help / color / mirror / Atom feed
From: Christian Couder <christian.couder@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>, Matt Cote <mcote@nvidia.com>,
	Christian Couder <christian.couder@gmail.com>
Subject: [PATCH v2 3/8] connect: pass a reader to write_command_and_capabilities()
Date: Fri, 20 Nov 2026 17:48:16 +0100	[thread overview]
Message-ID: <20261120164812.707946-13-christian.couder@gmail.com> (raw)
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


  parent reply	other threads:[~2026-11-20 16:48 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 19:58 [PATCH 0/3] Fix promisor-remote reply missing from fetch Christian Couder
2026-11-12 19:59 ` [PATCH 1/3] connect: let write_command_and_capabilities() send a promisor-remote reply Christian Couder
2026-11-12 22:18   ` Junio C Hamano
2026-11-12 22:34     ` Junio C Hamano
2026-11-13 13:10       ` Christian Couder
2026-11-13 19:11         ` Junio C Hamano
2026-11-20 16:57           ` Christian Couder
2026-11-12 19:59 ` [PATCH 2/3] fetch-pack: ensure fetch sends " Christian Couder
2026-11-12 19:59 ` [PATCH 3/3] doc: gitprotocol-v2: send the promisor-remote reply with fetch Christian Couder
2026-11-20 16:48 ` [PATCH v2 0/8] Fix promisor-remote reply missing from fetch Christian Couder
2026-11-20 16:48   ` [PATCH v2 1/8] connect: pass capability lines to write_command_and_capabilities() Christian Couder
2026-11-20 16:48   ` [PATCH v2 2/8] connect: send session-id with ls-refs and bundle-uri Christian Couder
2026-11-20 16:48   ` Christian Couder [this message]
2026-11-20 16:48   ` [PATCH v2 4/8] connect: buffer the ls-refs and bundle-uri requests Christian Couder
2026-11-20 16:48   ` [PATCH v2 5/8] connect: use write_command_and_capabilities() in get_remote_refs() Christian Couder
2026-11-20 16:48   ` [PATCH v2 6/8] connect: use write_command_and_capabilities() in get_remote_bundle_uri() Christian Couder
2026-11-20 16:48   ` [PATCH v2 7/8] fetch-pack: ensure fetch sends a promisor-remote reply Christian Couder
2026-11-20 16:48   ` [PATCH v2 8/8] doc: gitprotocol-v2: send the promisor-remote reply with fetch Christian Couder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120164812.707946-13-christian.couder@gmail.com \
    --to=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mcote@nvidia.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox