]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: plumb repo into ref stores
authorJonathan Tan <jonathantanmy@google.com>
Fri, 8 Oct 2021 21:08:14 +0000 (14:08 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 8 Oct 2021 22:06:05 +0000 (15:06 -0700)
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>
refs.c
refs/files-backend.c
refs/packed-backend.c
refs/packed-backend.h
refs/refs-internal.h

diff --git a/refs.c b/refs.c
index 2be0d0f0576368be8b2331ce72ab4bd756eba690..9c4e388153b64dc42d8e8ea64659a277dbbe749c 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1873,7 +1873,8 @@ static struct ref_store *lookup_ref_store_map(struct hashmap *map,
  * Create, record, and return a ref_store instance for the specified
  * gitdir.
  */
-static struct ref_store *ref_store_init(const char *gitdir,
+static struct ref_store *ref_store_init(struct repository *repo,
+                                       const char *gitdir,
                                        unsigned int flags)
 {
        const char *be_name = "files";
@@ -1883,7 +1884,7 @@ static struct ref_store *ref_store_init(const char *gitdir,
        if (!be)
                BUG("reference backend %s is unknown", be_name);
 
-       refs = be->init(gitdir, flags);
+       refs = be->init(repo, gitdir, flags);
        return refs;
 }
 
@@ -1895,7 +1896,7 @@ struct ref_store *get_main_ref_store(struct repository *r)
        if (!r->gitdir)
                BUG("attempting to get main_ref_store outside of repository");
 
-       r->refs_private = ref_store_init(r->gitdir, REF_STORE_ALL_CAPS);
+       r->refs_private = ref_store_init(r, r->gitdir, REF_STORE_ALL_CAPS);
        r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
        return r->refs_private;
 }
@@ -1925,6 +1926,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
        struct ref_store *refs;
        char *to_free = NULL;
        size_t len;
+       struct repository *subrepo;
 
        if (!submodule)
                return NULL;
@@ -1950,8 +1952,19 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
        if (submodule_to_gitdir(&submodule_sb, submodule))
                goto done;
 
-       /* assume that add_submodule_odb() has been called */
-       refs = ref_store_init(submodule_sb.buf,
+       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,
+                               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",
                               refs, submodule);
@@ -1977,10 +1990,12 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
                return refs;
 
        if (wt->id)
-               refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
+               refs = ref_store_init(the_repository,
+                                     git_common_path("worktrees/%s", wt->id),
                                      REF_STORE_ALL_CAPS);
        else
-               refs = ref_store_init(get_git_common_dir(),
+               refs = ref_store_init(the_repository,
+                                     get_git_common_dir(),
                                      REF_STORE_ALL_CAPS);
 
        if (refs)
index 1148c0cf09a7e86630e387badd399d15ffd67c23..6a481e968ff16700f355653fb8cea4684fa4c048 100644 (file)
@@ -79,13 +79,15 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
  * Create a new submodule ref cache and add it to the internal
  * set of caches.
  */
-static struct ref_store *files_ref_store_create(const char *gitdir,
+static struct ref_store *files_ref_store_create(struct repository *repo,
+                                               const char *gitdir,
                                                unsigned int flags)
 {
        struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
        struct ref_store *ref_store = (struct ref_store *)refs;
        struct strbuf sb = STRBUF_INIT;
 
+       ref_store->repo = repo;
        ref_store->gitdir = xstrdup(gitdir);
        base_ref_store_init(ref_store, &refs_be_files);
        refs->store_flags = flags;
@@ -93,7 +95,7 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
        get_common_dir_noenv(&sb, gitdir);
        refs->gitcommondir = strbuf_detach(&sb, NULL);
        strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
-       refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
+       refs->packed_ref_store = packed_ref_store_create(repo, sb.buf, flags);
        strbuf_release(&sb);
 
        chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
index f8aa97d7998ecb2db1e677337b3d04eb052e86a8..ea3493b24ed34a712b763991c74d63a927169317 100644 (file)
@@ -193,13 +193,15 @@ static int release_snapshot(struct snapshot *snapshot)
        }
 }
 
-struct ref_store *packed_ref_store_create(const char *path,
+struct ref_store *packed_ref_store_create(struct repository *repo,
+                                         const char *path,
                                          unsigned int store_flags)
 {
        struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
        struct ref_store *ref_store = (struct ref_store *)refs;
 
        base_ref_store_init(ref_store, &refs_be_packed);
+       ref_store->repo = repo;
        ref_store->gitdir = xstrdup(path);
        refs->store_flags = store_flags;
 
index a01a0aff9c77280d7b3e605230af822fdab24ca9..f61a73ec25b4cb55d96bac23ada11a53e2781f95 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef REFS_PACKED_BACKEND_H
 #define REFS_PACKED_BACKEND_H
 
+struct repository;
 struct ref_transaction;
 
 /*
@@ -12,7 +13,8 @@ struct ref_transaction;
  * even among packed refs.
  */
 
-struct ref_store *packed_ref_store_create(const char *path,
+struct ref_store *packed_ref_store_create(struct repository *repo,
+                                         const char *path,
                                          unsigned int store_flags);
 
 /*
index 96911fb26e2f4af868e26cefe990434dbe921b50..d28440c9cc78803c64e8286ffc19da68c3bd1335 100644 (file)
@@ -539,7 +539,8 @@ struct ref_store;
  * should call base_ref_store_init() to initialize the shared part of
  * the ref_store and to record the ref_store for later lookup.
  */
-typedef struct ref_store *ref_store_init_fn(const char *gitdir,
+typedef struct ref_store *ref_store_init_fn(struct repository *repo,
+                                           const char *gitdir,
                                            unsigned int flags);
 
 typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
@@ -697,7 +698,12 @@ struct ref_store {
        /* The backend describing this ref_store's storage scheme: */
        const struct ref_storage_be *be;
 
-       /* The gitdir that this ref_store applies to: */
+       struct repository *repo;
+
+       /*
+        * The gitdir that this ref_store applies to. Note that this is not
+        * necessarily repo->gitdir if the repo has multiple worktrees.
+        */
        char *gitdir;
 };