Skip to content

Commit e6877b3

Browse files
committed
odb: add odb_source_files_try() for heterogeneous source iteration
The odb_source vtable introduced in this release allows multiple backend implementations via the odb_source_type enum. However, source-chain iteration sites that need files-specific internals (pack store, loose cache, MIDX) currently use odb_source_files_downcast(), which calls BUG() for non-files source types. This makes it impossible to add a non-files source to the chain without crashing. Add odb_source_files_try() as a companion to the existing downcast function. It returns NULL for non-files sources instead of aborting. This follows the pattern used elsewhere in git where a "try" or "maybe" variant provides a fallback path while the strict version retains its safety guarantee. The existing odb_source_files_downcast() is unchanged and continues to BUG() on type mismatch, protecting call sites that should only ever receive a files source. A subsequent commit will convert the source-chain iteration sites to use this new helper. Signed-off-by: Aaron Paterson <apaterson@pm.me>
1 parent dc6ecd5 commit e6877b3

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

odb/source-files.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ static inline struct odb_source_files *odb_source_files_downcast(struct odb_sour
3232
return container_of(source, struct odb_source_files, base);
3333
}
3434

35+
/*
36+
* Try to cast the given source to the files backend. Returns NULL if
37+
* the source uses a different backend. Use this in loops that iterate
38+
* over heterogeneous source chains (e.g. when alternates may include
39+
* non-files backends). Use odb_source_files_downcast() when the source
40+
* is known to be a files backend.
41+
*/
42+
static inline struct odb_source_files *odb_source_files_try(struct odb_source *source)
43+
{
44+
if (source->type != ODB_SOURCE_FILES)
45+
return NULL;
46+
return container_of(source, struct odb_source_files, base);
47+
}
48+
3549
#endif

0 commit comments

Comments
 (0)