Skip to content

Commit ef7dc2e

Browse files
jonathantanmygitster
authored andcommitted
promisor-remote: support per-repository config
Instead of using global variables to store promisor remote information, store this config in struct repository instead, and add repository-agnostic non-static functions corresponding to the existing non-static functions that only work on the_repository. The actual lazy-fetching of missing objects currently does not work on repositories other than the_repository, and will still not work after this commit, so add a BUG message explaining this. A subsequent commit will remove this limitation. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ebaf3bc commit ef7dc2e

File tree

4 files changed

+82
-46
lines changed

4 files changed

+82
-46
lines changed

promisor-remote.c

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include "transport.h"
66
#include "strvec.h"
77

8+
struct promisor_remote_config {
9+
struct promisor_remote *promisors;
10+
struct promisor_remote **promisors_tail;
11+
};
12+
813
static int fetch_objects(const char *remote_name,
914
const struct object_id *oids,
1015
int oid_nr)
@@ -35,10 +40,8 @@ static int fetch_objects(const char *remote_name,
3540
return finish_command(&child) ? -1 : 0;
3641
}
3742

38-
static struct promisor_remote *promisors;
39-
static struct promisor_remote **promisors_tail = &promisors;
40-
41-
static struct promisor_remote *promisor_remote_new(const char *remote_name)
43+
static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
44+
const char *remote_name)
4245
{
4346
struct promisor_remote *r;
4447

@@ -50,18 +53,19 @@ static struct promisor_remote *promisor_remote_new(const char *remote_name)
5053

5154
FLEX_ALLOC_STR(r, name, remote_name);
5255

53-
*promisors_tail = r;
54-
promisors_tail = &r->next;
56+
*config->promisors_tail = r;
57+
config->promisors_tail = &r->next;
5558

5659
return r;
5760
}
5861

59-
static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
62+
static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
63+
const char *remote_name,
6064
struct promisor_remote **previous)
6165
{
6266
struct promisor_remote *r, *p;
6367

64-
for (p = NULL, r = promisors; r; p = r, r = r->next)
68+
for (p = NULL, r = config->promisors; r; p = r, r = r->next)
6569
if (!strcmp(r->name, remote_name)) {
6670
if (previous)
6771
*previous = p;
@@ -71,7 +75,8 @@ static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
7175
return NULL;
7276
}
7377

74-
static void promisor_remote_move_to_tail(struct promisor_remote *r,
78+
static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
79+
struct promisor_remote *r,
7580
struct promisor_remote *previous)
7681
{
7782
if (r->next == NULL)
@@ -80,14 +85,15 @@ static void promisor_remote_move_to_tail(struct promisor_remote *r,
8085
if (previous)
8186
previous->next = r->next;
8287
else
83-
promisors = r->next ? r->next : r;
88+
config->promisors = r->next ? r->next : r;
8489
r->next = NULL;
85-
*promisors_tail = r;
86-
promisors_tail = &r->next;
90+
*config->promisors_tail = r;
91+
config->promisors_tail = &r->next;
8792
}
8893

8994
static int promisor_remote_config(const char *var, const char *value, void *data)
9095
{
96+
struct promisor_remote_config *config = data;
9197
const char *name;
9298
size_t namelen;
9399
const char *subkey;
@@ -103,8 +109,8 @@ static int promisor_remote_config(const char *var, const char *value, void *data
103109

104110
remote_name = xmemdupz(name, namelen);
105111

106-
if (!promisor_remote_lookup(remote_name, NULL))
107-
promisor_remote_new(remote_name);
112+
if (!promisor_remote_lookup(config, remote_name, NULL))
113+
promisor_remote_new(config, remote_name);
108114

109115
free(remote_name);
110116
return 0;
@@ -113,9 +119,9 @@ static int promisor_remote_config(const char *var, const char *value, void *data
113119
struct promisor_remote *r;
114120
char *remote_name = xmemdupz(name, namelen);
115121

116-
r = promisor_remote_lookup(remote_name, NULL);
122+
r = promisor_remote_lookup(config, remote_name, NULL);
117123
if (!r)
118-
r = promisor_remote_new(remote_name);
124+
r = promisor_remote_new(config, remote_name);
119125

120126
free(remote_name);
121127

@@ -128,59 +134,63 @@ static int promisor_remote_config(const char *var, const char *value, void *data
128134
return 0;
129135
}
130136

131-
static int initialized;
132-
133-
static void promisor_remote_init(void)
137+
static void promisor_remote_init(struct repository *r)
134138
{
135-
if (initialized)
139+
struct promisor_remote_config *config;
140+
141+
if (r->promisor_remote_config)
136142
return;
137-
initialized = 1;
143+
config = r->promisor_remote_config =
144+
xcalloc(sizeof(*r->promisor_remote_config), 1);
145+
config->promisors_tail = &config->promisors;
138146

139-
git_config(promisor_remote_config, NULL);
147+
repo_config(r, promisor_remote_config, config);
140148

141-
if (the_repository->repository_format_partial_clone) {
149+
if (r->repository_format_partial_clone) {
142150
struct promisor_remote *o, *previous;
143151

144-
o = promisor_remote_lookup(the_repository->repository_format_partial_clone,
152+
o = promisor_remote_lookup(config,
153+
r->repository_format_partial_clone,
145154
&previous);
146155
if (o)
147-
promisor_remote_move_to_tail(o, previous);
156+
promisor_remote_move_to_tail(config, o, previous);
148157
else
149-
promisor_remote_new(the_repository->repository_format_partial_clone);
158+
promisor_remote_new(config, r->repository_format_partial_clone);
150159
}
151160
}
152161

153-
static void promisor_remote_clear(void)
162+
void promisor_remote_clear(struct promisor_remote_config *config)
154163
{
155-
while (promisors) {
156-
struct promisor_remote *r = promisors;
157-
promisors = promisors->next;
164+
while (config->promisors) {
165+
struct promisor_remote *r = config->promisors;
166+
config->promisors = config->promisors->next;
158167
free(r);
159168
}
160169

161-
promisors_tail = &promisors;
170+
config->promisors_tail = &config->promisors;
162171
}
163172

164-
void promisor_remote_reinit(void)
173+
void repo_promisor_remote_reinit(struct repository *r)
165174
{
166-
initialized = 0;
167-
promisor_remote_clear();
168-
promisor_remote_init();
175+
promisor_remote_clear(r->promisor_remote_config);
176+
FREE_AND_NULL(r->promisor_remote_config);
177+
promisor_remote_init(r);
169178
}
170179

171-
struct promisor_remote *promisor_remote_find(const char *remote_name)
180+
struct promisor_remote *repo_promisor_remote_find(struct repository *r,
181+
const char *remote_name)
172182
{
173-
promisor_remote_init();
183+
promisor_remote_init(r);
174184

175185
if (!remote_name)
176-
return promisors;
186+
return r->promisor_remote_config->promisors;
177187

178-
return promisor_remote_lookup(remote_name, NULL);
188+
return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
179189
}
180190

181-
int has_promisor_remote(void)
191+
int repo_has_promisor_remote(struct repository *r)
182192
{
183-
return !!promisor_remote_find(NULL);
193+
return !!repo_promisor_remote_find(r, NULL);
184194
}
185195

186196
static int remove_fetched_oids(struct repository *repo,
@@ -228,9 +238,11 @@ int promisor_remote_get_direct(struct repository *repo,
228238
if (oid_nr == 0)
229239
return 0;
230240

231-
promisor_remote_init();
241+
promisor_remote_init(repo);
232242

233-
for (r = promisors; r; r = r->next) {
243+
if (repo != the_repository)
244+
BUG("only the_repository is supported for now");
245+
for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
234246
if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
235247
if (remaining_nr == 1)
236248
continue;

promisor-remote.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,25 @@ struct promisor_remote {
1717
const char name[FLEX_ARRAY];
1818
};
1919

20-
void promisor_remote_reinit(void);
21-
struct promisor_remote *promisor_remote_find(const char *remote_name);
22-
int has_promisor_remote(void);
20+
void repo_promisor_remote_reinit(struct repository *r);
21+
static inline void promisor_remote_reinit(void)
22+
{
23+
repo_promisor_remote_reinit(the_repository);
24+
}
25+
26+
void promisor_remote_clear(struct promisor_remote_config *config);
27+
28+
struct promisor_remote *repo_promisor_remote_find(struct repository *r, const char *remote_name);
29+
static inline struct promisor_remote *promisor_remote_find(const char *remote_name)
30+
{
31+
return repo_promisor_remote_find(the_repository, remote_name);
32+
}
33+
34+
int repo_has_promisor_remote(struct repository *r);
35+
static inline int has_promisor_remote(void)
36+
{
37+
return repo_has_promisor_remote(the_repository);
38+
}
2339

2440
/*
2541
* Fetches all requested objects from all promisor remotes, trying them one at

repository.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lockfile.h"
1212
#include "submodule-config.h"
1313
#include "sparse-index.h"
14+
#include "promisor-remote.h"
1415

1516
/* The main repository */
1617
static struct repository the_repo;
@@ -262,6 +263,11 @@ void repo_clear(struct repository *repo)
262263
if (repo->index != &the_index)
263264
FREE_AND_NULL(repo->index);
264265
}
266+
267+
if (repo->promisor_remote_config) {
268+
promisor_remote_clear(repo->promisor_remote_config);
269+
FREE_AND_NULL(repo->promisor_remote_config);
270+
}
265271
}
266272

267273
int repo_read_index(struct repository *repo)

repository.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct lock_file;
1010
struct pathspec;
1111
struct raw_object_store;
1212
struct submodule_cache;
13+
struct promisor_remote_config;
1314

1415
enum untracked_cache_setting {
1516
UNTRACKED_CACHE_UNSET = -1,
@@ -141,6 +142,7 @@ struct repository {
141142

142143
/* Configurations related to promisor remotes. */
143144
char *repository_format_partial_clone;
145+
struct promisor_remote_config *promisor_remote_config;
144146

145147
/* Configurations */
146148

0 commit comments

Comments
 (0)