Skip to content

Commit 395b584

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd0c0fb commit 395b584

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

builtin/upload-pack.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
13
#include "builtin.h"
24
#include "exec-cmd.h"
35
#include "gettext.h"
@@ -63,9 +65,9 @@ int cmd_upload_pack(int argc,
6365
switch (determine_protocol_version_server()) {
6466
case protocol_v2:
6567
if (advertise_refs)
66-
protocol_v2_advertise_capabilities();
68+
protocol_v2_advertise_capabilities(the_repository);
6769
else
68-
protocol_v2_serve_loop(stateless_rpc);
70+
protocol_v2_serve_loop(the_repository, stateless_rpc);
6971
break;
7072
case protocol_v1:
7173
/*

serve.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define USE_THE_REPOSITORY_VARIABLE
2-
31
#include "git-compat-util.h"
42
#include "repository.h"
53
#include "config.h"
@@ -159,7 +157,7 @@ static struct protocol_capability capabilities[] = {
159157
},
160158
};
161159

162-
void protocol_v2_advertise_capabilities(void)
160+
void protocol_v2_advertise_capabilities(struct repository *r)
163161
{
164162
struct strbuf capability = STRBUF_INIT;
165163
struct strbuf value = STRBUF_INIT;
@@ -170,7 +168,7 @@ void protocol_v2_advertise_capabilities(void)
170168
for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
171169
struct protocol_capability *c = &capabilities[i];
172170

173-
if (c->advertise(the_repository, &value)) {
171+
if (c->advertise(r, &value)) {
174172
strbuf_addstr(&capability, c->name);
175173

176174
if (value.len) {
@@ -214,20 +212,20 @@ static struct protocol_capability *get_capability(const char *key, const char **
214212
return NULL;
215213
}
216214

217-
static int receive_client_capability(const char *key)
215+
static int receive_client_capability(struct repository *r, const char *key)
218216
{
219217
const char *value;
220218
const struct protocol_capability *c = get_capability(key, &value);
221219

222-
if (!c || c->command || !c->advertise(the_repository, NULL))
220+
if (!c || c->command || !c->advertise(r, NULL))
223221
return 0;
224222

225223
if (c->receive)
226-
c->receive(the_repository, value);
224+
c->receive(r, value);
227225
return 1;
228226
}
229227

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)
231229
{
232230
const char *out;
233231

@@ -238,7 +236,7 @@ static int parse_command(const char *key, struct protocol_capability **command)
238236
if (*command)
239237
die("command '%s' requested after already requesting command '%s'",
240238
out, (*command)->name);
241-
if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command || value)
239+
if (!cmd || !cmd->advertise(r, NULL) || !cmd->command || value)
242240
die("invalid command '%s'", out);
243241

244242
*command = cmd;
@@ -253,7 +251,7 @@ enum request_state {
253251
PROCESS_REQUEST_DONE,
254252
};
255253

256-
static int process_request(void)
254+
static int process_request(struct repository *r)
257255
{
258256
enum request_state state = PROCESS_REQUEST_KEYS;
259257
struct packet_reader reader;
@@ -278,8 +276,8 @@ static int process_request(void)
278276
case PACKET_READ_EOF:
279277
BUG("Should have already died when seeing EOF");
280278
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))
283281
seen_capability_or_command = 1;
284282
else
285283
die("unknown capability '%s'", reader.line);
@@ -319,30 +317,30 @@ static int process_request(void)
319317
if (!command)
320318
die("no command requested");
321319

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))
323321
die("mismatched object format: server %s; client %s",
324-
the_repository->hash_algo->name,
322+
r->hash_algo->name,
325323
hash_algos[client_hash_algo].name);
326324

327-
command->command(the_repository, &reader);
325+
command->command(r, &reader);
328326

329327
return 0;
330328
}
331329

332-
void protocol_v2_serve_loop(int stateless_rpc)
330+
void protocol_v2_serve_loop(struct repository *r, int stateless_rpc)
333331
{
334332
if (!stateless_rpc)
335-
protocol_v2_advertise_capabilities();
333+
protocol_v2_advertise_capabilities(r);
336334

337335
/*
338336
* If stateless-rpc was requested then exit after
339337
* a single request/response exchange
340338
*/
341339
if (stateless_rpc) {
342-
process_request();
340+
process_request(r);
343341
} else {
344342
for (;;)
345-
if (process_request())
343+
if (process_request(r))
346344
break;
347345
}
348346
}

serve.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef SERVE_H
22
#define SERVE_H
33

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

79
#endif /* SERVE_H */

t/helper/test-serve-v2.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
13
#include "test-tool.h"
24
#include "gettext.h"
35
#include "parse-options.h"
6+
#include "repository.h"
47
#include "serve.h"
58
#include "setup.h"
69

@@ -28,9 +31,9 @@ int cmd__serve_v2(int argc, const char **argv)
2831
PARSE_OPT_KEEP_UNKNOWN_OPT);
2932

3033
if (advertise_capabilities)
31-
protocol_v2_advertise_capabilities();
34+
protocol_v2_advertise_capabilities(the_repository);
3235
else
33-
protocol_v2_serve_loop(stateless_rpc);
36+
protocol_v2_serve_loop(the_repository, stateless_rpc);
3437

3538
return 0;
3639
}

0 commit comments

Comments
 (0)