Skip to content

Commit 48524fa

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9213e4 commit 48524fa

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

csum-file.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void hashflush(struct hashfile *f)
5050

5151
if (offset) {
5252
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);
5454
flush(f, f->buffer, offset);
5555
f->offset = 0;
5656
}
@@ -71,14 +71,14 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
7171
hashflush(f);
7272

7373
if (f->skip_hash)
74-
hashclr(f->buffer, the_repository->hash_algo);
74+
hashclr(f->buffer, f->algop);
7575
else
76-
the_hash_algo->unsafe_final_fn(f->buffer, &f->ctx);
76+
f->algop->unsafe_final_fn(f->buffer, &f->ctx);
7777

7878
if (result)
79-
hashcpy(result, f->buffer, the_repository->hash_algo);
79+
hashcpy(result, f->buffer, f->algop);
8080
if (flags & CSUM_HASH_IN_STREAM)
81-
flush(f, f->buffer, the_hash_algo->rawsz);
81+
flush(f, f->buffer, f->algop->rawsz);
8282
if (flags & CSUM_FSYNC)
8383
fsync_component_or_die(component, f->fd, f->name);
8484
if (flags & CSUM_CLOSE) {
@@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
128128
* f->offset is necessarily zero.
129129
*/
130130
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);
132132
flush(f, buf, nr);
133133
} else {
134134
/*
@@ -174,7 +174,9 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
174174
f->name = name;
175175
f->do_crc = 0;
176176
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);
178180

179181
f->buffer_len = buffer_len;
180182
f->buffer = xmalloc(buffer_len);
@@ -208,7 +210,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo
208210
{
209211
hashflush(f);
210212
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);
212214
}
213215

214216
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -219,7 +221,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
219221
lseek(f->fd, offset, SEEK_SET) != offset)
220222
return -1;
221223
f->total = offset;
222-
the_hash_algo->unsafe_clone_fn(&f->ctx, &checkpoint->ctx);
224+
f->algop->unsafe_clone_fn(&f->ctx, &checkpoint->ctx);
223225
f->offset = 0; /* hashflush() was called in checkpoint */
224226
return 0;
225227
}

csum-file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct hashfile {
2020
size_t buffer_len;
2121
unsigned char *buffer;
2222
unsigned char *check_buffer;
23+
const struct git_hash_algo *algop;
2324

2425
/**
2526
* If non-zero, skip_hash indicates that we should

0 commit comments

Comments
 (0)