builtin/repack.c: use a string_list for 'midx_pack_names'

When writing a new MIDX, repack must determine whether or not there are any packs in the MIDX it is replacing (if one exists) that are not somehow represented in the new MIDX (e.g., either by preserving the pack verbatim, or rolling it up as part of a geometric repack, etc.). In order to do this, it keeps track of a list of pack names from the MIDX present in the repository at the start of the repack operation. Since we manipulate and close the object store, we cannot rely on the repository's in-core representation of the MIDX, since this is subject to change and/or go away. When this behavior was introduced in 5ee86c273b (repack: exclude cruft pack(s) from the MIDX where possible, 2025-06-23), we maintained an array of character pointers instead of using a convenience API, such as string-list.h. Store the list of MIDX pack names in a string_list, thereby reducing the number of parameters we have to pass to `midx_has_unknown_packs()`. 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 c3690c97d7b08d9876fcaf0a572b4956bc9b4c33
1 file changed +17 -23
builtin/repack.c
+17 -23
@@ -118,18 +118,17 @@ struct repack_write_midx_opts {
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,
121 +static int midx_has_unknown_packs(struct string_list *midx_pack_names,
122 struct string_list *include,
123 struct pack_geometry *geometry,
124 struct existing_packs *existing)
125 {
127 - size_t i;
126 + struct string_list_item *item;
127
128 string_list_sort(include);
129
131 - for (i = 0; i < midx_pack_names_nr; i++) {
132 - const char *pack_name = midx_pack_names[i];
130 + for_each_string_list_item(item, midx_pack_names) {
131 + const char *pack_name = item->string;
132
133 /*
134 * Determine whether or not each MIDX'd pack from the existing
@@ -191,8 +190,7 @@ static int midx_has_unknown_packs(char **midx_pack_names,
190
191 static void midx_included_packs(struct string_list *include,
192 struct existing_packs *existing,
194 - char **midx_pack_names,
195 - size_t midx_pack_names_nr,
193 + struct string_list *midx_pack_names,
194 struct string_list *names,
195 struct pack_geometry *geometry)
196 {
@@ -247,8 +245,8 @@ static void midx_included_packs(struct string_list *include,
245 }
246
247 if (midx_must_contain_cruft ||
250 - midx_has_unknown_packs(midx_pack_names, midx_pack_names_nr,
251 - include, geometry, existing)) {
248 + midx_has_unknown_packs(midx_pack_names, include, geometry,
249 + existing)) {
250 /*
251 * If there are one or more unknown pack(s) present (see
252 * midx_has_unknown_packs() for what makes a pack
@@ -606,13 +604,12 @@ int cmd_repack(int argc,
604 struct child_process cmd = CHILD_PROCESS_INIT;
605 struct string_list_item *item;
606 struct string_list names = STRING_LIST_INIT_DUP;
607 + struct string_list midx_pack_names = STRING_LIST_INIT_DUP;
608 struct existing_packs existing = EXISTING_PACKS_INIT;
609 struct pack_geometry geometry = { 0 };
610 struct tempfile *refs_snapshot = NULL;
611 int i, ret;
612 int show_progress;
614 - char **midx_pack_names = NULL;
615 - size_t midx_pack_names_nr = 0;
613
614 /* variables to be filled by option parsing */
615 struct repack_config_ctx config_ctx;
@@ -985,13 +982,12 @@ int cmd_repack(int argc,
982 struct multi_pack_index *m =
983 get_multi_pack_index(repo->objects->sources);
984
988 - ALLOC_ARRAY(midx_pack_names,
989 - m->num_packs + m->num_packs_in_base);
990 -
991 - for (; m; m = m->base_midx)
992 - for (uint32_t i = 0; i < m->num_packs; i++)
993 - midx_pack_names[midx_pack_names_nr++] =
994 - xstrdup(m->pack_names[i]);
985 + for (; m; m = m->base_midx) {
986 + for (uint32_t i = 0; i < m->num_packs; i++) {
987 + string_list_append(&midx_pack_names,
988 + m->pack_names[i]);
989 + }
990 + }
991 }
992
993 close_object_store(repo->objects);
@@ -1019,8 +1015,8 @@ int cmd_repack(int argc,
1015 .write_bitmaps = write_bitmaps > 0,
1016 .midx_must_contain_cruft = midx_must_contain_cruft
1017 };
1022 - midx_included_packs(&include, &existing, midx_pack_names,
1023 - midx_pack_names_nr, &names, &geometry);
1018 + midx_included_packs(&include, &existing, &midx_pack_names,
1019 + &names, &geometry);
1020
1021 ret = write_midx_included_packs(&opts);
1022
@@ -1067,11 +1063,9 @@ int cmd_repack(int argc,
1063 cleanup:
1064 string_list_clear(&keep_pack_list, 0);
1065 string_list_clear(&names, 1);
1066 + string_list_clear(&midx_pack_names, 0);
1067 existing_packs_release(&existing);
1068 pack_geometry_release(&geometry);
1072 - for (size_t i = 0; i < midx_pack_names_nr; i++)
1073 - free(midx_pack_names[i]);
1074 - free(midx_pack_names);
1069 pack_objects_args_release(&po_args);
1070 pack_objects_args_release(&cruft_po_args);
1071