Skip to content

Commit fd3cb05

Browse files
chooglengitster
authored andcommitted
remote: move static variables into per-repository struct
remote.c does not works with non-the_repository because it stores its state as static variables. To support non-the_repository, we can use a per-repository struct for the remotes subsystem. Prepare for this change by defining a struct remote_state that holds the remotes subsystem state and move the static variables of remote.c into the_repository->remote_state. This introduces no behavioral or API changes. Signed-off-by: Glen Choo <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e083ef5 commit fd3cb05

File tree

4 files changed

+170
-67
lines changed

4 files changed

+170
-67
lines changed

remote.c

Lines changed: 124 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,6 @@ struct counted_string {
2121
size_t len;
2222
const char *s;
2323
};
24-
struct rewrite {
25-
const char *base;
26-
size_t baselen;
27-
struct counted_string *instead_of;
28-
int instead_of_nr;
29-
int instead_of_alloc;
30-
};
31-
struct rewrites {
32-
struct rewrite **rewrite;
33-
int rewrite_alloc;
34-
int rewrite_nr;
35-
};
36-
37-
static struct remote **remotes;
38-
static int remotes_alloc;
39-
static int remotes_nr;
40-
static struct hashmap remotes_hash;
41-
42-
static struct branch **branches;
43-
static int branches_alloc;
44-
static int branches_nr;
45-
46-
static struct branch *current_branch;
47-
static const char *pushremote_name;
48-
49-
static struct rewrites rewrites;
50-
static struct rewrites rewrites_push;
5124

5225
static int valid_remote(const struct remote *remote)
5326
{
@@ -94,14 +67,16 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
9467

9568
static void add_pushurl_alias(struct remote *remote, const char *url)
9669
{
97-
const char *pushurl = alias_url(url, &rewrites_push);
70+
const char *pushurl =
71+
alias_url(url, &the_repository->remote_state->rewrites_push);
9872
if (pushurl != url)
9973
add_pushurl(remote, pushurl);
10074
}
10175

10276
static void add_url_alias(struct remote *remote, const char *url)
10377
{
104-
add_url(remote, alias_url(url, &rewrites));
78+
add_url(remote,
79+
alias_url(url, &the_repository->remote_state->rewrites));
10580
add_pushurl_alias(remote, url);
10681
}
10782

@@ -127,12 +102,6 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
127102
return strcmp(a->name, b->name);
128103
}
129104

130-
static inline void init_remotes_hash(void)
131-
{
132-
if (!remotes_hash.cmpfn)
133-
hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
134-
}
135-
136105
static struct remote *make_remote(const char *name, int len)
137106
{
138107
struct remote *ret;
@@ -142,12 +111,12 @@ static struct remote *make_remote(const char *name, int len)
142111
if (!len)
143112
len = strlen(name);
144113

145-
init_remotes_hash();
146114
lookup.str = name;
147115
lookup.len = len;
148116
hashmap_entry_init(&lookup_entry, memhash(name, len));
149117

150-
e = hashmap_get(&remotes_hash, &lookup_entry, &lookup);
118+
e = hashmap_get(&the_repository->remote_state->remotes_hash,
119+
&lookup_entry, &lookup);
151120
if (e)
152121
return container_of(e, struct remote, ent);
153122

@@ -158,15 +127,41 @@ static struct remote *make_remote(const char *name, int len)
158127
refspec_init(&ret->push, REFSPEC_PUSH);
159128
refspec_init(&ret->fetch, REFSPEC_FETCH);
160129

161-
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
162-
remotes[remotes_nr++] = ret;
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;
163135

164136
hashmap_entry_init(&ret->ent, lookup_entry.hash);
165-
if (hashmap_put_entry(&remotes_hash, ret, ent))
137+
if (hashmap_put_entry(&the_repository->remote_state->remotes_hash, ret,
138+
ent))
166139
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
167140
return ret;
168141
}
169142

143+
static void remote_clear(struct remote *remote)
144+
{
145+
int i;
146+
147+
free((char *)remote->name);
148+
free((char *)remote->foreign_vcs);
149+
150+
for (i = 0; i < remote->url_nr; i++) {
151+
free((char *)remote->url[i]);
152+
}
153+
FREE_AND_NULL(remote->pushurl);
154+
155+
for (i = 0; i < remote->pushurl_nr; i++) {
156+
free((char *)remote->pushurl[i]);
157+
}
158+
FREE_AND_NULL(remote->pushurl);
159+
free((char *)remote->receivepack);
160+
free((char *)remote->uploadpack);
161+
FREE_AND_NULL(remote->http_proxy);
162+
FREE_AND_NULL(remote->http_proxy_authmethod);
163+
}
164+
170165
static void add_merge(struct branch *branch, const char *name)
171166
{
172167
ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
@@ -179,15 +174,20 @@ static struct branch *make_branch(const char *name, size_t len)
179174
struct branch *ret;
180175
int i;
181176

182-
for (i = 0; i < branches_nr; i++) {
183-
if (!strncmp(name, branches[i]->name, len) &&
184-
!branches[i]->name[len])
185-
return branches[i];
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];
186183
}
187184

188-
ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
185+
ALLOC_GROW(the_repository->remote_state->branches,
186+
the_repository->remote_state->branches_nr + 1,
187+
the_repository->remote_state->branches_alloc);
189188
CALLOC_ARRAY(ret, 1);
190-
branches[branches_nr++] = ret;
189+
the_repository->remote_state
190+
->branches[the_repository->remote_state->branches_nr++] = ret;
191191
ret->name = xstrndup(name, len);
192192
ret->refname = xstrfmt("refs/heads/%s", ret->name);
193193

@@ -327,12 +327,16 @@ static int handle_config(const char *key, const char *value, void *cb)
327327
if (!strcmp(subkey, "insteadof")) {
328328
if (!value)
329329
return config_error_nonbool(key);
330-
rewrite = make_rewrite(&rewrites, name, namelen);
330+
rewrite = make_rewrite(
331+
&the_repository->remote_state->rewrites, name,
332+
namelen);
331333
add_instead_of(rewrite, xstrdup(value));
332334
} else if (!strcmp(subkey, "pushinsteadof")) {
333335
if (!value)
334336
return config_error_nonbool(key);
335-
rewrite = make_rewrite(&rewrites_push, name, namelen);
337+
rewrite = make_rewrite(
338+
&the_repository->remote_state->rewrites_push,
339+
name, namelen);
336340
add_instead_of(rewrite, xstrdup(value));
337341
}
338342
}
@@ -342,7 +346,9 @@ static int handle_config(const char *key, const char *value, void *cb)
342346

343347
/* Handle remote.* variables */
344348
if (!name && !strcmp(subkey, "pushdefault"))
345-
return git_config_string(&pushremote_name, key, value);
349+
return git_config_string(
350+
&the_repository->remote_state->pushremote_name, key,
351+
value);
346352

347353
if (!name)
348354
return 0;
@@ -425,18 +431,34 @@ static int handle_config(const char *key, const char *value, void *cb)
425431
static void alias_all_urls(void)
426432
{
427433
int i, j;
428-
for (i = 0; i < remotes_nr; i++) {
434+
for (i = 0; i < the_repository->remote_state->remotes_nr; i++) {
429435
int add_pushurl_aliases;
430-
if (!remotes[i])
436+
if (!the_repository->remote_state->remotes[i])
431437
continue;
432-
for (j = 0; j < remotes[i]->pushurl_nr; j++) {
433-
remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
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);
434446
}
435-
add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
436-
for (j = 0; j < remotes[i]->url_nr; j++) {
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++) {
437453
if (add_pushurl_aliases)
438-
add_pushurl_alias(remotes[i], remotes[i]->url[j]);
439-
remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
454+
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);
440462
}
441463
}
442464
}
@@ -450,12 +472,13 @@ static void read_config(void)
450472
return;
451473
loaded = 1;
452474

453-
current_branch = NULL;
475+
the_repository->remote_state->current_branch = NULL;
454476
if (startup_info->have_repository) {
455477
const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
456478
if (head_ref && (flag & REF_ISSYMREF) &&
457479
skip_prefix(head_ref, "refs/heads/", &head_ref)) {
458-
current_branch = make_branch(head_ref, strlen(head_ref));
480+
the_repository->remote_state->current_branch =
481+
make_branch(head_ref, strlen(head_ref));
459482
}
460483
}
461484
git_config(handle_config, NULL);
@@ -493,10 +516,10 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
493516
*explicit = 1;
494517
return branch->pushremote_name;
495518
}
496-
if (pushremote_name) {
519+
if (the_repository->remote_state->pushremote_name) {
497520
if (explicit)
498521
*explicit = 1;
499-
return pushremote_name;
522+
return the_repository->remote_state->pushremote_name;
500523
}
501524
return remote_for_branch(branch, explicit);
502525
}
@@ -534,7 +557,8 @@ static struct remote *remote_get_1(const char *name,
534557
if (name)
535558
name_given = 1;
536559
else
537-
name = get_default(current_branch, &name_given);
560+
name = get_default(the_repository->remote_state->current_branch,
561+
&name_given);
538562

539563
ret = make_remote(name, 0);
540564
if (valid_remote_nick(name) && have_git_dir()) {
@@ -573,11 +597,13 @@ int for_each_remote(each_remote_fn fn, void *priv)
573597
{
574598
int i, result = 0;
575599
read_config();
576-
for (i = 0; i < remotes_nr && !result; i++) {
577-
struct remote *r = remotes[i];
578-
if (!r)
600+
for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
601+
i++) {
602+
struct remote *remote =
603+
the_repository->remote_state->remotes[i];
604+
if (!remote)
579605
continue;
580-
result = fn(r, priv);
606+
result = fn(remote, priv);
581607
}
582608
return result;
583609
}
@@ -1685,7 +1711,7 @@ struct branch *branch_get(const char *name)
16851711

16861712
read_config();
16871713
if (!name || !*name || !strcmp(name, "HEAD"))
1688-
ret = current_branch;
1714+
ret = the_repository->remote_state->current_branch;
16891715
else
16901716
ret = make_branch(name, strlen(name));
16911717
set_merge(ret);
@@ -2585,3 +2611,34 @@ void apply_push_cas(struct push_cas_option *cas,
25852611
check_if_includes_upstream(ref);
25862612
}
25872613
}
2614+
2615+
struct remote_state *remote_state_new(void)
2616+
{
2617+
struct remote_state *r = xmalloc(sizeof(*r));
2618+
2619+
memset(r, 0, sizeof(*r));
2620+
2621+
hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);
2622+
return r;
2623+
}
2624+
2625+
void remote_state_clear(struct remote_state *remote_state)
2626+
{
2627+
int i;
2628+
2629+
for (i = 0; i < remote_state->remotes_nr; i++) {
2630+
remote_clear(remote_state->remotes[i]);
2631+
}
2632+
FREE_AND_NULL(remote_state->remotes);
2633+
remote_state->remotes_alloc = 0;
2634+
remote_state->remotes_nr = 0;
2635+
2636+
hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent);
2637+
2638+
for (i = 0; i < remote_state->branches_nr; i++) {
2639+
FREE_AND_NULL(remote_state->branches[i]);
2640+
}
2641+
FREE_AND_NULL(remote_state->branches);
2642+
remote_state->branches_alloc = 0;
2643+
remote_state->branches_nr = 0;
2644+
}

remote.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ enum {
2323
REMOTE_BRANCHES
2424
};
2525

26+
struct rewrite {
27+
const char *base;
28+
size_t baselen;
29+
struct counted_string *instead_of;
30+
int instead_of_nr;
31+
int instead_of_alloc;
32+
};
33+
34+
struct rewrites {
35+
struct rewrite **rewrite;
36+
int rewrite_alloc;
37+
int rewrite_nr;
38+
};
39+
40+
struct remote_state {
41+
struct remote **remotes;
42+
int remotes_alloc;
43+
int remotes_nr;
44+
struct hashmap remotes_hash;
45+
46+
struct branch **branches;
47+
int branches_alloc;
48+
int branches_nr;
49+
50+
struct branch *current_branch;
51+
const char *pushremote_name;
52+
53+
struct rewrites rewrites;
54+
struct rewrites rewrites_push;
55+
};
56+
57+
void remote_state_clear(struct remote_state *remote_state);
58+
struct remote_state *remote_state_new(void);
59+
2660
struct remote {
2761
struct hashmap_entry ent;
2862

0 commit comments

Comments
 (0)