Commit

e927cfeb21d6a217b708216862deb36f144f064b

odb: make creation of on-disk structures pluggable

When creating a new "files" object database source we have to create a
couple of directories. These directories are of course specific to this
particular backend, and a different backend may require a setup that is
completely different.

Make the creation of on-disk structures pluggable to accommodate for
this.

Note that there is one exception though: the "objects" directory must
exist in a repository regardless of which backend is in use. If it
doesn't exist then the repository is not treated as a Git repository at
all. Consequently, we create this directory regardless of the backend.

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

Patrick Steinhardt <ps@pks.im> · 2026-08-07 03:34 UTC · committed by Junio C Hamano

Browse files
3 files changed, +60 −16
modified odb/source-files.c
@@ -9,6 +9,7 @@
9 9 #include "odb/source-files.h"
10 10 #include "odb/source-loose.h"
11 11 #include "packfile.h"
12 + #include "path.h"
12 13 #include "strbuf.h"
13 14 #include "write-or-die.h"
14 15
@@ -41,6 +42,23 @@ static void odb_source_files_close(struct odb_source *source)
41 42 odb_source_close(&files->packed->base);
42 43 }
43 44
45 + static int odb_source_files_create_on_disk(struct odb_source *source)
46 + {
47 + struct strbuf path = STRBUF_INIT;
48 +
49 + safe_create_dir(source->odb->repo, source->path, 1);
50 +
51 + strbuf_addf(&path, "%s/pack", source->path);
52 + safe_create_dir(source->odb->repo, path.buf, 1);
53 +
54 + strbuf_reset(&path);
55 + strbuf_addf(&path, "%s/info", source->path);
56 + safe_create_dir(source->odb->repo, path.buf, 1);
57 +
58 + strbuf_release(&path);
59 + return 0;
60 + }
61 +
44 62 static void odb_source_files_prepare(struct odb_source *source,
45 63 enum odb_prepare_flags flags)
46 64 {
@@ -271,6 +289,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
271 289
272 290 files->base.free = odb_source_files_free;
273 291 files->base.close = odb_source_files_close;
292 + files->base.create_on_disk = odb_source_files_create_on_disk;
274 293 files->base.prepare = odb_source_files_prepare;
275 294 files->base.read_object_info = odb_source_files_read_object_info;
276 295 files->base.read_object_stream = odb_source_files_read_object_stream;
modified odb/source.h
@@ -89,6 +89,18 @@ struct odb_source {
89 89 */
90 90 void (*close)(struct odb_source *source);
91 91
92 + /*
93 + * This callback is expected to create on-disk data structures that are
94 + * required for this source to operate.
95 + *
96 + * The callback is expected to return 0 on success, a negative error
97 + * code otherwise.
98 + *
99 + * This callback may be NULL in case the source does not need any
100 + * on-disk setup.
101 + */
102 + int (*create_on_disk)(struct odb_source *source);
103 +
92 104 /*
93 105 * This callback is expected to prepare the source so that it becomes
94 106 * ready for use. It optionally clears underlying caches of the object
@@ -316,6 +328,17 @@ static inline void odb_source_close(struct odb_source *source)
316 328 source->close(source);
317 329 }
318 330
331 + /*
332 + * Create on-disk data structures that are required for this source to operate
333 + * correctly. Returns 0 on success, a negative error code otherwise.
334 + */
335 + static inline int odb_source_create_on_disk(struct odb_source *source)
336 + {
337 + if (!source->create_on_disk)
338 + return 0;
339 + return source->create_on_disk(source);
340 + }
341 +
319 342 /*
320 343 * Prepare the object database source and clear any caches. Depending on the
321 344 * backend used this may have the effect that concurrently-written objects
modified setup.c
@@ -2649,25 +2649,27 @@ static int create_default_files(struct repository *repo,
2649 2649
2650 2650 static void create_object_database(struct repository *repo)
2651 2651 {
2652 - struct strbuf path = STRBUF_INIT;
2653 - size_t baselen;
2652 + /*
2653 + * Create the "objects" directory in the common directory. This is done
2654 + * so that the repository can be discovered regardless of the backend
2655 + * used.
2656 + *
2657 + * Note that we only do this in case the object directory wasn't
2658 + * overwritten via an environment variable. If it _is_ being overridden
2659 + * then we skip this step, as the repository won't be discoverable
2660 + * anyway without the environment variable.
2661 + */
2662 + if (!getenv(DB_ENVIRONMENT)) {
2663 + struct strbuf objects_dir = STRBUF_INIT;
2664 + repo_common_path_append(repo, &objects_dir, "objects");
2665 + safe_create_dir(repo, objects_dir.buf, 1);
2666 + strbuf_release(&objects_dir);
2667 + }
2654 2668
2655 2669 repo->objects = odb_new(repo, ODB_NEW_HONOR_ENV);
2656 2670
2657 - strbuf_addstr(&path, repo_get_object_directory(repo));
2658 - baselen = path.len;
2659 -
2660 - safe_create_dir(repo, path.buf, 1);
2661 -
2662 - strbuf_setlen(&path, baselen);
2663 - strbuf_addstr(&path, "/pack");
2664 - safe_create_dir(repo, path.buf, 1);
2665 -
2666 - strbuf_setlen(&path, baselen);
2667 - strbuf_addstr(&path, "/info");
2668 - safe_create_dir(repo, path.buf, 1);
2669 -
2670 - strbuf_release(&path);
2671 + if (odb_source_create_on_disk(repo->objects->sources) < 0)
2672 + die(_("failed creating object database"));
2671 2673 }
2672 2674
2673 2675 static void separate_git_dir(const char *git_dir, const char *git_link)