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

[PATCH 10/13] odb: handle initialization of sources in `odb_new()`

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 19, 2025, 07:50 UTC
Message-ID
<20251119-b4-pks-odb-creation-v1-10-2b2ed2612cb6@pks.im>
In-Reply-To
<20251119-b4-pks-odb-creation-v1-0-2b2ed2612cb6@pks.im>

The logic to set up a new object database is currently distributed across two functions in "repository.c":

  - In `initialize_repository()` we initialize an empty object database.
    This object database is not fully initialized and doesn't have any
    sources attached to it.
  - The primary object database source is then created in
    `repo_set_gitdir()`.

Ideally though, the logic should be entirely self-contained so that we can iterate more readily on how exactly the sources themselves get set up.

Refactor `odb_new()` to handle both allocation and setup of the object database. This ensures that the object database is always initialized and ready for use, and it allows us to change how the sources get set up eventually.

Note that `repo_set_gitdir()` still reaches into the sources when the function gets called with an already-initialized object database. This will be fixed in the next commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c        | 14 +++++++++++++-
 odb.h        | 15 ++++++++++++++-
 repository.c | 20 ++++++++------------
 3 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/odb.c b/odb.c
index ccc6e999e7..88b40c81c0 100644
--- a/odb.c
+++ b/odb.c
@@ -1034,15 +1034,27 @@ int odb_write_object_stream(struct object_database *odb,
 	return odb_source_loose_write_stream(odb->sources, stream, len, oid);
 }
 
-struct object_database *odb_new(struct repository *repo)
+struct object_database *odb_new(struct repository *repo,
+				const char *primary_source,
+				const char *secondary_sources)
 {
 	struct object_database *o = xmalloc(sizeof(*o));
+	char *to_free = NULL;
 
 	memset(o, 0, sizeof(*o));
 	o->repo = repo;
 	o->packfiles = packfile_store_new(o);
 	pthread_mutex_init(&o->replace_mutex, NULL);
 	string_list_init_dup(&o->submodule_source_paths);
+
+	if (!primary_source)
+		primary_source = to_free = xstrfmt("%s/objects", repo->commondir);
+	o->sources = odb_source_new(o, primary_source, true);
+	o->sources_tail = &o->sources->next;
+	o->alternate_db = xstrdup_or_null(secondary_sources);
+
+	free(to_free);
+
 	return o;
 }
 
diff --git a/odb.h b/odb.h
index 99c4d48972..41b3c03027 100644
--- a/odb.h
+++ b/odb.h
@@ -159,7 +159,20 @@ struct object_database {
 	struct string_list submodule_source_paths;
 };
 
-struct object_database *odb_new(struct repository *repo);
+/*
+ * Create a new object database for the given repository.
+ *
+ * If the primary source parameter is set it will override the usual primary
+ * object directory derived from the repository's common directory. The
+ * alternate sources are expected to be a PATH_SEP-separated list of secondary
+ * sources. Note that these alternate sources will be added in addition to, not
+ * instead of, the alternates identified by the primary source.
+ *
+ * Returns the newly created object database.
+ */
+struct object_database *odb_new(struct repository *repo,
+				const char *primary_source,
+				const char *alternate_sources);
 
 /* Free the object database and release all resources. */
 void odb_free(struct object_database *o);
diff --git a/repository.c b/repository.c
index 455c2d279f..5975c8f341 100644
--- a/repository.c
+++ b/repository.c
@@ -52,7 +52,6 @@ static void set_default_hash_algo(struct repository *repo)
 
 void initialize_repository(struct repository *repo)
 {
-	repo->objects = odb_new(repo);
 	repo->remote_state = remote_state_new();
 	repo->parsed_objects = parsed_object_pool_new(repo);
 	ALLOC_ARRAY(repo->index, 1);
@@ -160,29 +159,26 @@ void repo_set_gitdir(struct repository *repo,
 	 * until after xstrdup(root). Then we can free it.
 	 */
 	char *old_gitdir = repo->gitdir;
-	char *objects_path = NULL;
 
 	repo->gitdir = xstrdup(gitfile ? gitfile : root);
 	free(old_gitdir);
 
 	repo_set_commondir(repo, o->commondir);
-	expand_base_dir(&objects_path, o->object_dir,
-			repo->commondir, "objects");
-
-	if (!repo->objects->sources) {
-		repo->objects->sources = odb_source_new(repo->objects,
-							objects_path, true);
-		repo->objects->sources_tail = &repo->objects->sources->next;
-		free(objects_path);
+
+	if (!repo->objects) {
+		repo->objects = odb_new(repo, o->object_dir, o->alternate_db);
 	} else {
+		char *objects_path = NULL;
+		expand_base_dir(&objects_path, o->object_dir,
+				repo->commondir, "objects");
 		free(repo->objects->sources->path);
 		repo->objects->sources->path = objects_path;
+		free(repo->objects->alternate_db);
+		repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
 	}
 
 	repo->disable_ref_updates = o->disable_ref_updates;
 
-	free(repo->objects->alternate_db);
-	repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
 	expand_base_dir(&repo->graft_file, o->graft_file,
 			repo->commondir, "info/grafts");
 	expand_base_dir(&repo->index_file, o->index_file,
-- 
2.52.0.rc2.482.gaa765fefd0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 16 of 21 in “Centralize management of object database sources”
  1. 00/13 Centralize management of object database sourcesPatrick Steinhardt, Nov 19, 2025
  2. 01/13 path: move `enter_repo()` into "setup.c"Patrick Steinhardt, Nov 19, 2025
  3. 02/13 setup: convert `set_git_dir()` to have file scopePatrick Steinhardt, Nov 19, 2025
  4. 03/13 odb: adopt logic to close object databasesPatrick Steinhardt, Nov 19, 2025
  5. 04/13 odb: refactor `odb_clear()` to `odb_free()`Patrick Steinhardt, Nov 19, 2025
  6. 05/13 odb: move logic to disable ref updates into repoPatrick Steinhardt, Nov 19, 2025
  7. Junio C HamanoNov 19, 2025
  8. Patrick SteinhardtNov 21, 2025
  9. 06/13 oidset: introduce `oidset_equal()`Patrick Steinhardt, Nov 19, 2025
  10. Junio C HamanoNov 19, 2025
  11. 07/13 builtin/index-pack: fix deferred fsck outside reposPatrick Steinhardt, Nov 19, 2025
  12. Junio C HamanoNov 19, 2025
  13. Patrick SteinhardtNov 21, 2025
  14. 08/13 t/helper: stop setting up `the_repository` repeatedlyPatrick Steinhardt, Nov 19, 2025
  15. 09/13 http-push: stop setting up `the_repository` for each referencePatrick Steinhardt, Nov 19, 2025
  16. 10/13 odb: handle initialization of sources in `odb_new()`Patrick Steinhardt, Nov 19, 2025
  17. 11/13 chdir-notify: add function to unregister listenersPatrick Steinhardt, Nov 19, 2025
  18. 12/13 odb: handle changing a repository's commondirPatrick Steinhardt, Nov 19, 2025
  19. Junio C HamanoNov 20, 2025
  20. Patrick SteinhardtNov 21, 2025
  21. 13/13 odb: handle recreation of quarantine directoriesPatrick Steinhardt, Nov 19, 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.