serve: stop using `the_repository`
Stop using `the_repository` in the "serve" subsystem by passing in a repository when advertising capabilities or serving requests. Adjust callers accordingly by using `the_repository`. While there may be some callers that have a repository available in their context, this trivial conversion allows for easier verification and bubbles up the use of `the_repository` by one level. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Dec 17, 2024 at 07:43 UTC
395b584b5751b009d657d8c3aed371f2a233d919
4 files changed
+30
-25
builtin/upload-pack.c
+4
-2
@@ -1,3 +1,5 @@
1
+#define USE_THE_REPOSITORY_VARIABLE
2
+
3
#include "builtin.h"
4
#include "exec-cmd.h"
5
#include "gettext.h"
@@ -63,9 +65,9 @@ int cmd_upload_pack(int argc,
65
switch (determine_protocol_version_server()) {
66
case protocol_v2:
67
if (advertise_refs)
66
- protocol_v2_advertise_capabilities();
68
+ protocol_v2_advertise_capabilities(the_repository);
69
else
68
- protocol_v2_serve_loop(stateless_rpc);
70
+ protocol_v2_serve_loop(the_repository, stateless_rpc);
71
break;
72
case protocol_v1:
73
/*
serve.c
+17
-19
@@ -1,5 +1,3 @@
1
-#define USE_THE_REPOSITORY_VARIABLE
2
-
1
#include "git-compat-util.h"
2
#include "repository.h"
3
#include "config.h"
@@ -159,7 +157,7 @@ static struct protocol_capability capabilities[] = {
157
},
158
};
159
162
-void protocol_v2_advertise_capabilities(void)
160
+void protocol_v2_advertise_capabilities(struct repository *r)
161
{
162
struct strbuf capability = STRBUF_INIT;
163
struct strbuf value = STRBUF_INIT;
@@ -170,7 +168,7 @@ void protocol_v2_advertise_capabilities(void)
168
for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
169
struct protocol_capability *c = &capabilities[i];
170
173
- if (c->advertise(the_repository, &value)) {
171
+ if (c->advertise(r, &value)) {
172
strbuf_addstr(&capability, c->name);
173
174
if (value.len) {
@@ -214,20 +212,20 @@ static struct protocol_capability *get_capability(const char *key, const char **
212
return NULL;
213
}
214
217
-static int receive_client_capability(const char *key)
215
+static int receive_client_capability(struct repository *r, const char *key)
216
{
217
const char *value;
218
const struct protocol_capability *c = get_capability(key, &value);
219
222
- if (!c || c->command || !c->advertise(the_repository, NULL))
220
+ if (!c || c->command || !c->advertise(r, NULL))
221
return 0;
222
223
if (c->receive)
226
- c->receive(the_repository, value);
224
+ c->receive(r, value);
225
return 1;
226
}
227
230
-static int parse_command(const char *key, struct protocol_capability **command)
228
+static int parse_command(struct repository *r, const char *key, struct protocol_capability **command)
229
{
230
const char *out;
231
@@ -238,7 +236,7 @@ static int parse_command(const char *key, struct protocol_capability **command)
236
if (*command)
237
die("command '%s' requested after already requesting command '%s'",
238
out, (*command)->name);
241
- if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
239
+ if (!cmd || !cmd->advertise(r, NULL) || !cmd->command || value)
240
die("invalid command '%s'", out);
241
242
*command = cmd;
@@ -253,7 +251,7 @@ enum request_state {
251
PROCESS_REQUEST_DONE,
252
};
253
256
-static int process_request(void)
254
+static int process_request(struct repository *r)
255
{
256
enum request_state state = PROCESS_REQUEST_KEYS;
257
struct packet_reader reader;
@@ -278,8 +276,8 @@ static int process_request(void)
276
case PACKET_READ_EOF:
277
BUG("Should have already died when seeing EOF");
278
case PACKET_READ_NORMAL:
281
- if (parse_command(reader.line, &command) ||
282
- receive_client_capability(reader.line))
279
+ if (parse_command(r, reader.line, &command) ||
280
+ receive_client_capability(r, reader.line))
281
seen_capability_or_command = 1;
282
else
283
die("unknown capability '%s'", reader.line);
@@ -319,30 +317,30 @@ static int process_request(void)
317
if (!command)
318
die("no command requested");
319
322
- if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
320
+ if (client_hash_algo != hash_algo_by_ptr(r->hash_algo))
321
die("mismatched object format: server %s; client %s",
324
- the_repository->hash_algo->name,
322
+ r->hash_algo->name,
323
hash_algos[client_hash_algo].name);
324
327
- command->command(the_repository, &reader);
325
+ command->command(r, &reader);
326
327
return 0;
328
}
329
332
-void protocol_v2_serve_loop(int stateless_rpc)
330
+void protocol_v2_serve_loop(struct repository *r, int stateless_rpc)
331
{
332
if (!stateless_rpc)
335
- protocol_v2_advertise_capabilities();
333
+ protocol_v2_advertise_capabilities(r);
334
335
/*
336
* If stateless-rpc was requested then exit after
337
* a single request/response exchange
338
*/
339
if (stateless_rpc) {
342
- process_request();
340
+ process_request(r);
341
} else {
342
for (;;)
345
- if (process_request())
343
+ if (process_request(r))
344
break;
345
}
346
}
serve.h
+4
-2
@@ -1,7 +1,9 @@
1
#ifndef SERVE_H
2
#define SERVE_H
3
4
-void protocol_v2_advertise_capabilities(void);
5
-void protocol_v2_serve_loop(int stateless_rpc);
4
+struct repository;
5
+
6
+void protocol_v2_advertise_capabilities(struct repository *r);
7
+void protocol_v2_serve_loop(struct repository *r, int stateless_rpc);
8
9
#endif /* SERVE_H */
t/helper/test-serve-v2.c
+5
-2
@@ -1,6 +1,9 @@
1
+#define USE_THE_REPOSITORY_VARIABLE
2
+
3
#include "test-tool.h"
4
#include "gettext.h"
5
#include "parse-options.h"
6
+#include "repository.h"
7
#include "serve.h"
8
#include "setup.h"
9
@@ -28,9 +31,9 @@ int cmd__serve_v2(int argc, const char **argv)
31
PARSE_OPT_KEEP_UNKNOWN_OPT);
32
33
if (advertise_capabilities)
31
- protocol_v2_advertise_capabilities();
34
+ protocol_v2_advertise_capabilities(the_repository);
35
else
33
- protocol_v2_serve_loop(stateless_rpc);
36
+ protocol_v2_serve_loop(the_repository, stateless_rpc);
37
38
return 0;
39
}