]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repository: stop initializing the object database in `repo_set_gitdir()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 26 May 2026 05:56:59 +0000 (07:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 May 2026 11:07:02 +0000 (20:07 +0900)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
repository.c
repository.h
setup.c

index 58a13f7c4f5d85ab65630f33dfd84c078eb5fdf3..2c2395105fcf2d5c99009962e1d3162c057d3241 100644 (file)
@@ -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);
 
index c3ec0f4b790b0057bdd40949b14d0d101073ceaf..36e2db26332c0ee7c3c3dbd52eb53749c6b9c439 100644 (file)
@@ -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 c5015923f159a9fdf497b28b657f9dad15e66a1f..3bd3f6c5924ef336ffe0f04ce45f364a89628cbc 100644 (file)
--- 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);