midx: use `strvec` for `keep_hashes`

The `keep_hashes` array in `write_midx_internal()` accumulates the checksums of MIDX files that should be retained when pruning stale entries from the MIDX chain. For similar reasons as in a previous commit, rewrite this using a strvec, requiring us to pass one fewer parameter. Unlike the aforementioned previous commit, use a `strvec` instead of a `string_list`, which provides a more ergonomic interface to adjust the values at a particular index. The ordering is important here, as this value is used to determine the contents of the resulting `multi-pack-index-chain` file when writing with "--incremental". Since the previous commit already builds the array in forward order, the conversion is straightforward: replace indexed assignments with `strvec_push()`, drop the pre-counting and `CALLOC_ARRAY()`, and simplify cleanup via `strvec_clear()`. 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:57 UTC 046a8686a406ebff7ca01dd4a3fabf9a679e010f
2 files changed +38 -66
midx-write.c
+28 -56
@@ -29,8 +29,7 @@ extern void clear_midx_files_ext(struct odb_source *source, const char *ext,
29 const char *keep_hash);
30 extern void clear_incremental_midx_files_ext(struct odb_source *source,
31 const char *ext,
32 - const char **keep_hashes,
33 - uint32_t hashes_nr);
32 + const struct strvec *keep_hashes);
33 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
34 const char *idx_name);
35
@@ -1109,8 +1108,7 @@ done:
1108 }
1109
1110 static void clear_midx_files(struct odb_source *source,
1112 - const char **hashes, uint32_t hashes_nr,
1113 - unsigned incremental)
1111 + const struct strvec *hashes, unsigned incremental)
1112 {
1113 /*
1114 * if incremental:
@@ -1124,13 +1122,15 @@ static void clear_midx_files(struct odb_source *source,
1122 */
1123 struct strbuf buf = STRBUF_INIT;
1124 const char *exts[] = { MIDX_EXT_BITMAP, MIDX_EXT_REV, MIDX_EXT_MIDX };
1127 - uint32_t i, j;
1125 + uint32_t i;
1126
1127 for (i = 0; i < ARRAY_SIZE(exts); i++) {
1130 - clear_incremental_midx_files_ext(source, exts[i],
1131 - hashes, hashes_nr);
1132 - for (j = 0; j < hashes_nr; j++)
1133 - clear_midx_files_ext(source, exts[i], hashes[j]);
1128 + clear_incremental_midx_files_ext(source, exts[i], hashes);
1129 + if (hashes) {
1130 + for (size_t j = 0; j < hashes->nr; j++)
1131 + clear_midx_files_ext(source, exts[i],
1132 + hashes->v[j]);
1133 + }
1134 }
1135
1136 if (incremental)
@@ -1267,8 +1267,7 @@ static int write_midx_internal(struct write_midx_opts *opts)
1267 int pack_name_concat_len = 0;
1268 int dropped_packs = 0;
1269 int result = -1;
1270 - const char **keep_hashes = NULL;
1271 - size_t keep_hashes_nr = 0;
1270 + struct strvec keep_hashes = STRVEC_INIT;
1271 struct chunkfile *cf;
1272
1273 trace2_region_enter("midx", "write_midx_internal", r);
@@ -1708,32 +1707,12 @@ static int write_midx_internal(struct write_midx_opts *opts)
1707 if (ctx.num_multi_pack_indexes_before == UINT32_MAX)
1708 die(_("too many multi-pack-indexes"));
1709
1711 - if (ctx.compact) {
1712 - struct multi_pack_index *m;
1713 -
1714 - /*
1715 - * Keep all MIDX layers excluding those in the range [from, to].
1716 - */
1717 - for (m = ctx.base_midx; m; m = m->base_midx)
1718 - keep_hashes_nr++;
1719 - for (m = ctx.m;
1720 - m && midx_hashcmp(m, ctx.compact_to, r->hash_algo);
1721 - m = m->base_midx)
1722 - keep_hashes_nr++;
1723 -
1724 - keep_hashes_nr++; /* include the compacted layer */
1725 - } else {
1726 - keep_hashes_nr = ctx.num_multi_pack_indexes_before + 1;
1727 - }
1728 - CALLOC_ARRAY(keep_hashes, keep_hashes_nr);
1729 -
1710 if (ctx.incremental) {
1711 FILE *chainf = fdopen_lock_file(&lk, "w");
1712 struct strbuf final_midx_name = STRBUF_INIT;
1713 struct multi_pack_index *m = ctx.base_midx;
1714 struct multi_pack_index **layers = NULL;
1715 size_t layers_nr = 0, layers_alloc = 0;
1736 - size_t j = 0;
1716
1717 if (!chainf) {
1718 error_errno(_("unable to open multi-pack-index chain file"));
@@ -1761,12 +1740,12 @@ static int write_midx_internal(struct write_midx_opts *opts)
1740 layers[layers_nr++] = mp;
1741 }
1742 while (layers_nr)
1764 - keep_hashes[j++] =
1765 - xstrdup(midx_get_checksum_hex(layers[--layers_nr]));
1743 + strvec_push(&keep_hashes,
1744 + midx_get_checksum_hex(layers[--layers_nr]));
1745
1767 - keep_hashes[j++] =
1768 - xstrdup(hash_to_hex_algop(midx_hash,
1769 - r->hash_algo));
1746 + strvec_push(&keep_hashes,
1747 + hash_to_hex_algop(midx_hash,
1748 + r->hash_algo));
1749
1750 for (mp = ctx.m;
1751 mp && midx_hashcmp(mp, ctx.compact_to,
@@ -1776,31 +1755,29 @@ static int write_midx_internal(struct write_midx_opts *opts)
1755 layers[layers_nr++] = mp;
1756 }
1757 while (layers_nr)
1779 - keep_hashes[j++] =
1780 - xstrdup(midx_get_checksum_hex(layers[--layers_nr]));
1758 + strvec_push(&keep_hashes,
1759 + midx_get_checksum_hex(layers[--layers_nr]));
1760 } else {
1761 for (; m; m = m->base_midx) {
1762 ALLOC_GROW(layers, layers_nr + 1, layers_alloc);
1763 layers[layers_nr++] = m;
1764 }
1765 while (layers_nr)
1787 - keep_hashes[j++] =
1788 - xstrdup(midx_get_checksum_hex(layers[--layers_nr]));
1766 + strvec_push(&keep_hashes,
1767 + midx_get_checksum_hex(layers[--layers_nr]));
1768
1790 - keep_hashes[j++] =
1791 - xstrdup(hash_to_hex_algop(midx_hash,
1792 - r->hash_algo));
1769 + strvec_push(&keep_hashes,
1770 + hash_to_hex_algop(midx_hash,
1771 + r->hash_algo));
1772 }
1773
1795 - ASSERT(j == keep_hashes_nr);
1796 -
1774 free(layers);
1775
1799 - for (uint32_t i = 0; i < j; i++)
1800 - fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
1776 + for (size_t i = 0; i < keep_hashes.nr; i++)
1777 + fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes.v[i]);
1778 } else {
1802 - keep_hashes[ctx.num_multi_pack_indexes_before] =
1803 - xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
1779 + strvec_push(&keep_hashes,
1780 + hash_to_hex_algop(midx_hash, r->hash_algo));
1781 }
1782
1783 if (ctx.m || ctx.base_midx)
@@ -1809,8 +1786,7 @@ static int write_midx_internal(struct write_midx_opts *opts)
1786 if (commit_lock_file(&lk) < 0)
1787 die_errno(_("could not write multi-pack-index"));
1788
1812 - clear_midx_files(opts->source, keep_hashes, keep_hashes_nr,
1813 - ctx.incremental);
1789 + clear_midx_files(opts->source, &keep_hashes, ctx.incremental);
1790 result = 0;
1791
1792 cleanup:
@@ -1826,11 +1802,7 @@ cleanup:
1802 free(ctx.entries);
1803 free(ctx.pack_perm);
1804 free(ctx.pack_order);
1829 - if (keep_hashes) {
1830 - for (uint32_t i = 0; i < keep_hashes_nr; i++)
1831 - free((char *)keep_hashes[i]);
1832 - free(keep_hashes);
1833 - }
1805 + strvec_clear(&keep_hashes);
1806 strbuf_release(&midx_name);
1807 close_midx(midx_to_free);
1808
midx.c
+10 -10
@@ -12,6 +12,7 @@
12 #include "chunk-format.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 +#include "strvec.h"
16
17 #define MIDX_PACK_ERROR ((void *)(intptr_t)-1)
18
@@ -19,8 +20,7 @@ int midx_checksum_valid(struct multi_pack_index *m);
20 void clear_midx_files_ext(struct odb_source *source, const char *ext,
21 const char *keep_hash);
22 void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
22 - char **keep_hashes,
23 - uint32_t hashes_nr);
23 + const struct strvec *keep_hashes);
24 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
25 const char *idx_name);
26
@@ -799,22 +799,22 @@ void clear_midx_files_ext(struct odb_source *source, const char *ext,
799 }
800
801 void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
802 - char **keep_hashes,
803 - uint32_t hashes_nr)
802 + const struct strvec *keep_hashes)
803 {
804 struct clear_midx_data data = {
805 .keep = STRSET_INIT,
806 .ext = ext,
807 };
808 struct strbuf buf = STRBUF_INIT;
810 - uint32_t i;
809
812 - for (i = 0; i < hashes_nr; i++) {
813 - strbuf_reset(&buf);
814 - strbuf_addf(&buf, "multi-pack-index-%s.%s", keep_hashes[i],
815 - ext);
810 + if (keep_hashes) {
811 + for (size_t i = 0; i < keep_hashes->nr; i++) {
812 + strbuf_reset(&buf);
813 + strbuf_addf(&buf, "multi-pack-index-%s.%s",
814 + keep_hashes->v[i], ext);
815
817 - strset_add(&data.keep, buf.buf);
816 + strset_add(&data.keep, buf.buf);
817 + }
818 }
819
820 for_each_file_in_pack_subdir(source->path, "multi-pack-index.d",