+70
-30
index c1eed04691..40abe3868c 100644
--- a/midx-write.c
+++ b/midx-write.c
strbuf_release(&buf);
}
-static bool midx_needs_update(struct write_midx_context *ctx)
+static bool midx_needs_update(struct multi_pack_index *midx, struct write_midx_context *ctx)
{
- struct multi_pack_index *midx = ctx->m;
+ struct strset packs = STRSET_INIT;
+ struct strbuf buf = STRBUF_INIT;
bool needed = true;
/*
if (ctx->incremental)
goto out;
- /*
- * If there is no MIDX then either it doesn't exist, or we're doing a
- * geometric repack. We cannot (yet) determine whether we need to
- * update the multi-pack index in the second case.
- */
- if (!midx)
- goto out;
-
/*
* Otherwise, we need to verify that the packs covered by the existing
- * MIDX match the packs that we already have. This test is somewhat
- * lenient and will be fixed.
+ * MIDX match the packs that we already have. The logic to do so is way
+ * more complicated than it has any right to be. This is because:
+ *
+ * - We cannot assume any ordering.
+ *
+ * - The MIDX packs may not be loaded at all, and loading them would
+ * be wasteful. So we need to use the pack names tracked by the
+ * MIDX itself.
+ *
+ * - The MIDX pack names are tracking the ".idx" files, whereas the
+ * packs themselves are tracking the ".pack" files. So we need to
+ * strip suffixes.
*/
if (ctx->nr != midx->num_packs + midx->num_packs_in_base)
goto out;
+ for (uint32_t i = 0; i < ctx->nr; i++) {
+ strbuf_reset(&buf);
+ strbuf_addstr(&buf, pack_basename(ctx->info[i].p));
+ strbuf_strip_suffix(&buf, ".pack");
+
+ if (!strset_add(&packs, buf.buf))
+ BUG("same pack added twice?");
+ }
+
+ for (uint32_t i = 0; i < ctx->nr; i++) {
+ strbuf_reset(&buf);
+ strbuf_addstr(&buf, midx->pack_names[i]);
+ strbuf_strip_suffix(&buf, ".idx");
+
+ if (!strset_contains(&packs, buf.buf))
+ goto out;
+ strset_remove(&packs, buf.buf);
+ }
+
needed = false;
out:
+ strbuf_release(&buf);
+ strset_clear(&packs);
return needed;
}
struct write_midx_context ctx = {
.preferred_pack_idx = NO_PREFERRED_PACK,
};
+ struct multi_pack_index *midx_to_free = NULL;
int bitmapped_packs_concat_len = 0;
int pack_name_concat_len = 0;
int dropped_packs = 0;
for_each_file_in_pack_dir(source->path, add_pack_to_midx, &ctx);
stop_progress(&ctx.progress);
- if (!packs_to_include && !packs_to_drop && !midx_needs_update(&ctx)) {
- struct bitmap_index *bitmap_git;
- int bitmap_exists;
- int want_bitmap = flags & MIDX_WRITE_BITMAP;
-
- bitmap_git = prepare_midx_bitmap_git(ctx.m);
- bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
- free_bitmap_index(bitmap_git);
-
- if (bitmap_exists || !want_bitmap) {
- /*
- * The correct MIDX already exists, and so does a
- * corresponding bitmap (or one wasn't requested).
- */
- if (!want_bitmap)
- clear_midx_files_ext(source, "bitmap", NULL);
- result = 0;
- goto cleanup;
+ if (!packs_to_drop) {
+ /*
+ * If there is no MIDX then either it doesn't exist, or we're
+ * doing a geometric repack. Try to load it from the source to
+ * tell these two cases apart.
+ */
+ struct multi_pack_index *midx = ctx.m;
+ if (!midx)
+ midx = midx_to_free = load_multi_pack_index(ctx.source);
+
+ if (midx && !midx_needs_update(midx, &ctx)) {
+ struct bitmap_index *bitmap_git;
+ int bitmap_exists;
+ int want_bitmap = flags & MIDX_WRITE_BITMAP;
+
+ bitmap_git = prepare_midx_bitmap_git(midx);
+ bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
+ free_bitmap_index(bitmap_git);
+
+ if (bitmap_exists || !want_bitmap) {
+ /*
+ * The correct MIDX already exists, and so does a
+ * corresponding bitmap (or one wasn't requested).
+ */
+ if (!want_bitmap)
+ clear_midx_files_ext(source, "bitmap", NULL);
+ result = 0;
+ goto cleanup;
+ }
}
+
+ close_midx(midx_to_free);
+ midx_to_free = NULL;
}
if (ctx.incremental && !ctx.nr) {
free(keep_hashes);
}
strbuf_release(&midx_name);
+ close_midx(midx_to_free);
trace2_region_leave("midx", "write_midx_internal", r);