[PATCH v2 5/8] connect: use write_command_and_capabilities() in get_remote_refs()
- From
Christian Couder <christian.couder@gmail.com>
- Date
- Nov 20, 2026, 16:48 UTC
- Message-ID
- <20261120164812.707946-15-christian.couder@gmail.com>
- In-Reply-To
- <20261120164812.707946-10-christian.couder@gmail.com>
As previous commits made write_command_and_capabilities() more generic, we can now use that function in get_remote_refs() instead of send_capabilities() and the open-coded "server-option" loop. Let's do that then, which prepares for removing send_capabilities() in a following commit.
Let's also refactor writing the "promisor-remote" reply into a new add_promisor_remote_reply() helper function. This duplicates code which is in send_capabilities() but we are going to remove that function in a following commit. Let's declare this new helper in "connect.h" as it will also be used in "fetch-pack.c" in a following commit.
Note that the order of the `ls-refs` request lines changes. After
"command, agent, [session-id]"
it used to be:
"object-format, promisor-remote, server-option*"
but now it is:
"server-option*, promisor-remote, object-format".
This should not affect the server's behavior as "serve.c" and JGit both accept capabilities in any order after "command=...".
Also note that "command=ls-refs" no longer ends with "\n", which is harmless because the server chomps newlines.
Signed-off-by: Christian Couder <christian.couder@gmail.com> --- connect.c | 34 +++++++++++++++++++++------------- connect.h | 8 ++++++++ 2 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/connect.c b/connect.c index 1d923978ba..9d5585f72b 100644 --- a/connect.c +++ b/connect.c @@ -501,6 +501,21 @@ void check_stateless_delimiter(int stateless_rpc, die("%s", error); } +void add_promisor_remote_reply(struct strvec *capabilities) +{ + const char *promisor_remote_info; + char *reply; + + if (!server_feature_v2("promisor-remote", &promisor_remote_info)) + return; + + promisor_remote_reply(promisor_remote_info, &reply); + if (reply) { + strvec_pushf(capabilities, "promisor-remote=%s", reply); + free(reply); + } +} + static void send_capabilities(struct strbuf *req_buf, struct packet_reader *reader) { @@ -600,26 +615,18 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, { size_t i; struct strbuf req_buf = STRBUF_INIT; + struct strvec capabilities = STRVEC_INIT; struct strvec *ref_prefixes = transport_options ? &transport_options->ref_prefixes : NULL; const char **unborn_head_target = transport_options ? &transport_options->unborn_head_target : NULL; *list = NULL; - ensure_server_supports_v2("ls-refs"); - packet_buf_write(&req_buf, "command=ls-refs\n"); - - /* Send capabilities */ - send_capabilities(&req_buf, reader); - - if (server_options && server_options->nr) { - ensure_server_supports_v2("server-option"); - for (i = 0; i < server_options->nr; i++) - packet_buf_write(&req_buf, "server-option=%s", - server_options->items[i].string); - } + add_server_options(&capabilities, server_options); + add_promisor_remote_reply(&capabilities); + write_command_and_capabilities(&req_buf, "ls-refs", + &capabilities, reader); - packet_buf_delim(&req_buf); /* When pushing we don't want to request the peeled tags */ if (!for_push) packet_buf_write(&req_buf, "peel\n"); @@ -634,6 +641,7 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, 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); /* Process response from server */ diff --git a/connect.h b/connect.h index cff987ae38..00afc8c95a 100644 --- a/connect.h +++ b/connect.h @@ -44,6 +44,14 @@ struct strvec; void add_server_options(struct strvec *capabilities, const struct string_list *server_options); +/* + * If the server advertised the "promisor-remote" capability, compute + * our reply, which also marks the accepted promisor remotes, and + * append "promisor-remote=<reply>" to `capabilities` if any remote + * was accepted. + */ +void add_promisor_remote_reply(struct strvec *capabilities); + /* * Write a protocol v2 command request, along with the capability * advertisements, into req_buf. The "agent", "session-id" and
-- 2.56.0