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>
This commit is contained in:
Glen Choo 2021-11-17 16:53:23 -08:00 committed by Junio C Hamano
parent fd3cb0501e
commit 085b98f6cd
2 changed files with 75 additions and 86 deletions

153
remote.c
View File

@ -65,19 +65,19 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
remote->pushurl[remote->pushurl_nr++] = pushurl; remote->pushurl[remote->pushurl_nr++] = pushurl;
} }
static void add_pushurl_alias(struct remote *remote, const char *url) static void add_pushurl_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{ {
const char *pushurl = const char *pushurl = alias_url(url, &remote_state->rewrites_push);
alias_url(url, &the_repository->remote_state->rewrites_push);
if (pushurl != url) if (pushurl != url)
add_pushurl(remote, pushurl); add_pushurl(remote, pushurl);
} }
static void add_url_alias(struct remote *remote, const char *url) static void add_url_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{ {
add_url(remote, add_url(remote, alias_url(url, &remote_state->rewrites));
alias_url(url, &the_repository->remote_state->rewrites)); add_pushurl_alias(remote_state, remote, url);
add_pushurl_alias(remote, url);
} }
struct remotes_hash_key { struct remotes_hash_key {
@ -102,7 +102,8 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
return strcmp(a->name, b->name); return strcmp(a->name, b->name);
} }
static struct remote *make_remote(const char *name, int len) static struct remote *make_remote(struct remote_state *remote_state,
const char *name, int len)
{ {
struct remote *ret; struct remote *ret;
struct remotes_hash_key lookup; struct remotes_hash_key lookup;
@ -115,8 +116,7 @@ static struct remote *make_remote(const char *name, int len)
lookup.len = len; lookup.len = len;
hashmap_entry_init(&lookup_entry, memhash(name, len)); hashmap_entry_init(&lookup_entry, memhash(name, len));
e = hashmap_get(&the_repository->remote_state->remotes_hash, e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup);
&lookup_entry, &lookup);
if (e) if (e)
return container_of(e, struct remote, ent); return container_of(e, struct remote, ent);
@ -127,15 +127,12 @@ static struct remote *make_remote(const char *name, int len)
refspec_init(&ret->push, REFSPEC_PUSH); refspec_init(&ret->push, REFSPEC_PUSH);
refspec_init(&ret->fetch, REFSPEC_FETCH); refspec_init(&ret->fetch, REFSPEC_FETCH);
ALLOC_GROW(the_repository->remote_state->remotes, ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
the_repository->remote_state->remotes_nr + 1, remote_state->remotes_alloc);
the_repository->remote_state->remotes_alloc); remote_state->remotes[remote_state->remotes_nr++] = ret;
the_repository->remote_state
->remotes[the_repository->remote_state->remotes_nr++] = ret;
hashmap_entry_init(&ret->ent, lookup_entry.hash); hashmap_entry_init(&ret->ent, lookup_entry.hash);
if (hashmap_put_entry(&the_repository->remote_state->remotes_hash, ret, if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent))
ent))
BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
return ret; return ret;
} }
@ -169,25 +166,22 @@ static void add_merge(struct branch *branch, const char *name)
branch->merge_name[branch->merge_nr++] = name; branch->merge_name[branch->merge_nr++] = name;
} }
static struct branch *make_branch(const char *name, size_t len) static struct branch *make_branch(struct remote_state *remote_state,
const char *name, size_t len)
{ {
struct branch *ret; struct branch *ret;
int i; int i;
for (i = 0; i < the_repository->remote_state->branches_nr; i++) { for (i = 0; i < remote_state->branches_nr; i++) {
if (!strncmp(name, if (!strncmp(name, remote_state->branches[i]->name, len) &&
the_repository->remote_state->branches[i]->name, !remote_state->branches[i]->name[len])
len) && return remote_state->branches[i];
!the_repository->remote_state->branches[i]->name[len])
return the_repository->remote_state->branches[i];
} }
ALLOC_GROW(the_repository->remote_state->branches, ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1,
the_repository->remote_state->branches_nr + 1, remote_state->branches_alloc);
the_repository->remote_state->branches_alloc);
CALLOC_ARRAY(ret, 1); CALLOC_ARRAY(ret, 1);
the_repository->remote_state remote_state->branches[remote_state->branches_nr++] = ret;
->branches[the_repository->remote_state->branches_nr++] = ret;
ret->name = xstrndup(name, len); ret->name = xstrndup(name, len);
ret->refname = xstrfmt("refs/heads/%s", ret->name); ret->refname = xstrfmt("refs/heads/%s", ret->name);
@ -229,7 +223,8 @@ static const char *skip_spaces(const char *s)
return s; return s;
} }
static void read_remotes_file(struct remote *remote) static void read_remotes_file(struct remote_state *remote_state,
struct remote *remote)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r"); FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
@ -244,7 +239,8 @@ static void read_remotes_file(struct remote *remote)
strbuf_rtrim(&buf); strbuf_rtrim(&buf);
if (skip_prefix(buf.buf, "URL:", &v)) if (skip_prefix(buf.buf, "URL:", &v))
add_url_alias(remote, xstrdup(skip_spaces(v))); add_url_alias(remote_state, remote,
xstrdup(skip_spaces(v)));
else if (skip_prefix(buf.buf, "Push:", &v)) else if (skip_prefix(buf.buf, "Push:", &v))
refspec_append(&remote->push, skip_spaces(v)); refspec_append(&remote->push, skip_spaces(v));
else if (skip_prefix(buf.buf, "Pull:", &v)) else if (skip_prefix(buf.buf, "Pull:", &v))
@ -254,7 +250,8 @@ static void read_remotes_file(struct remote *remote)
fclose(f); fclose(f);
} }
static void read_branches_file(struct remote *remote) static void read_branches_file(struct remote_state *remote_state,
struct remote *remote)
{ {
char *frag; char *frag;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
@ -286,7 +283,7 @@ static void read_branches_file(struct remote *remote)
else else
frag = (char *)git_default_branch_name(0); frag = (char *)git_default_branch_name(0);
add_url_alias(remote, strbuf_detach(&buf, NULL)); add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
frag, remote->name); frag, remote->name);
@ -305,10 +302,12 @@ static int handle_config(const char *key, const char *value, void *cb)
const char *subkey; const char *subkey;
struct remote *remote; struct remote *remote;
struct branch *branch; struct branch *branch;
struct remote_state *remote_state = cb;
if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) { if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
if (!name) if (!name)
return 0; return 0;
branch = make_branch(name, namelen); branch = make_branch(remote_state, name, namelen);
if (!strcmp(subkey, "remote")) { if (!strcmp(subkey, "remote")) {
return git_config_string(&branch->remote_name, key, value); return git_config_string(&branch->remote_name, key, value);
} else if (!strcmp(subkey, "pushremote")) { } else if (!strcmp(subkey, "pushremote")) {
@ -327,15 +326,13 @@ static int handle_config(const char *key, const char *value, void *cb)
if (!strcmp(subkey, "insteadof")) { if (!strcmp(subkey, "insteadof")) {
if (!value) if (!value)
return config_error_nonbool(key); return config_error_nonbool(key);
rewrite = make_rewrite( rewrite = make_rewrite(&remote_state->rewrites, name,
&the_repository->remote_state->rewrites, name,
namelen); namelen);
add_instead_of(rewrite, xstrdup(value)); add_instead_of(rewrite, xstrdup(value));
} else if (!strcmp(subkey, "pushinsteadof")) { } else if (!strcmp(subkey, "pushinsteadof")) {
if (!value) if (!value)
return config_error_nonbool(key); return config_error_nonbool(key);
rewrite = make_rewrite( rewrite = make_rewrite(&remote_state->rewrites_push,
&the_repository->remote_state->rewrites_push,
name, namelen); name, namelen);
add_instead_of(rewrite, xstrdup(value)); add_instead_of(rewrite, xstrdup(value));
} }
@ -346,8 +343,7 @@ static int handle_config(const char *key, const char *value, void *cb)
/* Handle remote.* variables */ /* Handle remote.* variables */
if (!name && !strcmp(subkey, "pushdefault")) if (!name && !strcmp(subkey, "pushdefault"))
return git_config_string( return git_config_string(&remote_state->pushremote_name, key,
&the_repository->remote_state->pushremote_name, key,
value); value);
if (!name) if (!name)
@ -358,7 +354,7 @@ static int handle_config(const char *key, const char *value, void *cb)
name); name);
return 0; return 0;
} }
remote = make_remote(name, namelen); remote = make_remote(remote_state, name, namelen);
remote->origin = REMOTE_CONFIG; remote->origin = REMOTE_CONFIG;
if (current_config_scope() == CONFIG_SCOPE_LOCAL || if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
current_config_scope() == CONFIG_SCOPE_WORKTREE) current_config_scope() == CONFIG_SCOPE_WORKTREE)
@ -428,61 +424,51 @@ static int handle_config(const char *key, const char *value, void *cb)
return 0; return 0;
} }
static void alias_all_urls(void) static void alias_all_urls(struct remote_state *remote_state)
{ {
int i, j; int i, j;
for (i = 0; i < the_repository->remote_state->remotes_nr; i++) { for (i = 0; i < remote_state->remotes_nr; i++) {
int add_pushurl_aliases; int add_pushurl_aliases;
if (!the_repository->remote_state->remotes[i]) if (!remote_state->remotes[i])
continue; continue;
for (j = 0; for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {
j < the_repository->remote_state->remotes[i]->pushurl_nr; remote_state->remotes[i]->pushurl[j] =
j++) { alias_url(remote_state->remotes[i]->pushurl[j],
the_repository->remote_state->remotes[i]->pushurl[j] = &remote_state->rewrites);
alias_url(
the_repository->remote_state->remotes[i]
->pushurl[j],
&the_repository->remote_state->rewrites);
} }
add_pushurl_aliases = add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;
the_repository->remote_state->remotes[i]->pushurl_nr == for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {
0;
for (j = 0;
j < the_repository->remote_state->remotes[i]->url_nr;
j++) {
if (add_pushurl_aliases) if (add_pushurl_aliases)
add_pushurl_alias( add_pushurl_alias(
the_repository->remote_state->remotes[i], remote_state, remote_state->remotes[i],
the_repository->remote_state->remotes[i] remote_state->remotes[i]->url[j]);
->url[j]); remote_state->remotes[i]->url[j] =
the_repository->remote_state->remotes[i] alias_url(remote_state->remotes[i]->url[j],
->url[j] = alias_url( &remote_state->rewrites);
the_repository->remote_state->remotes[i]->url[j],
&the_repository->remote_state->rewrites);
} }
} }
} }
static void read_config(void) static void read_config(struct repository *repo)
{ {
static int loaded;
int flag; int flag;
if (loaded) if (repo->remote_state->initialized)
return; return;
loaded = 1; repo->remote_state->initialized = 1;
the_repository->remote_state->current_branch = NULL; repo->remote_state->current_branch = NULL;
if (startup_info->have_repository) { if (startup_info->have_repository) {
const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag); const char *head_ref = refs_resolve_ref_unsafe(
get_main_ref_store(repo), "HEAD", 0, NULL, &flag);
if (head_ref && (flag & REF_ISSYMREF) && if (head_ref && (flag & REF_ISSYMREF) &&
skip_prefix(head_ref, "refs/heads/", &head_ref)) { skip_prefix(head_ref, "refs/heads/", &head_ref)) {
the_repository->remote_state->current_branch = repo->remote_state->current_branch = make_branch(
make_branch(head_ref, strlen(head_ref)); repo->remote_state, head_ref, strlen(head_ref));
} }
} }
git_config(handle_config, NULL); repo_config(repo, handle_config, repo->remote_state);
alias_all_urls(); alias_all_urls(repo->remote_state);
} }
static int valid_remote_nick(const char *name) static int valid_remote_nick(const char *name)
@ -552,7 +538,7 @@ static struct remote *remote_get_1(const char *name,
struct remote *ret; struct remote *ret;
int name_given = 0; int name_given = 0;
read_config(); read_config(the_repository);
if (name) if (name)
name_given = 1; name_given = 1;
@ -560,15 +546,15 @@ static struct remote *remote_get_1(const char *name,
name = get_default(the_repository->remote_state->current_branch, name = get_default(the_repository->remote_state->current_branch,
&name_given); &name_given);
ret = make_remote(name, 0); ret = make_remote(the_repository->remote_state, name, 0);
if (valid_remote_nick(name) && have_git_dir()) { if (valid_remote_nick(name) && have_git_dir()) {
if (!valid_remote(ret)) if (!valid_remote(ret))
read_remotes_file(ret); read_remotes_file(the_repository->remote_state, ret);
if (!valid_remote(ret)) if (!valid_remote(ret))
read_branches_file(ret); read_branches_file(the_repository->remote_state, ret);
} }
if (name_given && !valid_remote(ret)) if (name_given && !valid_remote(ret))
add_url_alias(ret, name); add_url_alias(the_repository->remote_state, ret, name);
if (!valid_remote(ret)) if (!valid_remote(ret))
return NULL; return NULL;
return ret; return ret;
@ -596,7 +582,7 @@ int remote_is_configured(struct remote *remote, int in_repo)
int for_each_remote(each_remote_fn fn, void *priv) int for_each_remote(each_remote_fn fn, void *priv)
{ {
int i, result = 0; int i, result = 0;
read_config(); read_config(the_repository);
for (i = 0; i < the_repository->remote_state->remotes_nr && !result; for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
i++) { i++) {
struct remote *remote = struct remote *remote =
@ -1709,11 +1695,12 @@ struct branch *branch_get(const char *name)
{ {
struct branch *ret; struct branch *ret;
read_config(); read_config(the_repository);
if (!name || !*name || !strcmp(name, "HEAD")) if (!name || !*name || !strcmp(name, "HEAD"))
ret = the_repository->remote_state->current_branch; ret = the_repository->remote_state->current_branch;
else else
ret = make_branch(name, strlen(name)); ret = make_branch(the_repository->remote_state, name,
strlen(name));
set_merge(ret); set_merge(ret);
return ret; return ret;
} }

View File

@ -52,6 +52,8 @@ struct remote_state {
struct rewrites rewrites; struct rewrites rewrites;
struct rewrites rewrites_push; struct rewrites rewrites_push;
int initialized;
}; };
void remote_state_clear(struct remote_state *remote_state); void remote_state_clear(struct remote_state *remote_state);