]> git.ipfire.org Git - thirdparty/git.git/commitdiff
connect.c: refactor sending of agent & object-format
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Mon, 16 May 2022 20:10:58 +0000 (20:10 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 16 May 2022 22:02:09 +0000 (15:02 -0700)
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>
connect.c

index afc79a6236e8d5fa7d4f6092c0e89d45e46cba41..e6d0b1d34bd61c626b855edb8879c12b35538ade 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -473,6 +473,24 @@ void check_stateless_delimiter(int stateless_rpc,
                die("%s", error);
 }
 
+static void send_capabilities(int fd_out, struct packet_reader *reader)
+{
+       const char *hash_name;
+
+       if (server_supports_v2("agent", 0))
+               packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
+
+       if (server_feature_v2("object-format", &hash_name)) {
+               int hash_algo = hash_algo_by_name(hash_name);
+               if (hash_algo == GIT_HASH_UNKNOWN)
+                       die(_("unknown object format '%s' specified by server"), hash_name);
+               reader->hash_algo = &hash_algos[hash_algo];
+               packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
+       } else {
+               reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
+       }
+}
+
 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
                             struct ref **list, int for_push,
                             struct transport_ls_refs_options *transport_options,
@@ -480,7 +498,6 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
                             int stateless_rpc)
 {
        int i;
-       const char *hash_name;
        struct strvec *ref_prefixes = transport_options ?
                &transport_options->ref_prefixes : NULL;
        const char **unborn_head_target = transport_options ?
@@ -490,18 +507,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
        if (server_supports_v2("ls-refs", 1))
                packet_write_fmt(fd_out, "command=ls-refs\n");
 
-       if (server_supports_v2("agent", 0))
-               packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
-
-       if (server_feature_v2("object-format", &hash_name)) {
-               int hash_algo = hash_algo_by_name(hash_name);
-               if (hash_algo == GIT_HASH_UNKNOWN)
-                       die(_("unknown object format '%s' specified by server"), hash_name);
-               reader->hash_algo = &hash_algos[hash_algo];
-               packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
-       } else {
-               reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
-       }
+       /* Send capabilities */
+       send_capabilities(fd_out, reader);
 
        if (server_options && server_options->nr &&
            server_supports_v2("server-option", 1))