remote: replace static variables with struct remote_state
A message that is not in the archive (sent off-list or not yet received): id:[email protected]
v5 cover remote: replace static variables with struct remote_state
This series aims to make the remotes subsystem work with non-the_repository, which will allow submodule remotes to be accessed in-process, rather than through child processes. This is accomplished by creating a struct remote_state and adding it to struct repository.
Thanks for the kind review, Jonathan. I've incorporated most of the feedback from v4, except squashing patches 2-4. My reasoning is that those changes are mechanical and keeping them separate makes it more obvious whether or not each step has been done correctly. However, I agree that they are just intermediate steps that touch the same lines, so I am happy to squash them if other reviewers prefer that.
Changes since v4:
* Fixed an incorrect description of 'git push' without explicit refspecs
* In patch 5, remove the branches array and keep the hashmap (note that I did not make a similar change to remotes)
* Drop patch 6 because it is untested in this series (will incorporate into a future series)
Changes since v3:
* Add a test case for pushing to a remote in detached HEAD. This test would have caught the segfault that resulted in this reroll.
* Remove the NEEDSWORK saying that init_remotes_hash() should be moved into remote_state_new() and just do it.
* Remove the backpointer to remote_state and add a remote_state parameter instead.
* In patch 4, add more remotes_* functions. These functions were not needed in v3 because of the backpointer.
* In patch 5, add a function that checks if a branch is in a repo. Add a branch hashmap that makes this operation fast.
* In patch 6, add more repo_* functions. These functions were not needed in v3 because of the backpointer.
Changes since v2:
* Add .remote_state to struct branch and struct remote, changing the implementation appropriately.
* In patch 2, properly consider the initialized state of remote_state. In v2, I forgot to convert a static inside read_config() into a private member of struct remote_state. Fix this.
* In a new patch 3, add helper methods that get a remote via remote_state and the remote name.
* Move read_config(repo) calls to the external facing-functions. This keeps "struct repository" away from the remote.c internals.
Changes since v1:
* In v1, we moved static variables into the_repository->remote_state in two steps: static variables > static remote_state > the_repository->remote_state. In v2, make this change in one step:
static variables > the_repository->remote_state.
* Add more instances of repo_* that were missed.
Glen Choo (5):
t5516: add test case for pushing remote refspecs
remote: move static variables into per-repository struct
remote: use remote_state parameter internally
remote: remove the_repository->remote_state from static methods
remote: die if branch is not found in repository
remote.c | 368 +++++++++++++++++++++++++++++------------- remote.h | 35 ++++ repository.c | 8 + repository.h | 4 + t/t5516-fetch-push.sh | 9 ++ 5 files changed, 311 insertions(+), 113 deletions(-)
Range-diff against v4:
1: 9b29ec27c6 ! 1: 7e60457e11 t5516: add test case for pushing remote refspecs
@@ Metadata
## Commit message ##
t5516: add test case for pushing remote refspecs
- In detached HEAD, "git push remote-name" should push the refspecs in
- remote.remote-name.push. Since there is no test case that checks this
- behavior, add one.
+ "git push remote-name" (that is, with no refspec given on the command
+ line) should push the refspecs in remote.remote-name.push. There is no
+ test case that checks this behavior in detached HEAD, so add one.
@@ t/t5516-fetch-push.sh: do
done
-+test_expect_success "push to remote with detached HEAD and config remote.*.push = src:dest" ' ++test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
+ mk_test testrepo heads/main &&
+ git checkout $the_first_commit &&
+ test_config remote.there.url testrepo &&
2: ca9b5ab66a = 2: ecc637ee74 remote: move static variables into per-repository struct
3: 5d6a245cae = 3: a915155979 remote: use remote_state parameter internally
4: 53f2e31f72 = 4: 8ef43570e9 remote: remove the_repository->remote_state from static methods
5: d3281c14eb ! 5: 8bb7bddda4 remote: die if branch is not found in repository
@@ Commit message
To prevent misuse, add a die_on_missing_branch() helper function that
dies if a given branch is not from a given repository. Speed up the
- existence check by using a new branches_hash hashmap to remote_state,
- and use the hashmap to remove the branch array iteration in
- make_branch().
+ existence check by replacing the branches list with a branches_hash
+ hashmap.
Like read_config(), die_on_missing_branch() is only called from
non-static functions; static functions are less prone to misuse because
@@ remote.c: static void add_merge(struct branch *branch, const char *name)
+ if (ret)
+ return ret;
- ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1,
- remote_state->branches_alloc);
-@@ remote.c: static struct branch *make_branch(struct remote_state *remote_state,
+- ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1,
+- remote_state->branches_alloc);
+ CALLOC_ARRAY(ret, 1);
+- remote_state->branches[remote_state->branches_nr++] = ret;
ret->name = xstrndup(name, len);
ret->refname = xstrfmt("refs/heads/%s", ret->name);
@@ remote.c: struct remote_state *remote_state_new(void)
return r;
}
+@@ remote.c: void remote_state_clear(struct remote_state *remote_state)
+ remote_state->remotes_nr = 0;
+
+ hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent);
+-
+- for (i = 0; i < remote_state->branches_nr; i++) {
+- FREE_AND_NULL(remote_state->branches[i]);
+- }
+- FREE_AND_NULL(remote_state->branches);
+- remote_state->branches_alloc = 0;
+- remote_state->branches_nr = 0;
++ hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent);
+ }
## remote.h ##
@@ remote.h: struct remote_state {
- struct branch **branches;
- int branches_alloc;
- int branches_nr;
+ int remotes_nr;
+ struct hashmap remotes_hash;
+
+- struct branch **branches;
+- int branches_alloc;
+- int branches_nr;
+ struct hashmap branches_hash;
struct branch *current_branch;6: 0974994cc6 < -: ---------- remote: add struct repository parameter to external functions
Signature · 1 line
2.33.GIT
PATCH v5 1/5 t5516: add test case for pushing remote refspecs
"git push remote-name" (that is, with no refspec given on the command line) should push the refspecs in remote.remote-name.push. There is no test case that checks this behavior in detached HEAD, so add one.
--- t/t5516-fetch-push.sh | 9 +++++++++ 1 file changed, 9 insertions(+)
t/t5516-fetch-push.sh +9 -0
@@ -541,6 +541,15 @@ do done +test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" ' + mk_test testrepo heads/main && + git checkout $the_first_commit && + test_config remote.there.url testrepo && + test_config remote.there.push refs/heads/main:refs/heads/main && + git push there && + check_push_result testrepo $the_commit heads/main +' + test_expect_success 'push with remote.pushdefault' ' mk_test up_repo heads/main && mk_test down_repo heads/main &&
Signature · 1 line
2.33.GIT
PATCH v5 2/5 remote: move static variables into per-repository struct
In reply to Glen Chooremote.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.
--- remote.c | 191 +++++++++++++++++++++++++++++++++------------------ remote.h | 34 +++++++++ repository.c | 8 +++ repository.h | 4 ++ 4 files changed, 170 insertions(+), 67 deletions(-)
remote.c +124 -67
@@ -21,33 +21,6 @@ struct counted_string {size_t len;const char *s; }; -struct rewrite { -const char *base; -size_t baselen; -struct counted_string *instead_of; -int instead_of_nr; -int instead_of_alloc; -}; -struct rewrites { -struct rewrite **rewrite; -int rewrite_alloc; -int rewrite_nr; -}; - -static struct remote **remotes; -static int remotes_alloc; -static int remotes_nr; -static struct hashmap remotes_hash; - -static struct branch **branches; -static int branches_alloc; -static int branches_nr; - -static struct branch *current_branch; -static const char *pushremote_name; - -static struct rewrites rewrites; -static struct rewrites rewrites_push;static int valid_remote (const struct remote *remote) {
@@ -94,14 +67,16 @@ static void add_pushurl(struct remote *remote, const char *pushurl)static void add_pushurl_alias (struct remote *remote,const char *url) { -const char *pushurl =alias_url (url, &rewrites_push); +const char *pushurl = +alias_url (url, &the_repository->remote_state->rewrites_push);if (pushurl != url)add_pushurl (remote, pushurl); }static void add_url_alias (struct remote *remote,const char *url) { -add_url (remote,alias_url (url, &rewrites)); +add_url (remote, +alias_url (url, &the_repository->remote_state->rewrites));add_pushurl_alias (remote, url); }
@@ -127,12 +102,6 @@ static int remotes_hash_cmp(const void *unused_cmp_data,return strcmp (a->name, b->name); } -static inline void init_remotes_hash (void ) -{ -if (!remotes_hash.cmpfn) -hashmap_init (&remotes_hash, remotes_hash_cmp,NULL ,0 ); -} -static struct remote *make_remote (const char *name,int len) {struct remote *ret;
@@ -142,12 +111,12 @@ static struct remote *make_remote(const char *name, int len)if (!len) len =strlen (name); -init_remotes_hash (); lookup.str = name; lookup.len = len;hashmap_entry_init (&lookup_entry,memhash (name, len)); - e =hashmap_get (&remotes_hash, &lookup_entry, &lookup); + e =hashmap_get (&the_repository->remote_state->remotes_hash, + &lookup_entry, &lookup);if (e) return container_of(e, struct remote, ent);
@@ -158,15 +127,41 @@ static struct remote *make_remote(const char *name, int len)refspec_init (&ret->push,REFSPEC_PUSH );refspec_init (&ret->fetch,REFSPEC_FETCH ); -ALLOC_GROW (remotes, remotes_nr +1 , remotes_alloc); - remotes[remotes_nr++] = ret; +ALLOC_GROW (the_repository->remote_state->remotes, + the_repository->remote_state->remotes_nr +1 , + the_repository->remote_state->remotes_alloc); + the_repository->remote_state + ->remotes[the_repository->remote_state->remotes_nr++] = ret;hashmap_entry_init (&ret->ent, lookup_entry.hash); -if (hashmap_put_entry (&remotes_hash, ret, ent)) +if (hashmap_put_entry (&the_repository->remote_state->remotes_hash, ret, + ent))BUG ("hashmap_put overwrote entry after hashmap_get returned NULL" );return ret; } +static void remote_clear (struct remote *remote) +{ +int i; + +free ((char *)remote->name); +free ((char *)remote->foreign_vcs); + +for (i =0 ; i < remote->url_nr; i++) { +free ((char *)remote->url[i]); + } +FREE_AND_NULL (remote->pushurl); + +for (i =0 ; i < remote->pushurl_nr; i++) { +free ((char *)remote->pushurl[i]); + } +FREE_AND_NULL (remote->pushurl); +free ((char *)remote->receivepack); +free ((char *)remote->uploadpack); +FREE_AND_NULL (remote->http_proxy); +FREE_AND_NULL (remote->http_proxy_authmethod); +} +static void add_merge (struct branch *branch,const char *name) { ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
@@ -179,15 +174,20 @@ static struct branch *make_branch(const char *name, size_t len)struct branch *ret;int i; -for (i =0 ; i < branches_nr; i++) { -if (!strncmp (name, branches[i]->name, len) && - !branches[i]->name[len]) -return branches[i]; +for (i =0 ; i < the_repository->remote_state->branches_nr; i++) { +if (!strncmp (name, + the_repository->remote_state->branches[i]->name, + len) && + !the_repository->remote_state->branches[i]->name[len]) +return the_repository->remote_state->branches[i]; } -ALLOC_GROW (branches, branches_nr +1 , branches_alloc); +ALLOC_GROW (the_repository->remote_state->branches, + the_repository->remote_state->branches_nr +1 , + the_repository->remote_state->branches_alloc);CALLOC_ARRAY (ret,1 ); - branches[branches_nr++] = ret; + the_repository->remote_state + ->branches[the_repository->remote_state->branches_nr++] = ret; ret->name =xstrndup (name, len); ret->refname =xstrfmt ("refs/heads/%s" , ret->name);
@@ -327,12 +327,16 @@ static int handle_config(const char *key, const char *value, void *cb)if (!strcmp (subkey,"insteadof" )) {if (!value)return config_error_nonbool (key); - rewrite =make_rewrite (&rewrites, name, namelen); + rewrite =make_rewrite ( + &the_repository->remote_state->rewrites, name, + namelen);add_instead_of (rewrite,xstrdup (value)); }else if (!strcmp (subkey,"pushinsteadof" )) {if (!value)return config_error_nonbool (key); - rewrite =make_rewrite (&rewrites_push, name, namelen); + rewrite =make_rewrite ( + &the_repository->remote_state->rewrites_push, + name, namelen);add_instead_of (rewrite,xstrdup (value)); } }
@@ -342,7 +346,9 @@ static int handle_config(const char *key, const char *value, void *cb)/* Handle remote.* variables */ if (!name && !strcmp (subkey,"pushdefault" )) -return git_config_string (&pushremote_name, key, value); +return git_config_string ( + &the_repository->remote_state->pushremote_name, key, + value);if (!name)return 0 ;
@@ -425,18 +431,34 @@ static int handle_config(const char *key, const char *value, void *cb)static void alias_all_urls (void ) {int i, j; -for (i =0 ; i < remotes_nr; i++) { +for (i =0 ; i < the_repository->remote_state->remotes_nr; i++) {int add_pushurl_aliases; -if (!remotes[i]) +if (!the_repository->remote_state->remotes[i])continue ; -for (j =0 ; j < remotes[i]->pushurl_nr; j++) { - remotes[i]->pushurl[j] =alias_url (remotes[i]->pushurl[j], &rewrites); +for (j =0 ; + j < the_repository->remote_state->remotes[i]->pushurl_nr; + j++) { + the_repository->remote_state->remotes[i]->pushurl[j] = +alias_url ( + the_repository->remote_state->remotes[i] + ->pushurl[j], + &the_repository->remote_state->rewrites); } - add_pushurl_aliases = remotes[i]->pushurl_nr ==0 ; -for (j =0 ; j < remotes[i]->url_nr; j++) { + add_pushurl_aliases = + the_repository->remote_state->remotes[i]->pushurl_nr == +0 ; +for (j =0 ; + j < the_repository->remote_state->remotes[i]->url_nr; + j++) {if (add_pushurl_aliases) -add_pushurl_alias (remotes[i], remotes[i]->url[j]); - remotes[i]->url[j] =alias_url (remotes[i]->url[j], &rewrites); +add_pushurl_alias ( + the_repository->remote_state->remotes[i], + the_repository->remote_state->remotes[i] + ->url[j]); + the_repository->remote_state->remotes[i] + ->url[j] =alias_url ( + the_repository->remote_state->remotes[i]->url[j], + &the_repository->remote_state->rewrites); } } }
@@ -450,12 +472,13 @@ static void read_config(void)return ; loaded =1 ; - current_branch =NULL ; + the_repository->remote_state->current_branch =NULL ;if (startup_info->have_repository) {const char *head_ref =resolve_ref_unsafe ("HEAD" ,0 ,NULL , &flag);if (head_ref && (flag &REF_ISSYMREF ) &&skip_prefix (head_ref,"refs/heads/" , &head_ref)) { - current_branch =make_branch (head_ref,strlen (head_ref)); + the_repository->remote_state->current_branch = +make_branch (head_ref,strlen (head_ref)); } }git_config (handle_config,NULL );
@@ -493,10 +516,10 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit) *explicit =1 ;return branch->pushremote_name; } -if (pushremote_name) { +if (the_repository->remote_state->pushremote_name) {if (explicit) *explicit =1 ; -return pushremote_name; +return the_repository->remote_state->pushremote_name; }return remote_for_branch (branch, explicit); }
@@ -534,7 +557,8 @@ static struct remote *remote_get_1(const char *name,if (name) name_given =1 ;else - name =get_default (current_branch, &name_given); + name =get_default (the_repository->remote_state->current_branch, + &name_given); ret =make_remote (name,0 );if (valid_remote_nick (name) &&have_git_dir ()) {
@@ -573,11 +597,13 @@ int for_each_remote(each_remote_fn fn, void *priv) {int i, result =0 ;read_config (); -for (i =0 ; i < remotes_nr && !result; i++) { -struct remote *r = remotes[i]; -if (!r) +for (i =0 ; i < the_repository->remote_state->remotes_nr && !result; + i++) { +struct remote *remote = + the_repository->remote_state->remotes[i]; +if (!remote)continue ; - result =fn (r, priv); + result =fn (remote, priv); }return result; }
@@ -1685,7 +1711,7 @@ struct branch *branch_get(const char *name)read_config ();if (!name || !*name || !strcmp (name,"HEAD" )) - ret = current_branch; + ret = the_repository->remote_state->current_branch;else ret =make_branch (name,strlen (name));set_merge (ret);
@@ -2585,3 +2611,34 @@ void apply_push_cas(struct push_cas_option *cas,check_if_includes_upstream (ref); } } + +struct remote_state *remote_state_new (void ) +{ +struct remote_state *r =xmalloc (sizeof (*r)); + +memset (r,0 ,sizeof (*r)); + +hashmap_init (&r->remotes_hash, remotes_hash_cmp,NULL ,0 ); +return r; +} + +void remote_state_clear (struct remote_state *remote_state) +{ +int i; + +for (i =0 ; i < remote_state->remotes_nr; i++) { +remote_clear (remote_state->remotes[i]); + } +FREE_AND_NULL (remote_state->remotes); + remote_state->remotes_alloc =0 ; + remote_state->remotes_nr =0 ; + + hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); + +for (i =0 ; i < remote_state->branches_nr; i++) { +FREE_AND_NULL (remote_state->branches[i]); + } +FREE_AND_NULL (remote_state->branches); + remote_state->branches_alloc =0 ; + remote_state->branches_nr =0 ; +}
remote.h +34 -0
@@ -23,6 +23,40 @@ enum {REMOTE_BRANCHES }; +struct rewrite { +const char *base; +size_t baselen; +struct counted_string *instead_of; +int instead_of_nr; +int instead_of_alloc; +}; + +struct rewrites { +struct rewrite **rewrite; +int rewrite_alloc; +int rewrite_nr; +}; + +struct remote_state { +struct remote **remotes; +int remotes_alloc; +int remotes_nr; +struct hashmap remotes_hash; + +struct branch **branches; +int branches_alloc; +int branches_nr; + +struct branch *current_branch; +const char *pushremote_name; + +struct rewrites rewrites; +struct rewrites rewrites_push; +}; + +void remote_state_clear (struct remote_state *remote_state); +struct remote_state *remote_state_new (void ); +struct remote {struct hashmap_entry ent;
repository.c +8 -0
@@ -9,6 +9,7 @@#include "config.h" #include "object.h" #include "lockfile.h" +#include "remote.h" #include "submodule-config.h" #include "sparse-index.h" #include "promisor-remote.h"
@@ -24,6 +25,7 @@ void initialize_the_repository(void) the_repo.index = &the_index; the_repo.objects =raw_object_store_new (); + the_repo.remote_state =remote_state_new (); the_repo.parsed_objects =parsed_object_pool_new ();repo_set_hash_algo (&the_repo,GIT_HASH_SHA1 );
@@ -164,6 +166,7 @@ int repo_init(struct repository *repo, repo->objects =raw_object_store_new (); repo->parsed_objects =parsed_object_pool_new (); + repo->remote_state =remote_state_new ();if (repo_init_gitdir (repo, gitdir)) goto error;
@@ -270,6 +273,11 @@ void repo_clear(struct repository *repo)promisor_remote_clear (repo->promisor_remote_config);FREE_AND_NULL (repo->promisor_remote_config); } + +if (repo->remote_state) { +remote_state_clear (repo->remote_state); +FREE_AND_NULL (repo->remote_state); + } }int repo_read_index (struct repository *repo)
repository.h +4 -0
@@ -11,6 +11,7 @@ struct pathspec;struct raw_object_store ;struct submodule_cache ;struct promisor_remote_config ; +struct remote_state ;enum untracked_cache_setting {UNTRACKED_CACHE_KEEP ,
@@ -127,6 +128,9 @@ struct repository {*/ struct index_state *index; +/* Repository's remotes and associated structures. */ +struct remote_state *remote_state; +/* Repository's current hash algorithm, as serialized on disk. */ const struct git_hash_algo *hash_algo;
Signature · 1 line
2.33.GIT
PATCH v5 3/5 remote: use remote_state parameter internally
In reply to Glen ChooWithout 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().
--- remote.c | 159 +++++++++++++++++++++++++------------------------------ remote.h | 2 + 2 files changed, 75 insertions(+), 86 deletions(-)
remote.c +73 -86
@@ -65,19 +65,19 @@ static void add_pushurl(struct remote *remote, const char *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 = -alias_url (url, &the_repository->remote_state->rewrites_push); +const char *pushurl =alias_url (url, &remote_state->rewrites_push);if (pushurl != url)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, -alias_url (url, &the_repository->remote_state->rewrites)); -add_pushurl_alias (remote, url); +add_url (remote,alias_url (url, &remote_state->rewrites)); +add_pushurl_alias (remote_state, remote, url); }struct remotes_hash_key {
@@ -102,7 +102,8 @@ static int remotes_hash_cmp(const void *unused_cmp_data,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 remotes_hash_key lookup;
@@ -115,8 +116,7 @@ static struct remote *make_remote(const char *name, int len) lookup.len = len;hashmap_entry_init (&lookup_entry,memhash (name, len)); - e =hashmap_get (&the_repository->remote_state->remotes_hash, - &lookup_entry, &lookup); + e =hashmap_get (&remote_state->remotes_hash, &lookup_entry, &lookup);if (e) 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->fetch,REFSPEC_FETCH ); -ALLOC_GROW (the_repository->remote_state->remotes, - the_repository->remote_state->remotes_nr +1 , - the_repository->remote_state->remotes_alloc); - the_repository->remote_state - ->remotes[the_repository->remote_state->remotes_nr++] = ret; +ALLOC_GROW (remote_state->remotes, remote_state->remotes_nr +1 , + remote_state->remotes_alloc); + remote_state->remotes[remote_state->remotes_nr++] = ret;hashmap_entry_init (&ret->ent, lookup_entry.hash); -if (hashmap_put_entry (&the_repository->remote_state->remotes_hash, ret, - ent)) +if (hashmap_put_entry (&remote_state->remotes_hash, ret, ent))BUG ("hashmap_put overwrote entry after hashmap_get returned NULL" );return ret; }
@@ -169,25 +166,22 @@ static void add_merge(struct branch *branch, const char *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;int i; -for (i =0 ; i < the_repository->remote_state->branches_nr; i++) { -if (!strncmp (name, - the_repository->remote_state->branches[i]->name, - len) && - !the_repository->remote_state->branches[i]->name[len]) -return the_repository->remote_state->branches[i]; +for (i =0 ; i < remote_state->branches_nr; i++) { +if (!strncmp (name, remote_state->branches[i]->name, len) && + !remote_state->branches[i]->name[len]) +return remote_state->branches[i]; } -ALLOC_GROW (the_repository->remote_state->branches, - the_repository->remote_state->branches_nr +1 , - the_repository->remote_state->branches_alloc); +ALLOC_GROW (remote_state->branches, remote_state->branches_nr +1 , + remote_state->branches_alloc);CALLOC_ARRAY (ret,1 ); - the_repository->remote_state - ->branches[the_repository->remote_state->branches_nr++] = ret; + remote_state->branches[remote_state->branches_nr++] = ret; ret->name =xstrndup (name, len); ret->refname =xstrfmt ("refs/heads/%s" , ret->name);
@@ -229,7 +223,8 @@ static const char *skip_spaces(const char *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 ;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);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))refspec_append (&remote->push,skip_spaces (v));else if (skip_prefix (buf.buf,"Pull:" , &v))
@@ -254,7 +250,8 @@ static void read_remotes_file(struct remote *remote)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;struct strbuf buf =STRBUF_INIT ;
@@ -286,7 +283,7 @@ static void read_branches_file(struct remote *remote)else 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" , frag, remote->name);
@@ -305,10 +302,12 @@ static int handle_config(const char *key, const char *value, void *cb)const char *subkey;struct remote *remote;struct branch *branch; +struct remote_state *remote_state = cb; +if (parse_config_key (key,"branch" , &name, &namelen, &subkey) >=0 ) {if (!name)return 0 ; - branch =make_branch (name, namelen); + branch =make_branch (remote_state, name, namelen);if (!strcmp (subkey,"remote" )) {return git_config_string (&branch->remote_name, key, value); }else if (!strcmp (subkey,"pushremote" )) {
@@ -327,16 +326,14 @@ static int handle_config(const char *key, const char *value, void *cb)if (!strcmp (subkey,"insteadof" )) {if (!value)return config_error_nonbool (key); - rewrite =make_rewrite ( - &the_repository->remote_state->rewrites, name, - namelen); + rewrite =make_rewrite (&remote_state->rewrites, name, + namelen);add_instead_of (rewrite,xstrdup (value)); }else if (!strcmp (subkey,"pushinsteadof" )) {if (!value)return config_error_nonbool (key); - rewrite =make_rewrite ( - &the_repository->remote_state->rewrites_push, - name, namelen); + rewrite =make_rewrite (&remote_state->rewrites_push, + name, namelen);add_instead_of (rewrite,xstrdup (value)); } }
@@ -346,9 +343,8 @@ static int handle_config(const char *key, const char *value, void *cb)/* Handle remote.* variables */ if (!name && !strcmp (subkey,"pushdefault" )) -return git_config_string ( - &the_repository->remote_state->pushremote_name, key, - value); +return git_config_string (&remote_state->pushremote_name, key, + value);if (!name)return 0 ;
@@ -358,7 +354,7 @@ static int handle_config(const char *key, const char *value, void *cb) name);return 0 ; } - remote =make_remote (name, namelen); + remote =make_remote (remote_state, name, namelen); remote->origin =REMOTE_CONFIG ;if (current_config_scope () ==CONFIG_SCOPE_LOCAL ||current_config_scope () ==CONFIG_SCOPE_WORKTREE )
@@ -428,61 +424,51 @@ static int handle_config(const char *key, const char *value, void *cb)return 0 ; } -static void alias_all_urls (void ) +static void alias_all_urls (struct remote_state *remote_state) {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; -if (!the_repository->remote_state->remotes[i]) +if (!remote_state->remotes[i])continue ; -for (j =0 ; - j < the_repository->remote_state->remotes[i]->pushurl_nr; - j++) { - the_repository->remote_state->remotes[i]->pushurl[j] = -alias_url ( - the_repository->remote_state->remotes[i] - ->pushurl[j], - &the_repository->remote_state->rewrites); +for (j =0 ; j < remote_state->remotes[i]->pushurl_nr; j++) { + remote_state->remotes[i]->pushurl[j] = +alias_url (remote_state->remotes[i]->pushurl[j], + &remote_state->rewrites); } - add_pushurl_aliases = - the_repository->remote_state->remotes[i]->pushurl_nr == -0 ; -for (j =0 ; - j < the_repository->remote_state->remotes[i]->url_nr; - j++) { + add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr ==0 ; +for (j =0 ; j < remote_state->remotes[i]->url_nr; j++) {if (add_pushurl_aliases)add_pushurl_alias ( - the_repository->remote_state->remotes[i], - the_repository->remote_state->remotes[i] - ->url[j]); - the_repository->remote_state->remotes[i] - ->url[j] =alias_url ( - the_repository->remote_state->remotes[i]->url[j], - &the_repository->remote_state->rewrites); + remote_state, remote_state->remotes[i], + remote_state->remotes[i]->url[j]); + remote_state->remotes[i]->url[j] = +alias_url (remote_state->remotes[i]->url[j], + &remote_state->rewrites); } } } -static void read_config (void ) +static void read_config (struct repository *repo) { -static int loaded;int flag; -if (loaded) +if (repo->remote_state->initialized)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) { -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 ) &&skip_prefix (head_ref,"refs/heads/" , &head_ref)) { - the_repository->remote_state->current_branch = -make_branch (head_ref,strlen (head_ref)); + repo->remote_state->current_branch =make_branch ( + repo->remote_state, head_ref,strlen (head_ref)); } } -git_config (handle_config,NULL ); -alias_all_urls (); +repo_config (repo, handle_config, repo->remote_state); +alias_all_urls (repo->remote_state); }static int valid_remote_nick (const char *name)
@@ -552,7 +538,7 @@ static struct remote *remote_get_1(const char *name,struct remote *ret;int name_given =0 ; -read_config (); +read_config (the_repository);if (name) 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_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 (ret)) -read_remotes_file (ret); +read_remotes_file (the_repository->remote_state, ret);if (!valid_remote (ret)) -read_branches_file (ret); +read_branches_file (the_repository->remote_state, 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))return NULL ;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 i, result =0 ; -read_config (); +read_config (the_repository);for (i =0 ; i < the_repository->remote_state->remotes_nr && !result; i++) { struct remote *remote =
@@ -1709,11 +1695,12 @@ struct branch *branch_get(const char *name) {struct branch *ret; -read_config (); +read_config (the_repository);if (!name || !*name || !strcmp (name,"HEAD" )) ret = the_repository->remote_state->current_branch;else - ret =make_branch (name,strlen (name)); + ret =make_branch (the_repository->remote_state, name, +strlen (name));set_merge (ret);return ret; }
remote.h +2 -0
@@ -52,6 +52,8 @@ struct remote_state {struct rewrites rewrites;struct rewrites rewrites_push; + +int initialized; };void remote_state_clear (struct remote_state *remote_state);
Signature · 1 line
2.33.GIT
PATCH v5 4/5 remote: remove the_repository->remote_state from static methods
In reply to Glen ChooReplace all remaining references of the_repository->remote_state in static functions with a struct remote_state parameter.
To do so, move read_config() calls to non-static functions and create a family of static functions, "remotes_*", that behave like "repo_*", but accept struct remote_state instead of struct repository. In the case where a static function calls a non-static function, replace the non-static function with its "remotes_*" equivalent.
--- remote.c | 96 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 25 deletions(-)
remote.c +71 -25
@@ -483,7 +483,9 @@ static int valid_remote_nick(const char *name)return 1 ; } -const char *remote_for_branch (struct branch *branch,int *explicit) +static const char *remotes_remote_for_branch (struct remote_state *remote_state, +struct branch *branch, +int *explicit) {if (branch && branch->remote_name) {if (explicit)
@@ -495,32 +497,55 @@ const char *remote_for_branch(struct branch *branch, int *explicit) return "origin"; } -const char *pushremote_for_branch(struct branch *branch, int *explicit) +const char *remote_for_branch(struct branch *branch, int *explicit) +{ + read_config(the_repository); + return remotes_remote_for_branch(the_repository->remote_state, branch, + explicit); +} + +static const char * +remotes_pushremote_for_branch(struct remote_state *remote_state, + struct branch *branch, int *explicit) { if (branch && branch->pushremote_name) { if (explicit) *explicit = 1; return branch->pushremote_name; } - if (the_repository->remote_state->pushremote_name) { + if (remote_state->pushremote_name) { if (explicit) *explicit = 1; - return the_repository->remote_state->pushremote_name; + return remote_state->pushremote_name; } - return remote_for_branch(branch, explicit); + return remotes_remote_for_branch(remote_state, branch, explicit); } +const char *pushremote_for_branch(struct branch *branch, int *explicit) +{ + read_config(the_repository); + return remotes_pushremote_for_branch(the_repository->remote_state, + branch, explicit); +} + +static struct remote *remotes_remote_get(struct remote_state *remote_state, + const char *name); + const char *remote_ref_for_branch(struct branch *branch, int for_push) { + read_config(the_repository); if (branch) { if (!for_push) { if (branch->merge_nr) { return branch->merge_name[0]; } } else { - const char *dst, *remote_name = - pushremote_for_branch(branch, NULL); - struct remote *remote = remote_get(remote_name); + const char *dst, + *remote_name = remotes_pushremote_for_branch( + the_repository->remote_state, branch, + NULL); + struct remote *remote = remotes_remote_get( + the_repository->remote_state, remote_name); if (remote && remote->push.nr && (dst = apply_refspecs(&remote->push,
@@ -532,42 +557,58 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push)return NULL ; } -static struct remote *remote_get_1 (const char *name, -const char *(*get_default)(struct branch *,int *)) +static struct remote * +remotes_remote_get_1 (struct remote_state *remote_state,const char *name, +const char *(*get_default)(struct remote_state *, +struct branch *,int *)) {struct remote *ret;int name_given =0 ; -read_config (the_repository); -if (name) name_given =1 ;else - name =get_default (the_repository->remote_state->current_branch, + name =get_default (remote_state, remote_state->current_branch, &name_given); - ret =make_remote (the_repository->remote_state, name,0 ); + ret =make_remote (remote_state, name,0 );if (valid_remote_nick (name) &&have_git_dir ()) {if (!valid_remote (ret)) -read_remotes_file (the_repository->remote_state, ret); +read_remotes_file (remote_state, ret);if (!valid_remote (ret)) -read_branches_file (the_repository->remote_state, ret); +read_branches_file (remote_state, ret); }if (name_given && !valid_remote (ret)) -add_url_alias (the_repository->remote_state, ret, name); +add_url_alias (remote_state, ret, name);if (!valid_remote (ret))return NULL ;return ret; } +static inline struct remote * +remotes_remote_get (struct remote_state *remote_state,const char *name) +{ +return remotes_remote_get_1 (remote_state, name, + remotes_remote_for_branch); +} +struct remote *remote_get (const char *name) { -return remote_get_1 (name, remote_for_branch); +read_config (the_repository); +return remotes_remote_get (the_repository->remote_state, name); +} + +static inline struct remote * +remotes_pushremote_get (struct remote_state *remote_state,const char *name) +{ +return remotes_remote_get_1 (remote_state, name, + remotes_pushremote_for_branch); }struct remote *pushremote_get (const char *name) { -return remote_get_1 (name, pushremote_for_branch); +read_config (the_repository); +return remotes_pushremote_get (the_repository->remote_state, name); }int remote_is_configured (struct remote *remote,int in_repo)
@@ -1654,7 +1695,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, } } -static void set_merge (struct branch *ret) +static void set_merge (struct remote_state *remote_state,struct branch *ret) {struct remote *remote;char *ref;
@@ -1674,7 +1715,7 @@ static void set_merge(struct branch *ret)return ; } - remote =remote_get (ret->remote_name); + remote =remotes_remote_get (remote_state, ret->remote_name);CALLOC_ARRAY (ret->merge, ret->merge_nr);for (i =0 ; i < ret->merge_nr; i++) {
@@ -1701,7 +1742,7 @@ struct branch *branch_get(const char *name)else ret =make_branch (the_repository->remote_state, name,strlen (name)); -set_merge (ret); +set_merge (the_repository->remote_state, ret);return ret; }
@@ -1772,11 +1813,14 @@ static const char *tracking_for_push_dest(struct remote *remote,return ret; } -static const char *branch_get_push_1 (struct branch *branch,struct strbuf *err) +static const char *branch_get_push_1 (struct remote_state *remote_state, +struct branch *branch,struct strbuf *err) {struct remote *remote; - remote =remote_get (pushremote_for_branch (branch,NULL )); + remote =remotes_remote_get ( + remote_state, +remotes_pushremote_for_branch (remote_state, branch,NULL ));if (!remote) return error_buf(err, _("branch '%s' has no remote for pushing"),
@@ -1834,11 +1878,13 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)const char *branch_get_push (struct branch *branch,struct strbuf *err) { +read_config (the_repository);if (!branch)return error_buf (err,_ ("HEAD does not point to a branch" ));if (!branch->push_tracking_ref) - branch->push_tracking_ref =branch_get_push_1 (branch, err); + branch->push_tracking_ref =branch_get_push_1 ( + the_repository->remote_state, branch, err);return branch->push_tracking_ref; }
Signature · 1 line
2.33.GIT
PATCH v5 5/5 remote: die if branch is not found in repository
In reply to Glen ChooIn a subsequent commit, we would like external-facing functions to be able to accept "struct repository" and "struct branch" as a pair. This is useful for functions like pushremote_for_branch(), which need to take values from the remote_state and branch, even if branch == NULL. However, a caller may supply an unrelated repository and branch, which is not supported behavior.
To prevent misuse, add a die_on_missing_branch() helper function that dies if a given branch is not from a given repository. Speed up the existence check by replacing the branches list with a branches_hash hashmap.
Like read_config(), die_on_missing_branch() is only called from non-static functions; static functions are less prone to misuse because they have strong conventions for keeping remote_state and branch in sync.
--- remote.c | 84 +++++++++++++++++++++++++++++++++++++++++++++----------- remote.h | 5 ++-- 2 files changed, 70 insertions(+), 19 deletions(-)
remote.c +68 -16
@@ -166,25 +166,74 @@ static void add_merge(struct branch *branch, const char *name) branch->merge_name[branch->merge_nr++] = name; } +struct branches_hash_key { +const char *str; +int len; +}; + +static int branches_hash_cmp (const void *unused_cmp_data, +const struct hashmap_entry *eptr, +const struct hashmap_entry *entry_or_key, +const void *keydata) +{ +const struct branch *a, *b; +const struct branches_hash_key *key = keydata; + + a = container_of(eptr, const struct branch, ent); + b = container_of(entry_or_key, const struct branch, ent); + +if (key) +return strncmp (a->name, key->str, key->len) || + a->name[key->len]; +else +return strcmp (a->name, b->name); +} + +static struct branch *find_branch (struct remote_state *remote_state, +const char *name,size_t len) +{ +struct branches_hash_key lookup; +struct hashmap_entry lookup_entry, *e; + +if (!len) + len =strlen (name); + + lookup.str = name; + lookup.len = len; +hashmap_entry_init (&lookup_entry,memhash (name, len)); + + e =hashmap_get (&remote_state->branches_hash, &lookup_entry, &lookup); +if (e) + return container_of(e, struct branch, ent); + +return NULL ; +} + +static void die_on_missing_branch (struct repository *repo, +struct branch *branch) +{ +/* branch == NULL is always valid because it represents detached HEAD. */ +if (branch && + branch !=find_branch (repo->remote_state, branch->name,0 )) +die ("branch %s was not found in the repository" , branch->name); +} +static struct branch *make_branch (struct remote_state *remote_state,const char *name,size_t len) {struct branch *ret; -int i; -for (i =0 ; i < remote_state->branches_nr; i++) { -if (!strncmp (name, remote_state->branches[i]->name, len) && - !remote_state->branches[i]->name[len]) -return remote_state->branches[i]; - } + ret =find_branch (remote_state, name, len); +if (ret) +return ret; -ALLOC_GROW (remote_state->branches, remote_state->branches_nr +1 , - remote_state->branches_alloc);CALLOC_ARRAY (ret,1 ); - remote_state->branches[remote_state->branches_nr++] = ret; ret->name =xstrndup (name, len); ret->refname =xstrfmt ("refs/heads/%s" , ret->name); +hashmap_entry_init (&ret->ent,memhash (name, len)); +if (hashmap_put_entry (&remote_state->branches_hash, ret, ent)) +BUG ("hashmap_put overwrote entry after hashmap_get returned NULL" );return ret; }
@@ -500,6 +549,8 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state,const char *remote_for_branch (struct branch *branch,int *explicit) {read_config (the_repository); +die_on_missing_branch (the_repository, branch); +return remotes_remote_for_branch (the_repository->remote_state, branch, explicit); }
@@ -524,6 +575,8 @@ remotes_pushremote_for_branch(struct remote_state *remote_state,const char *pushremote_for_branch (struct branch *branch,int *explicit) {read_config (the_repository); +die_on_missing_branch (the_repository, branch); +return remotes_pushremote_for_branch (the_repository->remote_state, branch, explicit); }
@@ -534,6 +587,8 @@ static struct remote *remotes_remote_get(struct remote_state *remote_state,const char *remote_ref_for_branch (struct branch *branch,int for_push) {read_config (the_repository); +die_on_missing_branch (the_repository, branch); +if (branch) {if (!for_push) {if (branch->merge_nr) {
@@ -1879,6 +1934,8 @@ static const char *branch_get_push_1(struct remote_state *remote_state,const char *branch_get_push (struct branch *branch,struct strbuf *err) {read_config (the_repository); +die_on_missing_branch (the_repository, branch); +if (!branch)return error_buf (err,_ ("HEAD does not point to a branch" ));
@@ -2652,6 +2709,7 @@ struct remote_state *remote_state_new(void)memset (r,0 ,sizeof (*r));hashmap_init (&r->remotes_hash, remotes_hash_cmp,NULL ,0 ); +hashmap_init (&r->branches_hash, branches_hash_cmp,NULL ,0 );return r; }
@@ -2667,11 +2725,5 @@ void remote_state_clear(struct remote_state *remote_state) remote_state->remotes_nr =0 ; hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); - -for (i =0 ; i < remote_state->branches_nr; i++) { -FREE_AND_NULL (remote_state->branches[i]); - } -FREE_AND_NULL (remote_state->branches); - remote_state->branches_alloc =0 ; - remote_state->branches_nr =0 ; + hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent); }
remote.h +2 -3
@@ -43,9 +43,7 @@ struct remote_state {int remotes_nr;struct hashmap remotes_hash; -struct branch **branches; -int branches_alloc; -int branches_nr; +struct hashmap branches_hash;struct branch *current_branch;const char *pushremote_name;
@@ -292,6 +290,7 @@ int remote_find_tracking(struct remote *remote, struct refspec_item *refspec);* branch_get(name) for "refs/heads/{name}", or with branch_get(NULL) for HEAD. */ struct branch { +struct hashmap_entry ent;/* The short name of the branch. */ const char *name;
Signature · 1 line
2.33.GIT
Quoted 11 lines · Glen Choo
This series aims to make the remotes subsystem work with non-the_repository, which will allow submodule remotes to be accessed in-process, rather than through child processes. This is accomplished by creating a struct
remote_stateand adding it to struct repository.Thanks for the kind review, Jonathan. I've incorporated most of the feedback from v4, except squashing patches 2-4. My reasoning is that those changes are mechanical and keeping them separate makes it more obvious whether or not each step has been done correctly. However, I agree that they are just intermediate steps that touch the same lines, so I am happy to squash them if other reviewers prefer that.
Thanks. This patch series looks good to me.