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

[PATCH v2 4/8] repository: stop initializing the object database in `repo_set_gitdir()`

From
Patrick Steinhardt <ps@pks.im>
Date
May 26, 2026, 05:56 UTC
Message-ID
<20260526-b4-pks-setup-centralize-odb-creation-v2-4-2fa5b385c13e@pks.im>
In-Reply-To
<20260526-b4-pks-setup-centralize-odb-creation-v2-0-2fa5b385c13e@pks.im>

The function `repo_set_gitdir()` obviously sets the Git directory for a given repository. Less obviously though, the function also configures a couple of auxiliary settings.

One such thing is that we create the object database in this function. This logic only happens conditionally though, as `set_git_dir()` may be called multiple times during repository setup, and we don't want to create the object database multiple times. This is somewhat tangled and hard to follow.

Remove the logic from `repo_set_gitdir()` and instead initialize the object database outside of it. This leads to some duplication right now, but that duplication will be removed in a subsequent step where we will start initializing the object database as part of applying the repo's format.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 repository.c | 8 ++------
 repository.h | 3 ---
 setup.c      | 7 ++++---
 3 files changed, 6 insertions(+), 12 deletions(-)
diff --git a/repository.c b/repository.c
index 58a13f7c4f..2c2395105f 100644
--- a/repository.c
+++ b/repository.c
@@ -181,12 +181,6 @@ void repo_set_gitdir(struct repository *repo,
 	free(old_gitdir);
 
 	repo_set_commondir(repo, o->commondir);
-
-	if (!repo->objects)
-		repo->objects = odb_new(repo, o->object_dir, o->alternate_db);
-	else if (!o->skip_initializing_odb)
-		BUG("cannot reinitialize an already-initialized object directory");
-
 	repo->disable_ref_updates = o->disable_ref_updates;
 
 	expand_base_dir(&repo->graft_file, o->graft_file,
@@ -302,6 +296,8 @@ int repo_init(struct repository *repo,
 		goto error;
 	}
 
+	repo->objects = odb_new(repo, NULL, NULL);
+
 	if (worktree)
 		repo_set_worktree(repo, worktree);
 
diff --git a/repository.h b/repository.h
index c3ec0f4b79..36e2db2633 100644
--- a/repository.h
+++ b/repository.h
@@ -221,12 +221,9 @@ const char *repo_get_work_tree(struct repository *repo);
  */
 struct set_gitdir_args {
 	const char *commondir;
-	const char *object_dir;
 	const char *graft_file;
 	const char *index_file;
-	const char *alternate_db;
 	bool disable_ref_updates;
-	bool skip_initializing_odb;
 };
 
 void repo_set_gitdir(struct repository *repo, const char *root,
diff --git a/setup.c b/setup.c
index c5015923f1..3bd3f6c592 100644
--- a/setup.c
+++ b/setup.c
@@ -1045,17 +1045,18 @@ static void setup_git_env_internal(struct repository *repo,
 	struct strvec to_free = STRVEC_INIT;
 
 	args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
-	args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
 	args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
 	args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
-	args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
 	if (getenv(GIT_QUARANTINE_ENVIRONMENT))
 		args.disable_ref_updates = true;
-	args.skip_initializing_odb = skip_initializing_odb;
 
 	repo_set_gitdir(repo, git_dir, &args);
 	strvec_clear(&to_free);
 
+	if (!skip_initializing_odb)
+		repo->objects = odb_new(repo, getenv_safe(&to_free, DB_ENVIRONMENT),
+					getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
+
 	if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
 		disable_replace_refs();
 	replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
-- 
2.54.0.926.g75ba10bac6.dirty
Previous: Karthik NayakNext: Karthik Nayak
Message 10 of 17 in “setup: centralize object database creation”
  1. 0/8 setup: centralize object database creationPatrick Steinhardt, May 26, 2026
  2. 1/8 t0001: plug test gaps for git-init(1) with GIT_OBJECT_DIRECTORYPatrick Steinhardt, May 26, 2026
  3. Karthik NayakJun 3, 2026
  4. 2/8 setup: drop `setup_git_env()`Patrick Steinhardt, May 26, 2026
  5. 3/8 setup: deduplicate logic to apply repository formatPatrick Steinhardt, May 26, 2026
  6. Karthik NayakJun 3, 2026
  7. Patrick SteinhardtJun 4, 2026
  8. 5/8 setup: stop creating the object database in `setup_git_env()`Patrick Steinhardt, May 26, 2026
  9. Karthik NayakJun 3, 2026
  10. 4/8 repository: stop initializing the object database in `repo_set_gitdir()`Patrick Steinhardt, May 26, 2026
  11. Karthik NayakJun 3, 2026
  12. Patrick SteinhardtJun 4, 2026
  13. 6/8 setup: stop initializing object database without repositoryPatrick Steinhardt, May 26, 2026
  14. 7/8 repository: stop reading loose object map twice on repo initPatrick Steinhardt, May 26, 2026
  15. 8/8 setup: construct object database in `apply_repository_format()`Patrick Steinhardt, May 26, 2026
  16. Karthik NayakJun 3, 2026
  17. Patrick SteinhardtJun 4, 2026

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.