refs: pass repo when retrieving submodule ref store
Looking up submodule ref stores has two deficiencies:
- The initialized subrepo will be attributed to `the_repository`.
- The submodule ref store will be tracked in a global map.
This makes it impossible to have submodule ref stores for a repository
other than `the_repository`.
Modify the function to accept the parent repository as parameter and
move the global map into `struct repository`. Like this it becomes
possible to look up submodule ref stores for arbitrary repositories.
Note that this also adds a new reference to `the_repository` in
`resolve_gitlink_ref()`, which is part of the refs interfaces. This will
get adjusted in the next patch.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committedMay 17, 2024 at 10:18 UTC965f8991e59d84ba1b86e528f9c27852e746fa90
index 01e8453930..1d660d5ace 100644--- a/refs.c+++ b/refs.c@@ -1949,8 +1949,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, struct ref_store *refs; int flags;- refs = get_submodule_ref_store(submodule);-+ refs = repo_get_submodule_ref_store(the_repository, submodule); if (!refs) return -1;@@ -1960,9 +1959,6 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, return 0; }-/* A strmap of ref_stores, stored by submodule name: */-static struct strmap submodule_ref_stores;- /* A strmap of ref_stores, stored by worktree id: */ static struct strmap worktree_ref_stores;@@ -2036,7 +2032,8 @@ static void register_ref_store_map(struct strmap *map, BUG("%s ref_store '%s' initialized twice", type, name); }-struct ref_store *get_submodule_ref_store(const char *submodule)+struct ref_store *repo_get_submodule_ref_store(struct repository *repo,+ const char *submodule) { struct strbuf submodule_sb = STRBUF_INIT; struct ref_store *refs;@@ -2057,7 +2054,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule) /* We need to strip off one or more trailing slashes */ submodule = to_free = xmemdupz(submodule, len);- refs = lookup_ref_store_map(&submodule_ref_stores, submodule);+ refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule); if (refs) goto done;@@ -2069,20 +2066,15 @@ struct ref_store *get_submodule_ref_store(const char *submodule) goto done; subrepo = xmalloc(sizeof(*subrepo));- /*- * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary- * superprojects other than the_repository. This probably should be- * done by making it take a struct repository * parameter instead of a- * submodule path.- */- if (repo_submodule_init(subrepo, the_repository, submodule,++ if (repo_submodule_init(subrepo, repo, submodule, null_oid())) { free(subrepo); goto done; } refs = ref_store_init(subrepo, submodule_sb.buf, REF_STORE_READ | REF_STORE_ODB);- register_ref_store_map(&submodule_ref_stores, "submodule",+ register_ref_store_map(&repo->submodule_ref_stores, "submodule", refs, submodule); done:
refs.h
+2-1
index b11c250e8a..346a81e9e3 100644--- a/refs.h+++ b/refs.h@@ -954,7 +954,8 @@ struct ref_store *get_main_ref_store(struct repository *r); * For backwards compatibility, submodule=="" is treated the same as * submodule==NULL. */-struct ref_store *get_submodule_ref_store(const char *submodule);+struct ref_store *repo_get_submodule_ref_store(struct repository *repo,+ const char *submodule); struct ref_store *get_worktree_ref_store(const struct worktree *wt); /*
refs/refs-internal.h
+1-1
index 8624477e19..178a355eb0 100644--- a/refs/refs-internal.h+++ b/refs/refs-internal.h@@ -705,7 +705,7 @@ extern struct ref_storage_be refs_be_packed; /* * A representation of the reference store for the main repository or * a submodule. The ref_store instances for submodules are kept in a- * hash map; see get_submodule_ref_store() for more info.+ * hash map; see repo_get_submodule_ref_store() for more info. */ struct ref_store { /* The backend describing this ref_store's storage scheme: */