pack-write: pass hash_algo to `write_rev_file()`

The `write_rev_file()` function uses the global `the_hash_algo` variable to access the repository's hash_algo. To avoid global variable usage, pass a hash_algo from the layers above. Also modify children functions `write_rev_file_order()` and `write_rev_header()` to accept 'the_hash_algo'. Altough the layers above could have access to the hash_algo internally, simply pass in `the_hash_algo`. This avoids any compatibility issues and bubbles up global variable usage to upper layers which can be eventually resolved. However, in `midx-write.c`, since all usage of global variables is removed, don't reintroduce them and instead use the `repo` available in the context. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 19, 2025 at 12:19 UTC 6b2aa7fd371c93df44cebff072bef193104f43f8
4 files changed +29 -16
builtin/index-pack.c
+3 -3
@@ -2096,9 +2096,9 @@ int cmd_index_pack(int argc,
2096 curr_index = write_idx_file(the_hash_algo, index_name, idx_objects,
2097 nr_objects, &opts, pack_hash);
2098 if (rev_index)
2099 - curr_rev_index = write_rev_file(rev_index_name, idx_objects,
2100 - nr_objects, pack_hash,
2101 - opts.flags);
2099 + curr_rev_index = write_rev_file(the_hash_algo, rev_index_name,
2100 + idx_objects, nr_objects,
2101 + pack_hash, opts.flags);
2102 free(idx_objects);
2103
2104 if (!verify)
midx-write.c
+2 -2
@@ -658,8 +658,8 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
658 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
659 ctx->repo->hash_algo));
660
661 - tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
662 - midx_hash, WRITE_REV);
661 + tmp_file = write_rev_file_order(ctx->repo->hash_algo, NULL, ctx->pack_order,
662 + ctx->entries_nr, midx_hash, WRITE_REV);
663
664 if (finalize_object_file(tmp_file, buf.buf))
665 die(_("cannot store reverse index file"));
pack-write.c
+12 -9
@@ -194,11 +194,12 @@ static int pack_order_cmp(const void *va, const void *vb, void *ctx)
194 return 0;
195 }
196
197 -static void write_rev_header(struct hashfile *f)
197 +static void write_rev_header(const struct git_hash_algo *hash_algo,
198 + struct hashfile *f)
199 {
200 hashwrite_be32(f, RIDX_SIGNATURE);
201 hashwrite_be32(f, RIDX_VERSION);
201 - hashwrite_be32(f, oid_version(the_hash_algo));
202 + hashwrite_be32(f, oid_version(hash_algo));
203 }
204
205 static void write_rev_index_positions(struct hashfile *f,
@@ -215,7 +216,8 @@ static void write_rev_trailer(struct hashfile *f, const unsigned char *hash)
216 hashwrite(f, hash, the_hash_algo->rawsz);
217 }
218
218 -char *write_rev_file(const char *rev_name,
219 +char *write_rev_file(const struct git_hash_algo *hash_algo,
220 + const char *rev_name,
221 struct pack_idx_entry **objects,
222 uint32_t nr_objects,
223 const unsigned char *hash,
@@ -233,15 +235,16 @@ char *write_rev_file(const char *rev_name,
235 pack_order[i] = i;
236 QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
237
236 - ret = write_rev_file_order(rev_name, pack_order, nr_objects, hash,
237 - flags);
238 + ret = write_rev_file_order(hash_algo, rev_name, pack_order, nr_objects,
239 + hash, flags);
240
241 free(pack_order);
242
243 return ret;
244 }
245
244 -char *write_rev_file_order(const char *rev_name,
246 +char *write_rev_file_order(const struct git_hash_algo *hash_algo,
247 + const char *rev_name,
248 uint32_t *pack_order,
249 uint32_t nr_objects,
250 const unsigned char *hash,
@@ -280,7 +283,7 @@ char *write_rev_file_order(const char *rev_name,
283 return NULL;
284 }
285
283 - write_rev_header(f);
286 + write_rev_header(hash_algo, f);
287
288 write_rev_index_positions(f, pack_order, nr_objects);
289 write_rev_trailer(f, hash);
@@ -568,8 +571,8 @@ void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
571 if (adjust_shared_perm(*idx_tmp_name))
572 die_errno("unable to make temporary index file readable");
573
571 - rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
572 - pack_idx_opts->flags);
574 + rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written,
575 + hash, pack_idx_opts->flags);
576
577 if (pack_idx_opts->flags & WRITE_MTIMES) {
578 mtimes_tmp_name = write_mtimes_file(to_pack, written_list,
pack.h
+12 -2
@@ -105,8 +105,18 @@ struct ref;
105
106 void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought);
107
108 -char *write_rev_file(const char *rev_name, struct pack_idx_entry **objects, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
109 -char *write_rev_file_order(const char *rev_name, uint32_t *pack_order, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
108 +char *write_rev_file(const struct git_hash_algo *hash_algo,
109 + const char *rev_name,
110 + struct pack_idx_entry **objects,
111 + uint32_t nr_objects,
112 + const unsigned char *hash,
113 + unsigned flags);
114 +char *write_rev_file_order(const struct git_hash_algo *hash_algo,
115 + const char *rev_name,
116 + uint32_t *pack_order,
117 + uint32_t nr_objects,
118 + const unsigned char *hash,
119 + unsigned flags);
120
121 /*
122 * The "hdr" output buffer should be at least this big, which will handle sizes