setup: stop creating the object database in `setup_git_env()`

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>

Patrick Steinhardt committed Jun 4, 2026 at 09:46 UTC aae4ebc895272dc7e5a9ccfc135878b55c7322d7
1 file changed +26 -11
setup.c
+26 -11
@@ -1035,8 +1035,7 @@ cleanup_return:
1035 }
1036
1037 static void setup_git_env_internal(struct repository *repo,
1038 - const char *git_dir,
1039 - bool skip_initializing_odb)
1038 + const char *git_dir)
1039 {
1040 char *git_replace_ref_base;
1041 const char *shallow_file;
@@ -1053,10 +1052,6 @@ static void setup_git_env_internal(struct repository *repo,
1052 repo_set_gitdir(repo, git_dir, &args);
1053 strvec_clear(&to_free);
1054
1056 - if (!skip_initializing_odb)
1057 - repo->objects = odb_new(repo, getenv_safe(&to_free, DB_ENVIRONMENT),
1058 - getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
1059 -
1055 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
1056 disable_replace_refs();
1057 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
@@ -1072,10 +1067,10 @@ static void setup_git_env_internal(struct repository *repo,
1067 fetch_if_missing = 0;
1068 }
1069
1075 -static void set_git_dir_1(struct repository *repo, const char *path, bool skip_initializing_odb)
1070 +static void set_git_dir_1(struct repository *repo, const char *path)
1071 {
1072 xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
1078 - setup_git_env_internal(repo, path, skip_initializing_odb);
1073 + setup_git_env_internal(repo, path);
1074 }
1075
1076 static void update_relative_gitdir(const char *name UNUSED,
@@ -1089,7 +1084,7 @@ static void update_relative_gitdir(const char *name UNUSED,
1084 trace_printf_key(&trace_setup_key,
1085 "setup: move $GIT_DIR to '%s'",
1086 path);
1092 - set_git_dir_1(repo, path, true);
1087 + set_git_dir_1(repo, path);
1088 free(path);
1089 }
1090
@@ -1102,7 +1097,7 @@ static void set_git_dir(struct repository *repo, const char *path, int make_real
1097 path = realpath.buf;
1098 }
1099
1105 - set_git_dir_1(repo, path, false);
1100 + set_git_dir_1(repo, path);
1101 if (!is_absolute_path(path))
1102 chdir_notify_register(NULL, update_relative_gitdir, repo);
1103
@@ -1879,8 +1874,15 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
1874 }
1875
1876 if (is_git_directory(".")) {
1877 + struct strvec to_free = STRVEC_INIT;
1878 +
1879 set_git_dir(repo, ".", 0);
1880 + repo->objects = odb_new(repo,
1881 + getenv_safe(&to_free, DB_ENVIRONMENT),
1882 + getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
1883 check_and_apply_repository_format(repo, NULL);
1884 +
1885 + strvec_clear(&to_free);
1886 return path;
1887 }
1888
@@ -2032,13 +2034,19 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2034 startup_info->have_repository ||
2035 /* GIT_DIR_EXPLICIT */
2036 getenv(GIT_DIR_ENVIRONMENT)) {
2037 + struct strvec to_free = STRVEC_INIT;
2038 +
2039 if (!repo->gitdir) {
2040 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
2041 if (!gitdir)
2042 gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
2039 - setup_git_env_internal(repo, gitdir, false);
2043 + setup_git_env_internal(repo, gitdir);
2044 }
2045
2046 + repo->objects = odb_new(repo,
2047 + getenv_safe(&to_free, DB_ENVIRONMENT),
2048 + getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
2049 +
2050 if (startup_info->have_repository) {
2051 struct strbuf err = STRBUF_INIT;
2052
@@ -2048,6 +2056,8 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2056 clear_repository_format(&repo_fmt);
2057 strbuf_release(&err);
2058 }
2059 +
2060 + strvec_clear(&to_free);
2061 }
2062 /*
2063 * Since precompose_string_if_needed() needs to look at
@@ -2796,6 +2806,7 @@ int init_db(struct repository *repo,
2806 int exist_ok = flags & INIT_DB_EXIST_OK;
2807 char *original_git_dir = real_pathdup(git_dir, 1);
2808 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
2809 + struct strvec to_free = STRVEC_INIT;
2810
2811 if (real_git_dir) {
2812 struct stat st;
@@ -2816,6 +2827,9 @@ int init_db(struct repository *repo,
2827 }
2828 startup_info->have_repository = 1;
2829
2830 + repo->objects = odb_new(repo, getenv_safe(&to_free, DB_ENVIRONMENT),
2831 + getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT));
2832 +
2833 /*
2834 * Check to see if the repository version is right.
2835 * Note that a newly created repository does not have
@@ -2879,6 +2893,7 @@ int init_db(struct repository *repo,
2893 }
2894
2895 clear_repository_format(&repo_fmt);
2896 + strvec_clear(&to_free);
2897 free(original_git_dir);
2898 return 0;
2899 }