builtin/repack.c: extract opts struct for 'write_midx_included_packs()'
The function 'write_midx_included_packs()', which is responsible for writing a new MIDX with a given set of included packs, currently takes a list of six arguments. In order to extract this function out of the builtin, we have to pass in a few additional parameters, like 'midx_must_contain_cruft' and 'packdir', which are currently declared as static variables within the builtin/repack.c compilation unit. Instead of adding additional parameters to `write_midx_included_packs()` extract out an "opts" struct that names these parameters, and pass a pointer to that, making it less cumbersome to add additional parameters. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Taylor Blau committed
Oct 15, 2025 at 18:28 UTC
e6b09077216ecc1c767506f39be736ba3dcccecb
1 file changed
+34
-18
builtin/repack.c
+34
-18
@@ -107,6 +107,17 @@ static int repack_config(const char *var, const char *value,
107
return git_default_config(var, value, ctx, cb);
108
}
109
110
+struct repack_write_midx_opts {
111
+ struct string_list *include;
112
+ struct pack_geometry *geometry;
113
+ struct string_list *names;
114
+ const char *refs_snapshot;
115
+ const char *packdir;
116
+ int show_progress;
117
+ int write_bitmaps;
118
+ int midx_must_contain_cruft;
119
+};
120
+
121
static int midx_has_unknown_packs(char **midx_pack_names,
122
size_t midx_pack_names_nr,
123
struct string_list *include,
@@ -290,19 +301,15 @@ static void midx_included_packs(struct string_list *include,
301
strbuf_release(&buf);
302
}
303
293
-static int write_midx_included_packs(struct string_list *include,
294
- struct pack_geometry *geometry,
295
- struct string_list *names,
296
- const char *refs_snapshot,
297
- int show_progress, int write_bitmaps)
304
+static int write_midx_included_packs(struct repack_write_midx_opts *opts)
305
{
306
struct child_process cmd = CHILD_PROCESS_INIT;
307
struct string_list_item *item;
301
- struct packed_git *preferred = pack_geometry_preferred_pack(geometry);
308
+ struct packed_git *preferred = pack_geometry_preferred_pack(opts->geometry);
309
FILE *in;
310
int ret;
311
305
- if (!include->nr)
312
+ if (!opts->include->nr)
313
return 0;
314
315
cmd.in = -1;
@@ -311,18 +318,18 @@ static int write_midx_included_packs(struct string_list *include,
318
strvec_push(&cmd.args, "multi-pack-index");
319
strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
320
314
- if (show_progress)
321
+ if (opts->show_progress)
322
strvec_push(&cmd.args, "--progress");
323
else
324
strvec_push(&cmd.args, "--no-progress");
325
319
- if (write_bitmaps)
326
+ if (opts->write_bitmaps)
327
strvec_push(&cmd.args, "--bitmap");
328
329
if (preferred)
330
strvec_pushf(&cmd.args, "--preferred-pack=%s",
331
pack_basename(preferred));
325
- else if (names->nr) {
332
+ else if (opts->names->nr) {
333
/* The largest pack was repacked, meaning that either
334
* one or two packs exist depending on whether the
335
* repository has a cruft pack or not.
@@ -335,7 +342,7 @@ static int write_midx_included_packs(struct string_list *include,
342
* `--max-pack-size` was given, but any one of them
343
* will suffice, so pick the first one.)
344
*/
338
- for_each_string_list_item(item, names) {
345
+ for_each_string_list_item(item, opts->names) {
346
struct generated_pack *pack = item->util;
347
if (generated_pack_has_ext(pack, ".mtimes"))
348
continue;
@@ -355,15 +362,16 @@ static int write_midx_included_packs(struct string_list *include,
362
;
363
}
364
358
- if (refs_snapshot)
359
- strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
365
+ if (opts->refs_snapshot)
366
+ strvec_pushf(&cmd.args, "--refs-snapshot=%s",
367
+ opts->refs_snapshot);
368
369
ret = start_command(&cmd);
370
if (ret)
371
return ret;
372
373
in = xfdopen(cmd.in, "w");
366
- for_each_string_list_item(item, include)
374
+ for_each_string_list_item(item, opts->include)
375
fprintf(in, "%s\n", item->string);
376
fclose(in);
377
@@ -1001,15 +1009,23 @@ int cmd_repack(int argc,
1009
1010
if (write_midx) {
1011
struct string_list include = STRING_LIST_INIT_DUP;
1012
+ struct repack_write_midx_opts opts = {
1013
+ .include = &include,
1014
+ .geometry = &geometry,
1015
+ .names = &names,
1016
+ .refs_snapshot = refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1017
+ .packdir = packdir,
1018
+ .show_progress = show_progress,
1019
+ .write_bitmaps = write_bitmaps > 0,
1020
+ .midx_must_contain_cruft = midx_must_contain_cruft
1021
+ };
1022
midx_included_packs(&include, &existing, midx_pack_names,
1023
midx_pack_names_nr, &names, &geometry);
1024
1007
- ret = write_midx_included_packs(&include, &geometry, &names,
1008
- refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1009
- show_progress, write_bitmaps > 0);
1025
+ ret = write_midx_included_packs(&opts);
1026
1027
if (!ret && write_bitmaps)
1012
- remove_redundant_bitmaps(&include, packdir);
1028
+ remove_redundant_bitmaps(&include, opts.packdir);
1029
1030
string_list_clear(&include, 0);
1031