Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
e5957ca3 7ebf19ce

+20 -14
+20 -14
pack-revindex.c
··· 1 - #define USE_THE_REPOSITORY_VARIABLE 2 - 3 1 #include "git-compat-util.h" 4 2 #include "gettext.h" 5 3 #include "pack-revindex.h" ··· 9 7 #include "strbuf.h" 10 8 #include "trace2.h" 11 9 #include "parse.h" 10 + #include "repository.h" 12 11 #include "midx.h" 13 12 #include "csum-file.h" 14 13 ··· 137 136 const unsigned num_ent = p->num_objects; 138 137 unsigned i; 139 138 const char *index = p->index_data; 140 - const unsigned hashsz = the_hash_algo->rawsz; 139 + const unsigned hashsz = p->repo->hash_algo->rawsz; 141 140 142 141 ALLOC_ARRAY(p->revindex, num_ent + 1); 143 142 index += 4 * 256; ··· 193 192 } 194 193 195 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 + } 197 200 198 201 struct revindex_header { 199 202 uint32_t signature; ··· 201 204 uint32_t hash_id; 202 205 }; 203 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, 205 209 uint32_t num_objects, 206 210 const uint32_t **data_p, size_t *len_p) 207 211 { ··· 228 232 229 233 revindex_size = xsize_t(st.st_size); 230 234 231 - if (revindex_size < RIDX_MIN_SIZE) { 235 + if (revindex_size < ridx_min_size(algo)) { 232 236 ret = error(_("reverse-index file %s is too small"), revindex_name); 233 237 goto cleanup; 234 238 } 235 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)) { 237 241 ret = error(_("reverse-index file %s is corrupt"), revindex_name); 238 242 goto cleanup; 239 243 } ··· 279 283 280 284 revindex_name = pack_revindex_filename(p); 281 285 282 - ret = load_revindex_from_disk(revindex_name, 286 + ret = load_revindex_from_disk(p->repo->hash_algo, 287 + revindex_name, 283 288 p->num_objects, 284 289 &p->revindex_map, 285 290 &p->revindex_size); ··· 322 327 if (!p->revindex_map || !p->revindex_data) 323 328 return res; 324 329 325 - if (!hashfile_checksum_valid(the_repository->hash_algo, 330 + if (!hashfile_checksum_valid(p->repo->hash_algo, 326 331 (const unsigned char *)p->revindex_map, p->revindex_size)) { 327 332 error(_("invalid checksum")); 328 333 res = -1; ··· 375 380 * not want to accidentally call munmap() in the middle of the 376 381 * MIDX. 377 382 */ 378 - trace2_data_string("load_midx_revindex", the_repository, 383 + trace2_data_string("load_midx_revindex", m->repo, 379 384 "source", "midx"); 380 385 m->revindex_data = (const uint32_t *)m->chunk_revindex; 381 386 return 0; 382 387 } 383 388 384 - trace2_data_string("load_midx_revindex", the_repository, 389 + trace2_data_string("load_midx_revindex", m->repo, 385 390 "source", "rev"); 386 391 387 392 get_midx_filename_ext(m->repo->hash_algo, &revindex_name, m->object_dir, 388 393 get_midx_checksum(m), MIDX_EXT_REV); 389 394 390 - ret = load_revindex_from_disk(revindex_name.buf, 395 + ret = load_revindex_from_disk(m->repo->hash_algo, 396 + revindex_name.buf, 391 397 m->num_objects, 392 398 &m->revindex_map, 393 399 &m->revindex_len); ··· 419 425 { 420 426 unsigned lo, hi; 421 427 422 - if (load_pack_revindex(the_repository, p) < 0) 428 + if (load_pack_revindex(p->repo, p) < 0) 423 429 return -1; 424 430 425 431 lo = 0; ··· 465 471 if (p->revindex) 466 472 return p->revindex[pos].offset; 467 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; 469 475 else 470 476 return nth_packed_object_offset(p, pack_pos_to_index(p, pos)); 471 477 }