csum-file: stop depending on the_repository

There are multiple sites in "csum-file.c" where we use the global
`the_repository` variable, either explicitly or implicitly by using
`the_hash_algo`.

Refactor the code to stop using `the_repository` by adapting functions
to receive required data as parameters. Adapt callsites accordingly by
either using `the_repository->hash_algo`, or by using a context-provided
hash algorithm in case the subsystem already got rid of its dependency
on `the_repository`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2025-03-10 08:13:20 +01:00
committed by Junio C Hamano
parent e969bc8759
commit 228457c9d9
14 changed files with 56 additions and 39 deletions

View File

@@ -8,8 +8,6 @@
* able to verify hasn't been messed with afterwards.
*/
#define USE_THE_REPOSITORY_VARIABLE
#include "git-compat-util.h"
#include "csum-file.h"
#include "git-zlib.h"
@@ -148,21 +146,23 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
}
}
struct hashfile *hashfd_check(const char *name)
struct hashfile *hashfd_check(const struct git_hash_algo *algop,
const char *name)
{
int sink, check;
struct hashfile *f;
sink = xopen("/dev/null", O_WRONLY);
check = xopen(name, O_RDONLY);
f = hashfd(sink, name);
f = hashfd(algop, sink, name);
f->check_fd = check;
f->check_buffer = xmalloc(f->buffer_len);
return f;
}
static struct hashfile *hashfd_internal(int fd, const char *name,
static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
int fd, const char *name,
struct progress *tp,
size_t buffer_len)
{
@@ -176,7 +176,7 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
f->do_crc = 0;
f->skip_hash = 0;
f->algop = unsafe_hash_algo(the_hash_algo);
f->algop = unsafe_hash_algo(algop);
f->algop->init_fn(&f->ctx);
f->buffer_len = buffer_len;
@@ -186,17 +186,19 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
return f;
}
struct hashfile *hashfd(int fd, const char *name)
struct hashfile *hashfd(const struct git_hash_algo *algop,
int fd, const char *name)
{
/*
* Since we are not going to use a progress meter to
* measure the rate of data passing through this hashfile,
* use a larger buffer size to reduce fsync() calls.
*/
return hashfd_internal(fd, name, NULL, 128 * 1024);
return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
}
struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
int fd, const char *name, struct progress *tp)
{
/*
* Since we are expecting to report progress of the
@@ -204,7 +206,7 @@ struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp
* size so the progress indicators arrive at a more
* frequent rate.
*/
return hashfd_internal(fd, name, tp, 8 * 1024);
return hashfd_internal(algop, fd, name, tp, 8 * 1024);
}
void hashfile_checkpoint_init(struct hashfile *f,
@@ -246,13 +248,15 @@ uint32_t crc32_end(struct hashfile *f)
return f->crc32;
}
int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
int hashfile_checksum_valid(const struct git_hash_algo *algop,
const unsigned char *data, size_t total_len)
{
unsigned char got[GIT_MAX_RAWSZ];
struct git_hash_ctx ctx;
const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo);
size_t data_len = total_len - algop->rawsz;
algop = unsafe_hash_algo(algop);
if (total_len < algop->rawsz)
return 0; /* say "too short"? */