]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: initialize fsck options via a function
authorPatrick Steinhardt <ps@pks.im>
Mon, 23 Mar 2026 15:02:53 +0000 (16:02 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Mar 2026 15:33:10 +0000 (08:33 -0700)
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>
builtin/fsck.c
builtin/index-pack.c
builtin/mktag.c
builtin/refs.c
builtin/unpack-objects.c
fetch-pack.c
fsck.c
fsck.h
object-file.c

index 9bab32effed7eca461559d37dd002eb5f6d83cd3..59e3b0f7ac28323e78bc356c7aefebd9ec996fdf 100644 (file)
@@ -42,8 +42,8 @@ static int check_full = 1;
 static int connectivity_only;
 static int check_strict;
 static int keep_cache_objects;
-static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
-static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
+static struct fsck_options fsck_walk_options;
+static struct fsck_options fsck_obj_options;
 static int errors_found;
 static int write_lost_and_found;
 static int verbose;
@@ -224,7 +224,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
                                      struct object_info *oi UNUSED,
                                      void *data UNUSED)
 {
-       struct fsck_options options = FSCK_OPTIONS_DEFAULT;
+       struct fsck_options options;
        struct object *obj = lookup_object(the_repository, oid);
 
        if (!obj || !(obj->flags & HAS_OBJ))
@@ -243,6 +243,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
                        object_as_type(obj, type, 0);
        }
 
+       fsck_options_init(&options, FSCK_OPTIONS_DEFAULT);
        options.walk = mark_used;
        fsck_walk(obj, NULL, &options);
        if (obj->type == OBJ_TREE)
@@ -1004,7 +1005,10 @@ int cmd_fsck(int argc,
 
        argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
 
+       fsck_options_init(&fsck_walk_options, FSCK_OPTIONS_DEFAULT);
        fsck_walk_options.walk = mark_object;
+
+       fsck_options_init(&fsck_obj_options, FSCK_OPTIONS_DEFAULT);
        fsck_obj_options.walk = mark_used;
        fsck_obj_options.error_func = fsck_objects_error_func;
        if (check_strict)
index d1e47279a8c7c94720d99fc85d1a2da1b0380a5b..c8d28bcf8e2e1422756af57cb0346022e2853ec4 100644 (file)
@@ -136,7 +136,7 @@ static int nr_threads;
 static int from_stdin;
 static int strict;
 static int do_fsck_object;
-static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
+static struct fsck_options fsck_options;
 static int verbose;
 static const char *progress_title;
 static int show_resolving_progress;
@@ -1908,6 +1908,8 @@ int cmd_index_pack(int argc,
        show_usage_if_asked(argc, argv, index_pack_usage);
 
        disable_replace_refs();
+
+       fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
        fsck_options.walk = mark_link;
 
        reset_pack_idx_option(&opts);
index 7cf6e1230ac450a0d3bf5b3fe839e9bda37160df..9f37f9dede7a02c3dacb2c1c2c925bcdecbed884 100644 (file)
@@ -16,7 +16,7 @@ static char const * const builtin_mktag_usage[] = {
 };
 static int option_strict = 1;
 
-static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
+static struct fsck_options fsck_options;
 
 static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
                                 void *fsck_report UNUSED,
@@ -94,6 +94,7 @@ int cmd_mktag(int argc,
        if (strbuf_read(&buf, 0, 0) < 0)
                die_errno(_("could not read from stdin"));
 
+       fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
        fsck_options.error_func = mktag_fsck_error_func;
        fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
                                   FSCK_WARN);
index 3064f888b24304ce07dba107be137ad8d420e731..1719ada54906877cd20355f626bae59804729255 100644 (file)
@@ -80,7 +80,7 @@ out:
 static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
                           struct repository *repo UNUSED)
 {
-       struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
+       struct fsck_options fsck_refs_options;
        struct worktree **worktrees;
        const char * const verify_usage[] = {
                REFS_VERIFY_USAGE,
@@ -93,6 +93,8 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
        };
        int ret = 0;
 
+       fsck_options_init(&fsck_refs_options, FSCK_OPTIONS_REFS);
+
        argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
        if (argc)
                usage(_("'git refs verify' takes no arguments"));
index 6fc64e9e4b8d5af9586a445198eb244afb870911..9e4bb9d25cf98a21bdebb6d73794b7220bad5782 100644 (file)
@@ -29,7 +29,7 @@ static unsigned int offset, len;
 static off_t consumed_bytes;
 static off_t max_input_size;
 static struct git_hash_ctx ctx;
-static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
+static struct fsck_options fsck_options;
 static struct progress *progress;
 
 /*
@@ -627,6 +627,8 @@ int cmd_unpack_objects(int argc,
 
        show_usage_if_asked(argc, argv, unpack_usage);
 
+       fsck_options_init(&fsck_options, FSCK_OPTIONS_STRICT);
+
        for (i = 1 ; i < argc; i++) {
                const char *arg = argv[i];
 
index ec5abb92b5b763ec1b56696fb359c5e2b4e2f5c1..733916236897f3ea691b6ab09de8429530c5caeb 100644 (file)
@@ -1099,7 +1099,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
                                 struct shallow_info *si,
                                 struct string_list *pack_lockfiles)
 {
-       struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
+       struct fsck_options fsck_options = { 0 };
        struct repository *r = the_repository;
        struct ref *ref = copy_ref_list(orig_ref);
        struct object_id oid;
@@ -1228,6 +1228,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
                alternate_shallow_file = setup_temporary_shallow(si->shallow);
        } else
                alternate_shallow_file = NULL;
+
+       fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
        if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
                     &fsck_options.gitmodules_found))
                die(_("git fetch-pack: fetch failed."));
@@ -1655,7 +1657,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
                                    struct string_list *pack_lockfiles)
 {
        struct repository *r = the_repository;
-       struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
+       struct fsck_options fsck_options;
        struct ref *ref = copy_ref_list(orig_ref);
        enum fetch_state state = FETCH_CHECK_LOCAL;
        struct oidset common = OIDSET_INIT;
@@ -1673,6 +1675,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
        struct strvec index_pack_args = STRVEC_INIT;
        const char *promisor_remote_config;
 
+       fsck_options_init(&fsck_options, FSCK_OPTIONS_MISSING_GITMODULES);
+
        if (server_feature_v2("promisor-remote", &promisor_remote_config))
                promisor_remote_reply(promisor_remote_config, NULL);
 
diff --git a/fsck.c b/fsck.c
index 0f02cf8f77bf1a19ddb4322e5982ca3c744d255b..1ff82085029661d5491edaae291089897dff7858 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -1380,6 +1380,51 @@ bool fsck_has_queued_checks(struct fsck_options *options)
               !oidset_equal(&options->gitattributes_found, &options->gitattributes_done);
 }
 
+void fsck_options_init(struct fsck_options *options,
+                      enum fsck_options_type type)
+{
+       static const struct fsck_options defaults[] = {
+               [FSCK_OPTIONS_DEFAULT] = {
+                       .skip_oids = OIDSET_INIT,
+                       .gitmodules_found = OIDSET_INIT,
+                       .gitmodules_done = OIDSET_INIT,
+                       .gitattributes_found = OIDSET_INIT,
+                       .gitattributes_done = OIDSET_INIT,
+                       .error_func = fsck_objects_error_function
+               },
+               [FSCK_OPTIONS_STRICT] = {
+                       .strict = 1,
+                       .gitmodules_found = OIDSET_INIT,
+                       .gitmodules_done = OIDSET_INIT,
+                       .gitattributes_found = OIDSET_INIT,
+                       .gitattributes_done = OIDSET_INIT,
+                       .error_func = fsck_objects_error_function,
+               },
+               [FSCK_OPTIONS_MISSING_GITMODULES] = {
+                       .strict = 1,
+                       .gitmodules_found = OIDSET_INIT,
+                       .gitmodules_done = OIDSET_INIT,
+                       .gitattributes_found = OIDSET_INIT,
+                       .gitattributes_done = OIDSET_INIT,
+                       .error_func = fsck_objects_error_cb_print_missing_gitmodules,
+               },
+               [FSCK_OPTIONS_REFS] = {
+                       .error_func = fsck_refs_error_function,
+               },
+       };
+
+       switch (type) {
+       case FSCK_OPTIONS_DEFAULT:
+       case FSCK_OPTIONS_STRICT:
+       case FSCK_OPTIONS_MISSING_GITMODULES:
+       case FSCK_OPTIONS_REFS:
+               memcpy(options, &defaults[type], sizeof(*options));
+               break;
+       default:
+               BUG("unknown fsck options type %d", type);
+       }
+}
+
 void fsck_options_clear(struct fsck_options *options)
 {
        free(options->msg_type);
diff --git a/fsck.h b/fsck.h
index 65ecbb7fe194ab5beceb81383b2f219b012b8395..9c973b53b20cd4cfa14eb5475f72d3fa64032ea7 100644 (file)
--- a/fsck.h
+++ b/fsck.h
@@ -180,34 +180,6 @@ struct fsck_options {
        kh_oid_map_t *object_names;
 };
 
-#define FSCK_OPTIONS_DEFAULT { \
-       .skip_oids = OIDSET_INIT, \
-       .gitmodules_found = OIDSET_INIT, \
-       .gitmodules_done = OIDSET_INIT, \
-       .gitattributes_found = OIDSET_INIT, \
-       .gitattributes_done = OIDSET_INIT, \
-       .error_func = fsck_objects_error_function \
-}
-#define FSCK_OPTIONS_STRICT { \
-       .strict = 1, \
-       .gitmodules_found = OIDSET_INIT, \
-       .gitmodules_done = OIDSET_INIT, \
-       .gitattributes_found = OIDSET_INIT, \
-       .gitattributes_done = OIDSET_INIT, \
-       .error_func = fsck_objects_error_function, \
-}
-#define FSCK_OPTIONS_MISSING_GITMODULES { \
-       .strict = 1, \
-       .gitmodules_found = OIDSET_INIT, \
-       .gitmodules_done = OIDSET_INIT, \
-       .gitattributes_found = OIDSET_INIT, \
-       .gitattributes_done = OIDSET_INIT, \
-       .error_func = fsck_objects_error_cb_print_missing_gitmodules, \
-}
-#define FSCK_REFS_OPTIONS_DEFAULT { \
-       .error_func = fsck_refs_error_function, \
-}
-
 /* descend in all linked child objects
  * the return value is:
  *    -1       error in processing the object
@@ -255,6 +227,16 @@ int fsck_finish(struct fsck_options *options);
  */
 bool fsck_has_queued_checks(struct fsck_options *options);
 
+enum fsck_options_type {
+       FSCK_OPTIONS_DEFAULT,
+       FSCK_OPTIONS_STRICT,
+       FSCK_OPTIONS_MISSING_GITMODULES,
+       FSCK_OPTIONS_REFS,
+};
+
+void fsck_options_init(struct fsck_options *options,
+                      enum fsck_options_type type);
+
 /*
  * Clear the fsck_options struct, freeing any allocated memory.
  */
index c62e5496e0795dfaa96d7d6cbdc62b8d46731335..186b2ff764a6648a16f0af24d3cf959ea98bd9f9 100644 (file)
@@ -1279,8 +1279,9 @@ static int index_mem(struct index_state *istate,
                }
        }
        if (flags & INDEX_FORMAT_CHECK) {
-               struct fsck_options opts = FSCK_OPTIONS_DEFAULT;
+               struct fsck_options opts;
 
+               fsck_options_init(&opts, FSCK_OPTIONS_DEFAULT);
                opts.strict = 1;
                opts.error_func = hash_format_check_report;
                if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))