Skip to content

Commit bf0a6b6

Browse files
szedergitster
authored andcommitted
builtin/multi-pack-index.c: let parse-options parse subcommands
'git multi-pack-index' parses its subcommands with a couple of if-else if statements. parse-options has just learned to parse subcommands, so let's use that facility instead, with the benefits of shorter code, handling missing or unknown subcommands, and listing subcommands for Bash completion. Note that the functions implementing each subcommand only accept the 'argc' and '**argv' parameters, so add a (unused) '*prefix' parameter to make them match the type expected by parse-options, and thus avoid casting function pointers. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f83736c commit bf0a6b6

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

builtin/multi-pack-index.c

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ static void read_packs_from_stdin(struct string_list *to)
104104
strbuf_release(&buf);
105105
}
106106

107-
static int cmd_multi_pack_index_write(int argc, const char **argv)
107+
static int cmd_multi_pack_index_write(int argc, const char **argv,
108+
const char *prefix)
108109
{
109110
struct option *options;
110111
static struct option builtin_multi_pack_index_write_options[] = {
@@ -160,7 +161,8 @@ static int cmd_multi_pack_index_write(int argc, const char **argv)
160161
opts.refs_snapshot, opts.flags);
161162
}
162163

163-
static int cmd_multi_pack_index_verify(int argc, const char **argv)
164+
static int cmd_multi_pack_index_verify(int argc, const char **argv,
165+
const char *prefix)
164166
{
165167
struct option *options;
166168
static struct option builtin_multi_pack_index_verify_options[] = {
@@ -186,7 +188,8 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv)
186188
return verify_midx_file(the_repository, opts.object_dir, opts.flags);
187189
}
188190

189-
static int cmd_multi_pack_index_expire(int argc, const char **argv)
191+
static int cmd_multi_pack_index_expire(int argc, const char **argv,
192+
const char *prefix)
190193
{
191194
struct option *options;
192195
static struct option builtin_multi_pack_index_expire_options[] = {
@@ -212,7 +215,8 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv)
212215
return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
213216
}
214217

215-
static int cmd_multi_pack_index_repack(int argc, const char **argv)
218+
static int cmd_multi_pack_index_repack(int argc, const char **argv,
219+
const char *prefix)
216220
{
217221
struct option *options;
218222
static struct option builtin_multi_pack_index_repack_options[] = {
@@ -247,7 +251,15 @@ int cmd_multi_pack_index(int argc, const char **argv,
247251
const char *prefix)
248252
{
249253
int res;
250-
struct option *builtin_multi_pack_index_options = common_opts;
254+
parse_opt_subcommand_fn *fn = NULL;
255+
struct option builtin_multi_pack_index_options[] = {
256+
OPT_SUBCOMMAND("repack", &fn, cmd_multi_pack_index_repack),
257+
OPT_SUBCOMMAND("write", &fn, cmd_multi_pack_index_write),
258+
OPT_SUBCOMMAND("verify", &fn, cmd_multi_pack_index_verify),
259+
OPT_SUBCOMMAND("expire", &fn, cmd_multi_pack_index_expire),
260+
OPT_END(),
261+
};
262+
struct option *options = parse_options_concat(builtin_multi_pack_index_options, common_opts);
251263

252264
git_config(git_default_config, NULL);
253265

@@ -256,31 +268,12 @@ int cmd_multi_pack_index(int argc, const char **argv,
256268
the_repository->objects->odb)
257269
opts.object_dir = xstrdup(the_repository->objects->odb->path);
258270

259-
argc = parse_options(argc, argv, prefix,
260-
builtin_multi_pack_index_options,
261-
builtin_multi_pack_index_usage,
262-
PARSE_OPT_STOP_AT_NON_OPTION);
263-
264-
if (!argc)
265-
goto usage;
266-
267-
if (!strcmp(argv[0], "repack"))
268-
res = cmd_multi_pack_index_repack(argc, argv);
269-
else if (!strcmp(argv[0], "write"))
270-
res = cmd_multi_pack_index_write(argc, argv);
271-
else if (!strcmp(argv[0], "verify"))
272-
res = cmd_multi_pack_index_verify(argc, argv);
273-
else if (!strcmp(argv[0], "expire"))
274-
res = cmd_multi_pack_index_expire(argc, argv);
275-
else {
276-
error(_("unrecognized subcommand: %s"), argv[0]);
277-
goto usage;
278-
}
271+
argc = parse_options(argc, argv, prefix, options,
272+
builtin_multi_pack_index_usage, 0);
273+
FREE_AND_NULL(options);
274+
275+
res = fn(argc, argv, prefix);
279276

280277
free(opts.object_dir);
281278
return res;
282-
283-
usage:
284-
usage_with_options(builtin_multi_pack_index_usage,
285-
builtin_multi_pack_index_options);
286279
}

0 commit comments

Comments
 (0)