builtin/repack.c: convert `--write-midx` to an `OPT_CALLBACK`
Change the --write-midx (-m) flag from an OPT_BOOL to an OPT_CALLBACK that accepts an optional mode argument. Introduce an enum with REPACK_WRITE_MIDX_NONE and REPACK_WRITE_MIDX_DEFAULT to distinguish between the two states, and update all existing boolean checks accordingly. For now, passing no argument (or just `-m`) selects the default mode, preserving existing behavior. A subsequent commit will add a new mode for writing incremental MIDXs. Extract repack_write_midx() as a dispatcher that selects the appropriate MIDX-writing implementation based on the mode. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Taylor Blau committed
May 19, 2026 at 11:58 UTC
d376967fbfb0b366c08be50a1c41d6b30c5e6d89
3 files changed
+58
-14
builtin/repack.c
+38
-12
@@ -97,6 +97,24 @@ static int repack_config(const char *var, const char *value,
97
return git_default_config(var, value, ctx, cb);
98
}
99
100
+static int option_parse_write_midx(const struct option *opt, const char *arg,
101
+ int unset)
102
+{
103
+ enum repack_write_midx_mode *cfg = opt->value;
104
+
105
+ if (unset) {
106
+ *cfg = REPACK_WRITE_MIDX_NONE;
107
+ return 0;
108
+ }
109
+
110
+ if (!arg || !*arg)
111
+ *cfg = REPACK_WRITE_MIDX_DEFAULT;
112
+ else
113
+ return error(_("unknown value for %s: %s"), opt->long_name, arg);
114
+
115
+ return 0;
116
+}
117
+
118
int cmd_repack(int argc,
119
const char **argv,
120
const char *prefix,
@@ -119,7 +137,7 @@ int cmd_repack(int argc,
137
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
138
struct pack_objects_args po_args = PACK_OBJECTS_ARGS_INIT;
139
struct pack_objects_args cruft_po_args = PACK_OBJECTS_ARGS_INIT;
122
- int write_midx = 0;
140
+ enum repack_write_midx_mode write_midx = REPACK_WRITE_MIDX_NONE;
141
const char *cruft_expiration = NULL;
142
const char *expire_to = NULL;
143
const char *filter_to = NULL;
@@ -185,8 +203,14 @@ int cmd_repack(int argc,
203
N_("do not repack this pack")),
204
OPT_INTEGER('g', "geometric", &geometry.split_factor,
205
N_("find a geometric progression with factor <N>")),
188
- OPT_BOOL('m', "write-midx", &write_midx,
189
- N_("write a multi-pack index of the resulting packs")),
206
+ OPT_CALLBACK_F(0, "write-midx", &write_midx,
207
+ N_("mode"),
208
+ N_("write a multi-pack index of the resulting packs"),
209
+ PARSE_OPT_OPTARG, option_parse_write_midx),
210
+ OPT_SET_INT_F('m', NULL, &write_midx,
211
+ N_("write a multi-pack index of the resulting packs"),
212
+ REPACK_WRITE_MIDX_DEFAULT,
213
+ PARSE_OPT_HIDDEN),
214
OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
215
N_("pack prefix to store a pack containing pruned objects")),
216
OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
@@ -221,14 +245,16 @@ int cmd_repack(int argc,
245
pack_everything |= ALL_INTO_ONE;
246
247
if (write_bitmaps < 0) {
224
- if (!write_midx &&
248
+ if (write_midx == REPACK_WRITE_MIDX_NONE &&
249
(!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
250
write_bitmaps = 0;
251
}
252
if (po_args.pack_kept_objects < 0)
229
- po_args.pack_kept_objects = write_bitmaps > 0 && !write_midx;
253
+ po_args.pack_kept_objects = write_bitmaps > 0 &&
254
+ write_midx == REPACK_WRITE_MIDX_NONE;
255
231
- if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
256
+ if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) &&
257
+ write_midx == REPACK_WRITE_MIDX_NONE)
258
die(_(incremental_bitmap_conflict_error));
259
260
if (write_bitmaps && po_args.local &&
@@ -244,7 +270,7 @@ int cmd_repack(int argc,
270
write_bitmaps = 0;
271
}
272
247
- if (write_midx && write_bitmaps) {
273
+ if (write_midx != REPACK_WRITE_MIDX_NONE && write_bitmaps) {
274
struct strbuf path = STRBUF_INIT;
275
276
strbuf_addf(&path, "%s/%s_XXXXXX",
@@ -297,7 +323,7 @@ int cmd_repack(int argc,
323
}
324
if (repo_has_promisor_remote(repo))
325
strvec_push(&cmd.args, "--exclude-promisor-objects");
300
- if (!write_midx) {
326
+ if (write_midx == REPACK_WRITE_MIDX_NONE) {
327
if (write_bitmaps > 0)
328
strvec_push(&cmd.args, "--write-bitmap-index");
329
else if (write_bitmaps < 0)
@@ -519,7 +545,7 @@ int cmd_repack(int argc,
545
if (delete_redundant && pack_everything & ALL_INTO_ONE)
546
existing_packs_mark_for_deletion(&existing, &names);
547
522
- if (write_midx) {
548
+ if (write_midx != REPACK_WRITE_MIDX_NONE) {
549
struct repack_write_midx_opts opts = {
550
.existing = &existing,
551
.geometry = &geometry,
@@ -528,11 +554,11 @@ int cmd_repack(int argc,
554
.packdir = packdir,
555
.show_progress = show_progress,
556
.write_bitmaps = write_bitmaps > 0,
531
- .midx_must_contain_cruft = midx_must_contain_cruft
557
+ .midx_must_contain_cruft = midx_must_contain_cruft,
558
+ .mode = write_midx,
559
};
560
534
- ret = write_midx_included_packs(&opts);
535
-
561
+ ret = repack_write_midx(&opts);
562
if (ret)
563
goto cleanup;
564
}
repack-midx.c
+13
-1
@@ -315,7 +315,7 @@ static int repack_fill_midx_stdin_packs(struct child_process *cmd,
315
return finish_command(cmd);
316
}
317
318
-int write_midx_included_packs(struct repack_write_midx_opts *opts)
318
+static int write_midx_included_packs(struct repack_write_midx_opts *opts)
319
{
320
struct child_process cmd = CHILD_PROCESS_INIT;
321
struct string_list include = STRING_LIST_INIT_DUP;
@@ -378,3 +378,15 @@ done:
378
379
return ret;
380
}
381
+
382
+int repack_write_midx(struct repack_write_midx_opts *opts)
383
+{
384
+ switch (opts->mode) {
385
+ case REPACK_WRITE_MIDX_NONE:
386
+ BUG("write_midx mode is NONE?");
387
+ case REPACK_WRITE_MIDX_DEFAULT:
388
+ return write_midx_included_packs(opts);
389
+ default:
390
+ BUG("unhandled write_midx mode: %d", opts->mode);
391
+ }
392
+}
repack.h
+7
-1
@@ -134,6 +134,11 @@ void pack_geometry_release(struct pack_geometry *geometry);
134
135
struct tempfile;
136
137
+enum repack_write_midx_mode {
138
+ REPACK_WRITE_MIDX_NONE,
139
+ REPACK_WRITE_MIDX_DEFAULT,
140
+};
141
+
142
struct repack_write_midx_opts {
143
struct existing_packs *existing;
144
struct pack_geometry *geometry;
@@ -143,10 +148,11 @@ struct repack_write_midx_opts {
148
int show_progress;
149
int write_bitmaps;
150
int midx_must_contain_cruft;
151
+ enum repack_write_midx_mode mode;
152
};
153
154
void midx_snapshot_refs(struct repository *repo, struct tempfile *f);
149
-int write_midx_included_packs(struct repack_write_midx_opts *opts);
155
+int repack_write_midx(struct repack_write_midx_opts *opts);
156
157
int write_filtered_pack(const struct write_pack_opts *opts,
158
struct existing_packs *existing,