Skip to content

Commit 87842f6

Browse files
pks-tgitster
authored andcommitted
odb/source: introduce source type for robustness
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>
1 parent 7e0aa0a commit 87842f6

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

odb/source-files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
3636
struct odb_source_files *files;
3737

3838
CALLOC_ARRAY(files, 1);
39-
odb_source_init(&files->base, odb, path, local);
39+
odb_source_init(&files->base, odb, ODB_SOURCE_FILES, path, local);
4040
files->loose = odb_source_loose_new(&files->base);
4141
files->packed = packfile_store_new(&files->base);
4242

odb/source-files.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
2525
void odb_source_files_free(struct odb_source_files *files);
2626

2727
/*
28-
* Cast the given object database source to the files backend.
28+
* Cast the given object database source to the files backend. This will cause
29+
* a BUG in case the source doesn't use this backend.
2930
*/
3031
static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
3132
{
33+
if (source->type != ODB_SOURCE_FILES)
34+
BUG("trying to downcast source of type '%d' to files", source->type);
3235
return container_of(source, struct odb_source_files, base);
3336
}
3437

odb/source.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ struct odb_source *odb_source_new(struct object_database *odb,
1313

1414
void odb_source_init(struct odb_source *source,
1515
struct object_database *odb,
16+
enum odb_source_type type,
1617
const char *path,
1718
bool local)
1819
{
1920
source->odb = odb;
21+
source->type = type;
2022
source->local = local;
2123
source->path = xstrdup(path);
2224
}

odb/source.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#ifndef ODB_SOURCE_H
22
#define ODB_SOURCE_H
33

4+
enum odb_source_type {
5+
/*
6+
* The "unknown" type, which should never be in use. This type mostly
7+
* exists to catch cases where the type field remains zeroed out.
8+
*/
9+
ODB_SOURCE_UNKNOWN,
10+
11+
/* The "files" backend that uses loose objects and packfiles. */
12+
ODB_SOURCE_FILES,
13+
};
14+
415
/*
516
* The source is the part of the object database that stores the actual
617
* objects. It thus encapsulates the logic to read and write the specific
@@ -19,6 +30,9 @@ struct odb_source {
1930
/* Object database that owns this object source. */
2031
struct object_database *odb;
2132

33+
/* The type used by this source. */
34+
enum odb_source_type type;
35+
2236
/*
2337
* Figure out whether this is the local source of the owning
2438
* repository, which would typically be its ".git/objects" directory.
@@ -58,6 +72,7 @@ struct odb_source *odb_source_new(struct object_database *odb,
5872
*/
5973
void odb_source_init(struct odb_source *source,
6074
struct object_database *odb,
75+
enum odb_source_type type,
6176
const char *path,
6277
bool local);
6378

0 commit comments

Comments
 (0)