Skip to content

Commit bdf92ad

Browse files
pks-tgitster
authored andcommitted
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>
1 parent cc0a909 commit bdf92ad

6 files changed

Lines changed: 37 additions & 4 deletions

File tree

odb/source-files.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
2828
static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
2929
{
3030
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));
3234
return container_of(source, struct odb_source_files, base);
3335
}
3436

odb/source-inmemory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
2626
static inline struct odb_source_inmemory *odb_source_inmemory_downcast(struct odb_source *source)
2727
{
2828
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));
3032
return container_of(source, struct odb_source_inmemory, base);
3133
}
3234

odb/source-loose.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
4141
static inline struct odb_source_loose *odb_source_loose_downcast(struct odb_source *source)
4242
{
4343
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));
4547
return container_of(source, struct odb_source_loose, base);
4648
}
4749

odb/source-packed.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
7878
static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_source *source)
7979
{
8080
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));
8284
return container_of(source, struct odb_source_packed, base);
8385
}
8486

odb/source.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
#include "odb/source.h"
55
#include "packfile.h"
66

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+
726
struct odb_source *odb_source_new(struct object_database *odb,
827
const char *path,
928
bool local)

odb/source.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ enum odb_source_type {
2525
ODB_SOURCE_INMEMORY,
2626
};
2727

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+
2834
struct object_id;
2935
struct odb_read_stream;
3036
struct strvec;

0 commit comments

Comments
 (0)