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

[PATCH v2 1/5] builtin/multi-pack-index: resolve default object directory lazily

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

The object directory used by git-multi-pack-index(1) defaults to the path of the primary object database source. This default is set eagerly in `cmd_multi_pack_index()` before any options are parsed, and again when "--no-object-dir" is given. Both cases access `the_repository` to do so, and the eager initialization additionally has to guard against the repository not being set up.

In a subsequent commit, the remaining references to `the_repository` in "builtin/multi-pack-index.c" are removed. In preparation for this change, leave the object directory unset in both cases and lazily fall back to the path of the primary source in `handle_object_dir_option()`, 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 <jltobler@gmail.com>
---
 builtin/multi-pack-index.c | 38 +++++++++++---------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index c48212290cf8..ba5f326dd6ef 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -1,6 +1,5 @@
 #define USE_THE_REPOSITORY_VARIABLE
 #include "builtin.h"
-#include "abspath.h"
 #include "config.h"
 #include "environment.h"
 #include "gettext.h"
@@ -64,7 +63,7 @@ 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;
@@ -73,33 +72,24 @@ 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);
+	const char *object_dir = opts.object_dir;
+	struct odb_files_dir *dir;
+
+	if (!object_dir)
+		object_dir = repo->objects->source->path;
+
+	dir = odb_source_files_find_dir(repo->objects, object_dir);
 	if (!dir)
 		die(_("object directory is not an alternate of the current repository: '%s'"),
-		    opts.object_dir);
+		    object_dir);
 	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(),
@@ -424,17 +414,11 @@ int cmd_multi_pack_index(int argc,
 
 	repo_config(the_repository, git_default_config, NULL);
 
-	if (the_repository &&
-	    the_repository->objects &&
-	    the_repository->objects->source)
-		opts.object_dir = xstrdup(the_repository->objects->source->path);
-
 	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.56.0
Previous: Justin ToblerNext: Patrick Steinhardt
Message 18 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.