git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/5] builtin/multi-pack-index: simplify how common options are wired up

From
Justin Tobler <jltobler@gmail.com>
Date
Dec 1, 2026, 18:16 UTC
Message-ID
<3bb2b9a69f1c6cd6c6690d961757acbec3c92aa6.1796147221.git.jltobler@gmail.com>
In-Reply-To
<cover.1796147221.git.jltobler@gmail.com>

All git-multi-pack-index(1) subcommands accept the "--object-dir=" and "--progress" options. To avoid duplicating these common options, a helper function concatenates them with the options array of each subcommand. This works, but requires a separate options array to be allocated and freed for every subcommand.

Define the common options via a preprocessor macro instead, which each options array can include directly. This avoids the extra allocations and is less verbose overall.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 builtin/multi-pack-index.c | 57 ++++++++++----------------------------
 1 file changed, 15 insertions(+), 42 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index e84df0c947ce..7dfae23411bc 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -87,18 +87,11 @@ static struct odb_source_packed *handle_object_dir_option(struct repository *rep
 	return dir->packed;
 }
 
-static struct option common_opts[] = {
-	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(),
-};
-
-static struct option *add_common_options(struct option *prev)
-{
-	return parse_options_concat(common_opts, prev);
-}
+#define MULTI_PACK_INDEX_COMMON_OPTIONS(opts) \
+	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)
 
 static int git_multi_pack_index_write_config(const char *var, const char *value,
 					     const struct config_context *ctx UNUSED,
@@ -139,8 +132,8 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 				      const char *prefix,
 				      struct repository *repo)
 {
-	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")),
@@ -166,8 +159,6 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	repo_config(repo, git_multi_pack_index_write_config, NULL);
 
-	options = add_common_options(builtin_multi_pack_index_write_options);
-
 	trace2_cmd_mode(argv[0]);
 
 	if (isatty(2))
@@ -196,8 +187,6 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	packed_source = handle_object_dir_option(repo);
 
-	FREE_AND_NULL(options);
-
 	if (opts.stdin_packs) {
 		struct string_list packs = STRING_LIST_INIT_DUP;
 
@@ -232,8 +221,8 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 	struct odb_source_packed *packed_source;
 	int ret;
 
-	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"),
@@ -248,8 +237,6 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 
 	repo_config(repo, git_multi_pack_index_write_config, NULL);
 
-	options = add_common_options(builtin_multi_pack_index_compact_options);
-
 	trace2_cmd_mode(argv[0]);
 
 	if (isatty(2))
@@ -272,8 +259,6 @@ static int cmd_multi_pack_index_compact(int argc, const char **argv,
 
 	packed_source = handle_object_dir_option(repo);
 
-	FREE_AND_NULL(options);
-
 	m = get_multi_pack_index(packed_source);
 
 	for (cur = m; cur && !(from_midx && to_midx); cur = cur->base_midx) {
@@ -307,14 +292,12 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 				       const char *prefix,
 				       struct repository *repo)
 {
-	struct option *options;
-	static struct option builtin_multi_pack_index_verify_options[] = {
+	static struct option options[] = {
+		MULTI_PACK_INDEX_COMMON_OPTIONS(&opts),
 		OPT_END(),
 	};
 	struct odb_source_packed *packed_source;
 
-	options = add_common_options(builtin_multi_pack_index_verify_options);
-
 	trace2_cmd_mode(argv[0]);
 
 	if (isatty(2))
@@ -327,8 +310,6 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 				   options);
 	packed_source = handle_object_dir_option(repo);
 
-	FREE_AND_NULL(options);
-
 	return verify_midx_file(packed_source, opts.flags);
 }
 
@@ -336,14 +317,12 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 				       const char *prefix,
 				       struct repository *repo)
 {
-	struct option *options;
-	static struct option builtin_multi_pack_index_expire_options[] = {
+	static struct option options[] = {
+		MULTI_PACK_INDEX_COMMON_OPTIONS(&opts),
 		OPT_END(),
 	};
 	struct odb_source_packed *packed_source;
 
-	options = add_common_options(builtin_multi_pack_index_expire_options);
-
 	trace2_cmd_mode(argv[0]);
 
 	if (isatty(2))
@@ -356,8 +335,6 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 				   options);
 	packed_source = handle_object_dir_option(repo);
 
-	FREE_AND_NULL(options);
-
 	return expire_midx_packs(packed_source, opts.flags);
 }
 
@@ -365,16 +342,14 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 				       const char *prefix,
 				       struct repository *repo)
 {
-	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")),
 		OPT_END(),
 	};
 	struct odb_source_packed *packed_source;
 
-	options = add_common_options(builtin_multi_pack_index_repack_options);
-
 	trace2_cmd_mode(argv[0]);
 
 	if (isatty(2))
@@ -388,8 +363,6 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 				   options);
 	packed_source = handle_object_dir_option(repo);
 
-	FREE_AND_NULL(options);
-
 	return midx_repack(packed_source, (size_t)opts.batch_size, opts.flags);
 }
 
-- 
2.56.0
Previous: Justin ToblerNext: Justin Tobler
Message 22 of 25 in “builtin/multi-pack-index: remove global state”
  1. 0/5 builtin/multi-pack-index: remove global stateJustin Tobler, Nov 30, 2026
  2. 1/5 builtin/multi-pack-index: resolve default object directory lazilyJustin Tobler, Nov 30, 2026
  3. Patrick SteinhardtDec 1, 2026
  4. Justin ToblerDec 1, 2026
  5. 2/5 builtin/multi-pack-index: get rid of `the_repository`Justin Tobler, Nov 30, 2026
  6. Patrick SteinhardtDec 1, 2026
  7. Justin ToblerDec 1, 2026
  8. 3/5 builtin/multi-pack-index: drop top-level optionsJustin Tobler, Nov 30, 2026
  9. Patrick SteinhardtDec 1, 2026
  10. Justin ToblerDec 1, 2026
  11. 4/5 builtin/multi-pack-index: simplify how common options are wired upJustin Tobler, Nov 30, 2026
  12. Patrick SteinhardtDec 1, 2026
  13. Justin ToblerDec 1, 2026
  14. 5/5 builtin/multi-pack-index: de-globalize options handlingJustin Tobler, Nov 30, 2026
  15. Patrick SteinhardtDec 1, 2026
  16. Justin ToblerDec 1, 2026
  17. 0/5 builtin/multi-pack-index: remove global stateJustin Tobler, Dec 1, 2026
  18. 1/5 builtin/multi-pack-index: resolve default object directory lazilyJustin Tobler, Dec 1, 2026
  19. Patrick SteinhardtDec 2, 2026
  20. 2/5 builtin/multi-pack-index: get rid of `the_repository`Justin Tobler, Dec 1, 2026
  21. 3/5 builtin/multi-pack-index: drop top-level optionsJustin Tobler, Dec 1, 2026
  22. 4/5 builtin/multi-pack-index: simplify how common options are wired upJustin Tobler, Dec 1, 2026
  23. 5/5 builtin/multi-pack-index: de-globalize options handlingJustin Tobler, Dec 1, 2026
  24. Patrick SteinhardtDec 2, 2026
  25. Patrick SteinhardtDec 2, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.