midx-write: include packs above custom incremental base

The previous commit made '--base' take effect on the normal incremental write path, which exposed an existing assumption in our helper function `should_include_pack()`, which is that any pack already present in `ctx->m` was skipped. That is only correct for non-incremental writes. For incremental writes, `ctx->base_midx` is the boundary that should be excluded from the new layer. If the caller selects an older base, or no base at all, then packs from layers above that base have to be included in the detached layer so that its bitmap has reachability closure. Teach `should_include_pack()` to choose the MIDX used for pack exclusion based on whether or not we are performing an incremental write. When doing so, use `ctx->base_midx`, and use `ctx->m` otherwise. The t5334 cases from the previous commit can now be marked as successful. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jun 12, 2026 at 16:07 UTC 6afc679fa62362ddaab955778d973c85a6cbf004
2 files changed +13 -7
midx-write.c
+11 -5
@@ -133,8 +133,17 @@ static uint32_t midx_pack_perm(struct write_midx_context *ctx,
133 static int should_include_pack(const struct write_midx_context *ctx,
134 const char *file_name)
135 {
136 + struct multi_pack_index *m = ctx->m;
137 /*
137 - * Note that at most one of ctx->m and ctx->to_include are set,
138 + * When writing incrementally, ctx->m may contain layers above
139 + * the selected base MIDX, which must be included in the new
140 + * layer.
141 + */
142 + if (ctx->incremental)
143 + m = ctx->base_midx;
144 +
145 + /*
146 + * Note that at most one of m and ctx->to_include are set,
147 * so we are testing midx_contains_pack() and
148 * string_list_has_string() independently (guarded by the
149 * appropriate NULL checks).
@@ -148,10 +157,7 @@ static int should_include_pack(const struct write_midx_context *ctx,
157 * should be performed independently (likely checking
158 * to_include before the existing MIDX).
159 */
151 - if (ctx->m && midx_contains_pack(ctx->m, file_name))
152 - return 0;
153 - else if (ctx->base_midx && midx_contains_pack(ctx->base_midx,
154 - file_name))
160 + if (m && midx_contains_pack(m, file_name))
161 return 0;
162 else if (ctx->to_include &&
163 !string_list_has_string(ctx->to_include, file_name))
t/t5334-incremental-multi-pack-index.sh
+2 -2
@@ -119,7 +119,7 @@ test_expect_success 'write MIDX layer with --base without --no-write-chain-file'
119 test_grep "cannot use --base without --no-write-chain-file" err
120 '
121
122 -test_expect_failure 'write MIDX layer with --base=none and --no-write-chain-file' '
122 +test_expect_success 'write MIDX layer with --base=none and --no-write-chain-file' '
123 test_commit base-none &&
124 git repack -d &&
125
@@ -136,7 +136,7 @@ test_expect_failure 'write MIDX layer with --base=none and --no-write-chain-file
136 cp "$midx_chain.bak" "$midx_chain"
137 '
138
139 -test_expect_failure 'write MIDX layer with --base=<hash> and --no-write-chain-file' '
139 +test_expect_success 'write MIDX layer with --base=<hash> and --no-write-chain-file' '
140 test_commit base-hash &&
141 git repack -d &&
142