refs: refactor logic to look up storage backends
In order to look up ref storage backends, we're currently using a linked list of backends, where each backend is expected to set up its `next` pointer to the next ref storage backend. This is kind of a weird setup as backends need to be aware of other backends without much of a reason. Refactor the code so that the array of backends is centrally defined in "refs.c", where each backend is now identified by an integer constant. Expose functions to translate from those integer constants to the name and vice versa, which will be required by subsequent patches. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Dec 29, 2023 at 08:26 UTC
0fcc285c5eaab904ea9a8ade2696fe61355cf587
7 files changed
+31
-13
refs.c
+25
-9
@@ -33,17 +33,33 @@
33
/*
34
* List of all available backends
35
*/
36
-static struct ref_storage_be *refs_backends = &refs_be_files;
36
+static const struct ref_storage_be *refs_backends[] = {
37
+ [REF_STORAGE_FORMAT_FILES] = &refs_be_files,
38
+};
39
38
-static struct ref_storage_be *find_ref_storage_backend(const char *name)
40
+static const struct ref_storage_be *find_ref_storage_backend(unsigned int ref_storage_format)
41
{
40
- struct ref_storage_be *be;
41
- for (be = refs_backends; be; be = be->next)
42
- if (!strcmp(be->name, name))
43
- return be;
42
+ if (ref_storage_format < ARRAY_SIZE(refs_backends))
43
+ return refs_backends[ref_storage_format];
44
return NULL;
45
}
46
47
+unsigned int ref_storage_format_by_name(const char *name)
48
+{
49
+ for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++)
50
+ if (refs_backends[i] && !strcmp(refs_backends[i]->name, name))
51
+ return i;
52
+ return REF_STORAGE_FORMAT_UNKNOWN;
53
+}
54
+
55
+const char *ref_storage_format_to_name(unsigned int ref_storage_format)
56
+{
57
+ const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format);
58
+ if (!be)
59
+ return "unknown";
60
+ return be->name;
61
+}
62
+
63
/*
64
* How to handle various characters in refnames:
65
* 0: An acceptable character for refs
@@ -2029,12 +2045,12 @@ static struct ref_store *ref_store_init(struct repository *repo,
2045
const char *gitdir,
2046
unsigned int flags)
2047
{
2032
- const char *be_name = "files";
2033
- struct ref_storage_be *be = find_ref_storage_backend(be_name);
2048
+ unsigned int format = REF_STORAGE_FORMAT_FILES;
2049
+ const struct ref_storage_be *be = find_ref_storage_backend(format);
2050
struct ref_store *refs;
2051
2052
if (!be)
2037
- BUG("reference backend %s is unknown", be_name);
2053
+ BUG("reference backend is unknown");
2054
2055
refs = be->init(repo, gitdir, flags);
2056
return refs;
refs.h
+3
@@ -11,6 +11,9 @@ struct string_list;
11
struct string_list_item;
12
struct worktree;
13
14
+unsigned int ref_storage_format_by_name(const char *name);
15
+const char *ref_storage_format_to_name(unsigned int ref_storage_format);
16
+
17
/*
18
* Resolve a reference, recursively following symbolic refererences.
19
*
refs/debug.c
-1
@@ -426,7 +426,6 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
426
}
427
428
struct ref_storage_be refs_be_debug = {
429
- .next = NULL,
429
.name = "debug",
430
.init = NULL,
431
.init_db = debug_init_db,
refs/files-backend.c
-1
@@ -3241,7 +3241,6 @@ static int files_init_db(struct ref_store *ref_store, struct strbuf *err UNUSED)
3241
}
3242
3243
struct ref_storage_be refs_be_files = {
3244
- .next = NULL,
3244
.name = "files",
3245
.init = files_ref_store_create,
3246
.init_db = files_init_db,
refs/packed-backend.c
-1
@@ -1705,7 +1705,6 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
1705
}
1706
1707
struct ref_storage_be refs_be_packed = {
1708
- .next = NULL,
1708
.name = "packed",
1709
.init = packed_ref_store_create,
1710
.init_db = packed_init_db,
refs/refs-internal.h
-1
@@ -663,7 +663,6 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
663
struct strbuf *referent);
664
665
struct ref_storage_be {
666
- struct ref_storage_be *next;
666
const char *name;
667
ref_store_init_fn *init;
668
ref_init_db_fn *init_db;
repository.h
+3
@@ -24,6 +24,9 @@ enum fetch_negotiation_setting {
24
FETCH_NEGOTIATION_NOOP,
25
};
26
27
+#define REF_STORAGE_FORMAT_UNKNOWN 0
28
+#define REF_STORAGE_FORMAT_FILES 1
29
+
30
struct repo_settings {
31
int initialized;
32