Skip to content

Commit 965f899

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f1782d1 commit 965f899

File tree

8 files changed

+33
-21
lines changed

8 files changed

+33
-21
lines changed

builtin/submodule--helper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
679679
displaypath);
680680
} else if (!(flags & OPT_CACHED)) {
681681
struct object_id oid;
682-
struct ref_store *refs = get_submodule_ref_store(path);
682+
struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
683+
path);
683684

684685
if (!refs) {
685686
print_status(flags, '-', path, ce_oid, displaypath);
@@ -903,7 +904,8 @@ static void generate_submodule_summary(struct summary_cb *info,
903904

904905
if (!info->cached && oideq(&p->oid_dst, null_oid())) {
905906
if (S_ISGITLINK(p->mod_dst)) {
906-
struct ref_store *refs = get_submodule_ref_store(p->sm_path);
907+
struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
908+
p->sm_path);
907909

908910
if (refs)
909911
refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);

refs.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,8 +1949,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
19491949
struct ref_store *refs;
19501950
int flags;
19511951

1952-
refs = get_submodule_ref_store(submodule);
1953-
1952+
refs = repo_get_submodule_ref_store(the_repository, submodule);
19541953
if (!refs)
19551954
return -1;
19561955

@@ -1960,9 +1959,6 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
19601959
return 0;
19611960
}
19621961

1963-
/* A strmap of ref_stores, stored by submodule name: */
1964-
static struct strmap submodule_ref_stores;
1965-
19661962
/* A strmap of ref_stores, stored by worktree id: */
19671963
static struct strmap worktree_ref_stores;
19681964

@@ -2036,7 +2032,8 @@ static void register_ref_store_map(struct strmap *map,
20362032
BUG("%s ref_store '%s' initialized twice", type, name);
20372033
}
20382034

2039-
struct ref_store *get_submodule_ref_store(const char *submodule)
2035+
struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
2036+
const char *submodule)
20402037
{
20412038
struct strbuf submodule_sb = STRBUF_INIT;
20422039
struct ref_store *refs;
@@ -2057,7 +2054,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
20572054
/* We need to strip off one or more trailing slashes */
20582055
submodule = to_free = xmemdupz(submodule, len);
20592056

2060-
refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
2057+
refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule);
20612058
if (refs)
20622059
goto done;
20632060

@@ -2069,20 +2066,15 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
20692066
goto done;
20702067

20712068
subrepo = xmalloc(sizeof(*subrepo));
2072-
/*
2073-
* NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
2074-
* superprojects other than the_repository. This probably should be
2075-
* done by making it take a struct repository * parameter instead of a
2076-
* submodule path.
2077-
*/
2078-
if (repo_submodule_init(subrepo, the_repository, submodule,
2069+
2070+
if (repo_submodule_init(subrepo, repo, submodule,
20792071
null_oid())) {
20802072
free(subrepo);
20812073
goto done;
20822074
}
20832075
refs = ref_store_init(subrepo, submodule_sb.buf,
20842076
REF_STORE_READ | REF_STORE_ODB);
2085-
register_ref_store_map(&submodule_ref_stores, "submodule",
2077+
register_ref_store_map(&repo->submodule_ref_stores, "submodule",
20862078
refs, submodule);
20872079

20882080
done:

refs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ struct ref_store *get_main_ref_store(struct repository *r);
954954
* For backwards compatibility, submodule=="" is treated the same as
955955
* submodule==NULL.
956956
*/
957-
struct ref_store *get_submodule_ref_store(const char *submodule);
957+
struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
958+
const char *submodule);
958959
struct ref_store *get_worktree_ref_store(const struct worktree *wt);
959960

960961
/*

refs/refs-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ extern struct ref_storage_be refs_be_packed;
705705
/*
706706
* A representation of the reference store for the main repository or
707707
* a submodule. The ref_store instances for submodules are kept in a
708-
* hash map; see get_submodule_ref_store() for more info.
708+
* hash map; see repo_get_submodule_ref_store() for more info.
709709
*/
710710
struct ref_store {
711711
/* The backend describing this ref_store's storage scheme: */

repository.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "sparse-index.h"
1515
#include "trace2.h"
1616
#include "promisor-remote.h"
17+
#include "refs.h"
1718

1819
/* The main repository */
1920
static struct repository the_repo;
@@ -289,6 +290,9 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)
289290

290291
void repo_clear(struct repository *repo)
291292
{
293+
struct hashmap_iter iter;
294+
struct strmap_entry *e;
295+
292296
FREE_AND_NULL(repo->gitdir);
293297
FREE_AND_NULL(repo->commondir);
294298
FREE_AND_NULL(repo->graft_file);
@@ -329,6 +333,10 @@ void repo_clear(struct repository *repo)
329333
FREE_AND_NULL(repo->remote_state);
330334
}
331335

336+
strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
337+
ref_store_release(e->value);
338+
strmap_clear(&repo->submodule_ref_stores, 1);
339+
332340
repo_clear_path_cache(&repo->cached_paths);
333341
}
334342

repository.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef REPOSITORY_H
22
#define REPOSITORY_H
33

4+
#include "strmap.h"
5+
46
struct config_set;
57
struct fsmonitor_settings;
68
struct git_hash_algo;
@@ -108,6 +110,12 @@ struct repository {
108110
*/
109111
struct ref_store *refs_private;
110112

113+
/*
114+
* A strmap of ref_stores, stored by submodule name, accessible via
115+
* `repo_get_submodule_ref_store()`.
116+
*/
117+
struct strmap submodule_ref_stores;
118+
111119
/*
112120
* Contains path to often used file names.
113121
*/

submodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ int is_staging_gitmodules_ok(struct index_state *istate)
9999
static int for_each_remote_ref_submodule(const char *submodule,
100100
each_ref_fn fn, void *cb_data)
101101
{
102-
return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
102+
return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository,
103+
submodule),
103104
fn, cb_data);
104105
}
105106

t/helper/test-ref-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)
8282
add_to_alternates_memory(sb.buf);
8383
strbuf_release(&sb);
8484

85-
*refs = get_submodule_ref_store(gitdir);
85+
*refs = repo_get_submodule_ref_store(the_repository, gitdir);
8686
} else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
8787
struct worktree **p, **worktrees = get_worktrees();
8888

0 commit comments

Comments
 (0)