Skip to content

Commit f223609

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 17cabd3 commit f223609

9 files changed

Lines changed: 81 additions & 38 deletions

File tree

builtin/fsck.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ static int check_full = 1;
4242
static int connectivity_only;
4343
static int check_strict;
4444
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;
4747
static int errors_found;
4848
static int write_lost_and_found;
4949
static int verbose;
@@ -224,7 +224,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
224224
struct object_info *oi UNUSED,
225225
void *data UNUSED)
226226
{
227-
struct fsck_options options = FSCK_OPTIONS_DEFAULT;
227+
struct fsck_options options;
228228
struct object *obj = lookup_object(the_repository, oid);
229229

230230
if (!obj || !(obj->flags & HAS_OBJ))
@@ -243,6 +243,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
243243
object_as_type(obj, type, 0);
244244
}
245245

246+
fsck_options_init(&options, FSCK_OPTIONS_DEFAULT);
246247
options.walk = mark_used;
247248
fsck_walk(obj, NULL, &options);
248249
if (obj->type == OBJ_TREE)
@@ -1004,7 +1005,10 @@ int cmd_fsck(int argc,
10041005

10051006
argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
10061007

1008+
fsck_options_init(&fsck_walk_options, FSCK_OPTIONS_DEFAULT);
10071009
fsck_walk_options.walk = mark_object;
1010+
1011+
fsck_options_init(&fsck_obj_options, FSCK_OPTIONS_DEFAULT);
10081012
fsck_obj_options.walk = mark_used;
10091013
fsck_obj_options.error_func = fsck_objects_error_func;
10101014
if (check_strict)

builtin/index-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int nr_threads;
136136
static int from_stdin;
137137
static int strict;
138138
static int do_fsck_object;
139-
static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
139+
static struct fsck_options fsck_options;
140140
static int verbose;
141141
static const char *progress_title;
142142
static int show_resolving_progress;
@@ -1908,6 +1908,8 @@ int cmd_index_pack(int argc,
19081908
show_usage_if_asked(argc, argv, index_pack_usage);
19091909

19101910
disable_replace_refs();
1911+
1912+
fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
19111913
fsck_options.walk = mark_link;
19121914

19131915
reset_pack_idx_option(&opts);

builtin/mktag.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static char const * const builtin_mktag_usage[] = {
1616
};
1717
static int option_strict = 1;
1818

19-
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
19+
static struct fsck_options fsck_options;
2020

2121
static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
2222
void *fsck_report UNUSED,
@@ -94,6 +94,7 @@ int cmd_mktag(int argc,
9494
if (strbuf_read(&buf, 0, 0) < 0)
9595
die_errno(_("could not read from stdin"));
9696

97+
fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
9798
fsck_options.error_func = mktag_fsck_error_func;
9899
fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
99100
FSCK_WARN);

builtin/refs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
8080
static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
8181
struct repository *repo UNUSED)
8282
{
83-
struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
83+
struct fsck_options fsck_refs_options;
8484
struct worktree **worktrees;
8585
const char * const verify_usage[] = {
8686
REFS_VERIFY_USAGE,
@@ -93,6 +93,8 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
9393
};
9494
int ret = 0;
9595

96+
fsck_options_init(&fsck_refs_options, FSCK_OPTIONS_REFS);
97+
9698
argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
9799
if (argc)
98100
usage(_("'git refs verify' takes no arguments"));

builtin/unpack-objects.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static unsigned int offset, len;
2929
static off_t consumed_bytes;
3030
static off_t max_input_size;
3131
static struct git_hash_ctx ctx;
32-
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
32+
static struct fsck_options fsck_options;
3333
static struct progress *progress;
3434

3535
/*
@@ -627,6 +627,8 @@ int cmd_unpack_objects(int argc,
627627

628628
show_usage_if_asked(argc, argv, unpack_usage);
629629

630+
fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
631+
630632
for (i = 1 ; i < argc; i++) {
631633
const char *arg = argv[i];
632634

fetch-pack.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
10991099
struct shallow_info *si,
11001100
struct string_list *pack_lockfiles)
11011101
{
1102-
struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
1102+
struct fsck_options fsck_options = { 0 };
11031103
struct repository *r = the_repository;
11041104
struct ref *ref = copy_ref_list(orig_ref);
11051105
struct object_id oid;
@@ -1228,6 +1228,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
12281228
alternate_shallow_file = setup_temporary_shallow(si->shallow);
12291229
} else
12301230
alternate_shallow_file = NULL;
1231+
1232+
fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
12311233
if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
12321234
&fsck_options.gitmodules_found))
12331235
die(_("git fetch-pack: fetch failed."));
@@ -1655,7 +1657,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
16551657
struct string_list *pack_lockfiles)
16561658
{
16571659
struct repository *r = the_repository;
1658-
struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
1660+
struct fsck_options fsck_options;
16591661
struct ref *ref = copy_ref_list(orig_ref);
16601662
enum fetch_state state = FETCH_CHECK_LOCAL;
16611663
struct oidset common = OIDSET_INIT;
@@ -1673,6 +1675,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
16731675
struct strvec index_pack_args = STRVEC_INIT;
16741676
const char *promisor_remote_config;
16751677

1678+
fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
1679+
16761680
if (server_feature_v2("promisor-remote", &promisor_remote_config))
16771681
promisor_remote_reply(promisor_remote_config, NULL);
16781682

fsck.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,51 @@ bool fsck_has_queued_checks(struct fsck_options *options)
13801380
!oidset_equal(&options->gitattributes_found, &options->gitattributes_done);
13811381
}
13821382

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+
13831428
void fsck_options_clear(struct fsck_options *options)
13841429
{
13851430
free(options->msg_type);

fsck.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,34 +180,6 @@ struct fsck_options {
180180
kh_oid_map_t *object_names;
181181
};
182182

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-
211183
/* descend in all linked child objects
212184
* the return value is:
213185
* -1 error in processing the object
@@ -255,6 +227,16 @@ int fsck_finish(struct fsck_options *options);
255227
*/
256228
bool fsck_has_queued_checks(struct fsck_options *options);
257229

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+
258240
/*
259241
* Clear the fsck_options struct, freeing any allocated memory.
260242
*/

object-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,9 @@ static int index_mem(struct index_state *istate,
12791279
}
12801280
}
12811281
if (flags & INDEX_FORMAT_CHECK) {
1282-
struct fsck_options opts = FSCK_OPTIONS_DEFAULT;
1282+
struct fsck_options opts;
12831283

1284+
fsck_options_init(&opts, FSCK_OPTIONS_DEFAULT);
12841285
opts.strict = 1;
12851286
opts.error_func = hash_format_check_report;
12861287
if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))

0 commit comments

Comments
 (0)