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 <jlto…@gmail.com>
---
builtin/multi-pack-index.c | 77 +++++++++++++++++++++++---------------
1 file changed, 47 insertions(+), 30 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index d3881aee6a11..eb5c77456b33 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 {
char *object_dir;
const char *preferred_pack;
const char *incremental_base;
@@ -70,8 +70,7 @@ static struct opts_multi_pack_index {
unsigned long batch_size;
unsigned flags;
int stdin_packs;
-} opts;
-
+};
static int parse_object_dir(const struct option *opt, const char *arg,
int unset)
@@ -85,9 +84,9 @@ 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)
{
- const char *object_dir = opts.object_dir;
struct odb_files_dir *dir;
if (!object_dir)
@@ -110,20 +109,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;
}
/*
@@ -147,7 +148,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[] = {
OPT_STRING(0, "preferred-pack", &opts.preferred_pack,
N_("preferred-pack"),
N_("pack for reuse when computing a multi-pack bitmap")),
@@ -172,7 +174,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]);
@@ -200,7 +202,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;
@@ -214,6 +216,7 @@ 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;
@@ -223,6 +226,7 @@ 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;
}
@@ -230,13 +234,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[] = {
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"),
@@ -250,7 +255,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]);
@@ -272,7 +277,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);
@@ -300,6 +305,7 @@ 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;
}
@@ -307,11 +313,13 @@ 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(),
};
struct odb_source_packed *packed_source;
+ int ret;
trace2_cmd_mode(argv[0]);
@@ -323,20 +331,25 @@ 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);
+ ret = verify_midx_file(packed_source, opts.flags);
+
+ free(opts.object_dir);
+ return ret;
}
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(),
};
struct odb_source_packed *packed_source;
+ int ret;
trace2_cmd_mode(argv[0]);
@@ -348,22 +361,27 @@ 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;
}
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[] = {
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]);
@@ -376,9 +394,12 @@ 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;
}
int cmd_multi_pack_index(int argc,
@@ -386,7 +407,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),
@@ -404,8 +424,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);
-
- free(opts.object_dir);
- return res;
+ return fn(argc, argv, prefix, repo);
}
--
2.56.0