]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source: introduce source type for robustness
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Mar 2026 14:19:45 +0000 (15:19 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Mar 2026 19:45:15 +0000 (11:45 -0800)
When a caller holds a `struct odb_source`, they have no way of telling
what type the source is. This doesn't really cause any problems in the
current status quo as we only have a single type anyway, "files". But
going forward we expect to add more types, and if so it will become
necessary to tell the sources apart.

Introduce a new enum to cover this use case and assert that the given
source actually matches the target source when performing the downcast.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
odb/source-files.c
odb/source-files.h
odb/source.c
odb/source.h

index df0ea9ee62d85315f7022a9a231c683511b4024f..7496e1d9f82266474ce500cf5b4e0f62c84a2faf 100644 (file)
@@ -36,7 +36,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
        struct odb_source_files *files;
 
        CALLOC_ARRAY(files, 1);
-       odb_source_init(&files->base, odb, path, local);
+       odb_source_init(&files->base, odb, ODB_SOURCE_FILES, path, local);
        files->loose = odb_source_loose_new(&files->base);
        files->packed = packfile_store_new(&files->base);
 
index 859a8f518a4406831b9c7c61d1814948ce5a56f2..803fa995fb961101f604aa9dbe9a1a4c2dfed76b 100644 (file)
@@ -25,10 +25,13 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
 void odb_source_files_free(struct odb_source_files *files);
 
 /*
- * Cast the given object database source to the files backend.
+ * Cast the given object database source to the files backend. This will cause
+ * a BUG in case the source doesn't use this backend.
  */
 static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
 {
+       if (source->type != ODB_SOURCE_FILES)
+               BUG("trying to downcast source of type '%d' to files", source->type);
        return container_of(source, struct odb_source_files, base);
 }
 
index d8b2176a94646a88328aa03b74a27d2a49f7498e..c7dcc528f62bb5bb5b0aefbc651382ab539745a5 100644 (file)
@@ -13,10 +13,12 @@ struct odb_source *odb_source_new(struct object_database *odb,
 
 void odb_source_init(struct odb_source *source,
                     struct object_database *odb,
+                    enum odb_source_type type,
                     const char *path,
                     bool local)
 {
        source->odb = odb;
+       source->type = type;
        source->local = local;
        source->path = xstrdup(path);
 }
index e6698b73a3c6606fbd2316116a9131e508f4d051..45b72b81a0ad182be04aaafc1012e96fcde12838 100644 (file)
@@ -1,6 +1,17 @@
 #ifndef ODB_SOURCE_H
 #define ODB_SOURCE_H
 
+enum odb_source_type {
+       /*
+        * The "unknown" type, which should never be in use. This type mostly
+        * exists to catch cases where the type field remains zeroed out.
+        */
+       ODB_SOURCE_UNKNOWN,
+
+       /* The "files" backend that uses loose objects and packfiles. */
+       ODB_SOURCE_FILES,
+};
+
 /*
  * The source is the part of the object database that stores the actual
  * objects. It thus encapsulates the logic to read and write the specific
@@ -19,6 +30,9 @@ struct odb_source {
        /* Object database that owns this object source. */
        struct object_database *odb;
 
+       /* The type used by this source. */
+       enum odb_source_type type;
+
        /*
         * Figure out whether this is the local source of the owning
         * repository, which would typically be its ".git/objects" directory.
@@ -58,6 +72,7 @@ struct odb_source *odb_source_new(struct object_database *odb,
  */
 void odb_source_init(struct odb_source *source,
                     struct object_database *odb,
+                    enum odb_source_type type,
                     const char *path,
                     bool local);