midx: add pack_perm to write_midx_context

In an effort to align write_midx_internal() with the chunk-format API, continue to group necessary data into "struct write_midx_context". This change collects the "uint32_t *pack_perm" and large_offsets_needed bit into the context. Update write_midx_object_offsets() to match chunk_write_fn. 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 7a3ada1192bd251e530a668f63a65b4befa076d0
1 file changed +21 -19
midx.c
+21 -19
@@ -461,6 +461,9 @@ struct write_midx_context {
461
462 struct pack_midx_entry *entries;
463 uint32_t entries_nr;
464 +
465 + uint32_t *pack_perm;
466 + unsigned large_offsets_needed:1;
467 };
468
469 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
@@ -736,27 +739,27 @@ static size_t write_midx_oid_lookup(struct hashfile *f,
739 return written;
740 }
741
739 -static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
740 - uint32_t *perm,
741 - struct pack_midx_entry *objects, uint32_t nr_objects)
742 +static size_t write_midx_object_offsets(struct hashfile *f,
743 + void *data)
744 {
743 - struct pack_midx_entry *list = objects;
745 + struct write_midx_context *ctx = data;
746 + struct pack_midx_entry *list = ctx->entries;
747 uint32_t i, nr_large_offset = 0;
748 size_t written = 0;
749
747 - for (i = 0; i < nr_objects; i++) {
750 + for (i = 0; i < ctx->entries_nr; i++) {
751 struct pack_midx_entry *obj = list++;
752
750 - if (perm[obj->pack_int_id] == PACK_EXPIRED)
753 + if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
754 BUG("object %s is in an expired pack with int-id %d",
755 oid_to_hex(&obj->oid),
756 obj->pack_int_id);
757
755 - hashwrite_be32(f, perm[obj->pack_int_id]);
758 + hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
759
757 - if (large_offset_needed && obj->offset >> 31)
760 + if (ctx->large_offsets_needed && obj->offset >> 31)
761 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
759 - else if (!large_offset_needed && obj->offset >> 32)
762 + else if (!ctx->large_offsets_needed && obj->offset >> 32)
763 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
764 oid_to_hex(&obj->oid),
765 obj->offset);
@@ -805,13 +808,11 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
808 struct hashfile *f = NULL;
809 struct lock_file lk;
810 struct write_midx_context ctx = { 0 };
808 - uint32_t *pack_perm = NULL;
811 uint64_t written = 0;
812 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
813 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
814 uint32_t num_large_offsets = 0;
815 struct progress *progress = NULL;
814 - int large_offsets_needed = 0;
816 int pack_name_concat_len = 0;
817 int dropped_packs = 0;
818 int result = 0;
@@ -857,11 +858,12 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
858
859 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr);
860
861 + ctx.large_offsets_needed = 0;
862 for (i = 0; i < ctx.entries_nr; i++) {
863 if (ctx.entries[i].offset > 0x7fffffff)
864 num_large_offsets++;
865 if (ctx.entries[i].offset > 0xffffffff)
864 - large_offsets_needed = 1;
866 + ctx.large_offsets_needed = 1;
867 }
868
869 QSORT(ctx.info, ctx.nr, pack_info_compare);
@@ -900,13 +902,13 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
902 *
903 * pack_perm[old_id] = new_id
904 */
903 - ALLOC_ARRAY(pack_perm, ctx.nr);
905 + ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
906 for (i = 0; i < ctx.nr; i++) {
907 if (ctx.info[i].expired) {
908 dropped_packs++;
907 - pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
909 + ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
910 } else {
909 - pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
911 + ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
912 }
913 }
914
@@ -927,7 +929,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
929 close_midx(ctx.m);
930
931 cur_chunk = 0;
930 - num_chunks = large_offsets_needed ? 5 : 4;
932 + num_chunks = ctx.large_offsets_needed ? 5 : 4;
933
934 if (ctx.nr - dropped_packs == 0) {
935 error(_("no pack files to index."));
@@ -954,7 +956,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
956
957 cur_chunk++;
958 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
957 - if (large_offsets_needed) {
959 + if (ctx.large_offsets_needed) {
960 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
961
962 cur_chunk++;
@@ -1004,7 +1006,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
1006 break;
1007
1008 case MIDX_CHUNKID_OBJECTOFFSETS:
1007 - written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, ctx.entries, ctx.entries_nr);
1009 + written += write_midx_object_offsets(f, &ctx);
1010 break;
1011
1012 case MIDX_CHUNKID_LARGEOFFSETS:
@@ -1039,7 +1041,7 @@ cleanup:
1041
1042 free(ctx.info);
1043 free(ctx.entries);
1042 - free(pack_perm);
1044 + free(ctx.pack_perm);
1045 free(midx_name);
1046 return result;
1047 }