Skip to content

Commit 8244d01

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbe8d30 commit 8244d01

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

builtin/fast-import.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,10 @@ static void end_packfile(void)
878878

879879
close_pack_windows(pack_data);
880880
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);
884885

885886
if (object_count <= unpack_limit) {
886887
if (!loosen_small_pack(pack_data)) {

builtin/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
13871387
strbuf_release(&msg);
13881388
finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
13891389
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,
13911391
curr_pack, nr_objects,
13921392
read_hash, consumed_bytes-the_hash_algo->rawsz);
13931393
if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))

builtin/pack-objects.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,9 @@ static void write_pack_file(void)
13181318
*/
13191319

13201320
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);
13231324
close(fd);
13241325
if (write_bitmap_index) {
13251326
if (write_bitmap_index != WRITE_BITMAP_QUIET)

bulk-checkin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
7070
CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
7171
} else {
7272
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,
7474
state->nr_written, hash,
7575
state->offset);
7676
close(fd);

pack-write.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
380380
* partial_pack_sha1 can refer to the same buffer if the caller is not
381381
* interested in the resulting SHA1 of pack data above partial_pack_offset.
382382
*/
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,
384385
unsigned char *new_pack_hash,
385386
const char *pack_name,
386387
uint32_t object_count,
@@ -393,8 +394,8 @@ void fixup_pack_header_footer(int pack_fd,
393394
char *buf;
394395
ssize_t read_result;
395396

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);
398399

399400
if (lseek(pack_fd, 0, SEEK_SET) != 0)
400401
die_errno("Failed seeking to start of '%s'", pack_name);
@@ -406,9 +407,9 @@ void fixup_pack_header_footer(int pack_fd,
406407
pack_name);
407408
if (lseek(pack_fd, 0, SEEK_SET) != 0)
408409
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));
410411
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));
412413
write_or_die(pack_fd, &hdr, sizeof(hdr));
413414
partial_pack_offset -= sizeof(hdr);
414415

@@ -423,7 +424,7 @@ void fixup_pack_header_footer(int pack_fd,
423424
break;
424425
if (n < 0)
425426
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);
427428

428429
aligned_sz -= n;
429430
if (!aligned_sz)
@@ -432,31 +433,30 @@ void fixup_pack_header_footer(int pack_fd,
432433
if (!partial_pack_hash)
433434
continue;
434435

435-
the_hash_algo->update_fn(&old_hash_ctx, buf, n);
436+
hash_algo->update_fn(&old_hash_ctx, buf, n);
436437
partial_pack_offset -= n;
437438
if (partial_pack_offset == 0) {
438439
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))
442442
die("Unexpected checksum for %s "
443443
"(disk corruption?)", pack_name);
444444
/*
445445
* Now let's compute the SHA1 of the remainder of the
446446
* pack, which also means making partial_pack_offset
447447
* big enough not to matter anymore.
448448
*/
449-
the_hash_algo->init_fn(&old_hash_ctx);
449+
hash_algo->init_fn(&old_hash_ctx);
450450
partial_pack_offset = ~partial_pack_offset;
451451
partial_pack_offset -= MSB(partial_pack_offset, 1);
452452
}
453453
}
454454
free(buf);
455455

456456
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);
460460
fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);
461461
}
462462

pack.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offs
9191
int verify_pack_index(struct packed_git *);
9292
int verify_pack(struct repository *, struct packed_git *, verify_fn fn, struct progress *, uint32_t);
9393
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);
9597
char *index_pack_lockfile(int fd, int *is_well_formed);
9698

9799
struct ref;

0 commit comments

Comments
 (0)