csum-file: store the hash algorithm as a struct field

Throughout the hashfile API, we rely on a reference to 'the_hash_algo', and call its _unsafe function variants directly. Prepare for a future change where we may use a different 'git_hash_algo' pointer (instead of just relying on 'the_hash_algo' throughout) by making the 'git_hash_algo' pointer a member of the 'hashfile' structure itself. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jan 23, 2025 at 12:34 UTC 48524fac643afd7ec70d43684902598ad6d5b954
2 files changed +12 -9
csum-file.c
+11 -9
@@ -50,7 +50,7 @@ void hashflush(struct hashfile *f)
50
51 if (offset) {
52 if (!f->skip_hash)
53 - the_hash_algo->unsafe_update_fn(&f->ctx, f->buffer, offset);
53 + f->algop->unsafe_update_fn(&f->ctx, f->buffer, offset);
54 flush(f, f->buffer, offset);
55 f->offset = 0;
56 }
@@ -71,14 +71,14 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
71 hashflush(f);
72
73 if (f->skip_hash)
74 - hashclr(f->buffer, the_repository->hash_algo);
74 + hashclr(f->buffer, f->algop);
75 else
76 - the_hash_algo->unsafe_final_fn(f->buffer, &f->ctx);
76 + f->algop->unsafe_final_fn(f->buffer, &f->ctx);
77
78 if (result)
79 - hashcpy(result, f->buffer, the_repository->hash_algo);
79 + hashcpy(result, f->buffer, f->algop);
80 if (flags & CSUM_HASH_IN_STREAM)
81 - flush(f, f->buffer, the_hash_algo->rawsz);
81 + flush(f, f->buffer, f->algop->rawsz);
82 if (flags & CSUM_FSYNC)
83 fsync_component_or_die(component, f->fd, f->name);
84 if (flags & CSUM_CLOSE) {
@@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
128 * f->offset is necessarily zero.
129 */
130 if (!f->skip_hash)
131 - the_hash_algo->unsafe_update_fn(&f->ctx, buf, nr);
131 + f->algop->unsafe_update_fn(&f->ctx, buf, nr);
132 flush(f, buf, nr);
133 } else {
134 /*
@@ -174,7 +174,9 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
174 f->name = name;
175 f->do_crc = 0;
176 f->skip_hash = 0;
177 - the_hash_algo->unsafe_init_fn(&f->ctx);
177 +
178 + f->algop = the_hash_algo;
179 + f->algop->unsafe_init_fn(&f->ctx);
180
181 f->buffer_len = buffer_len;
182 f->buffer = xmalloc(buffer_len);
@@ -208,7 +210,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo
210 {
211 hashflush(f);
212 checkpoint->offset = f->total;
211 - the_hash_algo->unsafe_clone_fn(&checkpoint->ctx, &f->ctx);
213 + f->algop->unsafe_clone_fn(&checkpoint->ctx, &f->ctx);
214 }
215
216 int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -219,7 +221,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
221 lseek(f->fd, offset, SEEK_SET) != offset)
222 return -1;
223 f->total = offset;
222 - the_hash_algo->unsafe_clone_fn(&f->ctx, &checkpoint->ctx);
224 + f->algop->unsafe_clone_fn(&f->ctx, &checkpoint->ctx);
225 f->offset = 0; /* hashflush() was called in checkpoint */
226 return 0;
227 }
csum-file.h
+1
@@ -20,6 +20,7 @@ struct hashfile {
20 size_t buffer_len;
21 unsigned char *buffer;
22 unsigned char *check_buffer;
23 + const struct git_hash_algo *algop;
24
25 /**
26 * If non-zero, skip_hash indicates that we should