remote: use remote_state parameter internally
Without changing external-facing functions, replace the_repository->remote_state internally by adding a struct remote_state parameter. As a result, external-facing functions are still tied to the_repository, but most static functions no longer reference the_repository->remote_state. The exceptions are those that are used in a way that depends on external-facing functions e.g. the callbacks to remote_get_1(). Signed-off-by: Glen Choo <chooglen@google.com> Reviewed-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Glen Choo committed
Nov 17, 2021 at 16:53 UTC
085b98f6cdbe9ed794e19ded00ccd0431c30faa0
2 files changed
+75
-86
remote.c
+73
-86
@@ -65,19 +65,19 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
65
remote->pushurl[remote->pushurl_nr++] = pushurl;
66
}
67
68
-static void add_pushurl_alias(struct remote *remote, const char *url)
68
+static void add_pushurl_alias(struct remote_state *remote_state,
69
+ struct remote *remote, const char *url)
70
{
70
- const char *pushurl =
71
- alias_url(url, &the_repository->remote_state->rewrites_push);
71
+ const char *pushurl = alias_url(url, &remote_state->rewrites_push);
72
if (pushurl != url)
73
add_pushurl(remote, pushurl);
74
}
75
76
-static void add_url_alias(struct remote *remote, const char *url)
76
+static void add_url_alias(struct remote_state *remote_state,
77
+ struct remote *remote, const char *url)
78
{
78
- add_url(remote,
79
- alias_url(url, &the_repository->remote_state->rewrites));
80
- add_pushurl_alias(remote, url);
79
+ add_url(remote, alias_url(url, &remote_state->rewrites));
80
+ add_pushurl_alias(remote_state, remote, url);
81
}
82
83
struct remotes_hash_key {
@@ -102,7 +102,8 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
102
return strcmp(a->name, b->name);
103
}
104
105
-static struct remote *make_remote(const char *name, int len)
105
+static struct remote *make_remote(struct remote_state *remote_state,
106
+ const char *name, int len)
107
{
108
struct remote *ret;
109
struct remotes_hash_key lookup;
@@ -115,8 +116,7 @@ static struct remote *make_remote(const char *name, int len)
116
lookup.len = len;
117
hashmap_entry_init(&lookup_entry, memhash(name, len));
118
118
- e = hashmap_get(&the_repository->remote_state->remotes_hash,
119
- &lookup_entry, &lookup);
119
+ e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup);
120
if (e)
121
return container_of(e, struct remote, ent);
122
@@ -127,15 +127,12 @@ static struct remote *make_remote(const char *name, int len)
127
refspec_init(&ret->push, REFSPEC_PUSH);
128
refspec_init(&ret->fetch, REFSPEC_FETCH);
129
130
- ALLOC_GROW(the_repository->remote_state->remotes,
131
- the_repository->remote_state->remotes_nr + 1,
132
- the_repository->remote_state->remotes_alloc);
133
- the_repository->remote_state
134
- ->remotes[the_repository->remote_state->remotes_nr++] = ret;
130
+ ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
131
+ remote_state->remotes_alloc);
132
+ remote_state->remotes[remote_state->remotes_nr++] = ret;
133
134
hashmap_entry_init(&ret->ent, lookup_entry.hash);
137
- if (hashmap_put_entry(&the_repository->remote_state->remotes_hash, ret,
138
- ent))
135
+ if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent))
136
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
137
return ret;
138
}
@@ -169,25 +166,22 @@ static void add_merge(struct branch *branch, const char *name)
166
branch->merge_name[branch->merge_nr++] = name;
167
}
168
172
-static struct branch *make_branch(const char *name, size_t len)
169
+static struct branch *make_branch(struct remote_state *remote_state,
170
+ const char *name, size_t len)
171
{
172
struct branch *ret;
173
int i;
174
177
- for (i = 0; i < the_repository->remote_state->branches_nr; i++) {
178
- if (!strncmp(name,
179
- the_repository->remote_state->branches[i]->name,
180
- len) &&
181
- !the_repository->remote_state->branches[i]->name[len])
182
- return the_repository->remote_state->branches[i];
175
+ for (i = 0; i < remote_state->branches_nr; i++) {
176
+ if (!strncmp(name, remote_state->branches[i]->name, len) &&
177
+ !remote_state->branches[i]->name[len])
178
+ return remote_state->branches[i];
179
}
180
185
- ALLOC_GROW(the_repository->remote_state->branches,
186
- the_repository->remote_state->branches_nr + 1,
187
- the_repository->remote_state->branches_alloc);
181
+ ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1,
182
+ remote_state->branches_alloc);
183
CALLOC_ARRAY(ret, 1);
189
- the_repository->remote_state
190
- ->branches[the_repository->remote_state->branches_nr++] = ret;
184
+ remote_state->branches[remote_state->branches_nr++] = ret;
185
ret->name = xstrndup(name, len);
186
ret->refname = xstrfmt("refs/heads/%s", ret->name);
187
@@ -229,7 +223,8 @@ static const char *skip_spaces(const char *s)
223
return s;
224
}
225
232
-static void read_remotes_file(struct remote *remote)
226
+static void read_remotes_file(struct remote_state *remote_state,
227
+ struct remote *remote)
228
{
229
struct strbuf buf = STRBUF_INIT;
230
FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
@@ -244,7 +239,8 @@ static void read_remotes_file(struct remote *remote)
239
strbuf_rtrim(&buf);
240
241
if (skip_prefix(buf.buf, "URL:", &v))
247
- add_url_alias(remote, xstrdup(skip_spaces(v)));
242
+ add_url_alias(remote_state, remote,
243
+ xstrdup(skip_spaces(v)));
244
else if (skip_prefix(buf.buf, "Push:", &v))
245
refspec_append(&remote->push, skip_spaces(v));
246
else if (skip_prefix(buf.buf, "Pull:", &v))
@@ -254,7 +250,8 @@ static void read_remotes_file(struct remote *remote)
250
fclose(f);
251
}
252
257
-static void read_branches_file(struct remote *remote)
253
+static void read_branches_file(struct remote_state *remote_state,
254
+ struct remote *remote)
255
{
256
char *frag;
257
struct strbuf buf = STRBUF_INIT;
@@ -286,7 +283,7 @@ static void read_branches_file(struct remote *remote)
283
else
284
frag = (char *)git_default_branch_name(0);
285
289
- add_url_alias(remote, strbuf_detach(&buf, NULL));
286
+ add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
287
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
288
frag, remote->name);
289
@@ -305,10 +302,12 @@ static int handle_config(const char *key, const char *value, void *cb)
302
const char *subkey;
303
struct remote *remote;
304
struct branch *branch;
305
+ struct remote_state *remote_state = cb;
306
+
307
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
308
if (!name)
309
return 0;
311
- branch = make_branch(name, namelen);
310
+ branch = make_branch(remote_state, name, namelen);
311
if (!strcmp(subkey, "remote")) {
312
return git_config_string(&branch->remote_name, key, value);
313
} else if (!strcmp(subkey, "pushremote")) {
@@ -327,16 +326,14 @@ static int handle_config(const char *key, const char *value, void *cb)
326
if (!strcmp(subkey, "insteadof")) {
327
if (!value)
328
return config_error_nonbool(key);
330
- rewrite = make_rewrite(
331
- &the_repository->remote_state->rewrites, name,
332
- namelen);
329
+ rewrite = make_rewrite(&remote_state->rewrites, name,
330
+ namelen);
331
add_instead_of(rewrite, xstrdup(value));
332
} else if (!strcmp(subkey, "pushinsteadof")) {
333
if (!value)
334
return config_error_nonbool(key);
337
- rewrite = make_rewrite(
338
- &the_repository->remote_state->rewrites_push,
339
- name, namelen);
335
+ rewrite = make_rewrite(&remote_state->rewrites_push,
336
+ name, namelen);
337
add_instead_of(rewrite, xstrdup(value));
338
}
339
}
@@ -346,9 +343,8 @@ static int handle_config(const char *key, const char *value, void *cb)
343
344
/* Handle remote.* variables */
345
if (!name && !strcmp(subkey, "pushdefault"))
349
- return git_config_string(
350
- &the_repository->remote_state->pushremote_name, key,
351
- value);
346
+ return git_config_string(&remote_state->pushremote_name, key,
347
+ value);
348
349
if (!name)
350
return 0;
@@ -358,7 +354,7 @@ static int handle_config(const char *key, const char *value, void *cb)
354
name);
355
return 0;
356
}
361
- remote = make_remote(name, namelen);
357
+ remote = make_remote(remote_state, name, namelen);
358
remote->origin = REMOTE_CONFIG;
359
if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
360
current_config_scope() == CONFIG_SCOPE_WORKTREE)
@@ -428,61 +424,51 @@ static int handle_config(const char *key, const char *value, void *cb)
424
return 0;
425
}
426
431
-static void alias_all_urls(void)
427
+static void alias_all_urls(struct remote_state *remote_state)
428
{
429
int i, j;
434
- for (i = 0; i < the_repository->remote_state->remotes_nr; i++) {
430
+ for (i = 0; i < remote_state->remotes_nr; i++) {
431
int add_pushurl_aliases;
436
- if (!the_repository->remote_state->remotes[i])
432
+ if (!remote_state->remotes[i])
433
continue;
438
- for (j = 0;
439
- j < the_repository->remote_state->remotes[i]->pushurl_nr;
440
- j++) {
441
- the_repository->remote_state->remotes[i]->pushurl[j] =
442
- alias_url(
443
- the_repository->remote_state->remotes[i]
444
- ->pushurl[j],
445
- &the_repository->remote_state->rewrites);
434
+ for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {
435
+ remote_state->remotes[i]->pushurl[j] =
436
+ alias_url(remote_state->remotes[i]->pushurl[j],
437
+ &remote_state->rewrites);
438
}
447
- add_pushurl_aliases =
448
- the_repository->remote_state->remotes[i]->pushurl_nr ==
449
- 0;
450
- for (j = 0;
451
- j < the_repository->remote_state->remotes[i]->url_nr;
452
- j++) {
439
+ add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;
440
+ for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {
441
if (add_pushurl_aliases)
442
add_pushurl_alias(
455
- the_repository->remote_state->remotes[i],
456
- the_repository->remote_state->remotes[i]
457
- ->url[j]);
458
- the_repository->remote_state->remotes[i]
459
- ->url[j] = alias_url(
460
- the_repository->remote_state->remotes[i]->url[j],
461
- &the_repository->remote_state->rewrites);
443
+ remote_state, remote_state->remotes[i],
444
+ remote_state->remotes[i]->url[j]);
445
+ remote_state->remotes[i]->url[j] =
446
+ alias_url(remote_state->remotes[i]->url[j],
447
+ &remote_state->rewrites);
448
}
449
}
450
}
451
466
-static void read_config(void)
452
+static void read_config(struct repository *repo)
453
{
468
- static int loaded;
454
int flag;
455
471
- if (loaded)
456
+ if (repo->remote_state->initialized)
457
return;
473
- loaded = 1;
458
+ repo->remote_state->initialized = 1;
459
475
- the_repository->remote_state->current_branch = NULL;
460
+ repo->remote_state->current_branch = NULL;
461
if (startup_info->have_repository) {
477
- const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
462
+ const char *head_ref = refs_resolve_ref_unsafe(
463
+ get_main_ref_store(repo), "HEAD", 0, NULL, &flag);
464
if (head_ref && (flag & REF_ISSYMREF) &&
465
skip_prefix(head_ref, "refs/heads/", &head_ref)) {
480
- the_repository->remote_state->current_branch =
481
- make_branch(head_ref, strlen(head_ref));
466
+ repo->remote_state->current_branch = make_branch(
467
+ repo->remote_state, head_ref, strlen(head_ref));
468
}
469
}
484
- git_config(handle_config, NULL);
485
- alias_all_urls();
470
+ repo_config(repo, handle_config, repo->remote_state);
471
+ alias_all_urls(repo->remote_state);
472
}
473
474
static int valid_remote_nick(const char *name)
@@ -552,7 +538,7 @@ static struct remote *remote_get_1(const char *name,
538
struct remote *ret;
539
int name_given = 0;
540
555
- read_config();
541
+ read_config(the_repository);
542
543
if (name)
544
name_given = 1;
@@ -560,15 +546,15 @@ static struct remote *remote_get_1(const char *name,
546
name = get_default(the_repository->remote_state->current_branch,
547
&name_given);
548
563
- ret = make_remote(name, 0);
549
+ ret = make_remote(the_repository->remote_state, name, 0);
550
if (valid_remote_nick(name) && have_git_dir()) {
551
if (!valid_remote(ret))
566
- read_remotes_file(ret);
552
+ read_remotes_file(the_repository->remote_state, ret);
553
if (!valid_remote(ret))
568
- read_branches_file(ret);
554
+ read_branches_file(the_repository->remote_state, ret);
555
}
556
if (name_given && !valid_remote(ret))
571
- add_url_alias(ret, name);
557
+ add_url_alias(the_repository->remote_state, ret, name);
558
if (!valid_remote(ret))
559
return NULL;
560
return ret;
@@ -596,7 +582,7 @@ int remote_is_configured(struct remote *remote, int in_repo)
582
int for_each_remote(each_remote_fn fn, void *priv)
583
{
584
int i, result = 0;
599
- read_config();
585
+ read_config(the_repository);
586
for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
587
i++) {
588
struct remote *remote =
@@ -1709,11 +1695,12 @@ struct branch *branch_get(const char *name)
1695
{
1696
struct branch *ret;
1697
1712
- read_config();
1698
+ read_config(the_repository);
1699
if (!name || !*name || !strcmp(name, "HEAD"))
1700
ret = the_repository->remote_state->current_branch;
1701
else
1716
- ret = make_branch(name, strlen(name));
1702
+ ret = make_branch(the_repository->remote_state, name,
1703
+ strlen(name));
1704
set_merge(ret);
1705
return ret;
1706
}
remote.h
+2
@@ -52,6 +52,8 @@ struct remote_state {
52
53
struct rewrites rewrites;
54
struct rewrites rewrites_push;
55
+
56
+ int initialized;
57
};
58
59
void remote_state_clear(struct remote_state *remote_state);