refs: plumb repo into ref stores

In preparation for the next 2 patches that adds (partial) support for arbitrary repositories to ref iterators, plumb a repository into all ref stores. There are no changes to program logic. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Oct 8, 2021 at 14:08 UTC 34224e14d6b50cb04430188332362e6a0327e5ed
5 files changed +40 -13
refs.c
+22 -7
@@ -1873,7 +1873,8 @@ static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1873 * Create, record, and return a ref_store instance for the specified
1874 * gitdir.
1875 */
1876 -static struct ref_store *ref_store_init(const char *gitdir,
1876 +static struct ref_store *ref_store_init(struct repository *repo,
1877 + const char *gitdir,
1878 unsigned int flags)
1879 {
1880 const char *be_name = "files";
@@ -1883,7 +1884,7 @@ static struct ref_store *ref_store_init(const char *gitdir,
1884 if (!be)
1885 BUG("reference backend %s is unknown", be_name);
1886
1886 - refs = be->init(gitdir, flags);
1887 + refs = be->init(repo, gitdir, flags);
1888 return refs;
1889 }
1890
@@ -1895,7 +1896,7 @@ struct ref_store *get_main_ref_store(struct repository *r)
1896 if (!r->gitdir)
1897 BUG("attempting to get main_ref_store outside of repository");
1898
1898 - r->refs_private = ref_store_init(r->gitdir, REF_STORE_ALL_CAPS);
1899 + r->refs_private = ref_store_init(r, r->gitdir, REF_STORE_ALL_CAPS);
1900 r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
1901 return r->refs_private;
1902 }
@@ -1925,6 +1926,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
1926 struct ref_store *refs;
1927 char *to_free = NULL;
1928 size_t len;
1929 + struct repository *subrepo;
1930
1931 if (!submodule)
1932 return NULL;
@@ -1950,8 +1952,19 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
1952 if (submodule_to_gitdir(&submodule_sb, submodule))
1953 goto done;
1954
1953 - /* assume that add_submodule_odb() has been called */
1954 - refs = ref_store_init(submodule_sb.buf,
1955 + subrepo = xmalloc(sizeof(*subrepo));
1956 + /*
1957 + * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
1958 + * superprojects other than the_repository. This probably should be
1959 + * done by making it take a struct repository * parameter instead of a
1960 + * submodule path.
1961 + */
1962 + if (repo_submodule_init(subrepo, the_repository, submodule,
1963 + null_oid())) {
1964 + free(subrepo);
1965 + goto done;
1966 + }
1967 + refs = ref_store_init(subrepo, submodule_sb.buf,
1968 REF_STORE_READ | REF_STORE_ODB);
1969 register_ref_store_map(&submodule_ref_stores, "submodule",
1970 refs, submodule);
@@ -1977,10 +1990,12 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
1990 return refs;
1991
1992 if (wt->id)
1980 - refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1993 + refs = ref_store_init(the_repository,
1994 + git_common_path("worktrees/%s", wt->id),
1995 REF_STORE_ALL_CAPS);
1996 else
1983 - refs = ref_store_init(get_git_common_dir(),
1997 + refs = ref_store_init(the_repository,
1998 + get_git_common_dir(),
1999 REF_STORE_ALL_CAPS);
2000
2001 if (refs)
refs/files-backend.c
+4 -2
@@ -79,13 +79,15 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
79 * Create a new submodule ref cache and add it to the internal
80 * set of caches.
81 */
82 -static struct ref_store *files_ref_store_create(const char *gitdir,
82 +static struct ref_store *files_ref_store_create(struct repository *repo,
83 + const char *gitdir,
84 unsigned int flags)
85 {
86 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
87 struct ref_store *ref_store = (struct ref_store *)refs;
88 struct strbuf sb = STRBUF_INIT;
89
90 + ref_store->repo = repo;
91 ref_store->gitdir = xstrdup(gitdir);
92 base_ref_store_init(ref_store, &refs_be_files);
93 refs->store_flags = flags;
@@ -93,7 +95,7 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
95 get_common_dir_noenv(&sb, gitdir);
96 refs->gitcommondir = strbuf_detach(&sb, NULL);
97 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
96 - refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
98 + refs->packed_ref_store = packed_ref_store_create(repo, sb.buf, flags);
99 strbuf_release(&sb);
100
101 chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
refs/packed-backend.c
+3 -1
@@ -193,13 +193,15 @@ static int release_snapshot(struct snapshot *snapshot)
193 }
194 }
195
196 -struct ref_store *packed_ref_store_create(const char *path,
196 +struct ref_store *packed_ref_store_create(struct repository *repo,
197 + const char *path,
198 unsigned int store_flags)
199 {
200 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
201 struct ref_store *ref_store = (struct ref_store *)refs;
202
203 base_ref_store_init(ref_store, &refs_be_packed);
204 + ref_store->repo = repo;
205 ref_store->gitdir = xstrdup(path);
206 refs->store_flags = store_flags;
207
refs/packed-backend.h
+3 -1
@@ -1,6 +1,7 @@
1 #ifndef REFS_PACKED_BACKEND_H
2 #define REFS_PACKED_BACKEND_H
3
4 +struct repository;
5 struct ref_transaction;
6
7 /*
@@ -12,7 +13,8 @@ struct ref_transaction;
13 * even among packed refs.
14 */
15
15 -struct ref_store *packed_ref_store_create(const char *path,
16 +struct ref_store *packed_ref_store_create(struct repository *repo,
17 + const char *path,
18 unsigned int store_flags);
19
20 /*
refs/refs-internal.h
+8 -2
@@ -539,7 +539,8 @@ struct ref_store;
539 * should call base_ref_store_init() to initialize the shared part of
540 * the ref_store and to record the ref_store for later lookup.
541 */
542 -typedef struct ref_store *ref_store_init_fn(const char *gitdir,
542 +typedef struct ref_store *ref_store_init_fn(struct repository *repo,
543 + const char *gitdir,
544 unsigned int flags);
545
546 typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
@@ -697,7 +698,12 @@ struct ref_store {
698 /* The backend describing this ref_store's storage scheme: */
699 const struct ref_storage_be *be;
700
700 - /* The gitdir that this ref_store applies to: */
701 + struct repository *repo;
702 +
703 + /*
704 + * The gitdir that this ref_store applies to. Note that this is not
705 + * necessarily repo->gitdir if the repo has multiple worktrees.
706 + */
707 char *gitdir;
708 };
709