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>

Derrick Stolee committed Feb 18, 2021 at 14:07 UTC 577dc49696afee67fb507e0bd72be4c8677b83c2
1 file changed +65 -65
midx.c
+65 -65
@@ -451,7 +451,7 @@ static int pack_info_compare(const void *_a, const void *_b)
451 return strcmp(a->pack_name, b->pack_name);
452 }
453
454 -struct pack_list {
454 +struct write_midx_context {
455 struct pack_info *info;
456 uint32_t nr;
457 uint32_t alloc;
@@ -463,37 +463,37 @@ struct pack_list {
463 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
464 const char *file_name, void *data)
465 {
466 - struct pack_list *packs = (struct pack_list *)data;
466 + struct write_midx_context *ctx = data;
467
468 if (ends_with(file_name, ".idx")) {
469 - display_progress(packs->progress, ++packs->pack_paths_checked);
470 - if (packs->m && midx_contains_pack(packs->m, file_name))
469 + display_progress(ctx->progress, ++ctx->pack_paths_checked);
470 + if (ctx->m && midx_contains_pack(ctx->m, file_name))
471 return;
472
473 - ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc);
473 + ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
474
475 - packs->info[packs->nr].p = add_packed_git(full_path,
476 - full_path_len,
477 - 0);
475 + ctx->info[ctx->nr].p = add_packed_git(full_path,
476 + full_path_len,
477 + 0);
478
479 - if (!packs->info[packs->nr].p) {
479 + if (!ctx->info[ctx->nr].p) {
480 warning(_("failed to add packfile '%s'"),
481 full_path);
482 return;
483 }
484
485 - if (open_pack_index(packs->info[packs->nr].p)) {
485 + if (open_pack_index(ctx->info[ctx->nr].p)) {
486 warning(_("failed to open pack-index '%s'"),
487 full_path);
488 - close_pack(packs->info[packs->nr].p);
489 - FREE_AND_NULL(packs->info[packs->nr].p);
488 + close_pack(ctx->info[ctx->nr].p);
489 + FREE_AND_NULL(ctx->info[ctx->nr].p);
490 return;
491 }
492
493 - packs->info[packs->nr].pack_name = xstrdup(file_name);
494 - packs->info[packs->nr].orig_pack_int_id = packs->nr;
495 - packs->info[packs->nr].expired = 0;
496 - packs->nr++;
493 + ctx->info[ctx->nr].pack_name = xstrdup(file_name);
494 + ctx->info[ctx->nr].orig_pack_int_id = ctx->nr;
495 + ctx->info[ctx->nr].expired = 0;
496 + ctx->nr++;
497 }
498 }
499
@@ -801,7 +801,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
801 uint32_t i;
802 struct hashfile *f = NULL;
803 struct lock_file lk;
804 - struct pack_list packs;
804 + struct write_midx_context ctx = { 0 };
805 uint32_t *pack_perm = NULL;
806 uint64_t written = 0;
807 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 *
820 midx_name);
821
822 if (m)
823 - packs.m = m;
823 + ctx.m = m;
824 else
825 - packs.m = load_multi_pack_index(object_dir, 1);
826 -
827 - packs.nr = 0;
828 - packs.alloc = packs.m ? packs.m->num_packs : 16;
829 - packs.info = NULL;
830 - ALLOC_ARRAY(packs.info, packs.alloc);
831 -
832 - if (packs.m) {
833 - for (i = 0; i < packs.m->num_packs; i++) {
834 - ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc);
835 -
836 - packs.info[packs.nr].orig_pack_int_id = i;
837 - packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]);
838 - packs.info[packs.nr].p = NULL;
839 - packs.info[packs.nr].expired = 0;
840 - packs.nr++;
825 + ctx.m = load_multi_pack_index(object_dir, 1);
826 +
827 + ctx.nr = 0;
828 + ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
829 + ctx.info = NULL;
830 + ALLOC_ARRAY(ctx.info, ctx.alloc);
831 +
832 + if (ctx.m) {
833 + for (i = 0; i < ctx.m->num_packs; i++) {
834 + ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
835 +
836 + ctx.info[ctx.nr].orig_pack_int_id = i;
837 + ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]);
838 + ctx.info[ctx.nr].p = NULL;
839 + ctx.info[ctx.nr].expired = 0;
840 + ctx.nr++;
841 }
842 }
843
844 - packs.pack_paths_checked = 0;
844 + ctx.pack_paths_checked = 0;
845 if (flags & MIDX_PROGRESS)
846 - packs.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
846 + ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
847 else
848 - packs.progress = NULL;
848 + ctx.progress = NULL;
849
850 - for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
851 - stop_progress(&packs.progress);
850 + for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
851 + stop_progress(&ctx.progress);
852
853 - if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop)
853 + if (ctx.m && ctx.nr == ctx.m->num_packs && !packs_to_drop)
854 goto cleanup;
855
856 - entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries);
856 + entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &nr_entries);
857
858 for (i = 0; i < nr_entries; i++) {
859 if (entries[i].offset > 0x7fffffff)
@@ -862,19 +862,19 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
862 large_offsets_needed = 1;
863 }
864
865 - QSORT(packs.info, packs.nr, pack_info_compare);
865 + QSORT(ctx.info, ctx.nr, pack_info_compare);
866
867 if (packs_to_drop && packs_to_drop->nr) {
868 int drop_index = 0;
869 int missing_drops = 0;
870
871 - for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) {
872 - int cmp = strcmp(packs.info[i].pack_name,
871 + for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
872 + int cmp = strcmp(ctx.info[i].pack_name,
873 packs_to_drop->items[drop_index].string);
874
875 if (!cmp) {
876 drop_index++;
877 - packs.info[i].expired = 1;
877 + ctx.info[i].expired = 1;
878 } else if (cmp > 0) {
879 error(_("did not see pack-file %s to drop"),
880 packs_to_drop->items[drop_index].string);
@@ -882,7 +882,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
882 missing_drops++;
883 i--;
884 } else {
885 - packs.info[i].expired = 0;
885 + ctx.info[i].expired = 0;
886 }
887 }
888
@@ -898,19 +898,19 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
898 *
899 * pack_perm[old_id] = new_id
900 */
901 - ALLOC_ARRAY(pack_perm, packs.nr);
902 - for (i = 0; i < packs.nr; i++) {
903 - if (packs.info[i].expired) {
901 + ALLOC_ARRAY(pack_perm, ctx.nr);
902 + for (i = 0; i < ctx.nr; i++) {
903 + if (ctx.info[i].expired) {
904 dropped_packs++;
905 - pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED;
905 + pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
906 } else {
907 - pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs;
907 + pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
908 }
909 }
910
911 - for (i = 0; i < packs.nr; i++) {
912 - if (!packs.info[i].expired)
913 - pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
911 + for (i = 0; i < ctx.nr; i++) {
912 + if (!ctx.info[i].expired)
913 + pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
914 }
915
916 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 *
921 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
922 FREE_AND_NULL(midx_name);
923
924 - if (packs.m)
925 - close_midx(packs.m);
924 + if (ctx.m)
925 + close_midx(ctx.m);
926
927 cur_chunk = 0;
928 num_chunks = large_offsets_needed ? 5 : 4;
929
930 - if (packs.nr - dropped_packs == 0) {
930 + if (ctx.nr - dropped_packs == 0) {
931 error(_("no pack files to index."));
932 result = 1;
933 goto cleanup;
934 }
935
936 - written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
936 + written = write_midx_header(f, num_chunks, ctx.nr - dropped_packs);
937
938 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
939 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 *
990
991 switch (chunk_ids[i]) {
992 case MIDX_CHUNKID_PACKNAMES:
993 - written += write_midx_pack_names(f, packs.info, packs.nr);
993 + written += write_midx_pack_names(f, ctx.info, ctx.nr);
994 break;
995
996 case MIDX_CHUNKID_OIDFANOUT:
@@ -1027,15 +1027,15 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
1027 commit_lock_file(&lk);
1028
1029 cleanup:
1030 - for (i = 0; i < packs.nr; i++) {
1031 - if (packs.info[i].p) {
1032 - close_pack(packs.info[i].p);
1033 - free(packs.info[i].p);
1030 + for (i = 0; i < ctx.nr; i++) {
1031 + if (ctx.info[i].p) {
1032 + close_pack(ctx.info[i].p);
1033 + free(ctx.info[i].p);
1034 }
1035 - free(packs.info[i].pack_name);
1035 + free(ctx.info[i].pack_name);
1036 }
1037
1038 - free(packs.info);
1038 + free(ctx.info);
1039 free(entries);
1040 free(pack_perm);
1041 free(midx_name);