midx: rename pack_info to write_midx_context

In an effort to streamline our chunk-based file formats, align some of
the code structure in write_midx_internal() to be similar to the
patterns in write_commit_graph_file().

Specifically, let's create a "struct write_midx_context" that can be
used as a data parameter to abstract function types.

This change only renames "struct pack_info" to "struct
write_midx_context" and the names of instances from "packs" to "ctx". In
future changes, we will expand the data inside "struct
write_midx_context" and align our chunk-writing method with the
chunk-format API.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Derrick Stolee 2021-02-18 14:07:26 +00:00 committed by Junio C Hamano
parent 47410aa837
commit 577dc49696
1 changed files with 62 additions and 62 deletions

124
midx.c
View File

@ -451,7 +451,7 @@ static int pack_info_compare(const void *_a, const void *_b)
return strcmp(a->pack_name, b->pack_name); return strcmp(a->pack_name, b->pack_name);
} }


struct pack_list { struct write_midx_context {
struct pack_info *info; struct pack_info *info;
uint32_t nr; uint32_t nr;
uint32_t alloc; uint32_t alloc;
@ -463,37 +463,37 @@ struct pack_list {
static void add_pack_to_midx(const char *full_path, size_t full_path_len, static void add_pack_to_midx(const char *full_path, size_t full_path_len,
const char *file_name, void *data) const char *file_name, void *data)
{ {
struct pack_list *packs = (struct pack_list *)data; struct write_midx_context *ctx = data;


if (ends_with(file_name, ".idx")) { if (ends_with(file_name, ".idx")) {
display_progress(packs->progress, ++packs->pack_paths_checked); display_progress(ctx->progress, ++ctx->pack_paths_checked);
if (packs->m && midx_contains_pack(packs->m, file_name)) if (ctx->m && midx_contains_pack(ctx->m, file_name))
return; return;


ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc); ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);


packs->info[packs->nr].p = add_packed_git(full_path, ctx->info[ctx->nr].p = add_packed_git(full_path,
full_path_len, full_path_len,
0); 0);


if (!packs->info[packs->nr].p) { if (!ctx->info[ctx->nr].p) {
warning(_("failed to add packfile '%s'"), warning(_("failed to add packfile '%s'"),
full_path); full_path);
return; return;
} }


if (open_pack_index(packs->info[packs->nr].p)) { if (open_pack_index(ctx->info[ctx->nr].p)) {
warning(_("failed to open pack-index '%s'"), warning(_("failed to open pack-index '%s'"),
full_path); full_path);
close_pack(packs->info[packs->nr].p); close_pack(ctx->info[ctx->nr].p);
FREE_AND_NULL(packs->info[packs->nr].p); FREE_AND_NULL(ctx->info[ctx->nr].p);
return; return;
} }


packs->info[packs->nr].pack_name = xstrdup(file_name); ctx->info[ctx->nr].pack_name = xstrdup(file_name);
packs->info[packs->nr].orig_pack_int_id = packs->nr; ctx->info[ctx->nr].orig_pack_int_id = ctx->nr;
packs->info[packs->nr].expired = 0; ctx->info[ctx->nr].expired = 0;
packs->nr++; ctx->nr++;
} }
} }


@ -801,7 +801,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
uint32_t i; uint32_t i;
struct hashfile *f = NULL; struct hashfile *f = NULL;
struct lock_file lk; struct lock_file lk;
struct pack_list packs; struct write_midx_context ctx = { 0 };
uint32_t *pack_perm = NULL; uint32_t *pack_perm = NULL;
uint64_t written = 0; uint64_t written = 0;
uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1]; uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
@ -820,40 +820,40 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
midx_name); midx_name);


if (m) if (m)
packs.m = m; ctx.m = m;
else else
packs.m = load_multi_pack_index(object_dir, 1); ctx.m = load_multi_pack_index(object_dir, 1);


packs.nr = 0; ctx.nr = 0;
packs.alloc = packs.m ? packs.m->num_packs : 16; ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
packs.info = NULL; ctx.info = NULL;
ALLOC_ARRAY(packs.info, packs.alloc); ALLOC_ARRAY(ctx.info, ctx.alloc);


if (packs.m) { if (ctx.m) {
for (i = 0; i < packs.m->num_packs; i++) { for (i = 0; i < ctx.m->num_packs; i++) {
ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc); ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);


packs.info[packs.nr].orig_pack_int_id = i; ctx.info[ctx.nr].orig_pack_int_id = i;
packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]); ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]);
packs.info[packs.nr].p = NULL; ctx.info[ctx.nr].p = NULL;
packs.info[packs.nr].expired = 0; ctx.info[ctx.nr].expired = 0;
packs.nr++; ctx.nr++;
} }
} }


packs.pack_paths_checked = 0; ctx.pack_paths_checked = 0;
if (flags & MIDX_PROGRESS) if (flags & MIDX_PROGRESS)
packs.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0); ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
else else
packs.progress = NULL; ctx.progress = NULL;


for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs); for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
stop_progress(&packs.progress); stop_progress(&ctx.progress);


if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop) if (ctx.m && ctx.nr == ctx.m->num_packs && !packs_to_drop)
goto cleanup; goto cleanup;


entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries); entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &nr_entries);


for (i = 0; i < nr_entries; i++) { for (i = 0; i < nr_entries; i++) {
if (entries[i].offset > 0x7fffffff) if (entries[i].offset > 0x7fffffff)
@ -862,19 +862,19 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
large_offsets_needed = 1; large_offsets_needed = 1;
} }


QSORT(packs.info, packs.nr, pack_info_compare); QSORT(ctx.info, ctx.nr, pack_info_compare);


if (packs_to_drop && packs_to_drop->nr) { if (packs_to_drop && packs_to_drop->nr) {
int drop_index = 0; int drop_index = 0;
int missing_drops = 0; int missing_drops = 0;


for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) { for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
int cmp = strcmp(packs.info[i].pack_name, int cmp = strcmp(ctx.info[i].pack_name,
packs_to_drop->items[drop_index].string); packs_to_drop->items[drop_index].string);


if (!cmp) { if (!cmp) {
drop_index++; drop_index++;
packs.info[i].expired = 1; ctx.info[i].expired = 1;
} else if (cmp > 0) { } else if (cmp > 0) {
error(_("did not see pack-file %s to drop"), error(_("did not see pack-file %s to drop"),
packs_to_drop->items[drop_index].string); packs_to_drop->items[drop_index].string);
@ -882,7 +882,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
missing_drops++; missing_drops++;
i--; i--;
} else { } else {
packs.info[i].expired = 0; ctx.info[i].expired = 0;
} }
} }


@ -898,19 +898,19 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
* *
* pack_perm[old_id] = new_id * pack_perm[old_id] = new_id
*/ */
ALLOC_ARRAY(pack_perm, packs.nr); ALLOC_ARRAY(pack_perm, ctx.nr);
for (i = 0; i < packs.nr; i++) { for (i = 0; i < ctx.nr; i++) {
if (packs.info[i].expired) { if (ctx.info[i].expired) {
dropped_packs++; dropped_packs++;
pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED; pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
} else { } else {
pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs; pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
} }
} }


for (i = 0; i < packs.nr; i++) { for (i = 0; i < ctx.nr; i++) {
if (!packs.info[i].expired) if (!ctx.info[i].expired)
pack_name_concat_len += strlen(packs.info[i].pack_name) + 1; pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
} }


if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
@ -921,19 +921,19 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
FREE_AND_NULL(midx_name); FREE_AND_NULL(midx_name);


if (packs.m) if (ctx.m)
close_midx(packs.m); close_midx(ctx.m);


cur_chunk = 0; cur_chunk = 0;
num_chunks = large_offsets_needed ? 5 : 4; num_chunks = large_offsets_needed ? 5 : 4;


if (packs.nr - dropped_packs == 0) { if (ctx.nr - dropped_packs == 0) {
error(_("no pack files to index.")); error(_("no pack files to index."));
result = 1; result = 1;
goto cleanup; goto cleanup;
} }


written = write_midx_header(f, num_chunks, packs.nr - dropped_packs); written = write_midx_header(f, num_chunks, ctx.nr - dropped_packs);


chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES; chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH; chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
@ -990,7 +990,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *


switch (chunk_ids[i]) { switch (chunk_ids[i]) {
case MIDX_CHUNKID_PACKNAMES: case MIDX_CHUNKID_PACKNAMES:
written += write_midx_pack_names(f, packs.info, packs.nr); written += write_midx_pack_names(f, ctx.info, ctx.nr);
break; break;


case MIDX_CHUNKID_OIDFANOUT: case MIDX_CHUNKID_OIDFANOUT:
@ -1027,15 +1027,15 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
commit_lock_file(&lk); commit_lock_file(&lk);


cleanup: cleanup:
for (i = 0; i < packs.nr; i++) { for (i = 0; i < ctx.nr; i++) {
if (packs.info[i].p) { if (ctx.info[i].p) {
close_pack(packs.info[i].p); close_pack(ctx.info[i].p);
free(packs.info[i].p); free(ctx.info[i].p);
} }
free(packs.info[i].pack_name); free(ctx.info[i].pack_name);
} }


free(packs.info); free(ctx.info);
free(entries); free(entries);
free(pack_perm); free(pack_perm);
free(midx_name); free(midx_name);