Skip to content

Commit 4278fbd

Browse files
calvin-wan-googlegitster
authored andcommitted
fetch-pack: refactor packet writing
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_command_and_capabilities(), enabling it to serve both fetch and additional commands. In this context, "command" refers to the "operations" supported by Git's wire protocol https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162 (object-info: support for retrieving object info, 2021-04-20). Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future. To move write_command_and_capabilities() to connect.c, we need to adjust how `advertise_sid` is managed. Previously, in fetch_pack.c, `advertise_sid` was a static variable, modified using git_config_get_bool(). In connect.c, we now initialize `advertise_sid` at the beginning by directly using git_config_get_bool(). This change is safe because: In the original fetch-pack.c code, there are only two places that write `advertise_sid` : 1. In function do_fetch_pack: if (!server_supports("session-id")) advertise_sid = 0; 2. In function fetch_pack_config(): git_config_get_bool("transfer.advertisesid", &advertise_sid); About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2. About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly. Helped-by: Jonathan Tan <[email protected]> Helped-by: Christian Couder <[email protected]> Signed-off-by: Calvin Wan <[email protected]> Signed-off-by: Eric Ju <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d138188 commit 4278fbd

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

connect.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,40 @@ int server_supports(const char *feature)
688688
return !!server_feature_value(feature, NULL);
689689
}
690690

691+
void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
692+
const struct string_list *server_options)
693+
{
694+
const char *hash_name;
695+
int advertise_sid;
696+
697+
git_config_get_bool("transfer.advertisesid", &advertise_sid);
698+
699+
ensure_server_supports_v2(command);
700+
packet_buf_write(req_buf, "command=%s", command);
701+
if (server_supports_v2("agent"))
702+
packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
703+
if (advertise_sid && server_supports_v2("session-id"))
704+
packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
705+
if (server_options && server_options->nr) {
706+
ensure_server_supports_v2("server-option");
707+
for (size_t i = 0; i < server_options->nr; i++)
708+
packet_buf_write(req_buf, "server-option=%s",
709+
server_options->items[i].string);
710+
}
711+
712+
if (server_feature_v2("object-format", &hash_name)) {
713+
const int hash_algo = hash_algo_by_name(hash_name);
714+
if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
715+
die(_("mismatched algorithms: client %s; server %s"),
716+
the_hash_algo->name, hash_name);
717+
packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
718+
} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
719+
die(_("the server does not support algorithm '%s'"),
720+
the_hash_algo->name);
721+
}
722+
packet_buf_delim(req_buf);
723+
}
724+
691725
enum protocol {
692726
PROTO_LOCAL = 1,
693727
PROTO_FILE,

connect.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ void check_stateless_delimiter(int stateless_rpc,
3030
struct packet_reader *reader,
3131
const char *error);
3232

33+
/*
34+
* Writes a command along with the requested
35+
* server capabilities/features into a request buffer.
36+
*/
37+
struct string_list;
38+
void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
39+
const struct string_list *server_options);
40+
3341
#endif

fetch-pack.c

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,37 +1319,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
13191319
return haves_added;
13201320
}
13211321

1322-
static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1323-
const struct string_list *server_options)
1324-
{
1325-
const char *hash_name;
1326-
1327-
ensure_server_supports_v2("fetch");
1328-
packet_buf_write(req_buf, "command=fetch");
1329-
if (server_supports_v2("agent"))
1330-
packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1331-
if (advertise_sid && server_supports_v2("session-id"))
1332-
packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1333-
if (server_options && server_options->nr) {
1334-
ensure_server_supports_v2("server-option");
1335-
for (size_t i = 0; i < server_options->nr; i++)
1336-
packet_buf_write(req_buf, "server-option=%s",
1337-
server_options->items[i].string);
1338-
}
1339-
1340-
if (server_feature_v2("object-format", &hash_name)) {
1341-
int hash_algo = hash_algo_by_name(hash_name);
1342-
if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1343-
die(_("mismatched algorithms: client %s; server %s"),
1344-
the_hash_algo->name, hash_name);
1345-
packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1346-
} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1347-
die(_("the server does not support algorithm '%s'"),
1348-
the_hash_algo->name);
1349-
}
1350-
packet_buf_delim(req_buf);
1351-
}
1352-
13531322
static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
13541323
struct fetch_pack_args *args,
13551324
const struct ref *wants, struct oidset *common,
@@ -1360,7 +1329,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
13601329
int done_sent = 0;
13611330
struct strbuf req_buf = STRBUF_INIT;
13621331

1363-
write_fetch_command_and_capabilities(&req_buf, args->server_options);
1332+
write_command_and_capabilities(&req_buf, "fetch", args->server_options);
13641333

13651334
if (args->use_thin_pack)
13661335
packet_buf_write(&req_buf, "thin-pack");
@@ -2188,7 +2157,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_tips,
21882157
the_repository, "%d",
21892158
negotiation_round);
21902159
strbuf_reset(&req_buf);
2191-
write_fetch_command_and_capabilities(&req_buf, server_options);
2160+
write_command_and_capabilities(&req_buf, "fetch", server_options);
21922161

21932162
packet_buf_write(&req_buf, "wait-for-done");
21942163

0 commit comments

Comments
 (0)