odb: split `struct odb_source` into separate header

Subsequent commits will expand the `struct odb_source` to become a generic interface for accessing an object database source. As part of these refactorings we'll add a set of function pointers that will significantly expand the structure overall. Prepare for this by splitting out the `struct odb_source` into a separate header. This keeps the high-level object database interface detached from the low-level object database sources. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC ba1c21d34346e5979f9308806274bfcda4949ad4
6 files changed +91 -69
Makefile
+1
@@ -1214,6 +1214,7 @@ LIB_OBJS += object-file.o
1214 LIB_OBJS += object-name.o
1215 LIB_OBJS += object.o
1216 LIB_OBJS += odb.o
1217 +LIB_OBJS += odb/source.o
1218 LIB_OBJS += odb/streaming.o
1219 LIB_OBJS += oid-array.o
1220 LIB_OBJS += oidmap.o
meson.build
+1
@@ -397,6 +397,7 @@ libgit_sources = [
397 'object-name.c',
398 'object.c',
399 'odb.c',
400 + 'odb/source.c',
401 'odb/streaming.c',
402 'oid-array.c',
403 'oidmap.c',
odb.c
-25
@@ -217,23 +217,6 @@ static void odb_source_read_alternates(struct odb_source *source,
217 free(path);
218 }
219
220 -
221 -static struct odb_source *odb_source_new(struct object_database *odb,
222 - const char *path,
223 - bool local)
224 -{
225 - struct odb_source *source;
226 -
227 - CALLOC_ARRAY(source, 1);
228 - source->odb = odb;
229 - source->local = local;
230 - source->path = xstrdup(path);
231 - source->loose = odb_source_loose_new(source);
232 - source->packfiles = packfile_store_new(source);
233 -
234 - return source;
235 -}
236 -
220 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
221 const char *source,
222 int depth)
@@ -373,14 +356,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
356 return source->next;
357 }
358
376 -static void odb_source_free(struct odb_source *source)
377 -{
378 - free(source->path);
379 - odb_source_loose_free(source->loose);
380 - packfile_store_free(source->packfiles);
381 - free(source);
382 -}
383 -
359 void odb_restore_primary_source(struct object_database *odb,
360 struct odb_source *restore_source,
361 const char *old_path)
odb.h
+1 -44
@@ -3,6 +3,7 @@
3
4 #include "hashmap.h"
5 #include "object.h"
6 +#include "odb/source.h"
7 #include "oidset.h"
8 #include "oidmap.h"
9 #include "string-list.h"
@@ -30,50 +31,6 @@ extern int fetch_if_missing;
31 */
32 char *compute_alternate_path(const char *path, struct strbuf *err);
33
33 -/*
34 - * The source is the part of the object database that stores the actual
35 - * objects. It thus encapsulates the logic to read and write the specific
36 - * on-disk format. An object database can have multiple sources:
37 - *
38 - * - The primary source, which is typically located in "$GIT_DIR/objects".
39 - * This is where new objects are usually written to.
40 - *
41 - * - Alternate sources, which are configured via "objects/info/alternates" or
42 - * via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
43 - * alternate sources are only used to read objects.
44 - */
45 -struct odb_source {
46 - struct odb_source *next;
47 -
48 - /* Object database that owns this object source. */
49 - struct object_database *odb;
50 -
51 - /* Private state for loose objects. */
52 - struct odb_source_loose *loose;
53 -
54 - /* Should only be accessed directly by packfile.c and midx.c. */
55 - struct packfile_store *packfiles;
56 -
57 - /*
58 - * Figure out whether this is the local source of the owning
59 - * repository, which would typically be its ".git/objects" directory.
60 - * This local object directory is usually where objects would be
61 - * written to.
62 - */
63 - bool local;
64 -
65 - /*
66 - * This object store is ephemeral, so there is no need to fsync.
67 - */
68 - int will_destroy;
69 -
70 - /*
71 - * Path to the source. If this is a relative path, it is relative to
72 - * the current working directory.
73 - */
74 - char *path;
75 -};
76 -
34 struct packed_git;
35 struct packfile_store;
36 struct cached_object_entry;
odb/source.c new
+28
@@ -0,0 +1,28 @@
1 +#include "git-compat-util.h"
2 +#include "object-file.h"
3 +#include "odb/source.h"
4 +#include "packfile.h"
5 +
6 +struct odb_source *odb_source_new(struct object_database *odb,
7 + const char *path,
8 + bool local)
9 +{
10 + struct odb_source *source;
11 +
12 + CALLOC_ARRAY(source, 1);
13 + source->odb = odb;
14 + source->local = local;
15 + source->path = xstrdup(path);
16 + source->loose = odb_source_loose_new(source);
17 + source->packfiles = packfile_store_new(source);
18 +
19 + return source;
20 +}
21 +
22 +void odb_source_free(struct odb_source *source)
23 +{
24 + free(source->path);
25 + odb_source_loose_free(source->loose);
26 + packfile_store_free(source->packfiles);
27 + free(source);
28 +}
odb/source.h new
+60
@@ -0,0 +1,60 @@
1 +#ifndef ODB_SOURCE_H
2 +#define ODB_SOURCE_H
3 +
4 +/*
5 + * The source is the part of the object database that stores the actual
6 + * objects. It thus encapsulates the logic to read and write the specific
7 + * on-disk format. An object database can have multiple sources:
8 + *
9 + * - The primary source, which is typically located in "$GIT_DIR/objects".
10 + * This is where new objects are usually written to.
11 + *
12 + * - Alternate sources, which are configured via "objects/info/alternates" or
13 + * via the GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable. These
14 + * alternate sources are only used to read objects.
15 + */
16 +struct odb_source {
17 + struct odb_source *next;
18 +
19 + /* Object database that owns this object source. */
20 + struct object_database *odb;
21 +
22 + /* Private state for loose objects. */
23 + struct odb_source_loose *loose;
24 +
25 + /* Should only be accessed directly by packfile.c and midx.c. */
26 + struct packfile_store *packfiles;
27 +
28 + /*
29 + * Figure out whether this is the local source of the owning
30 + * repository, which would typically be its ".git/objects" directory.
31 + * This local object directory is usually where objects would be
32 + * written to.
33 + */
34 + bool local;
35 +
36 + /*
37 + * This object store is ephemeral, so there is no need to fsync.
38 + */
39 + int will_destroy;
40 +
41 + /*
42 + * Path to the source. If this is a relative path, it is relative to
43 + * the current working directory.
44 + */
45 + char *path;
46 +};
47 +
48 +/*
49 + * Allocate and initialize a new source for the given object database located
50 + * at `path`. `local` indicates whether or not the source is the local and thus
51 + * primary object source of the object database.
52 + */
53 +struct odb_source *odb_source_new(struct object_database *odb,
54 + const char *path,
55 + bool local);
56 +
57 +/* Free the object database source, releasing all associated resources. */
58 +void odb_source_free(struct odb_source *source);
59 +
60 +#endif