setup: defer object database creation
In a subsequent commit we'll make the creation of the on-disk data structures of an object database pluggable. This will lead to an in-between state where we have already configured the repository's object database, but it's not usable yet until we eventually call `create_object_directory()`. Defer the object database creation so that we handle both steps in the same function. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jul 24, 2026 at 05:48 UTC
cc0a909026a26b5e10cf7283084b9dde102dfc46
2 files changed
+36
-8
setup.c
+27
-8
@@ -1760,6 +1760,13 @@ enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
1760
return result;
1761
}
1762
1763
+static void get_object_directories(char **object_directory,
1764
+ char **alternate_object_directories)
1765
+{
1766
+ *object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1767
+ *alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1768
+}
1769
+
1770
int apply_repository_format(struct repository *repo,
1771
const struct repository_format *format,
1772
enum apply_repository_format_flags flags,
@@ -1779,8 +1786,9 @@ int apply_repository_format(struct repository *repo,
1786
if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
1787
const char *shallow_file;
1788
1782
- object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1783
- alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1789
+ get_object_directories(&object_directory,
1790
+ &alternate_object_directories);
1791
+
1792
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
1793
if (shallow_file)
1794
set_alternate_shallow_file(repo, shallow_file);
@@ -1803,8 +1811,9 @@ int apply_repository_format(struct repository *repo,
1811
repo->repository_format_precious_objects =
1812
format->precious_objects;
1813
1806
- repo->objects = odb_new(repo, object_directory,
1807
- alternate_object_directories);
1814
+ if (!(flags & APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION))
1815
+ repo->objects = odb_new(repo, object_directory,
1816
+ alternate_object_directories);
1817
1818
free(alternate_object_directories);
1819
free(object_directory);
@@ -2654,11 +2663,16 @@ static int create_default_files(struct repository *repo,
2663
return reinit;
2664
}
2665
2657
-static void create_object_directory(struct repository *repo)
2666
+static void create_object_database(struct repository *repo)
2667
{
2668
+ char *object_directory, *alternate_object_directories;
2669
struct strbuf path = STRBUF_INIT;
2670
size_t baselen;
2671
2672
+ get_object_directories(&object_directory, &alternate_object_directories);
2673
+ repo->objects = odb_new(repo, object_directory,
2674
+ alternate_object_directories);
2675
+
2676
strbuf_addstr(&path, repo_get_object_directory(repo));
2677
baselen = path.len;
2678
@@ -2672,6 +2686,8 @@ static void create_object_directory(struct repository *repo)
2686
strbuf_addstr(&path, "/info");
2687
safe_create_dir(repo, path.buf, 1);
2688
2689
+ free(alternate_object_directories);
2690
+ free(object_directory);
2691
strbuf_release(&path);
2692
}
2693
@@ -2867,9 +2883,10 @@ int init_db(struct repository *repo,
2883
*/
2884
read_and_verify_repository_format(&repo_fmt, repo_get_git_dir(repo), NULL);
2885
repository_format_configure(&repo_fmt, hash, ref_storage_format);
2870
- if (apply_repository_format(repo, &repo_fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
2886
+ if (apply_repository_format(repo, &repo_fmt,
2887
+ APPLY_REPOSITORY_FORMAT_HONOR_ENV |
2888
+ APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION, &err) < 0)
2889
die("%s", err.buf);
2872
- startup_info->have_repository = 1;
2890
2891
/*
2892
* Ensure `core.hidedotfiles` is processed. This must happen after we
@@ -2885,7 +2902,9 @@ int init_db(struct repository *repo,
2902
2903
if (!(flags & INIT_DB_SKIP_REFDB))
2904
create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2888
- create_object_directory(repo);
2905
+ create_object_database(repo);
2906
+
2907
+ startup_info->have_repository = 1;
2908
2909
if (repo_settings_get_shared_repository(repo)) {
2910
char buf[10];
setup.h
+9
@@ -241,6 +241,15 @@ enum apply_repository_format_flags {
241
* relate to the object database.
242
*/
243
APPLY_REPOSITORY_FORMAT_HONOR_ENV = (1 << 0),
244
+
245
+ /*
246
+ * Usually, the object database is created after the repository format
247
+ * was applied. This step is skipped if this flag is set, which leaves
248
+ * us with a partially-working repository.
249
+ *
250
+ * This is useful when initializing a new repository.
251
+ */
252
+ APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION = (1 << 1),
253
};
254
255
/*