]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: get rid of `the_repository` when handling the primary source
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Jun 2025 06:47:00 +0000 (08:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Jun 2025 15:52:00 +0000 (08:52 -0700)
The functions `set_temporary_primary_odb()` and `restore_primary_odb()`
are responsible for managing a temporary primary source for the
database. Both of these functions implicitly rely on `the_repository`.

Refactor them to instead take an explicit object database parameter as
argument and adjust callers. Rename the functions accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
odb.c
odb.h
tmp-objdir.c

diff --git a/odb.c b/odb.c
index d83f7416e9e839d303ec00a874f693fac4987039..b154e91953d4a0c49a260b52e881aa5c6f38db8f 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -329,7 +329,8 @@ void odb_add_to_alternates_memory(struct object_database *odb,
                             '\n', NULL, 0);
 }
 
-struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
+struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
+                                                   const char *dir, int will_destroy)
 {
        struct odb_source *source;
 
@@ -337,14 +338,14 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
         * Make sure alternates are initialized, or else our entry may be
         * overwritten when they are.
         */
-       odb_prepare_alternates(the_repository->objects);
+       odb_prepare_alternates(odb);
 
        /*
         * Make a new primary odb and link the old primary ODB in as an
         * alternate
         */
        source = xcalloc(1, sizeof(*source));
-       source->odb = the_repository->objects;
+       source->odb = odb;
        source->path = xstrdup(dir);
 
        /*
@@ -353,8 +354,8 @@ struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy)
         */
        source->disable_ref_updates = 1;
        source->will_destroy = will_destroy;
-       source->next = the_repository->objects->sources;
-       the_repository->objects->sources = source;
+       source->next = odb->sources;
+       odb->sources = source;
        return source->next;
 }
 
@@ -366,19 +367,21 @@ static void free_object_directory(struct odb_source *source)
        free(source);
 }
 
-void restore_primary_odb(struct odb_source *restore_alt, const char *old_path)
+void odb_restore_primary_source(struct object_database *odb,
+                               struct odb_source *restore_source,
+                               const char *old_path)
 {
-       struct odb_source *cur_alt = the_repository->objects->sources;
+       struct odb_source *cur_source = odb->sources;
 
-       if (strcmp(old_path, cur_alt->path))
+       if (strcmp(old_path, cur_source->path))
                BUG("expected %s as primary object store; found %s",
-                   old_path, cur_alt->path);
+                   old_path, cur_source->path);
 
-       if (cur_alt->next != restore_alt)
+       if (cur_source->next != restore_source)
                BUG("we expect the old primary object store to be the first alternate");
 
-       the_repository->objects->sources = restore_alt;
-       free_object_directory(cur_alt);
+       odb->sources = restore_source;
+       free_object_directory(cur_source);
 }
 
 char *compute_alternate_path(const char *path, struct strbuf *err)
diff --git a/odb.h b/odb.h
index 7e65e9707c1af7b47a7f22acec724abf01701b2a..4e2d1004f8a5a1e21151dafc07f6f53e9c431768 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -73,17 +73,6 @@ struct odb_source {
        char *path;
 };
 
-/*
- * Replace the current writable object directory with the specified temporary
- * object directory; returns the former primary object directory.
- */
-struct odb_source *set_temporary_primary_odb(const char *dir, int will_destroy);
-
-/*
- * Restore a previous ODB replaced by set_temporary_main_odb.
- */
-void restore_primary_odb(struct odb_source *restore_alternate, const char *old_path);
-
 struct packed_git;
 struct multi_pack_index;
 struct cached_object_entry;
@@ -187,6 +176,21 @@ void odb_clear(struct object_database *o);
  */
 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir);
 
+/*
+ * Replace the current writable object directory with the specified temporary
+ * object directory; returns the former primary source.
+ */
+struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
+                                                   const char *dir, int will_destroy);
+
+/*
+ * Restore the primary source that was previously replaced by
+ * `odb_set_temporary_primary_source()`.
+ */
+void odb_restore_primary_source(struct object_database *odb,
+                               struct odb_source *restore_source,
+                               const char *old_path);
+
 /*
  * Iterate through all alternates of the database and execute the provided
  * callback function for each of them. Stop iterating once the callback
index 4120badf5ceb939d58f43a2479aa9f3253ca6f65..ae01eae9c415d3967ee19ff2d55367cb07b950d0 100644 (file)
@@ -47,7 +47,7 @@ int tmp_objdir_destroy(struct tmp_objdir *t)
                the_tmp_objdir = NULL;
 
        if (t->prev_source)
-               restore_primary_odb(t->prev_source, t->path.buf);
+               odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
 
        err = remove_dir_recursively(&t->path, 0);
 
@@ -279,7 +279,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
        if (t->prev_source) {
                if (t->repo->objects->sources->will_destroy)
                        BUG("migrating an ODB that was marked for destruction");
-               restore_primary_odb(t->prev_source, t->path.buf);
+               odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
                t->prev_source = NULL;
        }
 
@@ -311,7 +311,8 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
 {
        if (t->prev_source)
                BUG("the primary object database is already replaced");
-       t->prev_source = set_temporary_primary_odb(t->path.buf, will_destroy);
+       t->prev_source = odb_set_temporary_primary_source(t->repo->objects,
+                                                         t->path.buf, will_destroy);
        t->will_destroy = will_destroy;
 }
 
@@ -320,7 +321,8 @@ struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
        if (!the_tmp_objdir || !the_tmp_objdir->prev_source)
                return NULL;
 
-       restore_primary_odb(the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
+       odb_restore_primary_source(the_tmp_objdir->repo->objects,
+                                  the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
        the_tmp_objdir->prev_source = NULL;
        return the_tmp_objdir;
 }