git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #include "git-compat-util.h" |
| 2 | #include "object-file.h" |
| 3 | #include "odb/source-files.h" |
| 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] = "in-memory", |
| 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 | enum object_storage_format format, |
| 28 | const char *payload, |
| 29 | enum odb_new_flags flags) |
| 30 | { |
| 31 | struct odb_source *source; |
| 32 | |
| 33 | switch (format) { |
| 34 | case OBJECT_STORAGE_FORMAT_FILES: |
| 35 | source = &odb_source_files_new(odb, payload, flags)->base; |
| 36 | break; |
| 37 | default: |
| 38 | BUG("unknown object storage format"); |
| 39 | } |
| 40 | |
| 41 | return source; |
| 42 | } |
| 43 | |
| 44 | void odb_source_init(struct odb_source *source, |
| 45 | struct object_database *odb, |
| 46 | enum odb_source_type type, |
| 47 | const char *path) |
| 48 | { |
| 49 | source->odb = odb; |
| 50 | source->type = type; |
| 51 | source->path = xstrdup(path); |
| 52 | } |
| 53 | |
| 54 | void odb_source_free(struct odb_source *source) |
| 55 | { |
| 56 | if (!source) |
| 57 | return; |
| 58 | source->free(source); |
| 59 | } |
| 60 | |
| 61 | void odb_source_release(struct odb_source *source) |
| 62 | { |
| 63 | if (!source) |
| 64 | return; |
| 65 | free(source->path); |
| 66 | } |