[PATCH v2 5/5] builtin/multi-pack-index: de-globalize options handling
- From
Justin Tobler <jltobler@gmail.com>
- Date
- Dec 1, 2026, 18:16 UTC
- Message-ID
- <46a59a5daf5200f840832cbf25430a9a6cbfedad.1796147221.git.jltobler@gmail.com>
- In-Reply-To
- <cover.1796147221.git.jltobler@gmail.com>
The options for git-multi-pack-index(1) are stored in a global variable even though each option is only needed by the subcommand that parses it. The config callback for the "write" and "compact" subcommands also accesses the options, but they can just as well be passed to it via the callback data.
Now that `cmd_multi_pack_index()` no longer parses any of these options itself, nothing requires them to be global anymore. Refactor each subcommand to track its options in a local variable and pass them to the config callback accordingly. With this change, no global state remains in "builtin/multi-pack-index.c".
Signed-off-by: Justin Tobler <jltobler@gmail.com> --- builtin/multi-pack-index.c | 54 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 25 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c index 7dfae23411bc..ac99fec7d3c5 100644 --- a/builtin/multi-pack-index.c +++ b/builtin/multi-pack-index.c @@ -62,7 +62,7 @@ static char const * const builtin_multi_pack_index_usage[] = { NULL }; -static struct opts_multi_pack_index { +struct opts_multi_pack_index { const char *object_dir; const char *preferred_pack; const char *incremental_base; @@ -70,11 +70,11 @@ static struct opts_multi_pack_index { unsigned long batch_size; unsigned flags; int stdin_packs; -} opts; +}; -static struct odb_source_packed *handle_object_dir_option(struct repository *repo) +static struct odb_source_packed *handle_object_dir_option(struct repository *repo, + const char *object_dir) { - const char *object_dir = opts.object_dir; struct odb_files_dir *dir; if (!object_dir) @@ -95,20 +95,22 @@ static struct odb_source_packed *handle_object_dir_option(struct repository *rep static int git_multi_pack_index_write_config(const char *var, const char *value, const struct config_context *ctx UNUSED, - void *cb UNUSED) + void *cb_data) { + struct opts_multi_pack_index *opts = cb_data; + if (!strcmp(var, "pack.writebitmaphashcache")) { if (git_config_bool(var, value)) - opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; + opts->flags |= MIDX_WRITE_BITMAP_HASH_CACHE; else - opts.flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE; + opts->flags &= ~MIDX_WRITE_BITMAP_HASH_CACHE; } if (!strcmp(var, "pack.writebitmaplookuptable")) { if (git_config_bool(var, value)) - opts.flags |= MIDX_WRITE_BITMAP_LOOKUP_TABLE; + opts->flags |= MIDX_WRITE_BITMAP_LOOKUP_TABLE; else - opts.flags &= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE; + opts->flags &= ~MIDX_WRITE_BITMAP_LOOKUP_TABLE; } /* @@ -132,7 +134,8 @@ static int cmd_multi_pack_index_write(int argc, const char **argv, const char *prefix, struct repository *repo) { - static struct option options[] = { + struct opts_multi_pack_index opts = { 0 }; + struct option options[] = { MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_STRING(0, "preferred-pack", &opts.preferred_pack, N_("preferred-pack"), @@ -157,7 +160,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv, opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; - repo_config(repo, git_multi_pack_index_write_config, NULL); + repo_config(repo, git_multi_pack_index_write_config, &opts); trace2_cmd_mode(argv[0]); @@ -185,7 +188,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv, options); } - packed_source = handle_object_dir_option(repo); + packed_source = handle_object_dir_option(repo, opts.object_dir); if (opts.stdin_packs) { struct string_list packs = STRING_LIST_INIT_DUP; @@ -215,13 +218,14 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv, const char *prefix, struct repository *repo) { + struct opts_multi_pack_index opts = { 0 }; struct multi_pack_index *m, *cur; struct multi_pack_index *from_midx = NULL; struct multi_pack_index *to_midx = NULL; struct odb_source_packed *packed_source; int ret; - static struct option options[] = { + struct option options[] = { MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_STRING(0, "base", &opts.incremental_base, N_("checksum"), N_("base MIDX for incremental writes")), @@ -235,7 +239,7 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv, OPT_END(), }; - repo_config(repo, git_multi_pack_index_write_config, NULL); + repo_config(repo, git_multi_pack_index_write_config, &opts); trace2_cmd_mode(argv[0]); @@ -257,7 +261,7 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv, options); } - packed_source = handle_object_dir_option(repo); + packed_source = handle_object_dir_option(repo, opts.object_dir); m = get_multi_pack_index(packed_source); @@ -292,7 +296,8 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv, const char *prefix, struct repository *repo) { - static struct option options[] = { + struct opts_multi_pack_index opts = { 0 }; + struct option options[] = { MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_END(), }; @@ -308,7 +313,7 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv, if (argc) usage_with_options(builtin_multi_pack_index_verify_usage, options); - packed_source = handle_object_dir_option(repo); + packed_source = handle_object_dir_option(repo, opts.object_dir); return verify_midx_file(packed_source, opts.flags); } @@ -317,7 +322,8 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv, const char *prefix, struct repository *repo) { - static struct option options[] = { + struct opts_multi_pack_index opts = { 0 }; + struct option options[] = { MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_END(), }; @@ -333,7 +339,7 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv, if (argc) usage_with_options(builtin_multi_pack_index_expire_usage, options); - packed_source = handle_object_dir_option(repo); + packed_source = handle_object_dir_option(repo, opts.object_dir); return expire_midx_packs(packed_source, opts.flags); } @@ -342,7 +348,8 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv, const char *prefix, struct repository *repo) { - static struct option options[] = { + struct opts_multi_pack_index opts = { 0 }; + struct option options[] = { MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_UNSIGNED(0, "batch-size", &opts.batch_size, N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")), @@ -361,7 +368,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv, if (argc) usage_with_options(builtin_multi_pack_index_repack_usage, options); - packed_source = handle_object_dir_option(repo); + packed_source = handle_object_dir_option(repo, opts.object_dir); return midx_repack(packed_source, (size_t)opts.batch_size, opts.flags); } @@ -371,7 +378,6 @@ int cmd_multi_pack_index(int argc, const char *prefix, struct repository *repo) { - int res; parse_opt_subcommand_fn *fn = NULL; struct option options[] = { OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack), @@ -389,7 +395,5 @@ int cmd_multi_pack_index(int argc, argc = parse_options(argc, argv, prefix, options, builtin_multi_pack_index_usage, 0); - res = fn(argc, argv, prefix, repo); - - return res; + return fn(argc, argv, prefix, repo); }
-- 2.56.0