midx: cleanup internal usage of `the_repository` and `the_hash_algo`
In the `midx.c` file, there are multiple usages of `the_repository` and `the_hash_algo` within static functions of the file. Some of the usages can be simply swapped out with the available `repository` struct. While some of them can be swapped out by passing the repository to the required functions. This leaves out only some other usages of `the_repository` and `the_hash_algo` in the file in non-static functions, which we'll tackle in upcoming commits. 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
fae9bae7091958255fa7729f6cedb8cc4f9a3387
1 file changed
+27
-22
midx.c
+27
-22
@@ -25,7 +25,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
25
26
const unsigned char *get_midx_checksum(struct multi_pack_index *m)
27
{
28
- return m->data + m->data_len - the_hash_algo->rawsz;
28
+ return m->data + m->data_len - m->repo->hash_algo->rawsz;
29
}
30
31
void get_midx_filename(struct strbuf *out, const char *object_dir)
@@ -94,7 +94,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
94
95
#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
96
97
-static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
97
+static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
98
+ const char *object_dir,
99
const char *midx_name,
100
int local)
101
{
@@ -131,7 +132,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
132
m->data = midx_map;
133
m->data_len = midx_size;
134
m->local = local;
134
- m->repo = the_repository;
135
+ m->repo = r;
136
137
m->signature = get_be32(m->data);
138
if (m->signature != MIDX_SIGNATURE)
@@ -144,12 +145,12 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
145
m->version);
146
147
hash_version = m->data[MIDX_BYTE_HASH_VERSION];
147
- if (hash_version != oid_version(the_hash_algo)) {
148
+ if (hash_version != oid_version(r->hash_algo)) {
149
error(_("multi-pack-index hash version %u does not match version %u"),
149
- hash_version, oid_version(the_hash_algo));
150
+ hash_version, oid_version(r->hash_algo));
151
goto cleanup_fail;
152
}
152
- m->hash_len = the_hash_algo->rawsz;
153
+ m->hash_len = r->hash_algo->rawsz;
154
155
m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
156
@@ -206,8 +207,8 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
207
m->pack_names[i]);
208
}
209
209
- trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
210
- trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
210
+ trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
211
+ trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
212
213
free_chunkfile(cf);
214
return m;
@@ -240,8 +241,9 @@ void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
241
strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
242
}
243
243
-static int open_multi_pack_index_chain(const char *chain_file,
244
- int *fd, struct stat *st)
244
+static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
245
+ const char *chain_file, int *fd,
246
+ struct stat *st)
247
{
248
*fd = git_open(chain_file);
249
if (*fd < 0)
@@ -250,7 +252,7 @@ static int open_multi_pack_index_chain(const char *chain_file,
252
close(*fd);
253
return 0;
254
}
253
- if (st->st_size < the_hash_algo->hexsz) {
255
+ if (st->st_size < hash_algo->hexsz) {
256
close(*fd);
257
if (!st->st_size) {
258
/* treat empty files the same as missing */
@@ -292,7 +294,8 @@ static int add_midx_to_chain(struct multi_pack_index *midx,
294
return 1;
295
}
296
295
-static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
297
+static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
298
+ const char *object_dir,
299
int local,
300
int fd, struct stat *st,
301
int *incomplete_chain)
@@ -303,7 +306,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
306
uint32_t i, count;
307
FILE *fp = xfdopen(fd, "r");
308
306
- count = st->st_size / (the_hash_algo->hexsz + 1);
309
+ count = st->st_size / (r->hash_algo->hexsz + 1);
310
311
for (i = 0; i < count; i++) {
312
struct multi_pack_index *m;
@@ -312,7 +315,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
315
if (strbuf_getline_lf(&buf, fp) == EOF)
316
break;
317
315
- if (get_oid_hex(buf.buf, &layer)) {
318
+ if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
319
warning(_("invalid multi-pack-index chain: line '%s' "
320
"not a hash"),
321
buf.buf);
@@ -325,7 +328,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
328
strbuf_reset(&buf);
329
get_split_midx_filename_ext(&buf, object_dir, layer.hash,
330
MIDX_EXT_MIDX);
328
- m = load_multi_pack_index_one(object_dir, buf.buf, local);
331
+ m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
332
333
if (m) {
334
if (add_midx_to_chain(m, midx_chain)) {
@@ -348,7 +351,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
351
return midx_chain;
352
}
353
351
-static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
354
+static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
355
+ const char *object_dir,
356
int local)
357
{
358
struct strbuf chain_file = STRBUF_INIT;
@@ -357,10 +361,10 @@ static struct multi_pack_index *load_multi_pack_index_chain(const char *object_d
361
struct multi_pack_index *m = NULL;
362
363
get_midx_chain_filename(&chain_file, object_dir);
360
- if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
364
+ if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
365
int incomplete;
366
/* ownership of fd is taken over by load function */
363
- m = load_midx_chain_fd_st(object_dir, local, fd, &st,
367
+ m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
368
&incomplete);
369
}
370
@@ -376,9 +380,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
380
381
get_midx_filename(&midx_name, object_dir);
382
379
- m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
383
+ m = load_multi_pack_index_one(the_repository, object_dir,
384
+ midx_name.buf, local);
385
if (!m)
381
- m = load_multi_pack_index_chain(object_dir, local);
386
+ m = load_multi_pack_index_chain(the_repository, object_dir, local);
387
388
strbuf_release(&midx_name);
389
@@ -520,7 +525,7 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
525
uint32_t *result)
526
{
527
int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
523
- m->chunk_oid_lookup, the_hash_algo->rawsz,
528
+ m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
529
result);
530
if (result)
531
*result += m->num_objects_in_base;
@@ -551,7 +556,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
556
n = midx_for_object(&m, n);
557
558
oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
554
- the_repository->hash_algo);
559
+ m->repo->hash_algo);
560
return oid;
561
}
562