midx: extract bitmap write setup

The write_midx_bitmap() method is a long method that does a lot of steps. It requires the write_midx_context struct for use in prepare_midx_packing_data() and find_commits_for_midx_bitmap(), but after that only needs the pack_order array. This is a messy, but completely non-functional refactoring. The code is only being moved around to reduce visibility of the write_midx_context during the longest part of computing reachability bitmaps. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 19, 2022 at 15:26 UTC 90b2bb710da47f12967a6f6a32640c197ca82685
1 file changed +32 -24
midx.c
+32 -24
@@ -1053,40 +1053,35 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
1053 return cb.commits;
1054 }
1055
1056 -static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
1057 - struct write_midx_context *ctx,
1056 +static int write_midx_bitmap(const char *midx_name,
1057 + const unsigned char *midx_hash,
1058 + struct packing_data *pdata,
1059 + struct commit **commits,
1060 + uint32_t commits_nr,
1061 + uint32_t *pack_order,
1062 const char *refs_snapshot,
1063 unsigned flags)
1064 {
1061 - struct packing_data pdata;
1062 - struct pack_idx_entry **index;
1063 - struct commit **commits = NULL;
1064 - uint32_t i, commits_nr;
1065 + int ret, i;
1066 uint16_t options = 0;
1066 - char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name, hash_to_hex(midx_hash));
1067 - int ret;
1068 -
1069 - if (!ctx->entries_nr)
1070 - BUG("cannot write a bitmap without any objects");
1067 + struct pack_idx_entry **index;
1068 + char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
1069 + hash_to_hex(midx_hash));
1070
1071 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
1072 options |= BITMAP_OPT_HASH_CACHE;
1073
1075 - prepare_midx_packing_data(&pdata, ctx);
1076 -
1077 - commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, ctx);
1078 -
1074 /*
1075 * Build the MIDX-order index based on pdata.objects (which is already
1076 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1077 * this order).
1078 */
1084 - ALLOC_ARRAY(index, pdata.nr_objects);
1085 - for (i = 0; i < pdata.nr_objects; i++)
1086 - index[i] = &pdata.objects[i].idx;
1079 + ALLOC_ARRAY(index, pdata->nr_objects);
1080 + for (i = 0; i < pdata->nr_objects; i++)
1081 + index[i] = &pdata->objects[i].idx;
1082
1083 bitmap_writer_show_progress(flags & MIDX_PROGRESS);
1089 - bitmap_writer_build_type_index(&pdata, index, pdata.nr_objects);
1084 + bitmap_writer_build_type_index(pdata, index, pdata->nr_objects);
1085
1086 /*
1087 * bitmap_writer_finish expects objects in lex order, but pack_order
@@ -1101,16 +1096,16 @@ static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
1096 * happens between bitmap_writer_build_type_index() and
1097 * bitmap_writer_finish().
1098 */
1104 - for (i = 0; i < pdata.nr_objects; i++)
1105 - index[ctx->pack_order[i]] = &pdata.objects[i].idx;
1099 + for (i = 0; i < pdata->nr_objects; i++)
1100 + index[pack_order[i]] = &pdata->objects[i].idx;
1101
1102 bitmap_writer_select_commits(commits, commits_nr, -1);
1108 - ret = bitmap_writer_build(&pdata);
1103 + ret = bitmap_writer_build(pdata);
1104 if (ret < 0)
1105 goto cleanup;
1106
1107 bitmap_writer_set_checksum(midx_hash);
1113 - bitmap_writer_finish(index, pdata.nr_objects, bitmap_name, options);
1108 + bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options);
1109
1110 cleanup:
1111 free(index);
@@ -1443,8 +1438,21 @@ static int write_midx_internal(const char *object_dir,
1438 if (flags & MIDX_WRITE_REV_INDEX &&
1439 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1440 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1441 +
1442 if (flags & MIDX_WRITE_BITMAP) {
1447 - if (write_midx_bitmap(midx_name.buf, midx_hash, &ctx,
1443 + struct packing_data pdata;
1444 + struct commit **commits;
1445 + uint32_t commits_nr;
1446 +
1447 + if (!ctx.entries_nr)
1448 + BUG("cannot write a bitmap without any objects");
1449 +
1450 + prepare_midx_packing_data(&pdata, &ctx);
1451 +
1452 + commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1453 +
1454 + if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1455 + commits, commits_nr, ctx.pack_order,
1456 refs_snapshot, flags) < 0) {
1457 error(_("could not write multi-pack bitmap"));
1458 result = 1;