Skip to content

Commit 0fcc285

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 465a22b commit 0fcc285

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

refs.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,33 @@
3333
/*
3434
* List of all available backends
3535
*/
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+
};
3739

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)
3941
{
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];
4444
return NULL;
4545
}
4646

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+
4763
/*
4864
* How to handle various characters in refnames:
4965
* 0: An acceptable character for refs
@@ -2029,12 +2045,12 @@ static struct ref_store *ref_store_init(struct repository *repo,
20292045
const char *gitdir,
20302046
unsigned int flags)
20312047
{
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);
20342050
struct ref_store *refs;
20352051

20362052
if (!be)
2037-
BUG("reference backend %s is unknown", be_name);
2053+
BUG("reference backend is unknown");
20382054

20392055
refs = be->init(repo, gitdir, flags);
20402056
return refs;

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ struct string_list;
1111
struct string_list_item;
1212
struct worktree;
1313

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+
1417
/*
1518
* Resolve a reference, recursively following symbolic refererences.
1619
*

refs/debug.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
426426
}
427427

428428
struct ref_storage_be refs_be_debug = {
429-
.next = NULL,
430429
.name = "debug",
431430
.init = NULL,
432431
.init_db = debug_init_db,

refs/files-backend.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3241,7 +3241,6 @@ static int files_init_db(struct ref_store *ref_store, struct strbuf *err UNUSED)
32413241
}
32423242

32433243
struct ref_storage_be refs_be_files = {
3244-
.next = NULL,
32453244
.name = "files",
32463245
.init = files_ref_store_create,
32473246
.init_db = files_init_db,

refs/packed-backend.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,6 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
17051705
}
17061706

17071707
struct ref_storage_be refs_be_packed = {
1708-
.next = NULL,
17091708
.name = "packed",
17101709
.init = packed_ref_store_create,
17111710
.init_db = packed_init_db,

refs/refs-internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
663663
struct strbuf *referent);
664664

665665
struct ref_storage_be {
666-
struct ref_storage_be *next;
667666
const char *name;
668667
ref_store_init_fn *init;
669668
ref_init_db_fn *init_db;

repository.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ enum fetch_negotiation_setting {
2424
FETCH_NEGOTIATION_NOOP,
2525
};
2626

27+
#define REF_STORAGE_FORMAT_UNKNOWN 0
28+
#define REF_STORAGE_FORMAT_FILES 1
29+
2730
struct repo_settings {
2831
int initialized;
2932

0 commit comments

Comments
 (0)