]> git.ipfire.org Git - thirdparty/git.git/commitdiff
csum-file: stop depending on `the_repository`
authorPatrick Steinhardt <ps@pks.im>
Mon, 10 Mar 2025 07:13:20 +0000 (08:13 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Mar 2025 20:16:18 +0000 (13:16 -0700)
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>
14 files changed:
builtin/fast-import.c
builtin/index-pack.c
builtin/pack-objects.c
commit-graph.c
csum-file.c
csum-file.h
midx-write.c
midx.c
pack-bitmap-write.c
pack-bitmap.c
pack-check.c
pack-revindex.c
pack-write.c
read-cache.c

index 397a6f46ad85d5b3af5fdf68847193c09bd49eaf..86e6e754816b9ab43ca4db10598474577a9be592 100644 (file)
@@ -770,7 +770,7 @@ static void start_packfile(void)
        p->pack_fd = pack_fd;
        p->do_not_close = 1;
        p->repo = the_repository;
-       pack_file = hashfd(pack_fd, p->pack_name);
+       pack_file = hashfd(the_repository->hash_algo, pack_fd, p->pack_name);
 
        pack_data = p;
        pack_size = write_pack_header(pack_file, 0);
index 52cc97d52cb6740fbfeb1e61841c52b3692482a7..3eb5af20950ba02bb5404e53ce928902c000a515 100644 (file)
@@ -1381,7 +1381,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
                REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
                memset(objects + nr_objects + 1, 0,
                       nr_unresolved * sizeof(*objects));
-               f = hashfd(output_fd, curr_pack);
+               f = hashfd(the_repository->hash_algo, output_fd, curr_pack);
                fix_unresolved_deltas(f);
                strbuf_addf(&msg, Q_("completed with %d local object",
                                     "completed with %d local objects",
index 58a9b1612626e0fca9c403b5ed9744d80757227e..8e282f2a980dbfd76ce22703ab4508c7f65024ff 100644 (file)
@@ -1311,7 +1311,8 @@ static void write_pack_file(void)
                char *pack_tmp_name = NULL;
 
                if (pack_to_stdout)
-                       f = hashfd_throughput(1, "<stdout>", progress_state);
+                       f = hashfd_throughput(the_repository->hash_algo, 1,
+                                             "<stdout>", progress_state);
                else
                        f = create_tmp_packfile(&pack_tmp_name);
 
index 1021ccb983d4ee02655df851f792c3ca24edc99c..8286d5dda241ffdca1b8e6e519a7316bb620afd9 100644 (file)
@@ -2090,11 +2090,13 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
                        return -1;
                }
 
-               f = hashfd(get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
+               f = hashfd(the_repository->hash_algo,
+                          get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
        } else {
                hold_lock_file_for_update_mode(&lk, ctx->graph_name,
                                               LOCK_DIE_ON_ERROR, 0444);
-               f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
+               f = hashfd(the_repository->hash_algo,
+                          get_lock_file_fd(&lk), get_lock_file_path(&lk));
        }
 
        cf = init_chunkfile(f);
@@ -2716,7 +2718,8 @@ static void graph_report(const char *fmt, ...)
 
 static int commit_graph_checksum_valid(struct commit_graph *g)
 {
-       return hashfile_checksum_valid(g->data, g->data_len);
+       return hashfile_checksum_valid(the_repository->hash_algo,
+                                      g->data, g->data_len);
 }
 
 static int verify_one_commit_graph(struct repository *r,
index b58c183a4f020a7698e2e9fcbfd7cdc349e1fe61..6e21e3cac8a636c016be59f6426b27d1dc66accc 100644 (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"? */
 
index ffccbf09966c083b72c9f619c6b8406c59208515..07ae11024afc3459bd89827a37889c41d13cf162 100644 (file)
@@ -45,9 +45,12 @@ int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
 #define CSUM_FSYNC             2
 #define CSUM_HASH_IN_STREAM    4
 
-struct hashfile *hashfd(int fd, const char *name);
-struct hashfile *hashfd_check(const char *name);
-struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
+struct hashfile *hashfd(const struct git_hash_algo *algop,
+                       int fd, const char *name);
+struct hashfile *hashfd_check(const struct git_hash_algo *algop,
+                             const char *name);
+struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
+                                  int fd, const char *name, struct progress *tp);
 
 /*
  * Free the hashfile without flushing its contents to disk. This only
@@ -66,7 +69,8 @@ void crc32_begin(struct hashfile *);
 uint32_t crc32_end(struct hashfile *);
 
 /* Verify checksum validity while reading. Returns non-zero on success. */
-int hashfile_checksum_valid(const unsigned char *data, size_t len);
+int hashfile_checksum_valid(const struct git_hash_algo *algop,
+                           const unsigned char *data, size_t len);
 
 /*
  * Returns the total number of bytes fed to the hashfile so far (including ones
index 48d6558253ec9357a1edf709af7c5e90df8e22c1..26d9d8bb148a4e9dae06626fa7d9443768785e3f 100644 (file)
@@ -1342,10 +1342,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
                        return -1;
                }
 
-               f = hashfd(get_tempfile_fd(incr), get_tempfile_path(incr));
+               f = hashfd(r->hash_algo, get_tempfile_fd(incr),
+                          get_tempfile_path(incr));
        } else {
                hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
-               f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
+               f = hashfd(r->hash_algo, get_lock_file_fd(&lk),
+                          get_lock_file_path(&lk));
        }
 
        cf = init_chunkfile(f);
diff --git a/midx.c b/midx.c
index d91088efb87ca0d59b1ad215556a8cfe1612b8af..807fdf72f7b81cbaab07480ff97e257c3a6bf9a6 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -747,7 +747,8 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
 
 int midx_checksum_valid(struct multi_pack_index *m)
 {
-       return hashfile_checksum_valid(m->data, m->data_len);
+       return hashfile_checksum_valid(m->repo->hash_algo,
+                                      m->data, m->data_len);
 }
 
 struct clear_midx_data {
index 34e86d49947d231abb6c9958e4e02810f5a723db..50e5c491ccb6af2cbb4b4a677b7412300bb060c6 100644 (file)
@@ -1030,7 +1030,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
        if (writer->pseudo_merges_nr)
                options |= BITMAP_OPT_PSEUDO_MERGES;
 
-       f = hashfd(fd, tmp_file.buf);
+       f = hashfd(the_repository->hash_algo, fd, tmp_file.buf);
 
        memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
        header.version = htons(default_version);
index 6406953d3223719b01dcb255a3f69965d578cf26..f0e2c000252fd855d95e3287a53f5792258112d2 100644 (file)
@@ -3024,7 +3024,8 @@ int bitmap_is_preferred_refname(struct repository *r, const char *refname)
        return 0;
 }
 
-static int verify_bitmap_file(const char *name)
+static int verify_bitmap_file(const struct git_hash_algo *algop,
+                             const char *name)
 {
        struct stat st;
        unsigned char *data;
@@ -3040,7 +3041,7 @@ static int verify_bitmap_file(const char *name)
 
        data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        close(fd);
-       if (!hashfile_checksum_valid(data, st.st_size))
+       if (!hashfile_checksum_valid(algop, data, st.st_size))
                res = error(_("bitmap file '%s' has invalid checksum"),
                            name);
 
@@ -3055,14 +3056,14 @@ int verify_bitmap_files(struct repository *r)
        for (struct multi_pack_index *m = get_multi_pack_index(r);
             m; m = m->next) {
                char *midx_bitmap_name = midx_bitmap_filename(m);
-               res |= verify_bitmap_file(midx_bitmap_name);
+               res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name);
                free(midx_bitmap_name);
        }
 
        for (struct packed_git *p = get_all_packs(r);
             p; p = p->next) {
                char *pack_bitmap_name = pack_bitmap_filename(p);
-               res |= verify_bitmap_file(pack_bitmap_name);
+               res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name);
                free(pack_bitmap_name);
        }
 
index d0aeb5ec41259bdafd9fdc567611416c20ca3728..6bcadc1e679590d62432cc9ec93a12cf8aa8262e 100644 (file)
@@ -180,7 +180,7 @@ int verify_pack_index(struct packed_git *p)
                return error("packfile %s index not opened", p->pack_name);
 
        /* Verify SHA1 sum of the index file */
-       if (!hashfile_checksum_valid(p->index_data, p->index_size))
+       if (!hashfile_checksum_valid(the_repository->hash_algo, p->index_data, p->index_size))
                err = error("Packfile index for %s hash mismatch",
                            p->pack_name);
        return err;
index d3832478d99edffae17db0bbe85aa981c1a3ad30..78139e3d7f5c9fa9bf727c24700fcd05dce26d8d 100644 (file)
@@ -322,7 +322,8 @@ int verify_pack_revindex(struct packed_git *p)
        if (!p->revindex_map || !p->revindex_data)
                return res;
 
-       if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) {
+       if (!hashfile_checksum_valid(the_repository->hash_algo,
+                                    (const unsigned char *)p->revindex_map, p->revindex_size)) {
                error(_("invalid checksum"));
                res = -1;
        }
index 823e40b42f20973b4ba5b5dbb77b6a7a5ae119c2..5eb89f44cf45b18bca10166d8a776fef65807821 100644 (file)
@@ -82,7 +82,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
 
        if (opts->flags & WRITE_IDX_VERIFY) {
                assert(index_name);
-               f = hashfd_check(index_name);
+               f = hashfd_check(the_repository->hash_algo, index_name);
        } else {
                if (!index_name) {
                        struct strbuf tmp_file = STRBUF_INIT;
@@ -92,7 +92,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
                        unlink(index_name);
                        fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
                }
-               f = hashfd(fd, index_name);
+               f = hashfd(the_repository->hash_algo, fd, index_name);
        }
 
        /* if last object's offset is >= 2^31 we should use index V2 */
@@ -268,7 +268,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
                        fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
                        path = xstrdup(rev_name);
                }
-               f = hashfd(fd, path);
+               f = hashfd(the_repository->hash_algo, fd, path);
        } else if (flags & WRITE_REV_VERIFY) {
                struct stat statbuf;
                if (stat(rev_name, &statbuf)) {
@@ -278,7 +278,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
                        } else
                                die_errno(_("could not stat: %s"), rev_name);
                }
-               f = hashfd_check(rev_name);
+               f = hashfd_check(the_repository->hash_algo, rev_name);
                path = xstrdup(rev_name);
        } else {
                return NULL;
@@ -346,7 +346,7 @@ static char *write_mtimes_file(const struct git_hash_algo *hash_algo,
 
        fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX");
        mtimes_name = strbuf_detach(&tmp_file, NULL);
-       f = hashfd(fd, mtimes_name);
+       f = hashfd(the_repository->hash_algo, fd, mtimes_name);
 
        write_mtimes_header(hash_algo, f);
        write_mtimes_objects(f, to_pack, objects, nr_objects);
@@ -534,7 +534,7 @@ struct hashfile *create_tmp_packfile(char **pack_tmp_name)
 
        fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX");
        *pack_tmp_name = strbuf_detach(&tmpname, NULL);
-       return hashfd(fd, *pack_tmp_name);
+       return hashfd(the_repository->hash_algo, fd, *pack_tmp_name);
 }
 
 static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
index e678c13e8f15e2fd7215f74a9ced20ad1839270f..a012cb223688959f33882599a419c078a42adaeb 100644 (file)
@@ -2848,7 +2848,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
        struct strbuf sb = STRBUF_INIT;
        int nr, nr_threads, ret;
 
-       f = hashfd(tempfile->fd, tempfile->filename.buf);
+       f = hashfd(the_repository->hash_algo, tempfile->fd, tempfile->filename.buf);
 
        prepare_repo_settings(r);
        f->skip_hash = r->settings.index_skip_hash;