builtin/multi-pack-index.c: split sub-commands

Handle sub-commands of the 'git multi-pack-index' builtin (e.g., "write", "repack", etc.) separately from one another. This allows sub-commands with unique options, without forcing cmd_multi_pack_index() to reject invalid combinations itself. This comes at the cost of some duplication and boilerplate. Luckily, the duplication is reduced to a minimum, since common options are shared among sub-commands due to a suggestion by Ævar. (Sub-commands do have to retain the common options, too, since this builtin accepts common options on either side of the sub-command). Roughly speaking, cmd_multi_pack_index() parses options (including common ones), and stops at the first non-option, which is the sub-command. It then dispatches to the appropriate sub-command, which parses the remaining options (also including common options). Unknown options are kept by the sub-commands in order to detect their presence (and complain that too many arguments were given). Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Mar 30, 2021 at 11:03 UTC 60ca94769ce45161c4bcfb4aa92212585d3da0f6
1 file changed +105 -25
builtin/multi-pack-index.c
+105 -25
@@ -17,6 +17,22 @@
17 #define BUILTIN_MIDX_REPACK_USAGE \
18 N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
19
20 +static char const * const builtin_multi_pack_index_write_usage[] = {
21 + BUILTIN_MIDX_WRITE_USAGE,
22 + NULL
23 +};
24 +static char const * const builtin_multi_pack_index_verify_usage[] = {
25 + BUILTIN_MIDX_VERIFY_USAGE,
26 + NULL
27 +};
28 +static char const * const builtin_multi_pack_index_expire_usage[] = {
29 + BUILTIN_MIDX_EXPIRE_USAGE,
30 + NULL
31 +};
32 +static char const * const builtin_multi_pack_index_repack_usage[] = {
33 + BUILTIN_MIDX_REPACK_USAGE,
34 + NULL
35 +};
36 static char const * const builtin_multi_pack_index_usage[] = {
37 BUILTIN_MIDX_WRITE_USAGE,
38 BUILTIN_MIDX_VERIFY_USAGE,
@@ -31,25 +47,98 @@ static struct opts_multi_pack_index {
47 unsigned flags;
48 } opts;
49
34 -int cmd_multi_pack_index(int argc, const char **argv,
35 - const char *prefix)
50 +static struct option common_opts[] = {
51 + OPT_FILENAME(0, "object-dir", &opts.object_dir,
52 + N_("object directory containing set of packfile and pack-index pairs")),
53 + OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), MIDX_PROGRESS),
54 + OPT_END(),
55 +};
56 +
57 +static struct option *add_common_options(struct option *prev)
58 +{
59 + return parse_options_concat(common_opts, prev);
60 +}
61 +
62 +static int cmd_multi_pack_index_write(int argc, const char **argv)
63 +{
64 + struct option *options = common_opts;
65 +
66 + argc = parse_options(argc, argv, NULL,
67 + options, builtin_multi_pack_index_write_usage,
68 + PARSE_OPT_KEEP_UNKNOWN);
69 + if (argc)
70 + usage_with_options(builtin_multi_pack_index_write_usage,
71 + options);
72 +
73 + return write_midx_file(opts.object_dir, opts.flags);
74 +}
75 +
76 +static int cmd_multi_pack_index_verify(int argc, const char **argv)
77 +{
78 + struct option *options = common_opts;
79 +
80 + argc = parse_options(argc, argv, NULL,
81 + options, builtin_multi_pack_index_verify_usage,
82 + PARSE_OPT_KEEP_UNKNOWN);
83 + if (argc)
84 + usage_with_options(builtin_multi_pack_index_verify_usage,
85 + options);
86 +
87 + return verify_midx_file(the_repository, opts.object_dir, opts.flags);
88 +}
89 +
90 +static int cmd_multi_pack_index_expire(int argc, const char **argv)
91 +{
92 + struct option *options = common_opts;
93 +
94 + argc = parse_options(argc, argv, NULL,
95 + options, builtin_multi_pack_index_expire_usage,
96 + PARSE_OPT_KEEP_UNKNOWN);
97 + if (argc)
98 + usage_with_options(builtin_multi_pack_index_expire_usage,
99 + options);
100 +
101 + return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
102 +}
103 +
104 +static int cmd_multi_pack_index_repack(int argc, const char **argv)
105 {
37 - static struct option builtin_multi_pack_index_options[] = {
38 - OPT_FILENAME(0, "object-dir", &opts.object_dir,
39 - N_("object directory containing set of packfile and pack-index pairs")),
40 - OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), MIDX_PROGRESS),
106 + struct option *options;
107 + static struct option builtin_multi_pack_index_repack_options[] = {
108 OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
109 N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
110 OPT_END(),
111 };
112
113 + options = add_common_options(builtin_multi_pack_index_repack_options);
114 +
115 + argc = parse_options(argc, argv, NULL,
116 + options,
117 + builtin_multi_pack_index_repack_usage,
118 + PARSE_OPT_KEEP_UNKNOWN);
119 + if (argc)
120 + usage_with_options(builtin_multi_pack_index_repack_usage,
121 + options);
122 +
123 + FREE_AND_NULL(options);
124 +
125 + return midx_repack(the_repository, opts.object_dir,
126 + (size_t)opts.batch_size, opts.flags);
127 +}
128 +
129 +int cmd_multi_pack_index(int argc, const char **argv,
130 + const char *prefix)
131 +{
132 + struct option *builtin_multi_pack_index_options = common_opts;
133 +
134 git_config(git_default_config, NULL);
135
136 if (isatty(2))
137 opts.flags |= MIDX_PROGRESS;
138 argc = parse_options(argc, argv, prefix,
139 builtin_multi_pack_index_options,
52 - builtin_multi_pack_index_usage, 0);
140 + builtin_multi_pack_index_usage,
141 + PARSE_OPT_STOP_AT_NON_OPTION);
142
143 if (!opts.object_dir)
144 opts.object_dir = get_object_directory();
@@ -58,25 +147,16 @@ int cmd_multi_pack_index(int argc, const char **argv,
147 usage_with_options(builtin_multi_pack_index_usage,
148 builtin_multi_pack_index_options);
149
61 - if (argc > 1) {
62 - die(_("too many arguments"));
63 - return 1;
64 - }
65 -
150 trace2_cmd_mode(argv[0]);
151
152 if (!strcmp(argv[0], "repack"))
69 - return midx_repack(the_repository, opts.object_dir,
70 - (size_t)opts.batch_size, opts.flags);
71 - if (opts.batch_size)
72 - die(_("--batch-size option is only for 'repack' subcommand"));
73 -
74 - if (!strcmp(argv[0], "write"))
75 - return write_midx_file(opts.object_dir, opts.flags);
76 - if (!strcmp(argv[0], "verify"))
77 - return verify_midx_file(the_repository, opts.object_dir, opts.flags);
78 - if (!strcmp(argv[0], "expire"))
79 - return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
80 -
81 - die(_("unrecognized subcommand: %s"), argv[0]);
153 + return cmd_multi_pack_index_repack(argc, argv);
154 + else if (!strcmp(argv[0], "write"))
155 + return cmd_multi_pack_index_write(argc, argv);
156 + else if (!strcmp(argv[0], "verify"))
157 + return cmd_multi_pack_index_verify(argc, argv);
158 + else if (!strcmp(argv[0], "expire"))
159 + return cmd_multi_pack_index_expire(argc, argv);
160 + else
161 + die(_("unrecognized subcommand: %s"), argv[0]);
162 }