Skip to content

Commit 86f4e31

Browse files
avargitster
authored andcommitted
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 ab67235 (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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6cd33dc commit 86f4e31

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

connect.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,31 @@ void check_stateless_delimiter(int stateless_rpc,
473473
die("%s", error);
474474
}
475475

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+
476494
struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
477495
struct ref **list, int for_push,
478496
struct transport_ls_refs_options *transport_options,
479497
const struct string_list *server_options,
480498
int stateless_rpc)
481499
{
482500
int i;
483-
const char *hash_name;
484501
struct strvec *ref_prefixes = transport_options ?
485502
&transport_options->ref_prefixes : NULL;
486503
const char **unborn_head_target = transport_options ?
@@ -490,18 +507,8 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
490507
if (server_supports_v2("ls-refs", 1))
491508
packet_write_fmt(fd_out, "command=ls-refs\n");
492509

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);
505512

506513
if (server_options && server_options->nr &&
507514
server_supports_v2("server-option", 1))

0 commit comments

Comments
 (0)