midx: pass down `hash_algo` to functions using global variables
The functions `get_split_midx_filename_ext()`, `get_midx_filename()` and `get_midx_filename_ext()` use `hash_to_hex()` which internally uses the `the_hash_algo` global variable. Remove this dependency on global variables by passing down the `hash_algo` through to the functions mentioned and instead calling `hash_to_hex_algop()` along with the obtained `hash_algo`. 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
f59de71cf700f9f8da27023ec8b5df117f99d9c8
5 files changed
+37
-29
midx-write.c
+11
-11
@@ -991,9 +991,10 @@ static int link_midx_to_chain(struct multi_pack_index *m)
991
for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
992
const unsigned char *hash = get_midx_checksum(m);
993
994
- get_midx_filename_ext(&from, m->object_dir, hash,
995
- midx_exts[i].non_split);
996
- get_split_midx_filename_ext(&to, m->object_dir, hash,
994
+ get_midx_filename_ext(m->repo->hash_algo, &from, m->object_dir,
995
+ hash, midx_exts[i].non_split);
996
+ get_split_midx_filename_ext(m->repo->hash_algo, &to,
997
+ m->object_dir, hash,
998
midx_exts[i].split);
999
1000
if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1012,9 +1013,8 @@ done:
1013
return ret;
1014
}
1015
1015
-static void clear_midx_files(const char *object_dir,
1016
- const char **hashes,
1017
- uint32_t hashes_nr,
1016
+static void clear_midx_files(struct repository *r, const char *object_dir,
1017
+ const char **hashes, uint32_t hashes_nr,
1018
unsigned incremental)
1019
{
1020
/*
@@ -1039,7 +1039,7 @@ static void clear_midx_files(const char *object_dir,
1039
}
1040
1041
if (incremental)
1042
- get_midx_filename(&buf, object_dir);
1042
+ get_midx_filename(r->hash_algo, &buf, object_dir);
1043
else
1044
get_midx_chain_filename(&buf, object_dir);
1045
@@ -1083,7 +1083,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1083
"%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1084
object_dir);
1085
else
1086
- get_midx_filename(&midx_name, object_dir);
1086
+ get_midx_filename(r->hash_algo, &midx_name, object_dir);
1087
if (safe_create_leading_directories(midx_name.buf))
1088
die_errno(_("unable to create leading directories of %s"),
1089
midx_name.buf);
@@ -1440,8 +1440,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1440
if (link_midx_to_chain(ctx.base_midx) < 0)
1441
return -1;
1442
1443
- get_split_midx_filename_ext(&final_midx_name, object_dir,
1444
- midx_hash, MIDX_EXT_MIDX);
1443
+ get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
1444
+ object_dir, midx_hash, MIDX_EXT_MIDX);
1445
1446
if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1447
error_errno(_("unable to rename new multi-pack-index layer"));
@@ -1474,7 +1474,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1474
if (commit_lock_file(&lk) < 0)
1475
die_errno(_("could not write multi-pack-index"));
1476
1477
- clear_midx_files(object_dir, keep_hashes,
1477
+ clear_midx_files(r, object_dir, keep_hashes,
1478
ctx.num_multi_pack_indexes_before + 1,
1479
ctx.incremental);
1480
midx.c
+15
-11
@@ -28,17 +28,19 @@ const unsigned char *get_midx_checksum(struct multi_pack_index *m)
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)
31
+void get_midx_filename(const struct git_hash_algo *hash_algo,
32
+ struct strbuf *out, const char *object_dir)
33
{
33
- get_midx_filename_ext(out, object_dir, NULL, NULL);
34
+ get_midx_filename_ext(hash_algo, out, object_dir, NULL, NULL);
35
}
36
36
-void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
37
+void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
38
+ struct strbuf *out, const char *object_dir,
39
const unsigned char *hash, const char *ext)
40
{
41
strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
42
if (ext)
41
- strbuf_addf(out, "-%s.%s", hash_to_hex(hash), ext);
43
+ strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, hash_algo), ext);
44
}
45
46
static int midx_read_oid_fanout(const unsigned char *chunk_start,
@@ -234,11 +236,13 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
236
strbuf_addstr(buf, "/multi-pack-index-chain");
237
}
238
237
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
239
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
240
+ struct strbuf *buf, const char *object_dir,
241
const unsigned char *hash, const char *ext)
242
{
243
get_midx_chain_dirname(buf, object_dir);
241
- strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
244
+ strbuf_addf(buf, "/multi-pack-index-%s.%s",
245
+ hash_to_hex_algop(hash, hash_algo), ext);
246
}
247
248
static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -326,8 +330,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
330
valid = 0;
331
332
strbuf_reset(&buf);
329
- get_split_midx_filename_ext(&buf, object_dir, layer.hash,
330
- MIDX_EXT_MIDX);
333
+ get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
334
+ layer.hash, MIDX_EXT_MIDX);
335
m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
336
337
if (m) {
@@ -379,7 +383,7 @@ struct multi_pack_index *load_multi_pack_index(struct repository *r,
383
struct strbuf midx_name = STRBUF_INIT;
384
struct multi_pack_index *m;
385
382
- get_midx_filename(&midx_name, object_dir);
386
+ get_midx_filename(r->hash_algo, &midx_name, object_dir);
387
388
m = load_multi_pack_index_one(r, object_dir,
389
midx_name.buf, local);
@@ -822,7 +826,7 @@ void clear_midx_file(struct repository *r)
826
{
827
struct strbuf midx = STRBUF_INIT;
828
825
- get_midx_filename(&midx, r->objects->odb->path);
829
+ get_midx_filename(r->hash_algo, &midx, r->objects->odb->path);
830
831
if (r->objects && r->objects->multi_pack_index) {
832
close_midx(r->objects->multi_pack_index);
@@ -891,7 +895,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
895
struct stat sb;
896
struct strbuf filename = STRBUF_INIT;
897
894
- get_midx_filename(&filename, object_dir);
898
+ get_midx_filename(r->hash_algo, &filename, object_dir);
899
900
if (!stat(filename.buf, &sb)) {
901
error(_("multi-pack-index file exists, but failed to parse"));
midx.h
+7
-3
@@ -7,6 +7,7 @@ struct object_id;
7
struct pack_entry;
8
struct repository;
9
struct bitmapped_pack;
10
+struct git_hash_algo;
11
12
#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
13
#define MIDX_VERSION 1
@@ -89,12 +90,15 @@ struct multi_pack_index {
90
#define MIDX_EXT_MIDX "midx"
91
92
const unsigned char *get_midx_checksum(struct multi_pack_index *m);
92
-void get_midx_filename(struct strbuf *out, const char *object_dir);
93
-void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
93
+void get_midx_filename(const struct git_hash_algo *hash_algo,
94
+ struct strbuf *out, const char *object_dir);
95
+void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
96
+ struct strbuf *out, const char *object_dir,
97
const unsigned char *hash, const char *ext);
98
void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
99
void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
97
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
100
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
101
+ struct strbuf *buf, const char *object_dir,
102
const unsigned char *hash, const char *ext);
103
104
struct multi_pack_index *load_multi_pack_index(struct repository *r,
pack-bitmap.c
+3
-3
@@ -375,8 +375,8 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
375
char *midx_bitmap_filename(struct multi_pack_index *midx)
376
{
377
struct strbuf buf = STRBUF_INIT;
378
- get_midx_filename_ext(&buf, midx->object_dir, get_midx_checksum(midx),
379
- MIDX_EXT_BITMAP);
378
+ get_midx_filename_ext(midx->repo->hash_algo, &buf, midx->object_dir,
379
+ get_midx_checksum(midx), MIDX_EXT_BITMAP);
380
381
return strbuf_detach(&buf, NULL);
382
}
@@ -415,7 +415,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
415
416
if (bitmap_git->pack || bitmap_git->midx) {
417
struct strbuf buf = STRBUF_INIT;
418
- get_midx_filename(&buf, midx->object_dir);
418
+ get_midx_filename(midx->repo->hash_algo, &buf, midx->object_dir);
419
trace2_data_string("bitmap", bitmap_repo(bitmap_git),
420
"ignoring extra midx bitmap file", buf.buf);
421
close(fd);
pack-revindex.c
+1
-1
@@ -383,7 +383,7 @@ int load_midx_revindex(struct multi_pack_index *m)
383
trace2_data_string("load_midx_revindex", the_repository,
384
"source", "rev");
385
386
- get_midx_filename_ext(&revindex_name, m->object_dir,
386
+ get_midx_filename_ext(m->repo->hash_algo, &revindex_name, m->object_dir,
387
get_midx_checksum(m), MIDX_EXT_REV);
388
389
ret = load_revindex_from_disk(revindex_name.buf,