refs: convert ref storage format to an enum

The ref storage format is tracked as a simple unsigned integer, which makes it harder than necessary to discover what that integer actually is or where its values are defined. Convert the ref storage format to instead be an enum. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 6, 2024 at 07:29 UTC 318efb966bc9b246703152f77cadd4e407de7cd9
8 files changed +29 -22
builtin/clone.c
+1 -1
@@ -970,7 +970,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
970 int submodule_progress;
971 int filter_submodules = 0;
972 int hash_algo;
973 - unsigned int ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
973 + enum ref_storage_format ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
974 const int do_not_override_repo_unix_permissions = -1;
975 const char *template_dir;
976 char *template_dir_dup = NULL;
builtin/init-db.c
+1 -1
@@ -81,7 +81,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
81 const char *ref_format = NULL;
82 const char *initial_branch = NULL;
83 int hash_algo = GIT_HASH_UNKNOWN;
84 - unsigned int ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
84 + enum ref_storage_format ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
85 int init_shared_repository = -1;
86 const struct option init_db_options[] = {
87 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
refs.c
+4 -3
@@ -37,14 +37,15 @@ static const struct ref_storage_be *refs_backends[] = {
37 [REF_STORAGE_FORMAT_REFTABLE] = &refs_be_reftable,
38 };
39
40 -static const struct ref_storage_be *find_ref_storage_backend(unsigned int ref_storage_format)
40 +static const struct ref_storage_be *find_ref_storage_backend(
41 + enum ref_storage_format ref_storage_format)
42 {
43 if (ref_storage_format < ARRAY_SIZE(refs_backends))
44 return refs_backends[ref_storage_format];
45 return NULL;
46 }
47
47 -unsigned int ref_storage_format_by_name(const char *name)
48 +enum ref_storage_format ref_storage_format_by_name(const char *name)
49 {
50 for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++)
51 if (refs_backends[i] && !strcmp(refs_backends[i]->name, name))
@@ -52,7 +53,7 @@ unsigned int ref_storage_format_by_name(const char *name)
53 return REF_STORAGE_FORMAT_UNKNOWN;
54 }
55
55 -const char *ref_storage_format_to_name(unsigned int ref_storage_format)
56 +const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format)
57 {
58 const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format);
59 if (!be)
refs.h
+8 -2
@@ -11,8 +11,14 @@ 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);
14 +enum ref_storage_format {
15 + REF_STORAGE_FORMAT_UNKNOWN,
16 + REF_STORAGE_FORMAT_FILES,
17 + REF_STORAGE_FORMAT_REFTABLE,
18 +};
19 +
20 +enum ref_storage_format ref_storage_format_by_name(const char *name);
21 +const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
22
23 /*
24 * Resolve a reference, recursively following symbolic refererences.
repository.c
+2 -1
@@ -124,7 +124,8 @@ void repo_set_compat_hash_algo(struct repository *repo, int algo)
124 repo_read_loose_object_map(repo);
125 }
126
127 -void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
127 +void repo_set_ref_storage_format(struct repository *repo,
128 + enum ref_storage_format format)
129 {
130 repo->ref_storage_format = format;
131 }
repository.h
+4 -6
@@ -1,6 +1,7 @@
1 #ifndef REPOSITORY_H
2 #define REPOSITORY_H
3
4 +#include "refs.h"
5 #include "strmap.h"
6
7 struct config_set;
@@ -26,10 +27,6 @@ enum fetch_negotiation_setting {
27 FETCH_NEGOTIATION_NOOP,
28 };
29
29 -#define REF_STORAGE_FORMAT_UNKNOWN 0
30 -#define REF_STORAGE_FORMAT_FILES 1
31 -#define REF_STORAGE_FORMAT_REFTABLE 2
32 -
30 struct repo_settings {
31 int initialized;
32
@@ -181,7 +178,7 @@ struct repository {
178 const struct git_hash_algo *compat_hash_algo;
179
180 /* Repository's reference storage format, as serialized on disk. */
184 - unsigned int ref_storage_format;
181 + enum ref_storage_format ref_storage_format;
182
183 /* A unique-id for tracing purposes. */
184 int trace2_repo_id;
@@ -220,7 +217,8 @@ void repo_set_gitdir(struct repository *repo, const char *root,
217 void repo_set_worktree(struct repository *repo, const char *path);
218 void repo_set_hash_algo(struct repository *repo, int algo);
219 void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
223 -void repo_set_ref_storage_format(struct repository *repo, unsigned int format);
220 +void repo_set_ref_storage_format(struct repository *repo,
221 + enum ref_storage_format format);
222 void initialize_repository(struct repository *repo);
223 RESULT_MUST_BE_USED
224 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
setup.c
+4 -4
@@ -1997,7 +1997,7 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
1997 }
1998
1999 void initialize_repository_version(int hash_algo,
2000 - unsigned int ref_storage_format,
2000 + enum ref_storage_format ref_storage_format,
2001 int reinit)
2002 {
2003 char repo_version_string[10];
@@ -2044,7 +2044,7 @@ static int is_reinit(void)
2044 return ret;
2045 }
2046
2047 -void create_reference_database(unsigned int ref_storage_format,
2047 +void create_reference_database(enum ref_storage_format ref_storage_format,
2048 const char *initial_branch, int quiet)
2049 {
2050 struct strbuf err = STRBUF_INIT;
@@ -2243,7 +2243,7 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
2243 }
2244
2245 static void validate_ref_storage_format(struct repository_format *repo_fmt,
2246 - unsigned int format)
2246 + enum ref_storage_format format)
2247 {
2248 const char *name = getenv("GIT_DEFAULT_REF_FORMAT");
2249
@@ -2263,7 +2263,7 @@ static void validate_ref_storage_format(struct repository_format *repo_fmt,
2263
2264 int init_db(const char *git_dir, const char *real_git_dir,
2265 const char *template_dir, int hash,
2266 - unsigned int ref_storage_format,
2266 + enum ref_storage_format ref_storage_format,
2267 const char *initial_branch,
2268 int init_shared_repository, unsigned int flags)
2269 {
setup.h
+5 -4
@@ -1,6 +1,7 @@
1 #ifndef SETUP_H
2 #define SETUP_H
3
4 +#include "refs.h"
5 #include "string-list.h"
6
7 int is_inside_git_dir(void);
@@ -128,7 +129,7 @@ struct repository_format {
129 int is_bare;
130 int hash_algo;
131 int compat_hash_algo;
131 - unsigned int ref_storage_format;
132 + enum ref_storage_format ref_storage_format;
133 int sparse_index;
134 char *work_tree;
135 struct string_list unknown_extensions;
@@ -192,13 +193,13 @@ const char *get_template_dir(const char *option_template);
193
194 int init_db(const char *git_dir, const char *real_git_dir,
195 const char *template_dir, int hash_algo,
195 - unsigned int ref_storage_format,
196 + enum ref_storage_format ref_storage_format,
197 const char *initial_branch, int init_shared_repository,
198 unsigned int flags);
199 void initialize_repository_version(int hash_algo,
199 - unsigned int ref_storage_format,
200 + enum ref_storage_format ref_storage_format,
201 int reinit);
201 -void create_reference_database(unsigned int ref_storage_format,
202 +void create_reference_database(enum ref_storage_format ref_storage_format,
203 const char *initial_branch, int quiet);
204
205 /*