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

The `fixup_pack_header_footer()` function uses the global `the_hash_algo` variable to access the repository's hash function. To avoid global variable usage, pass a hash_algo from the layers above. 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. 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 8244d01de6402606c9fb588ce2143aa629aa7d0e
6 files changed +26 -22
builtin/fast-import.c
+4 -3
@@ -878,9 +878,10 @@ static void end_packfile(void)
878
879 close_pack_windows(pack_data);
880 finalize_hashfile(pack_file, cur_pack_oid.hash, FSYNC_COMPONENT_PACK, 0);
881 - fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash,
882 - pack_data->pack_name, object_count,
883 - cur_pack_oid.hash, pack_size);
881 + fixup_pack_header_footer(the_hash_algo, pack_data->pack_fd,
882 + pack_data->hash, pack_data->pack_name,
883 + object_count, cur_pack_oid.hash,
884 + pack_size);
885
886 if (object_count <= unpack_limit) {
887 if (!loosen_small_pack(pack_data)) {
builtin/index-pack.c
+1 -1
@@ -1387,7 +1387,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
1387 strbuf_release(&msg);
1388 finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
1389 hashcpy(read_hash, pack_hash, the_repository->hash_algo);
1390 - fixup_pack_header_footer(output_fd, pack_hash,
1390 + fixup_pack_header_footer(the_hash_algo, output_fd, pack_hash,
1391 curr_pack, nr_objects,
1392 read_hash, consumed_bytes-the_hash_algo->rawsz);
1393 if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))
builtin/pack-objects.c
+3 -2
@@ -1318,8 +1318,9 @@ static void write_pack_file(void)
1318 */
1319
1320 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
1321 - fixup_pack_header_footer(fd, hash, pack_tmp_name,
1322 - nr_written, hash, offset);
1321 + fixup_pack_header_footer(the_hash_algo, fd, hash,
1322 + pack_tmp_name, nr_written,
1323 + hash, offset);
1324 close(fd);
1325 if (write_bitmap_index) {
1326 if (write_bitmap_index != WRITE_BITMAP_QUIET)
bulk-checkin.c
+1 -1
@@ -70,7 +70,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
70 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
71 } else {
72 int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0);
73 - fixup_pack_header_footer(fd, hash, state->pack_tmp_name,
73 + fixup_pack_header_footer(the_hash_algo, fd, hash, state->pack_tmp_name,
74 state->nr_written, hash,
75 state->offset);
76 close(fd);
pack-write.c
+14 -14
@@ -380,7 +380,8 @@ off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
380 * partial_pack_sha1 can refer to the same buffer if the caller is not
381 * interested in the resulting SHA1 of pack data above partial_pack_offset.
382 */
383 -void fixup_pack_header_footer(int pack_fd,
383 +void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,
384 + int pack_fd,
385 unsigned char *new_pack_hash,
386 const char *pack_name,
387 uint32_t object_count,
@@ -393,8 +394,8 @@ void fixup_pack_header_footer(int pack_fd,
394 char *buf;
395 ssize_t read_result;
396
396 - the_hash_algo->init_fn(&old_hash_ctx);
397 - the_hash_algo->init_fn(&new_hash_ctx);
397 + hash_algo->init_fn(&old_hash_ctx);
398 + hash_algo->init_fn(&new_hash_ctx);
399
400 if (lseek(pack_fd, 0, SEEK_SET) != 0)
401 die_errno("Failed seeking to start of '%s'", pack_name);
@@ -406,9 +407,9 @@ void fixup_pack_header_footer(int pack_fd,
407 pack_name);
408 if (lseek(pack_fd, 0, SEEK_SET) != 0)
409 die_errno("Failed seeking to start of '%s'", pack_name);
409 - the_hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
410 + hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
411 hdr.hdr_entries = htonl(object_count);
411 - the_hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
412 + hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
413 write_or_die(pack_fd, &hdr, sizeof(hdr));
414 partial_pack_offset -= sizeof(hdr);
415
@@ -423,7 +424,7 @@ void fixup_pack_header_footer(int pack_fd,
424 break;
425 if (n < 0)
426 die_errno("Failed to checksum '%s'", pack_name);
426 - the_hash_algo->update_fn(&new_hash_ctx, buf, n);
427 + hash_algo->update_fn(&new_hash_ctx, buf, n);
428
429 aligned_sz -= n;
430 if (!aligned_sz)
@@ -432,13 +433,12 @@ void fixup_pack_header_footer(int pack_fd,
433 if (!partial_pack_hash)
434 continue;
435
435 - the_hash_algo->update_fn(&old_hash_ctx, buf, n);
436 + hash_algo->update_fn(&old_hash_ctx, buf, n);
437 partial_pack_offset -= n;
438 if (partial_pack_offset == 0) {
439 unsigned char hash[GIT_MAX_RAWSZ];
439 - the_hash_algo->final_fn(hash, &old_hash_ctx);
440 - if (!hasheq(hash, partial_pack_hash,
441 - the_repository->hash_algo))
440 + hash_algo->final_fn(hash, &old_hash_ctx);
441 + if (!hasheq(hash, partial_pack_hash, hash_algo))
442 die("Unexpected checksum for %s "
443 "(disk corruption?)", pack_name);
444 /*
@@ -446,7 +446,7 @@ void fixup_pack_header_footer(int pack_fd,
446 * pack, which also means making partial_pack_offset
447 * big enough not to matter anymore.
448 */
449 - the_hash_algo->init_fn(&old_hash_ctx);
449 + hash_algo->init_fn(&old_hash_ctx);
450 partial_pack_offset = ~partial_pack_offset;
451 partial_pack_offset -= MSB(partial_pack_offset, 1);
452 }
@@ -454,9 +454,9 @@ void fixup_pack_header_footer(int pack_fd,
454 free(buf);
455
456 if (partial_pack_hash)
457 - the_hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
458 - the_hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
459 - write_or_die(pack_fd, new_pack_hash, the_hash_algo->rawsz);
457 + hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
458 + hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
459 + write_or_die(pack_fd, new_pack_hash, hash_algo->rawsz);
460 fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);
461 }
462
pack.h
+3 -1
@@ -91,7 +91,9 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offs
91 int verify_pack_index(struct packed_git *);
92 int verify_pack(struct repository *, struct packed_git *, verify_fn fn, struct progress *, uint32_t);
93 off_t write_pack_header(struct hashfile *f, uint32_t);
94 -void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
94 +void fixup_pack_header_footer(const struct git_hash_algo *, int,
95 + unsigned char *, const char *, uint32_t,
96 + unsigned char *, off_t);
97 char *index_pack_lockfile(int fd, int *is_well_formed);
98
99 struct ref;