Skip to content

Commit 085b98f

Browse files
chooglengitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd3cb05 commit 085b98f

File tree

2 files changed

+75
-86
lines changed

2 files changed

+75
-86
lines changed

remote.c

Lines changed: 73 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
6565
remote->pushurl[remote->pushurl_nr++] = pushurl;
6666
}
6767

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)
6970
{
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);
7272
if (pushurl != url)
7373
add_pushurl(remote, pushurl);
7474
}
7575

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)
7778
{
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);
8181
}
8282

8383
struct remotes_hash_key {
@@ -102,7 +102,8 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
102102
return strcmp(a->name, b->name);
103103
}
104104

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)
106107
{
107108
struct remote *ret;
108109
struct remotes_hash_key lookup;
@@ -115,8 +116,7 @@ static struct remote *make_remote(const char *name, int len)
115116
lookup.len = len;
116117
hashmap_entry_init(&lookup_entry, memhash(name, len));
117118

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);
120120
if (e)
121121
return container_of(e, struct remote, ent);
122122

@@ -127,15 +127,12 @@ static struct remote *make_remote(const char *name, int len)
127127
refspec_init(&ret->push, REFSPEC_PUSH);
128128
refspec_init(&ret->fetch, REFSPEC_FETCH);
129129

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;
135133

136134
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))
139136
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
140137
return ret;
141138
}
@@ -169,25 +166,22 @@ static void add_merge(struct branch *branch, const char *name)
169166
branch->merge_name[branch->merge_nr++] = name;
170167
}
171168

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)
173171
{
174172
struct branch *ret;
175173
int i;
176174

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];
183179
}
184180

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);
188183
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;
191185
ret->name = xstrndup(name, len);
192186
ret->refname = xstrfmt("refs/heads/%s", ret->name);
193187

@@ -229,7 +223,8 @@ static const char *skip_spaces(const char *s)
229223
return s;
230224
}
231225

232-
static void read_remotes_file(struct remote *remote)
226+
static void read_remotes_file(struct remote_state *remote_state,
227+
struct remote *remote)
233228
{
234229
struct strbuf buf = STRBUF_INIT;
235230
FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
@@ -244,7 +239,8 @@ static void read_remotes_file(struct remote *remote)
244239
strbuf_rtrim(&buf);
245240

246241
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)));
248244
else if (skip_prefix(buf.buf, "Push:", &v))
249245
refspec_append(&remote->push, skip_spaces(v));
250246
else if (skip_prefix(buf.buf, "Pull:", &v))
@@ -254,7 +250,8 @@ static void read_remotes_file(struct remote *remote)
254250
fclose(f);
255251
}
256252

257-
static void read_branches_file(struct remote *remote)
253+
static void read_branches_file(struct remote_state *remote_state,
254+
struct remote *remote)
258255
{
259256
char *frag;
260257
struct strbuf buf = STRBUF_INIT;
@@ -286,7 +283,7 @@ static void read_branches_file(struct remote *remote)
286283
else
287284
frag = (char *)git_default_branch_name(0);
288285

289-
add_url_alias(remote, strbuf_detach(&buf, NULL));
286+
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
290287
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
291288
frag, remote->name);
292289

@@ -305,10 +302,12 @@ static int handle_config(const char *key, const char *value, void *cb)
305302
const char *subkey;
306303
struct remote *remote;
307304
struct branch *branch;
305+
struct remote_state *remote_state = cb;
306+
308307
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
309308
if (!name)
310309
return 0;
311-
branch = make_branch(name, namelen);
310+
branch = make_branch(remote_state, name, namelen);
312311
if (!strcmp(subkey, "remote")) {
313312
return git_config_string(&branch->remote_name, key, value);
314313
} else if (!strcmp(subkey, "pushremote")) {
@@ -327,16 +326,14 @@ static int handle_config(const char *key, const char *value, void *cb)
327326
if (!strcmp(subkey, "insteadof")) {
328327
if (!value)
329328
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);
333331
add_instead_of(rewrite, xstrdup(value));
334332
} else if (!strcmp(subkey, "pushinsteadof")) {
335333
if (!value)
336334
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);
340337
add_instead_of(rewrite, xstrdup(value));
341338
}
342339
}
@@ -346,9 +343,8 @@ static int handle_config(const char *key, const char *value, void *cb)
346343

347344
/* Handle remote.* variables */
348345
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);
352348

353349
if (!name)
354350
return 0;
@@ -358,7 +354,7 @@ static int handle_config(const char *key, const char *value, void *cb)
358354
name);
359355
return 0;
360356
}
361-
remote = make_remote(name, namelen);
357+
remote = make_remote(remote_state, name, namelen);
362358
remote->origin = REMOTE_CONFIG;
363359
if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
364360
current_config_scope() == CONFIG_SCOPE_WORKTREE)
@@ -428,61 +424,51 @@ static int handle_config(const char *key, const char *value, void *cb)
428424
return 0;
429425
}
430426

431-
static void alias_all_urls(void)
427+
static void alias_all_urls(struct remote_state *remote_state)
432428
{
433429
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++) {
435431
int add_pushurl_aliases;
436-
if (!the_repository->remote_state->remotes[i])
432+
if (!remote_state->remotes[i])
437433
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);
446438
}
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++) {
453441
if (add_pushurl_aliases)
454442
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);
462448
}
463449
}
464450
}
465451

466-
static void read_config(void)
452+
static void read_config(struct repository *repo)
467453
{
468-
static int loaded;
469454
int flag;
470455

471-
if (loaded)
456+
if (repo->remote_state->initialized)
472457
return;
473-
loaded = 1;
458+
repo->remote_state->initialized = 1;
474459

475-
the_repository->remote_state->current_branch = NULL;
460+
repo->remote_state->current_branch = NULL;
476461
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);
478464
if (head_ref && (flag & REF_ISSYMREF) &&
479465
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));
482468
}
483469
}
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);
486472
}
487473

488474
static int valid_remote_nick(const char *name)
@@ -552,23 +538,23 @@ static struct remote *remote_get_1(const char *name,
552538
struct remote *ret;
553539
int name_given = 0;
554540

555-
read_config();
541+
read_config(the_repository);
556542

557543
if (name)
558544
name_given = 1;
559545
else
560546
name = get_default(the_repository->remote_state->current_branch,
561547
&name_given);
562548

563-
ret = make_remote(name, 0);
549+
ret = make_remote(the_repository->remote_state, name, 0);
564550
if (valid_remote_nick(name) && have_git_dir()) {
565551
if (!valid_remote(ret))
566-
read_remotes_file(ret);
552+
read_remotes_file(the_repository->remote_state, ret);
567553
if (!valid_remote(ret))
568-
read_branches_file(ret);
554+
read_branches_file(the_repository->remote_state, ret);
569555
}
570556
if (name_given && !valid_remote(ret))
571-
add_url_alias(ret, name);
557+
add_url_alias(the_repository->remote_state, ret, name);
572558
if (!valid_remote(ret))
573559
return NULL;
574560
return ret;
@@ -596,7 +582,7 @@ int remote_is_configured(struct remote *remote, int in_repo)
596582
int for_each_remote(each_remote_fn fn, void *priv)
597583
{
598584
int i, result = 0;
599-
read_config();
585+
read_config(the_repository);
600586
for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
601587
i++) {
602588
struct remote *remote =
@@ -1709,11 +1695,12 @@ struct branch *branch_get(const char *name)
17091695
{
17101696
struct branch *ret;
17111697

1712-
read_config();
1698+
read_config(the_repository);
17131699
if (!name || !*name || !strcmp(name, "HEAD"))
17141700
ret = the_repository->remote_state->current_branch;
17151701
else
1716-
ret = make_branch(name, strlen(name));
1702+
ret = make_branch(the_repository->remote_state, name,
1703+
strlen(name));
17171704
set_merge(ret);
17181705
return ret;
17191706
}

remote.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ struct remote_state {
5252

5353
struct rewrites rewrites;
5454
struct rewrites rewrites_push;
55+
56+
int initialized;
5557
};
5658

5759
void remote_state_clear(struct remote_state *remote_state);

0 commit comments

Comments
 (0)