odb/source: introduce function to map source type to name
Introduce a new function that maps an object source's type to a human-readable name. Use the function to provide better human-readable error messages for the downcasting functions. 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
bdf92ad403fad3aa63806274467b97221487faf3
6 files changed
+37
-4
odb/source-files.h
+3
-1
@@ -28,7 +28,9 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
28
static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
29
{
30
if (source->type != ODB_SOURCE_FILES)
31
- BUG("trying to downcast source of type '%d' to files", source->type);
31
+ BUG("trying to downcast source of type '%s' to '%s'",
32
+ odb_source_type_to_name(source->type),
33
+ odb_source_type_to_name(ODB_SOURCE_FILES));
34
return container_of(source, struct odb_source_files, base);
35
}
36
odb/source-inmemory.h
+3
-1
@@ -26,7 +26,9 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
26
static inline struct odb_source_inmemory *odb_source_inmemory_downcast(struct odb_source *source)
27
{
28
if (source->type != ODB_SOURCE_INMEMORY)
29
- BUG("trying to downcast source of type '%d' to in-memory", source->type);
29
+ BUG("trying to downcast source of type '%s' to '%s'",
30
+ odb_source_type_to_name(source->type),
31
+ odb_source_type_to_name(ODB_SOURCE_INMEMORY));
32
return container_of(source, struct odb_source_inmemory, base);
33
}
34
odb/source-loose.h
+3
-1
@@ -41,7 +41,9 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
41
static inline struct odb_source_loose *odb_source_loose_downcast(struct odb_source *source)
42
{
43
if (source->type != ODB_SOURCE_LOOSE)
44
- BUG("trying to downcast source of type '%d' to loose", source->type);
44
+ BUG("trying to downcast source of type '%s' to '%s'",
45
+ odb_source_type_to_name(source->type),
46
+ odb_source_type_to_name(ODB_SOURCE_LOOSE));
47
return container_of(source, struct odb_source_loose, base);
48
}
49
odb/source-packed.h
+3
-1
@@ -78,7 +78,9 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
78
static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_source *source)
79
{
80
if (source->type != ODB_SOURCE_PACKED)
81
- BUG("trying to downcast source of type '%d' to packed", source->type);
81
+ BUG("trying to downcast source of type '%s' to '%s'",
82
+ odb_source_type_to_name(source->type),
83
+ odb_source_type_to_name(ODB_SOURCE_PACKED));
84
return container_of(source, struct odb_source_packed, base);
85
}
86
odb/source.c
+19
@@ -4,6 +4,25 @@
4
#include "odb/source.h"
5
#include "packfile.h"
6
7
+static const char * const odb_source_names_by_type[] = {
8
+ [ODB_SOURCE_UNKNOWN] = "unknown",
9
+ [ODB_SOURCE_FILES] = "files",
10
+ [ODB_SOURCE_LOOSE] = "loose",
11
+ [ODB_SOURCE_PACKED] = "packed",
12
+ [ODB_SOURCE_INMEMORY] = "inmemory",
13
+};
14
+
15
+const char *odb_source_type_to_name(enum odb_source_type type)
16
+{
17
+ const char *name;
18
+ if (type < 0 || type >= ARRAY_SIZE(odb_source_names_by_type))
19
+ type = ODB_SOURCE_UNKNOWN;
20
+ name = odb_source_names_by_type[type];
21
+ if (!name)
22
+ BUG("name missing in `odb_source_names_by_type` for '%d'", type);
23
+ return name;
24
+}
25
+
26
struct odb_source *odb_source_new(struct object_database *odb,
27
const char *path,
28
bool local)
odb/source.h
+6
@@ -25,6 +25,12 @@ enum odb_source_type {
25
ODB_SOURCE_INMEMORY,
26
};
27
28
+/*
29
+ * Convert between the enum and its name. Returns the equivalent of "unknown"
30
+ * for unknown types.
31
+ */
32
+const char *odb_source_type_to_name(enum odb_source_type type);
33
+
34
struct object_id;
35
struct odb_read_stream;
36
struct strvec;