]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: stop creating the object database in `setup_git_env()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 26 May 2026 05:57:00 +0000 (07:57 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 May 2026 11:07:02 +0000 (20:07 +0900)
In the preceding commit we have stopped creating the object database in
`repo_set_gitdir()`. But the logic is still somewhat confusing as we
still end up creating it conditionally in `setup_git_dir()`, which is
called multiple times.

Drop the conditional logic and instead create the object database in all
places where we have discovered and configured a repository.

This leads to even more duplication than we already had in the preceding
commit, but an alert reader may notice that we now (almost) always call
`odb_new()` directly before having called `apply_repository_format()`.
The only exception to this is `setup_git_directory_gently()`, where we
also call the function when _not_ applying the repository format. This
will be fixed in the next commit, and once that's done we can then unify
creation of the object database into `apply_repository_format()`.

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

diff --git a/setup.c b/setup.c
index 3bd3f6c5924ef336ffe0f04ce45f364a89628cbc..0dc9fe4565a182d2be712b295f7104fcd08a99fd 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1035,8 +1035,7 @@ cleanup_return:
 }
 
 static void setup_git_env_internal(struct repository *repo,
-                                  const char *git_dir,
-                                  bool skip_initializing_odb)
+                                  const char *git_dir)
 {
        char *git_replace_ref_base;
        const char *shallow_file;
@@ -1053,10 +1052,6 @@ static void setup_git_env_internal(struct repository *repo,
        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);
@@ -1072,10 +1067,10 @@ static void setup_git_env_internal(struct repository *repo,
                fetch_if_missing = 0;
 }
 
-static void set_git_dir_1(struct repository *repo, const char *path, bool skip_initializing_odb)
+static void set_git_dir_1(struct repository *repo, const char *path)
 {
        xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
-       setup_git_env_internal(repo, path, skip_initializing_odb);
+       setup_git_env_internal(repo, path);
 }
 
 static void update_relative_gitdir(const char *name UNUSED,
@@ -1089,7 +1084,7 @@ static void update_relative_gitdir(const char *name UNUSED,
        trace_printf_key(&trace_setup_key,
                         "setup: move $GIT_DIR to '%s'",
                         path);
-       set_git_dir_1(repo, path, true);
+       set_git_dir_1(repo, path);
        free(path);
 }
 
@@ -1102,7 +1097,7 @@ static void set_git_dir(struct repository *repo, const char *path, int make_real
                path = realpath.buf;
        }
 
-       set_git_dir_1(repo, path, false);
+       set_git_dir_1(repo, path);
        if (!is_absolute_path(path))
                chdir_notify_register(NULL, update_relative_gitdir, repo);
 
@@ -1879,8 +1874,15 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
        }
 
        if (is_git_directory(".")) {
+               struct strvec to_free = STRVEC_INIT;
+
                set_git_dir(repo, ".", 0);
+               repo->objects = odb_new(repo,
+                                       getenv_safe(&to_free, DB_ENVIRONMENT),
+                                       getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
                check_and_apply_repository_format(repo, NULL);
+
+               strvec_clear(&to_free);
                return path;
        }
 
@@ -2032,13 +2034,19 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
            startup_info->have_repository ||
            /* GIT_DIR_EXPLICIT */
            getenv(GIT_DIR_ENVIRONMENT)) {
+               struct strvec to_free = STRVEC_INIT;
+
                if (!repo->gitdir) {
                        const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
                        if (!gitdir)
                                gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
-                       setup_git_env_internal(repo, gitdir, false);
+                       setup_git_env_internal(repo, gitdir);
                }
 
+               repo->objects = odb_new(repo,
+                                       getenv_safe(&to_free, DB_ENVIRONMENT),
+                                       getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
+
                if (startup_info->have_repository) {
                        struct strbuf err = STRBUF_INIT;
 
@@ -2048,6 +2056,8 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
                        clear_repository_format(&repo_fmt);
                        strbuf_release(&err);
                }
+
+               strvec_clear(&to_free);
        }
        /*
         * Since precompose_string_if_needed() needs to look at
@@ -2796,6 +2806,7 @@ int init_db(struct repository *repo,
        int exist_ok = flags & INIT_DB_EXIST_OK;
        char *original_git_dir = real_pathdup(git_dir, 1);
        struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
+       struct strvec to_free = STRVEC_INIT;
 
        if (real_git_dir) {
                struct stat st;
@@ -2816,6 +2827,9 @@ int init_db(struct repository *repo,
        }
        startup_info->have_repository = 1;
 
+       repo->objects = odb_new(repo, getenv_safe(&to_free, DB_ENVIRONMENT),
+                               getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
+
        /*
         * Check to see if the repository version is right.
         * Note that a newly created repository does not have
@@ -2879,6 +2893,7 @@ int init_db(struct repository *repo,
        }
 
        clear_repository_format(&repo_fmt);
+       strvec_clear(&to_free);
        free(original_git_dir);
        return 0;
 }