git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 10/17] odb: get rid of `the_repository` when handling the primary source

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 2, 2025, 10:27 UTC
Message-ID
<20250602-pks-object-store-wo-the-repository-v4-10-e986804a7c62@pks.im>
In-Reply-To
<20250602-pks-object-store-wo-the-repository-v4-0-e986804a7c62@pks.im>

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>
---
 odb.c        | 27 +++++++++++++++------------
 odb.h        | 25 ++++++++++++++-----------
 tmp-objdir.c | 10 ++++++----
 3 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/odb.c b/odb.c
index d83f7416e9e..b154e91953d 100644
--- 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 82ddbb71e46..5acfda88fa7 100644
--- 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,20 @@ 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 a previous backend 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
diff --git a/tmp-objdir.c b/tmp-objdir.c
index 4120badf5ce..ae01eae9c41 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -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;
 }
-- 
2.50.0.rc0.629.g846fc57c9e.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 16 of 23 in “object-store: carve out the object database subsystem”
  1. 00/17 object-store: carve out the object database subsystemPatrick Steinhardt, Jun 2, 2025
  2. 01/17 object-store: rename `raw_object_store` to `object_database`Patrick Steinhardt, Jun 2, 2025
  3. Toon ClaesJun 4, 2025
  4. Patrick SteinhardtJun 4, 2025
  5. 03/17 object-store: rename files to "odb.{c,h}"Patrick Steinhardt, Jun 2, 2025
  6. 02/17 object-store: rename `object_directory` to `odb_source`Patrick Steinhardt, Jun 2, 2025
  7. Toon ClaesJun 4, 2025
  8. Patrick SteinhardtJun 4, 2025
  9. 04/17 odb: introduce parent pointersPatrick Steinhardt, Jun 2, 2025
  10. 05/17 odb: get rid of `the_repository` in `find_odb()`Patrick Steinhardt, Jun 2, 2025
  11. 07/17 odb: get rid of `the_repository` in `odb_mkstemp()`Patrick Steinhardt, Jun 2, 2025
  12. 06/17 odb: get rid of `the_repository` in `assert_oid_type()`Patrick Steinhardt, Jun 2, 2025
  13. 09/17 odb: get rid of `the_repository` in `for_each()` functionsPatrick Steinhardt, Jun 2, 2025
  14. 08/17 odb: get rid of `the_repository` when handling alternatesPatrick Steinhardt, Jun 2, 2025
  15. 11/17 odb: get rid of `the_repository` when handling submodule sourcesPatrick Steinhardt, Jun 2, 2025
  16. 10/17 odb: get rid of `the_repository` when handling the primary sourcePatrick Steinhardt, Jun 2, 2025
  17. 12/17 odb: trivial refactorings to get rid of `the_repository`Patrick Steinhardt, Jun 2, 2025
  18. 13/17 odb: rename `oid_object_info()`Patrick Steinhardt, Jun 2, 2025
  19. 15/17 odb: rename `has_object()`Patrick Steinhardt, Jun 2, 2025
  20. 14/17 odb: rename `repo_read_object_file()`Patrick Steinhardt, Jun 2, 2025
  21. 17/17 odb: rename `read_object_with_reference()`Patrick Steinhardt, Jun 2, 2025
  22. 16/17 odb: rename `pretend_object_file()`Patrick Steinhardt, Jun 2, 2025
  23. Junio C HamanoJun 2, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.