From: Justin Tobler Date: Tue, 01 Dec 2026 18:16:11 GMT Subject: [PATCH v2 0/5] builtin/multi-pack-index: remove global state Message-ID: In-Reply-To: Greetings, This series cleans up git-multi-pack-index(1) by getting rid of `the_repository` and the global state used to track its options. Each subcommand now parses its options into a local structure, and the common options are wired up via a macro instead of allocating a combined options array for every subcommand. The main reason the options are global is that "--object-dir=" and "--[no-]progress" are also accepted before the subcommand, where the top-level builtin parses them into the same global state used by the subcommands. Patch 3 drops support for giving these options before the subcommand, as every subcommand already accepts both of them. The "--[no-]progress" option was previously removed as a top-level option in 0394f8d002 (builtin/multi-pack-index.c: disable top-level --[no-]progress, 2021-09-20) and appears to have been inadvertently reintroduced via 6b8fb17490 (builtin/multi-pack-index.c: make '--progress' a common option, 2026-02-24). So I think it should be fine to drop. While "--object-dir=" has never previously been dropped, it appears to exist as a top-level option for the same historical reason, from when all options were parsed by the top-level builtin. Since the subcommands already parse the common options themselves, it should ideally be dropped as well. If we want to keep this top-level option though, an alternative could be to propagate the option argument to each subcommand. In this case, I don't think it's worth it, but happy to change if others feel differently. This series is built on top of 86e79b2795 (The 26th batch, 2026-11-29). Changes since V1: - "--object-dir=" is now parsed via `OPT_STRING()`, so the option value no longer needs to be allocated or freed. - Clarified in the message of patch 2 that the repository may still be `NULL` until `parse_options()` has been called. - The synopsis and usage strings now explicitly list the common options instead of using the `[]` placeholder. - The common options are now added at the front of each options array, so the order of the "-h" output stays the same. Thanks for the review, -Justin Justin Tobler (5): builtin/multi-pack-index: resolve default object directory lazily builtin/multi-pack-index: get rid of `the_repository` builtin/multi-pack-index: drop top-level options builtin/multi-pack-index: simplify how common options are wired up builtin/multi-pack-index: de-globalize options handling Documentation/git-multi-pack-index.adoc | 23 ++-- builtin/multi-pack-index.c | 165 +++++++++--------------- t/lib-midx.sh | 2 +- t/t5319-multi-pack-index.sh | 32 ++--- 4 files changed, 91 insertions(+), 131 deletions(-) Range-diff against v1: 1: c381afb0da59 ! 1: 3e6369b99ba3 builtin/multi-pack-index: resolve default object directory lazily @@ Commit message where the repository is passed in explicitly. This also removes the need for the defensive `NULL` checks guarding the eager initialization. + With the default handled lazily, the callback for "--object-dir=" is + only left to resolve the given path via `real_pathdup()`. This is + unnecessary though, as `odb_source_files_find_dir()` already resolves + the path itself when looking up the matching object directory. Replace + the callback with a plain `OPT_STRING()`, which also means that the + option value no longer has to be allocated and freed. + Signed-off-by: Justin Tobler ## builtin/multi-pack-index.c ## -@@ builtin/multi-pack-index.c: static int parse_object_dir(const struct option *opt, const char *arg, - char **value = opt->value; - free(*value); - if (unset) -- *value = xstrdup(the_repository->objects->source->path); -+ *value = NULL; - else - *value = real_pathdup(arg, 1); - return 0; -@@ builtin/multi-pack-index.c: static int parse_object_dir(const struct option *opt, const char *arg, +@@ + #define USE_THE_REPOSITORY_VARIABLE + #include "builtin.h" +-#include "abspath.h" + #include "config.h" + #include "environment.h" + #include "gettext.h" +@@ builtin/multi-pack-index.c: static char const * const builtin_multi_pack_index_usage[] = { + }; + + static struct opts_multi_pack_index { +- char *object_dir; ++ const char *object_dir; + const char *preferred_pack; + const char *incremental_base; + char *refs_snapshot; +@@ builtin/multi-pack-index.c: static struct opts_multi_pack_index { + int stdin_packs; + } opts; +- +-static int parse_object_dir(const struct option *opt, const char *arg, +- int unset) +-{ +- char **value = opt->value; +- free(*value); +- if (unset) +- *value = xstrdup(the_repository->objects->source->path); +- else +- *value = real_pathdup(arg, 1); +- return 0; +-} +- static struct odb_source_packed *handle_object_dir_option(struct repository *repo) { - struct odb_files_dir *dir = odb_source_files_find_dir(repo->objects, opts.object_dir); @@ builtin/multi-pack-index.c: static int parse_object_dir(const struct option *opt return dir->packed; } + static struct option common_opts[] = { +- OPT_CALLBACK(0, "object-dir", &opts.object_dir, +- N_("directory"), +- N_("object directory containing set of packfile and pack-index pairs"), +- parse_object_dir), ++ OPT_STRING(0, "object-dir", &opts.object_dir, N_("directory"), ++ N_("object directory containing set of packfile and pack-index pairs")), + OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), + MIDX_PROGRESS), + OPT_END(), @@ builtin/multi-pack-index.c: int cmd_multi_pack_index(int argc, repo_config(the_repository, git_default_config, NULL); @@ builtin/multi-pack-index.c: int cmd_multi_pack_index(int argc, argc = parse_options(argc, argv, prefix, options, builtin_multi_pack_index_usage, 0); FREE_AND_NULL(options); + + res = fn(argc, argv, prefix, repo); + +- free(opts.object_dir); + return res; + } 2: 673945d65de3 ! 2: 28d7916f0a24 builtin/multi-pack-index: get rid of `the_repository` @@ Commit message builtin/multi-pack-index: get rid of `the_repository` There are a couple of remaining references to `the_repository` in - git-multi-pack-index(1). The command cannot run outside of a repository, - so these references can be trivially replaced with the repository - passed to the subcommands. + git-multi-pack-index(1). Replace them with the repository that is passed + to the builtin and its subcommands. + + Note that the repository may still be `NULL` until `parse_options()` has + been called. The only use of the repository before that point is + `repo_config()`, which handles a `NULL` repository. Signed-off-by: Justin Tobler @@ builtin/multi-pack-index.c @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" - #include "abspath.h" #include "config.h" + #include "environment.h" @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; 3: cc2876d6803f ! 3: 5fe18c5564c3 builtin/multi-pack-index: drop top-level options @@ Documentation/git-multi-pack-index.adoc: git-multi-pack-index - Write and verify -------- [verse] -'git multi-pack-index' [] write [--preferred-pack=] -+'git multi-pack-index' write [] [--preferred-pack=] - [--[no-]bitmap] [--[no-]incremental] [--[no-]stdin-packs] - [--refs-snapshot=] [--[no-]write-chain-file] - [--base=] +- [--[no-]bitmap] [--[no-]incremental] [--[no-]stdin-packs] +- [--refs-snapshot=] [--[no-]write-chain-file] +- [--base=] -'git multi-pack-index' [] compact [--[no-]incremental] -+'git multi-pack-index' compact [] [--[no-]incremental] - [--[no-]bitmap] [--base=] [--[no-]write-chain-file] - +- [--[no-]bitmap] [--base=] [--[no-]write-chain-file] +- -'git multi-pack-index' [] verify -'git multi-pack-index' [] expire -'git multi-pack-index' [] repack [--batch-size=] -+'git multi-pack-index' verify [] -+'git multi-pack-index' expire [] -+'git multi-pack-index' repack [] [--batch-size=] ++'git multi-pack-index' write [--object-dir=] [--[no-]progress] ++ [--preferred-pack=] [--[no-]bitmap] [--[no-]incremental] ++ [--[no-]stdin-packs] [--refs-snapshot=] ++ [--[no-]write-chain-file] [--base=] ++'git multi-pack-index' compact [--object-dir=] [--[no-]progress] ++ [--[no-]incremental] [--[no-]bitmap] [--base=] ++ [--[no-]write-chain-file] ++'git multi-pack-index' verify [--object-dir=] [--[no-]progress] ++'git multi-pack-index' expire [--object-dir=] [--[no-]progress] ++'git multi-pack-index' repack [--object-dir=] [--[no-]progress] ++ [--batch-size=] DESCRIPTION ----------- @@ builtin/multi-pack-index.c #define BUILTIN_MIDX_WRITE_USAGE \ - N_("git multi-pack-index [] write [--preferred-pack=]\n" \ -+ N_("git multi-pack-index write [] [--preferred-pack=]\n" \ - " [--[no-]bitmap] [--[no-]incremental] [--[no-]stdin-packs]\n" \ - " [--refs-snapshot=] [--[no-]write-chain-file]\n" \ - " [--base=]") +- " [--[no-]bitmap] [--[no-]incremental] [--[no-]stdin-packs]\n" \ +- " [--refs-snapshot=] [--[no-]write-chain-file]\n" \ +- " [--base=]") ++ N_("git multi-pack-index write [--object-dir=] [--[no-]progress]\n" \ ++ " [--preferred-pack=] [--[no-]bitmap] [--[no-]incremental]\n" \ ++ " [--[no-]stdin-packs] [--refs-snapshot=]\n" \ ++ " [--[no-]write-chain-file] [--base=]") #define BUILTIN_MIDX_COMPACT_USAGE \ - N_("git multi-pack-index [] compact [--[no-]incremental]\n" \ -+ N_("git multi-pack-index compact [] [--[no-]incremental]\n" \ - " [--[no-]bitmap] [--base=] [--[no-]write-chain-file]\n" \ - " ") +- " [--[no-]bitmap] [--base=] [--[no-]write-chain-file]\n" \ +- " ") ++ N_("git multi-pack-index compact [--object-dir=] [--[no-]progress]\n" \ ++ " [--[no-]incremental] [--[no-]bitmap] [--base=]\n" \ ++ " [--[no-]write-chain-file] ") #define BUILTIN_MIDX_VERIFY_USAGE \ - N_("git multi-pack-index [] verify") -+ N_("git multi-pack-index verify []") ++ N_("git multi-pack-index verify [--object-dir=] [--[no-]progress]") #define BUILTIN_MIDX_EXPIRE_USAGE \ - N_("git multi-pack-index [] expire") -+ N_("git multi-pack-index expire []") ++ N_("git multi-pack-index expire [--object-dir=] [--[no-]progress]") #define BUILTIN_MIDX_REPACK_USAGE \ - N_("git multi-pack-index [] repack [--batch-size=]") -+ N_("git multi-pack-index repack [] [--batch-size=]") ++ N_("git multi-pack-index repack [--object-dir=] [--[no-]progress]\n" \ ++ " [--batch-size=]") static char const * const builtin_multi_pack_index_write_usage[] = { BUILTIN_MIDX_WRITE_USAGE, 4: 1c16a0988239 ! 4: 3bb2b9a69f1c builtin/multi-pack-index: simplify how common options are wired up @@ builtin/multi-pack-index.c: static struct odb_source_packed *handle_object_dir_o } -static struct option common_opts[] = { -- OPT_CALLBACK(0, "object-dir", &opts.object_dir, -- N_("directory"), -- N_("object directory containing set of packfile and pack-index pairs"), -- parse_object_dir), +- OPT_STRING(0, "object-dir", &opts.object_dir, N_("directory"), +- N_("object directory containing set of packfile and pack-index pairs")), - OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), - MIDX_PROGRESS), - OPT_END(), @@ builtin/multi-pack-index.c: static struct odb_source_packed *handle_object_dir_o - return parse_options_concat(common_opts, prev); -} +#define MULTI_PACK_INDEX_COMMON_OPTIONS(opts) \ -+ OPT_CALLBACK(0, "object-dir", &(opts)->object_dir, \ -+ N_("directory"), \ -+ N_("object directory containing set of packfile and pack-index pairs"), \ -+ parse_object_dir), \ ++ OPT_STRING(0, "object-dir", &(opts)->object_dir, N_("directory"), \ ++ N_("object directory containing set of packfile and pack-index pairs")), \ + OPT_BIT(0, "progress", &(opts)->flags, N_("force progress reporting"), \ + MIDX_PROGRESS) @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, cons - struct option *options; - static struct option builtin_multi_pack_index_write_options[] = { + static struct option options[] = { ++ MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_STRING(0, "preferred-pack", &opts.preferred_pack, N_("preferred-pack"), N_("pack for reuse when computing a multi-pack bitmap")), @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, - N_("write multi-pack index containing only given indexes")), - OPT_FILENAME(0, "refs-snapshot", &opts.refs_snapshot, - N_("refs snapshot for selecting bitmap commits")), -+ MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), - OPT_END(), - }; - struct odb_source_packed *packed_source; -@@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, repo_config(repo, git_multi_pack_index_write_config, NULL); @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, co - struct option *options; - static struct option builtin_multi_pack_index_compact_options[] = { + static struct option options[] = { ++ MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_STRING(0, "base", &opts.incremental_base, N_("checksum"), N_("base MIDX for incremental writes")), OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"), @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, const char **argv, - OPT_NEGBIT(0, "write-chain-file", &opts.flags, - N_("write the multi-pack-index chain file"), - MIDX_WRITE_NO_CHAIN), -+ MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), - OPT_END(), - }; repo_config(repo, git_multi_pack_index_write_config, NULL); @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_repack(int argc, con - struct option *options; - static struct option builtin_multi_pack_index_repack_options[] = { + static 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")), -+ MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_END(), }; struct odb_source_packed *packed_source; 5: d8ac43810193 ! 5: 46a59a5daf52 builtin/multi-pack-index: de-globalize options handling @@ builtin/multi-pack-index.c: static char const * const builtin_multi_pack_index_u -static struct opts_multi_pack_index { +struct opts_multi_pack_index { - char *object_dir; + const char *object_dir; const char *preferred_pack; const char *incremental_base; @@ builtin/multi-pack-index.c: static struct opts_multi_pack_index { @@ builtin/multi-pack-index.c: static struct opts_multi_pack_index { unsigned flags; int stdin_packs; -} opts; -- +}; - static int parse_object_dir(const struct option *opt, const char *arg, - int unset) -@@ builtin/multi-pack-index.c: static int parse_object_dir(const struct option *opt, const char *arg, - return 0; - } - -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) @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, cons - 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"), - N_("pack for reuse when computing a multi-pack bitmap")), @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, cons if (opts.stdin_packs) { struct string_list packs = STRING_LIST_INIT_DUP; -@@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, - - string_list_clear(&packs, 0); - free(opts.refs_snapshot); -+ free(opts.object_dir); - - return ret; - -@@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_write(int argc, const char **argv, - opts.refs_snapshot, opts.flags); - - free(opts.refs_snapshot); -+ free(opts.object_dir); - return ret; - } - @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, const char **argv, const char *prefix, struct repository *repo) @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, co - 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")), - OPT_BIT(0, "bitmap", &opts.flags, N_("write multi-pack bitmap"), @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, const char **argv, OPT_END(), }; @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, co m = get_multi_pack_index(packed_source); -@@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_compact(int argc, const char **argv, - ret = write_midx_file_compact(packed_source, from_midx, to_midx, - opts.incremental_base, opts.flags); - -+ free(opts.object_dir); - return ret; - } - @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_verify(int argc, const char **argv, const char *prefix, struct repository *repo) @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_verify(int argc, con MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_END(), }; - struct odb_source_packed *packed_source; -+ int ret; - - trace2_cmd_mode(argv[0]); - @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_verify(int argc, const char **argv, if (argc) usage_with_options(builtin_multi_pack_index_verify_usage, @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_verify(int argc, con - 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); -+ ret = verify_midx_file(packed_source, opts.flags); -+ -+ free(opts.object_dir); -+ return ret; + return verify_midx_file(packed_source, opts.flags); } - - static int cmd_multi_pack_index_expire(int argc, const char **argv, +@@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_expire(int argc, const char **argv, const char *prefix, struct repository *repo) { @@ builtin/multi-pack-index.c: static int cmd_multi_pack_index_verify(int argc, con MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), OPT_END(), }; - struct odb_source_packed *packed_source; -+ int ret; - - trace2_cmd_mode(argv[0]); - @@ builtin/multi-pack-index.c: 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); -+ -+ ret = expire_midx_packs(packed_source, opts.flags); -- return expire_midx_packs(packed_source, opts.flags); -+ free(opts.object_dir); -+ return ret; + return expire_midx_packs(packed_source, opts.flags); } - - static int cmd_multi_pack_index_repack(int argc, const char **argv, +@@ builtin/multi-pack-index.c: 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")), - MULTI_PACK_INDEX_COMMON_OPTIONS(&opts), - OPT_END(), - }; - struct odb_source_packed *packed_source; -+ int ret; - - trace2_cmd_mode(argv[0]); - @@ builtin/multi-pack-index.c: 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); -+ -+ ret = midx_repack(packed_source, (size_t)opts.batch_size, opts.flags); -- return midx_repack(packed_source, (size_t)opts.batch_size, opts.flags); -+ free(opts.object_dir); -+ return ret; + return midx_repack(packed_source, (size_t)opts.batch_size, opts.flags); } - - int cmd_multi_pack_index(int argc, @@ builtin/multi-pack-index.c: int cmd_multi_pack_index(int argc, const char *prefix, struct repository *repo) @@ builtin/multi-pack-index.c: int cmd_multi_pack_index(int argc, - res = fn(argc, argv, prefix, repo); - -- free(opts.object_dir); - return res; + return fn(argc, argv, prefix, repo); } base-commit: 86e79b2795e3ce3b0ad7685edd368574b0c8b9f2 -- 2.56.0