connect.c: refactor sending of agent & object-format

Refactor the sending of the "agent" and "object-format" capabilities into a function. This was added in its current form in ab67235bc4 (connect: parse v2 refs with correct hash algorithm, 2020-05-25). When we connect to a v2 server we need to know about its object-format, and it needs to know about ours. Since most things in connect.c and transport.c piggy-back on the eager getting of remote refs via the handshake() those commands can make use of the just-sent-over object-format by ls-refs. But I'm about to add a command that may come after ls-refs, and may not, but we need the server to know about our user-agent and object-format. So let's split this into a function. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed May 16, 2022 at 20:10 UTC 86f4e31298e4440a08da277966a52adeb982a1fb
1 file changed +20 -13
connect.c
+20 -13
@@ -473,6 +473,24 @@ void check_stateless_delimiter(int stateless_rpc,
473 die("%s", error);
474 }
475
476 +static void send_capabilities(int fd_out, struct packet_reader *reader)
477 +{
478 + const char *hash_name;
479 +
480 + if (server_supports_v2("agent", 0))
481 + packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
482 +
483 + if (server_feature_v2("object-format", &hash_name)) {
484 + int hash_algo = hash_algo_by_name(hash_name);
485 + if (hash_algo == GIT_HASH_UNKNOWN)
486 + die(_("unknown object format '%s' specified by server"), hash_name);
487 + reader->hash_algo = &hash_algos[hash_algo];
488 + packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
489 + } else {
490 + reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
491 + }
492 +}
493 +
494 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
495 struct ref **list, int for_push,
496 struct transport_ls_refs_options *transport_options,
@@ -480,7 +498,6 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
498 int stateless_rpc)
499 {
500 int i;
483 - const char *hash_name;
501 struct strvec *ref_prefixes = transport_options ?
502 &transport_options->ref_prefixes : NULL;
503 const char **unborn_head_target = transport_options ?
@@ -490,18 +507,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
507 if (server_supports_v2("ls-refs", 1))
508 packet_write_fmt(fd_out, "command=ls-refs\n");
509
493 - if (server_supports_v2("agent", 0))
494 - packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
495 -
496 - if (server_feature_v2("object-format", &hash_name)) {
497 - int hash_algo = hash_algo_by_name(hash_name);
498 - if (hash_algo == GIT_HASH_UNKNOWN)
499 - die(_("unknown object format '%s' specified by server"), hash_name);
500 - reader->hash_algo = &hash_algos[hash_algo];
501 - packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
502 - } else {
503 - reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
504 - }
510 + /* Send capabilities */
511 + send_capabilities(fd_out, reader);
512
513 if (server_options && server_options->nr &&
514 server_supports_v2("server-option", 1))