midx-write: pass down repository to static functions

In 'midx-write.c' there are a lot of static functions which use global variables `the_repository` or `the_hash_algo`. In a follow up commit, the repository variable will be added to `write_midx_context`, which some of the functions can use. But for functions which do not have access to this struct, pass down the required information from non-static functions `write_midx_file` and `write_midx_file_only`. This requires that the function `hash_to_hex` is also replaced with `hash_to_hex_algop` since the former internally accesses the `the_hash_algo` global variable. This ensures that the usage of global variables is limited to these non-static functions, which will be cleaned up in a follow up commit. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Nov 27, 2024 at 17:28 UTC 3687a4b3e1c8edac080480677f35a5d267fee6e1
1 file changed +31 -26
midx-write.c
+31 -26
@@ -35,13 +35,13 @@ extern void clear_incremental_midx_files_ext(const char *object_dir,
35 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
36 const char *idx_name);
37
38 -static size_t write_midx_header(struct hashfile *f,
39 - unsigned char num_chunks,
38 +static size_t write_midx_header(const struct git_hash_algo *hash_algo,
39 + struct hashfile *f, unsigned char num_chunks,
40 uint32_t num_packs)
41 {
42 hashwrite_be32(f, MIDX_SIGNATURE);
43 hashwrite_u8(f, MIDX_VERSION);
44 - hashwrite_u8(f, oid_version(the_hash_algo));
44 + hashwrite_u8(f, oid_version(hash_algo));
45 hashwrite_u8(f, num_chunks);
46 hashwrite_u8(f, 0); /* unused */
47 hashwrite_be32(f, num_packs);
@@ -702,7 +702,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
702 return 0;
703 }
704
705 - if (!peel_iterated_oid(the_repository, oid, &peeled))
705 + if (!peel_iterated_oid(revs->repo, oid, &peeled))
706 oid = &peeled;
707
708 object = parse_object_or_die(oid, refname);
@@ -827,7 +827,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
827 return cb.commits;
828 }
829
830 -static int write_midx_bitmap(const char *midx_name,
830 +static int write_midx_bitmap(struct repository *r, const char *midx_name,
831 const unsigned char *midx_hash,
832 struct packing_data *pdata,
833 struct commit **commits,
@@ -840,9 +840,9 @@ static int write_midx_bitmap(const char *midx_name,
840 struct bitmap_writer writer;
841 struct pack_idx_entry **index;
842 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
843 - hash_to_hex(midx_hash));
843 + hash_to_hex_algop(midx_hash, r->hash_algo));
844
845 - trace2_region_enter("midx", "write_midx_bitmap", the_repository);
845 + trace2_region_enter("midx", "write_midx_bitmap", r);
846
847 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
848 options |= BITMAP_OPT_HASH_CACHE;
@@ -859,7 +859,7 @@ static int write_midx_bitmap(const char *midx_name,
859 for (i = 0; i < pdata->nr_objects; i++)
860 index[i] = &pdata->objects[i].idx;
861
862 - bitmap_writer_init(&writer, the_repository, pdata);
862 + bitmap_writer_init(&writer, r, pdata);
863 bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
864 bitmap_writer_build_type_index(&writer, index);
865
@@ -892,7 +892,7 @@ cleanup:
892 free(bitmap_name);
893 bitmap_writer_free(&writer);
894
895 - trace2_region_leave("midx", "write_midx_bitmap", the_repository);
895 + trace2_region_leave("midx", "write_midx_bitmap", r);
896
897 return ret;
898 }
@@ -1049,7 +1049,7 @@ static void clear_midx_files(const char *object_dir,
1049 strbuf_release(&buf);
1050 }
1051
1052 -static int write_midx_internal(const char *object_dir,
1052 +static int write_midx_internal(struct repository *r, const char *object_dir,
1053 struct string_list *packs_to_include,
1054 struct string_list *packs_to_drop,
1055 const char *preferred_pack_name,
@@ -1070,7 +1070,8 @@ static int write_midx_internal(const char *object_dir,
1070 const char **keep_hashes = NULL;
1071 struct chunkfile *cf;
1072
1073 - trace2_region_enter("midx", "write_midx_internal", the_repository);
1073 + trace2_region_enter("midx", "write_midx_internal", r);
1074 +
1075
1076 ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
1077 if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1087,8 +1088,7 @@ static int write_midx_internal(const char *object_dir,
1088 midx_name.buf);
1089
1090 if (!packs_to_include || ctx.incremental) {
1090 - struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
1091 - object_dir);
1091 + struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1092 if (m && !midx_checksum_valid(m)) {
1093 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1094 m = NULL;
@@ -1351,7 +1351,7 @@ static int write_midx_internal(const char *object_dir,
1351 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1352 write_midx_oid_fanout);
1353 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1354 - st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1354 + st_mult(ctx.entries_nr, r->hash_algo->rawsz),
1355 write_midx_oid_lookup);
1356 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1357 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
@@ -1373,7 +1373,8 @@ static int write_midx_internal(const char *object_dir,
1373 write_midx_bitmapped_packs);
1374 }
1375
1376 - write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1376 + write_midx_header(r->hash_algo, f, get_num_chunks(cf),
1377 + ctx.nr - dropped_packs);
1378 write_chunkfile(cf, &ctx);
1379
1380 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
@@ -1405,7 +1406,7 @@ static int write_midx_internal(const char *object_dir,
1406 FREE_AND_NULL(ctx.entries);
1407 ctx.entries_nr = 0;
1408
1408 - if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1409 + if (write_midx_bitmap(r, midx_name.buf, midx_hash, &pdata,
1410 commits, commits_nr, ctx.pack_order,
1411 flags) < 0) {
1412 error(_("could not write multi-pack bitmap"));
@@ -1449,12 +1450,13 @@ static int write_midx_internal(const char *object_dir,
1450 strbuf_release(&final_midx_name);
1451
1452 keep_hashes[ctx.num_multi_pack_indexes_before] =
1452 - xstrdup(hash_to_hex(midx_hash));
1453 + xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
1454
1455 for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1456 uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
1457
1457 - keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
1458 + keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
1459 + r->hash_algo));
1460 m = m->base_midx;
1461 }
1462
@@ -1462,7 +1464,7 @@ static int write_midx_internal(const char *object_dir,
1464 fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
1465 } else {
1466 keep_hashes[ctx.num_multi_pack_indexes_before] =
1465 - xstrdup(hash_to_hex(midx_hash));
1467 + xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
1468 }
1469
1470 if (ctx.m || ctx.base_midx)
@@ -1495,7 +1497,7 @@ cleanup:
1497 }
1498 strbuf_release(&midx_name);
1499
1498 - trace2_region_leave("midx", "write_midx_internal", the_repository);
1500 + trace2_region_leave("midx", "write_midx_internal", r);
1501
1502 return result;
1503 }
@@ -1505,8 +1507,8 @@ int write_midx_file(const char *object_dir,
1507 const char *refs_snapshot,
1508 unsigned flags)
1509 {
1508 - return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1509 - refs_snapshot, flags);
1510 + return write_midx_internal(the_repository, object_dir, NULL, NULL,
1511 + preferred_pack_name, refs_snapshot, flags);
1512 }
1513
1514 int write_midx_file_only(const char *object_dir,
@@ -1515,8 +1517,9 @@ int write_midx_file_only(const char *object_dir,
1517 const char *refs_snapshot,
1518 unsigned flags)
1519 {
1518 - return write_midx_internal(object_dir, packs_to_include, NULL,
1519 - preferred_pack_name, refs_snapshot, flags);
1520 + return write_midx_internal(the_repository, object_dir, packs_to_include,
1521 + NULL, preferred_pack_name, refs_snapshot,
1522 + flags);
1523 }
1524
1525 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
@@ -1572,7 +1575,8 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
1575 free(count);
1576
1577 if (packs_to_drop.nr)
1575 - result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
1578 + result = write_midx_internal(r, object_dir, NULL,
1579 + &packs_to_drop, NULL, NULL, flags);
1580
1581 string_list_clear(&packs_to_drop, 0);
1582
@@ -1769,7 +1773,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
1773 goto cleanup;
1774 }
1775
1772 - result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
1776 + result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
1777 + flags);
1778
1779 cleanup:
1780 free(include_pack);