pack-revindex: stop depending on `the_repository`

There are multiple sites in "pack-revindex.c" where we use the global `the_repository` variable, either explicitly or implicitly by using `the_hash_algo`. In all of those cases we already have a repository available in the calling context though. Refactor the code to instead use the caller-provided repository and remove the `USE_THE_REPOSITORY_VARIABLE` define. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 10, 2025 at 08:13 UTC e5957ca321802339c9710acadae2138262701e5d
1 file changed +20 -14
pack-revindex.c
+20 -14
@@ -1,5 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
2 -
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "pack-revindex.h"
@@ -9,6 +7,7 @@
7 #include "strbuf.h"
8 #include "trace2.h"
9 #include "parse.h"
10 +#include "repository.h"
11 #include "midx.h"
12 #include "csum-file.h"
13
@@ -137,7 +136,7 @@ static void create_pack_revindex(struct packed_git *p)
136 const unsigned num_ent = p->num_objects;
137 unsigned i;
138 const char *index = p->index_data;
140 - const unsigned hashsz = the_hash_algo->rawsz;
139 + const unsigned hashsz = p->repo->hash_algo->rawsz;
140
141 ALLOC_ARRAY(p->revindex, num_ent + 1);
142 index += 4 * 256;
@@ -193,7 +192,11 @@ static char *pack_revindex_filename(struct packed_git *p)
192 }
193
194 #define RIDX_HEADER_SIZE (12)
196 -#define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz))
195 +
196 +static size_t ridx_min_size(const struct git_hash_algo *algo)
197 +{
198 + return RIDX_HEADER_SIZE + (2 * algo->rawsz);
199 +}
200
201 struct revindex_header {
202 uint32_t signature;
@@ -201,7 +204,8 @@ struct revindex_header {
204 uint32_t hash_id;
205 };
206
204 -static int load_revindex_from_disk(char *revindex_name,
207 +static int load_revindex_from_disk(const struct git_hash_algo *algo,
208 + char *revindex_name,
209 uint32_t num_objects,
210 const uint32_t **data_p, size_t *len_p)
211 {
@@ -228,12 +232,12 @@ static int load_revindex_from_disk(char *revindex_name,
232
233 revindex_size = xsize_t(st.st_size);
234
231 - if (revindex_size < RIDX_MIN_SIZE) {
235 + if (revindex_size < ridx_min_size(algo)) {
236 ret = error(_("reverse-index file %s is too small"), revindex_name);
237 goto cleanup;
238 }
239
236 - if (revindex_size - RIDX_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) {
240 + if (revindex_size - ridx_min_size(algo) != st_mult(sizeof(uint32_t), num_objects)) {
241 ret = error(_("reverse-index file %s is corrupt"), revindex_name);
242 goto cleanup;
243 }
@@ -279,7 +283,8 @@ int load_pack_revindex_from_disk(struct packed_git *p)
283
284 revindex_name = pack_revindex_filename(p);
285
282 - ret = load_revindex_from_disk(revindex_name,
286 + ret = load_revindex_from_disk(p->repo->hash_algo,
287 + revindex_name,
288 p->num_objects,
289 &p->revindex_map,
290 &p->revindex_size);
@@ -322,7 +327,7 @@ int verify_pack_revindex(struct packed_git *p)
327 if (!p->revindex_map || !p->revindex_data)
328 return res;
329
325 - if (!hashfile_checksum_valid(the_repository->hash_algo,
330 + if (!hashfile_checksum_valid(p->repo->hash_algo,
331 (const unsigned char *)p->revindex_map, p->revindex_size)) {
332 error(_("invalid checksum"));
333 res = -1;
@@ -375,19 +380,20 @@ int load_midx_revindex(struct multi_pack_index *m)
380 * not want to accidentally call munmap() in the middle of the
381 * MIDX.
382 */
378 - trace2_data_string("load_midx_revindex", the_repository,
383 + trace2_data_string("load_midx_revindex", m->repo,
384 "source", "midx");
385 m->revindex_data = (const uint32_t *)m->chunk_revindex;
386 return 0;
387 }
388
384 - trace2_data_string("load_midx_revindex", the_repository,
389 + trace2_data_string("load_midx_revindex", m->repo,
390 "source", "rev");
391
392 get_midx_filename_ext(m->repo->hash_algo, &revindex_name, m->object_dir,
393 get_midx_checksum(m), MIDX_EXT_REV);
394
390 - ret = load_revindex_from_disk(revindex_name.buf,
395 + ret = load_revindex_from_disk(m->repo->hash_algo,
396 + revindex_name.buf,
397 m->num_objects,
398 &m->revindex_map,
399 &m->revindex_len);
@@ -419,7 +425,7 @@ int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
425 {
426 unsigned lo, hi;
427
422 - if (load_pack_revindex(the_repository, p) < 0)
428 + if (load_pack_revindex(p->repo, p) < 0)
429 return -1;
430
431 lo = 0;
@@ -465,7 +471,7 @@ off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
471 if (p->revindex)
472 return p->revindex[pos].offset;
473 else if (pos == p->num_objects)
468 - return p->pack_size - the_hash_algo->rawsz;
474 + return p->pack_size - p->repo->hash_algo->rawsz;
475 else
476 return nth_packed_object_offset(p, pack_pos_to_index(p, pos));
477 }