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 1/8] connect: pass capability lines to write_command_and_capabilities()
Date: Fri, 20 Nov 2026 17:48:14 +0100	[thread overview]
Message-ID: <20261120164812.707946-11-christian.couder@gmail.com> (raw)
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 <christian.couder@gmail.com>
---
 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=<value>" 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


  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   ` Christian Couder [this message]
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   ` [PATCH v2 3/8] connect: pass a reader to write_command_and_capabilities() Christian Couder
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-11-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