write-midx: add repository field to write_midx_context

The struct `write_midx_context` is used to pass context for creating
MIDX files. Add the repository field here to ensure that most functions
within `midx-write.c` have access to the field and can use that instead
of the global `the_repository` variable.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Karthik Nayak
2024-11-27 17:28:28 +01:00
committed by Junio C Hamano
parent 20df8141f5
commit dfa7c68245

View File

@ -110,6 +110,8 @@ struct write_midx_context {
uint32_t num_multi_pack_indexes_before; uint32_t num_multi_pack_indexes_before;
struct string_list *to_include; struct string_list *to_include;
struct repository *repo;
}; };
static int should_include_pack(const struct write_midx_context *ctx, static int should_include_pack(const struct write_midx_context *ctx,
@ -154,7 +156,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
return; return;
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
p = add_packed_git(the_repository, full_path, full_path_len, 0); p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
if (!p) { if (!p) {
warning(_("failed to add packfile '%s'"), warning(_("failed to add packfile '%s'"),
full_path); full_path);
@ -480,7 +482,7 @@ static int write_midx_oid_lookup(struct hashfile *f,
void *data) void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
unsigned char hash_len = the_hash_algo->rawsz; unsigned char hash_len = ctx->repo->hash_algo->rawsz;
struct pack_midx_entry *list = ctx->entries; struct pack_midx_entry *list = ctx->entries;
uint32_t i; uint32_t i;
@ -605,7 +607,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
uint32_t *pack_order, base_objects = 0; uint32_t *pack_order, base_objects = 0;
uint32_t i; uint32_t i;
trace2_region_enter("midx", "midx_pack_order", the_repository); trace2_region_enter("midx", "midx_pack_order", ctx->repo);
if (ctx->incremental && ctx->base_midx) if (ctx->incremental && ctx->base_midx)
base_objects = ctx->base_midx->num_objects + base_objects = ctx->base_midx->num_objects +
@ -640,7 +642,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
} }
free(data); free(data);
trace2_region_leave("midx", "midx_pack_order", the_repository); trace2_region_leave("midx", "midx_pack_order", ctx->repo);
return pack_order; return pack_order;
} }
@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
char *tmp_file; char *tmp_file;
trace2_region_enter("midx", "write_midx_reverse_index", the_repository); trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash)); strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
ctx->repo->hash_algo));
tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr, tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
midx_hash, WRITE_REV); midx_hash, WRITE_REV);
@ -664,7 +667,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
strbuf_release(&buf); strbuf_release(&buf);
free(tmp_file); free(tmp_file);
trace2_region_leave("midx", "write_midx_reverse_index", the_repository); trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
} }
static void prepare_midx_packing_data(struct packing_data *pdata, static void prepare_midx_packing_data(struct packing_data *pdata,
@ -672,10 +675,10 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
{ {
uint32_t i; uint32_t i;
trace2_region_enter("midx", "prepare_midx_packing_data", the_repository); trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
memset(pdata, 0, sizeof(struct packing_data)); memset(pdata, 0, sizeof(struct packing_data));
prepare_packing_data(the_repository, pdata); prepare_packing_data(ctx->repo, pdata);
for (i = 0; i < ctx->entries_nr; i++) { for (i = 0; i < ctx->entries_nr; i++) {
uint32_t pos = ctx->pack_order[i]; uint32_t pos = ctx->pack_order[i];
@ -686,7 +689,7 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
ctx->info[ctx->pack_perm[from->pack_int_id]].p); ctx->info[ctx->pack_perm[from->pack_int_id]].p);
} }
trace2_region_leave("midx", "prepare_midx_packing_data", the_repository); trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
} }
static int add_ref_to_pending(const char *refname, const char *referent UNUSED, static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
@ -784,17 +787,16 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
struct rev_info revs; struct rev_info revs;
struct bitmap_commit_cb cb = {0}; struct bitmap_commit_cb cb = {0};
trace2_region_enter("midx", "find_commits_for_midx_bitmap", trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
the_repository);
cb.ctx = ctx; cb.ctx = ctx;
repo_init_revisions(the_repository, &revs, NULL); repo_init_revisions(ctx->repo, &revs, NULL);
if (refs_snapshot) { if (refs_snapshot) {
read_refs_snapshot(refs_snapshot, &revs); read_refs_snapshot(refs_snapshot, &revs);
} else { } else {
setup_revisions(0, NULL, &revs, NULL); setup_revisions(0, NULL, &revs, NULL);
refs_for_each_ref(get_main_ref_store(the_repository), refs_for_each_ref(get_main_ref_store(ctx->repo),
add_ref_to_pending, &revs); add_ref_to_pending, &revs);
} }
@ -822,8 +824,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
release_revisions(&revs); release_revisions(&revs);
trace2_region_leave("midx", "find_commits_for_midx_bitmap", trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
the_repository);
return cb.commits; return cb.commits;
} }
@ -945,7 +946,7 @@ static int fill_packs_from_midx(struct write_midx_context *ctx,
*/ */
if (flags & MIDX_WRITE_REV_INDEX || if (flags & MIDX_WRITE_REV_INDEX ||
preferred_pack_name) { preferred_pack_name) {
if (prepare_midx_pack(the_repository, m, if (prepare_midx_pack(ctx->repo, m,
m->num_packs_in_base + i)) { m->num_packs_in_base + i)) {
error(_("could not load pack")); error(_("could not load pack"));
return 1; return 1;
@ -1073,6 +1074,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
trace2_region_enter("midx", "write_midx_internal", r); trace2_region_enter("midx", "write_midx_internal", r);
ctx.repo = r;
ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL); ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
if (ctx.incremental && (flags & MIDX_WRITE_BITMAP)) if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@ -1469,7 +1471,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
} }
if (ctx.m || ctx.base_midx) if (ctx.m || ctx.base_midx)
close_object_store(the_repository->objects); close_object_store(ctx.repo->objects);
if (commit_lock_file(&lk) < 0) if (commit_lock_file(&lk) < 0)
die_errno(_("could not write multi-pack-index")); die_errno(_("could not write multi-pack-index"));