fsck: initialize fsck options via a function
We initialize the `struct fsck_options` via a set of macros, often in global scope. In the next commit though we're about to introduce a new repository field to the options that must be initialized, and naturally we don't have a repo other than `the_repository` available in this scope. Refactor the code to instead intrdouce a new `fsck_options_init()` function that initializes the options for us and move initialization into function scope. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 23, 2026 at 16:02 UTC
f22360902621e0807a1c0a77476e3e4d323c708d
9 files changed
+81
-38
builtin/fsck.c
+7
-3
@@ -42,8 +42,8 @@ static int check_full = 1;
42
static int connectivity_only;
43
static int check_strict;
44
static int keep_cache_objects;
45
-static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
46
-static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
45
+static struct fsck_options fsck_walk_options;
46
+static struct fsck_options fsck_obj_options;
47
static int errors_found;
48
static int write_lost_and_found;
49
static int verbose;
@@ -224,7 +224,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
224
struct object_info *oi UNUSED,
225
void *data UNUSED)
226
{
227
- struct fsck_options options = FSCK_OPTIONS_DEFAULT;
227
+ struct fsck_options options;
228
struct object *obj = lookup_object(the_repository, oid);
229
230
if (!obj || !(obj->flags & HAS_OBJ))
@@ -243,6 +243,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
243
object_as_type(obj, type, 0);
244
}
245
246
+ fsck_options_init(&options, FSCK_OPTIONS_DEFAULT);
247
options.walk = mark_used;
248
fsck_walk(obj, NULL, &options);
249
if (obj->type == OBJ_TREE)
@@ -1004,7 +1005,10 @@ int cmd_fsck(int argc,
1005
1006
argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1007
1008
+ fsck_options_init(&fsck_walk_options, FSCK_OPTIONS_DEFAULT);
1009
fsck_walk_options.walk = mark_object;
1010
+
1011
+ fsck_options_init(&fsck_obj_options, FSCK_OPTIONS_DEFAULT);
1012
fsck_obj_options.walk = mark_used;
1013
fsck_obj_options.error_func = fsck_objects_error_func;
1014
if (check_strict)
builtin/index-pack.c
+3
-1
@@ -136,7 +136,7 @@ static int nr_threads;
136
static int from_stdin;
137
static int strict;
138
static int do_fsck_object;
139
-static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
139
+static struct fsck_options fsck_options;
140
static int verbose;
141
static const char *progress_title;
142
static int show_resolving_progress;
@@ -1908,6 +1908,8 @@ int cmd_index_pack(int argc,
1908
show_usage_if_asked(argc, argv, index_pack_usage);
1909
1910
disable_replace_refs();
1911
+
1912
+ fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
1913
fsck_options.walk = mark_link;
1914
1915
reset_pack_idx_option(&opts);
builtin/mktag.c
+2
-1
@@ -16,7 +16,7 @@ static char const * const builtin_mktag_usage[] = {
16
};
17
static int option_strict = 1;
18
19
-static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
19
+static struct fsck_options fsck_options;
20
21
static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
22
void *fsck_report UNUSED,
@@ -94,6 +94,7 @@ int cmd_mktag(int argc,
94
if (strbuf_read(&buf, 0, 0) < 0)
95
die_errno(_("could not read from stdin"));
96
97
+ fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
98
fsck_options.error_func = mktag_fsck_error_func;
99
fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
100
FSCK_WARN);
builtin/refs.c
+3
-1
@@ -80,7 +80,7 @@ out:
80
static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
81
struct repository *repo UNUSED)
82
{
83
- struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
83
+ struct fsck_options fsck_refs_options;
84
struct worktree **worktrees;
85
const char * const verify_usage[] = {
86
REFS_VERIFY_USAGE,
@@ -93,6 +93,8 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
93
};
94
int ret = 0;
95
96
+ fsck_options_init(&fsck_refs_options, FSCK_OPTIONS_REFS);
97
+
98
argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
99
if (argc)
100
usage(_("'git refs verify' takes no arguments"));
builtin/unpack-objects.c
+3
-1
@@ -29,7 +29,7 @@ static unsigned int offset, len;
29
static off_t consumed_bytes;
30
static off_t max_input_size;
31
static struct git_hash_ctx ctx;
32
-static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
32
+static struct fsck_options fsck_options;
33
static struct progress *progress;
34
35
/*
@@ -627,6 +627,8 @@ int cmd_unpack_objects(int argc,
627
628
show_usage_if_asked(argc, argv, unpack_usage);
629
630
+ fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
631
+
632
for (i = 1 ; i < argc; i++) {
633
const char *arg = argv[i];
634
fetch-pack.c
+6
-2
@@ -1099,7 +1099,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1099
struct shallow_info *si,
1100
struct string_list *pack_lockfiles)
1101
{
1102
- struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
1102
+ struct fsck_options fsck_options = { 0 };
1103
struct repository *r = the_repository;
1104
struct ref *ref = copy_ref_list(orig_ref);
1105
struct object_id oid;
@@ -1228,6 +1228,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1228
alternate_shallow_file = setup_temporary_shallow(si->shallow);
1229
} else
1230
alternate_shallow_file = NULL;
1231
+
1232
+ fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
1233
if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
1234
&fsck_options.gitmodules_found))
1235
die(_("git fetch-pack: fetch failed."));
@@ -1655,7 +1657,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1657
struct string_list *pack_lockfiles)
1658
{
1659
struct repository *r = the_repository;
1658
- struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
1660
+ struct fsck_options fsck_options;
1661
struct ref *ref = copy_ref_list(orig_ref);
1662
enum fetch_state state = FETCH_CHECK_LOCAL;
1663
struct oidset common = OIDSET_INIT;
@@ -1673,6 +1675,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1675
struct strvec index_pack_args = STRVEC_INIT;
1676
const char *promisor_remote_config;
1677
1678
+ fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
1679
+
1680
if (server_feature_v2("promisor-remote", &promisor_remote_config))
1681
promisor_remote_reply(promisor_remote_config, NULL);
1682
fsck.c
+45
@@ -1380,6 +1380,51 @@ bool fsck_has_queued_checks(struct fsck_options *options)
1380
!oidset_equal(&options->gitattributes_found, &options->gitattributes_done);
1381
}
1382
1383
+void fsck_options_init(struct fsck_options *options,
1384
+ enum fsck_options_type type)
1385
+{
1386
+ static const struct fsck_options defaults[] = {
1387
+ [FSCK_OPTIONS_DEFAULT] = {
1388
+ .skip_oids = OIDSET_INIT,
1389
+ .gitmodules_found = OIDSET_INIT,
1390
+ .gitmodules_done = OIDSET_INIT,
1391
+ .gitattributes_found = OIDSET_INIT,
1392
+ .gitattributes_done = OIDSET_INIT,
1393
+ .error_func = fsck_objects_error_function
1394
+ },
1395
+ [FSCK_OPTIONS_STRICT] = {
1396
+ .strict = 1,
1397
+ .gitmodules_found = OIDSET_INIT,
1398
+ .gitmodules_done = OIDSET_INIT,
1399
+ .gitattributes_found = OIDSET_INIT,
1400
+ .gitattributes_done = OIDSET_INIT,
1401
+ .error_func = fsck_objects_error_function,
1402
+ },
1403
+ [FSCK_OPTIONS_MISSING_GITMODULES] = {
1404
+ .strict = 1,
1405
+ .gitmodules_found = OIDSET_INIT,
1406
+ .gitmodules_done = OIDSET_INIT,
1407
+ .gitattributes_found = OIDSET_INIT,
1408
+ .gitattributes_done = OIDSET_INIT,
1409
+ .error_func = fsck_objects_error_cb_print_missing_gitmodules,
1410
+ },
1411
+ [FSCK_OPTIONS_REFS] = {
1412
+ .error_func = fsck_refs_error_function,
1413
+ },
1414
+ };
1415
+
1416
+ switch (type) {
1417
+ case FSCK_OPTIONS_DEFAULT:
1418
+ case FSCK_OPTIONS_STRICT:
1419
+ case FSCK_OPTIONS_MISSING_GITMODULES:
1420
+ case FSCK_OPTIONS_REFS:
1421
+ memcpy(options, &defaults[type], sizeof(*options));
1422
+ break;
1423
+ default:
1424
+ BUG("unknown fsck options type %d", type);
1425
+ }
1426
+}
1427
+
1428
void fsck_options_clear(struct fsck_options *options)
1429
{
1430
free(options->msg_type);
fsck.h
+10
-28
@@ -180,34 +180,6 @@ struct fsck_options {
180
kh_oid_map_t *object_names;
181
};
182
183
-#define FSCK_OPTIONS_DEFAULT { \
184
- .skip_oids = OIDSET_INIT, \
185
- .gitmodules_found = OIDSET_INIT, \
186
- .gitmodules_done = OIDSET_INIT, \
187
- .gitattributes_found = OIDSET_INIT, \
188
- .gitattributes_done = OIDSET_INIT, \
189
- .error_func = fsck_objects_error_function \
190
-}
191
-#define FSCK_OPTIONS_STRICT { \
192
- .strict = 1, \
193
- .gitmodules_found = OIDSET_INIT, \
194
- .gitmodules_done = OIDSET_INIT, \
195
- .gitattributes_found = OIDSET_INIT, \
196
- .gitattributes_done = OIDSET_INIT, \
197
- .error_func = fsck_objects_error_function, \
198
-}
199
-#define FSCK_OPTIONS_MISSING_GITMODULES { \
200
- .strict = 1, \
201
- .gitmodules_found = OIDSET_INIT, \
202
- .gitmodules_done = OIDSET_INIT, \
203
- .gitattributes_found = OIDSET_INIT, \
204
- .gitattributes_done = OIDSET_INIT, \
205
- .error_func = fsck_objects_error_cb_print_missing_gitmodules, \
206
-}
207
-#define FSCK_REFS_OPTIONS_DEFAULT { \
208
- .error_func = fsck_refs_error_function, \
209
-}
210
-
183
/* descend in all linked child objects
184
* the return value is:
185
* -1 error in processing the object
@@ -255,6 +227,16 @@ int fsck_finish(struct fsck_options *options);
227
*/
228
bool fsck_has_queued_checks(struct fsck_options *options);
229
230
+enum fsck_options_type {
231
+ FSCK_OPTIONS_DEFAULT,
232
+ FSCK_OPTIONS_STRICT,
233
+ FSCK_OPTIONS_MISSING_GITMODULES,
234
+ FSCK_OPTIONS_REFS,
235
+};
236
+
237
+void fsck_options_init(struct fsck_options *options,
238
+ enum fsck_options_type type);
239
+
240
/*
241
* Clear the fsck_options struct, freeing any allocated memory.
242
*/
object-file.c
+2
-1
@@ -1279,8 +1279,9 @@ static int index_mem(struct index_state *istate,
1279
}
1280
}
1281
if (flags & INDEX_FORMAT_CHECK) {
1282
- struct fsck_options opts = FSCK_OPTIONS_DEFAULT;
1282
+ struct fsck_options opts;
1283
1284
+ fsck_options_init(&opts, FSCK_OPTIONS_DEFAULT);
1285
opts.strict = 1;
1286
opts.error_func = hash_format_check_report;
1287
if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))