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 <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Aug 19, 2022 at 18:04 UTC bf0a6b65fcdb919ed27d0b77f0adfd65d84ea691
1 file changed +22 -29
builtin/multi-pack-index.c
+22 -29
@@ -104,7 +104,8 @@ static void read_packs_from_stdin(struct string_list *to)
104 strbuf_release(&buf);
105 }
106
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)
109 {
110 struct option *options;
111 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)
161 opts.refs_snapshot, opts.flags);
162 }
163
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)
166 {
167 struct option *options;
168 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)
188 return verify_midx_file(the_repository, opts.object_dir, opts.flags);
189 }
190
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)
193 {
194 struct option *options;
195 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)
215 return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
216 }
217
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)
220 {
221 struct option *options;
222 static struct option builtin_multi_pack_index_repack_options[] = {
@@ -247,7 +251,15 @@ int cmd_multi_pack_index(int argc, const char **argv,
251 const char *prefix)
252 {
253 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);
263
264 git_config(git_default_config, NULL);
265
@@ -256,31 +268,12 @@ int cmd_multi_pack_index(int argc, const char **argv,
268 the_repository->objects->odb)
269 opts.object_dir = xstrdup(the_repository->objects->odb->path);
270
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);
276
277 free(opts.object_dir);
278 return res;
282 -
283 -usage:
284 - usage_with_options(builtin_multi_pack_index_usage,
285 - builtin_multi_pack_index_options);
279 }