]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pack-write: pass hash_algo to `fixup_pack_header_footer()`
authorKarthik Nayak <karthik.188@gmail.com>
Sun, 19 Jan 2025 11:19:26 +0000 (12:19 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Jan 2025 20:36:34 +0000 (12:36 -0800)
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>
builtin/fast-import.c
builtin/index-pack.c
builtin/pack-objects.c
bulk-checkin.c
pack-write.c
pack.h

index 0f86392761abbe6acb217fef7f4fe7c3ff5ac1fa..6baf2b1b71e2443a7987a41e0cd246076682bf58 100644 (file)
@@ -878,9 +878,10 @@ static void end_packfile(void)
 
                close_pack_windows(pack_data);
                finalize_hashfile(pack_file, cur_pack_oid.hash, FSYNC_COMPONENT_PACK, 0);
-               fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash,
-                                        pack_data->pack_name, object_count,
-                                        cur_pack_oid.hash, pack_size);
+               fixup_pack_header_footer(the_hash_algo, pack_data->pack_fd,
+                                        pack_data->hash, pack_data->pack_name,
+                                        object_count, cur_pack_oid.hash,
+                                        pack_size);
 
                if (object_count <= unpack_limit) {
                        if (!loosen_small_pack(pack_data)) {
index 0b62b2589f10f97aacab4614db158666850b78f6..cb9de5ca2c50670dd1224177d32a2ad811469930 100644 (file)
@@ -1387,7 +1387,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
                strbuf_release(&msg);
                finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
                hashcpy(read_hash, pack_hash, the_repository->hash_algo);
-               fixup_pack_header_footer(output_fd, pack_hash,
+               fixup_pack_header_footer(the_hash_algo, output_fd, pack_hash,
                                         curr_pack, nr_objects,
                                         read_hash, consumed_bytes-the_hash_algo->rawsz);
                if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))
index 1c3b8426515c42f14c536eea559dd377212855f6..6188e8205952492cb72f92bb2c80c1201c2f7acf 100644 (file)
@@ -1318,8 +1318,9 @@ static void write_pack_file(void)
                         */
 
                        int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
-                       fixup_pack_header_footer(fd, hash, pack_tmp_name,
-                                                nr_written, hash, offset);
+                       fixup_pack_header_footer(the_hash_algo, fd, hash,
+                                                pack_tmp_name, nr_written,
+                                                hash, offset);
                        close(fd);
                        if (write_bitmap_index) {
                                if (write_bitmap_index != WRITE_BITMAP_QUIET)
index 433070a3bda461a2ad62da67cc5515f8822d2df3..e6595f8dc7d34b3e590fef2c088de26fb1e02832 100644 (file)
@@ -70,7 +70,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
                                  CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
        } else {
                int fd = finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK, 0);
-               fixup_pack_header_footer(fd, hash, state->pack_tmp_name,
+               fixup_pack_header_footer(the_hash_algo, fd, hash, state->pack_tmp_name,
                                         state->nr_written, hash,
                                         state->offset);
                close(fd);
index 98a8c0e7853d7b46b5ce9a9672e0249ff051b5f9..fc887850dfb9789132b8642733c6472944dbe32d 100644 (file)
@@ -380,7 +380,8 @@ off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
  * partial_pack_sha1 can refer to the same buffer if the caller is not
  * interested in the resulting SHA1 of pack data above partial_pack_offset.
  */
-void fixup_pack_header_footer(int pack_fd,
+void fixup_pack_header_footer(const struct git_hash_algo *hash_algo,
+                        int pack_fd,
                         unsigned char *new_pack_hash,
                         const char *pack_name,
                         uint32_t object_count,
@@ -393,8 +394,8 @@ void fixup_pack_header_footer(int pack_fd,
        char *buf;
        ssize_t read_result;
 
-       the_hash_algo->init_fn(&old_hash_ctx);
-       the_hash_algo->init_fn(&new_hash_ctx);
+       hash_algo->init_fn(&old_hash_ctx);
+       hash_algo->init_fn(&new_hash_ctx);
 
        if (lseek(pack_fd, 0, SEEK_SET) != 0)
                die_errno("Failed seeking to start of '%s'", pack_name);
@@ -406,9 +407,9 @@ void fixup_pack_header_footer(int pack_fd,
                          pack_name);
        if (lseek(pack_fd, 0, SEEK_SET) != 0)
                die_errno("Failed seeking to start of '%s'", pack_name);
-       the_hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
+       hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
        hdr.hdr_entries = htonl(object_count);
-       the_hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
+       hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
        write_or_die(pack_fd, &hdr, sizeof(hdr));
        partial_pack_offset -= sizeof(hdr);
 
@@ -423,7 +424,7 @@ void fixup_pack_header_footer(int pack_fd,
                        break;
                if (n < 0)
                        die_errno("Failed to checksum '%s'", pack_name);
-               the_hash_algo->update_fn(&new_hash_ctx, buf, n);
+               hash_algo->update_fn(&new_hash_ctx, buf, n);
 
                aligned_sz -= n;
                if (!aligned_sz)
@@ -432,13 +433,12 @@ void fixup_pack_header_footer(int pack_fd,
                if (!partial_pack_hash)
                        continue;
 
-               the_hash_algo->update_fn(&old_hash_ctx, buf, n);
+               hash_algo->update_fn(&old_hash_ctx, buf, n);
                partial_pack_offset -= n;
                if (partial_pack_offset == 0) {
                        unsigned char hash[GIT_MAX_RAWSZ];
-                       the_hash_algo->final_fn(hash, &old_hash_ctx);
-                       if (!hasheq(hash, partial_pack_hash,
-                                   the_repository->hash_algo))
+                       hash_algo->final_fn(hash, &old_hash_ctx);
+                       if (!hasheq(hash, partial_pack_hash, hash_algo))
                                die("Unexpected checksum for %s "
                                    "(disk corruption?)", pack_name);
                        /*
@@ -446,7 +446,7 @@ void fixup_pack_header_footer(int pack_fd,
                         * pack, which also means making partial_pack_offset
                         * big enough not to matter anymore.
                         */
-                       the_hash_algo->init_fn(&old_hash_ctx);
+                       hash_algo->init_fn(&old_hash_ctx);
                        partial_pack_offset = ~partial_pack_offset;
                        partial_pack_offset -= MSB(partial_pack_offset, 1);
                }
@@ -454,9 +454,9 @@ void fixup_pack_header_footer(int pack_fd,
        free(buf);
 
        if (partial_pack_hash)
-               the_hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
-       the_hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
-       write_or_die(pack_fd, new_pack_hash, the_hash_algo->rawsz);
+               hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
+       hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
+       write_or_die(pack_fd, new_pack_hash, hash_algo->rawsz);
        fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);
 }
 
diff --git a/pack.h b/pack.h
index a8da0406299bf267205256aaa8efb215f2ff73be..6d9d477adc83e83d9e9175ccf699c100b4c147c6 100644 (file)
--- a/pack.h
+++ b/pack.h
@@ -91,7 +91,9 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offs
 int verify_pack_index(struct packed_git *);
 int verify_pack(struct repository *, struct packed_git *, verify_fn fn, struct progress *, uint32_t);
 off_t write_pack_header(struct hashfile *f, uint32_t);
-void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
+void fixup_pack_header_footer(const struct git_hash_algo *, int,
+                             unsigned char *, const char *, uint32_t,
+                             unsigned char *, off_t);
 char *index_pack_lockfile(int fd, int *is_well_formed);
 
 struct ref;