fetch-pack: move write_fetch_command_and_capabilities() to connect.c
In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future. Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions. Because string_list is only used as a pointer, use a forward declaration [1]. [1]: https://lore.kernel.org/git/Z0RIqUAoEob8lGfM@pks.im/ Helped-by: Jonathan Tan <jonathantanmy@google.com> Helped-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Eric Ju <eric.peijian@gmail.com> Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pablo Sabater committed
Jul 24, 2026 at 12:54 UTC
40865f70d44d0665b0a0bd59acb8e18091d505a9
3 files changed
+38
-34
connect.c
+34
@@ -700,6 +700,40 @@ int server_supports(const char *feature)
700
return !!server_feature_value(feature, NULL);
701
}
702
703
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
704
+ const struct string_list *server_options)
705
+{
706
+ const char *hash_name;
707
+ int advertise_sid = 0;
708
+
709
+ repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
710
+
711
+ ensure_server_supports_v2("fetch");
712
+ packet_buf_write(req_buf, "command=fetch");
713
+ if (server_supports_v2("agent"))
714
+ packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
715
+ if (advertise_sid && server_supports_v2("session-id"))
716
+ packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
717
+ if (server_options && server_options->nr) {
718
+ ensure_server_supports_v2("server-option");
719
+ for (size_t i = 0; i < server_options->nr; i++)
720
+ packet_buf_write(req_buf, "server-option=%s",
721
+ server_options->items[i].string);
722
+ }
723
+
724
+ if (server_feature_v2("object-format", &hash_name)) {
725
+ const unsigned int hash_algo = hash_algo_by_name(hash_name);
726
+ if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
727
+ die(_("mismatched algorithms: client %s; server %s"),
728
+ the_hash_algo->name, hash_name);
729
+ packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
730
+ } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
731
+ die(_("the server does not support algorithm '%s'"),
732
+ the_hash_algo->name);
733
+ }
734
+ packet_buf_delim(req_buf);
735
+}
736
+
737
static const char *url_scheme_name(enum url_scheme scheme)
738
{
739
switch (scheme) {
connect.h
+4
@@ -34,4 +34,8 @@ void check_stateless_delimiter(int stateless_rpc,
34
struct packet_reader *reader,
35
const char *error);
36
37
+struct string_list;
38
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
39
+ const struct string_list *server_options);
40
+
41
#endif
fetch-pack.c
-34
@@ -1375,40 +1375,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
1375
return haves_added;
1376
}
1377
1378
-static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1379
- const struct string_list *server_options)
1380
-{
1381
- const char *hash_name;
1382
- int advertise_sid = 0;
1383
-
1384
- repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
1385
-
1386
- ensure_server_supports_v2("fetch");
1387
- packet_buf_write(req_buf, "command=fetch");
1388
- if (server_supports_v2("agent"))
1389
- packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1390
- if (advertise_sid && server_supports_v2("session-id"))
1391
- packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1392
- if (server_options && server_options->nr) {
1393
- ensure_server_supports_v2("server-option");
1394
- for (size_t i = 0; i < server_options->nr; i++)
1395
- packet_buf_write(req_buf, "server-option=%s",
1396
- server_options->items[i].string);
1397
- }
1398
-
1399
- if (server_feature_v2("object-format", &hash_name)) {
1400
- const unsigned int hash_algo = hash_algo_by_name(hash_name);
1401
- if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1402
- die(_("mismatched algorithms: client %s; server %s"),
1403
- the_hash_algo->name, hash_name);
1404
- packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1405
- } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
1406
- die(_("the server does not support algorithm '%s'"),
1407
- the_hash_algo->name);
1408
- }
1409
- packet_buf_delim(req_buf);
1410
-}
1411
-
1378
static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1379
struct fetch_pack_args *args,
1380
const struct ref *wants, struct oidset *common,