builtin/repack.c: inline packs within `write_midx_included_packs()`

To write a MIDX at the end of a repack operation, 'git repack' presently computes the set of packs to write into the MIDX, before invoking `write_midx_included_packs()` with a `string_list` containing those packs. The logic for computing which packs are supposed to appear in the resulting MIDX is within `midx_included_packs()`, where it is aware of details like which cruft pack(s) were written/combined, if/how we did a geometric repack, etc. Computing this list ourselves before providing it to the sole function to make use of that list `write_midx_included_packs()` is somewhat awkward. In the future, repack will learn how to write incremental MIDXs, which will use a very different pack selection routine. Instead of doing something like: struct string_list included_packs = STRING_LIST_INIT_DUP; if (incremental) { midx_incremental_included_packs(&included_packs, ...): write_midx_incremental_included_packs(&included_packs, ...); } else { midx_included_packs(&included_packs, ...): write_midx_included_packs(&included_packs, ...); } in the future, let's have each function that writes a MIDX be responsible for itself computing the list of included packs. Inline the declaration and initialization of `included_packs` into the `write_midx_included_packs()` function itself, and repeat that pattern in the future when we introduce new ways to write MIDXs. 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:29 UTC f17757487b2d212f86edaaf02306972e1a555bbd
1 file changed +8 -9
builtin/repack.c
+8 -9
@@ -109,7 +109,6 @@ static int repack_config(const char *var, const char *value,
109
110 struct repack_write_midx_opts {
111 struct existing_packs *existing;
112 - struct string_list *include;
112 struct pack_geometry *geometry;
113 struct string_list *names;
114 const char *refs_snapshot;
@@ -330,12 +329,14 @@ static void remove_redundant_bitmaps(struct string_list *include,
329 static int write_midx_included_packs(struct repack_write_midx_opts *opts)
330 {
331 struct child_process cmd = CHILD_PROCESS_INIT;
332 + struct string_list include = STRING_LIST_INIT_DUP;
333 struct string_list_item *item;
334 struct packed_git *preferred = pack_geometry_preferred_pack(opts->geometry);
335 FILE *in;
336 int ret = 0;
337
338 - if (!opts->include->nr)
338 + midx_included_packs(&include, opts);
339 + if (!include.nr)
340 goto done;
341
342 cmd.in = -1;
@@ -397,14 +398,17 @@ static int write_midx_included_packs(struct repack_write_midx_opts *opts)
398 goto done;
399
400 in = xfdopen(cmd.in, "w");
400 - for_each_string_list_item(item, opts->include)
401 + for_each_string_list_item(item, &include)
402 fprintf(in, "%s\n", item->string);
403 fclose(in);
404
405 ret = finish_command(&cmd);
406 done:
407 if (!ret && opts->write_bitmaps)
407 - remove_redundant_bitmaps(opts->include, opts->packdir);
408 + remove_redundant_bitmaps(&include, opts->packdir);
409 +
410 + string_list_clear(&include, 0);
411 +
412 return ret;
413 }
414
@@ -994,10 +998,8 @@ int cmd_repack(int argc,
998 existing_packs_mark_for_deletion(&existing, &names);
999
1000 if (write_midx) {
997 - struct string_list include = STRING_LIST_INIT_DUP;
1001 struct repack_write_midx_opts opts = {
1002 .existing = &existing,
1000 - .include = &include,
1003 .geometry = &geometry,
1004 .names = &names,
1005 .refs_snapshot = refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
@@ -1006,12 +1008,9 @@ int cmd_repack(int argc,
1008 .write_bitmaps = write_bitmaps > 0,
1009 .midx_must_contain_cruft = midx_must_contain_cruft
1010 };
1009 - midx_included_packs(&include, &opts);
1011
1012 ret = write_midx_included_packs(&opts);
1013
1013 - string_list_clear(&include, 0);
1014 -
1014 if (ret)
1015 goto cleanup;
1016 }