builtin/repack.c: extract redundant pack cleanup for --geometric

To reduce the complexity of the already quite-long `cmd_repack()` implementation, extract out the parts responsible for deleting redundant packs from a geometric repack out into its own sub-routine. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Sep 13, 2023 at 15:17 UTC 639c4a3992337e15c0faa2fa6d73462cc76ca7b9
1 file changed +29 -23
builtin/repack.c
+29 -23
@@ -540,6 +540,32 @@ static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
540 return NULL;
541 }
542
543 +static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
544 + struct string_list *names,
545 + struct existing_packs *existing)
546 +{
547 + struct strbuf buf = STRBUF_INIT;
548 + uint32_t i;
549 +
550 + for (i = 0; i < geometry->split; i++) {
551 + struct packed_git *p = geometry->pack[i];
552 + if (string_list_has_string(names, hash_to_hex(p->hash)))
553 + continue;
554 +
555 + strbuf_reset(&buf);
556 + strbuf_addstr(&buf, pack_basename(p));
557 + strbuf_strip_suffix(&buf, ".pack");
558 +
559 + if ((p->pack_keep) ||
560 + (string_list_has_string(&existing->kept_packs, buf.buf)))
561 + continue;
562 +
563 + remove_redundant_pack(packdir, buf.buf);
564 + }
565 +
566 + strbuf_release(&buf);
567 +}
568 +
569 static void free_pack_geometry(struct pack_geometry *geometry)
570 {
571 if (!geometry)
@@ -1201,29 +1227,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1227 remove_redundant_pack(packdir, item->string);
1228 }
1229
1204 - if (geometry.split_factor) {
1205 - struct strbuf buf = STRBUF_INIT;
1206 -
1207 - uint32_t i;
1208 - for (i = 0; i < geometry.split; i++) {
1209 - struct packed_git *p = geometry.pack[i];
1210 - if (string_list_has_string(&names,
1211 - hash_to_hex(p->hash)))
1212 - continue;
1213 -
1214 - strbuf_reset(&buf);
1215 - strbuf_addstr(&buf, pack_basename(p));
1216 - strbuf_strip_suffix(&buf, ".pack");
1217 -
1218 - if ((p->pack_keep) ||
1219 - (string_list_has_string(&existing.kept_packs,
1220 - buf.buf)))
1221 - continue;
1222 -
1223 - remove_redundant_pack(packdir, buf.buf);
1224 - }
1225 - strbuf_release(&buf);
1226 - }
1230 + if (geometry.split_factor)
1231 + geometry_remove_redundant_packs(&geometry, &names,
1232 + &existing);
1233 if (show_progress)
1234 opts |= PRUNE_PACKED_VERBOSE;
1235 prune_packed_objects(opts);