Skip to content

Commit c7d3aab

Browse files
peffgitster
authored andcommitted
serve: provide "receive" function for object-format capability
We get any "object-format" specified by the client by searching for it in the collected list of capabilities the client sent. We can instead just handle it as soon as they send it. This is slightly more efficient, and gets us one step closer to dropping that collected list. Note that we do still have to do our final hash check after receiving all capabilities (because they might not have sent an object-format line at all, and we still have to check that the default matches our repository algorithm). Since the check_algorithm() function would now be down to a single if() statement, I've just inlined it in its only caller. There should be no change of behavior here, except for two broken-protocol cases: - if the client sends multiple conflicting object-format capabilities (which they should not), we'll now choose the last one rather than the first. We could also detect and complain about the duplicates quite easily now, which we could not before, but I didn't do so here. - if the client sends a bogus "object-format" with no equals sign, we'll now say so, rather than "unknown object format: ''" Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e56e530 commit c7d3aab

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

serve.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "upload-pack.h"
1111

1212
static int advertise_sid = -1;
13+
static int client_hash_algo = GIT_HASH_SHA1;
1314

1415
static int always_advertise(struct repository *r,
1516
struct strbuf *value)
@@ -33,6 +34,17 @@ static int object_format_advertise(struct repository *r,
3334
return 1;
3435
}
3536

37+
static void object_format_receive(struct repository *r,
38+
const char *algo_name)
39+
{
40+
if (!algo_name)
41+
die("object-format capability requires an argument");
42+
43+
client_hash_algo = hash_algo_by_name(algo_name);
44+
if (client_hash_algo == GIT_HASH_UNKNOWN)
45+
die("unknown object format '%s'", algo_name);
46+
}
47+
3648
static int session_id_advertise(struct repository *r, struct strbuf *value)
3749
{
3850
if (advertise_sid == -1 &&
@@ -104,6 +116,7 @@ static struct protocol_capability capabilities[] = {
104116
{
105117
.name = "object-format",
106118
.advertise = object_format_advertise,
119+
.receive = object_format_receive,
107120
},
108121
{
109122
.name = "session-id",
@@ -228,22 +241,6 @@ static int has_capability(const struct strvec *keys, const char *capability,
228241
return 0;
229242
}
230243

231-
static void check_algorithm(struct repository *r, struct strvec *keys)
232-
{
233-
int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
234-
const char *algo_name;
235-
236-
if (has_capability(keys, "object-format", &algo_name)) {
237-
client = hash_algo_by_name(algo_name);
238-
if (client == GIT_HASH_UNKNOWN)
239-
die("unknown object format '%s'", algo_name);
240-
}
241-
242-
if (client != server)
243-
die("mismatched object format: server %s; client %s\n",
244-
r->hash_algo->name, hash_algos[client].name);
245-
}
246-
247244
enum request_state {
248245
PROCESS_REQUEST_KEYS,
249246
PROCESS_REQUEST_DONE,
@@ -317,7 +314,10 @@ static int process_request(void)
317314
if (!command)
318315
die("no command requested");
319316

320-
check_algorithm(the_repository, &keys);
317+
if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
318+
die("mismatched object format: server %s; client %s\n",
319+
the_repository->hash_algo->name,
320+
hash_algos[client_hash_algo].name);
321321

322322
if (has_capability(&keys, "session-id", &client_sid))
323323
trace2_data_string("transfer", NULL, "client-sid", client_sid);

0 commit comments

Comments
 (0)