Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
228457c9 e969bc87

+56 -39
+1 -1
builtin/fast-import.c
··· 770 770 p->pack_fd = pack_fd; 771 771 p->do_not_close = 1; 772 772 p->repo = the_repository; 773 - pack_file = hashfd(pack_fd, p->pack_name); 773 + pack_file = hashfd(the_repository->hash_algo, pack_fd, p->pack_name); 774 774 775 775 pack_data = p; 776 776 pack_size = write_pack_header(pack_file, 0);
+1 -1
builtin/index-pack.c
··· 1381 1381 REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1); 1382 1382 memset(objects + nr_objects + 1, 0, 1383 1383 nr_unresolved * sizeof(*objects)); 1384 - f = hashfd(output_fd, curr_pack); 1384 + f = hashfd(the_repository->hash_algo, output_fd, curr_pack); 1385 1385 fix_unresolved_deltas(f); 1386 1386 strbuf_addf(&msg, Q_("completed with %d local object", 1387 1387 "completed with %d local objects",
+2 -1
builtin/pack-objects.c
··· 1311 1311 char *pack_tmp_name = NULL; 1312 1312 1313 1313 if (pack_to_stdout) 1314 - f = hashfd_throughput(1, "<stdout>", progress_state); 1314 + f = hashfd_throughput(the_repository->hash_algo, 1, 1315 + "<stdout>", progress_state); 1315 1316 else 1316 1317 f = create_tmp_packfile(&pack_tmp_name); 1317 1318
+6 -3
commit-graph.c
··· 2090 2090 return -1; 2091 2091 } 2092 2092 2093 - f = hashfd(get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer)); 2093 + f = hashfd(the_repository->hash_algo, 2094 + get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer)); 2094 2095 } else { 2095 2096 hold_lock_file_for_update_mode(&lk, ctx->graph_name, 2096 2097 LOCK_DIE_ON_ERROR, 0444); 2097 - f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); 2098 + f = hashfd(the_repository->hash_algo, 2099 + get_lock_file_fd(&lk), get_lock_file_path(&lk)); 2098 2100 } 2099 2101 2100 2102 cf = init_chunkfile(f); ··· 2716 2718 2717 2719 static int commit_graph_checksum_valid(struct commit_graph *g) 2718 2720 { 2719 - return hashfile_checksum_valid(g->data, g->data_len); 2721 + return hashfile_checksum_valid(the_repository->hash_algo, 2722 + g->data, g->data_len); 2720 2723 } 2721 2724 2722 2725 static int verify_one_commit_graph(struct repository *r,
+16 -12
csum-file.c
··· 8 8 * able to verify hasn't been messed with afterwards. 9 9 */ 10 10 11 - #define USE_THE_REPOSITORY_VARIABLE 12 - 13 11 #include "git-compat-util.h" 14 12 #include "csum-file.h" 15 13 #include "git-zlib.h" ··· 148 146 } 149 147 } 150 148 151 - struct hashfile *hashfd_check(const char *name) 149 + struct hashfile *hashfd_check(const struct git_hash_algo *algop, 150 + const char *name) 152 151 { 153 152 int sink, check; 154 153 struct hashfile *f; 155 154 156 155 sink = xopen("/dev/null", O_WRONLY); 157 156 check = xopen(name, O_RDONLY); 158 - f = hashfd(sink, name); 157 + f = hashfd(algop, sink, name); 159 158 f->check_fd = check; 160 159 f->check_buffer = xmalloc(f->buffer_len); 161 160 162 161 return f; 163 162 } 164 163 165 - static struct hashfile *hashfd_internal(int fd, const char *name, 164 + static struct hashfile *hashfd_internal(const struct git_hash_algo *algop, 165 + int fd, const char *name, 166 166 struct progress *tp, 167 167 size_t buffer_len) 168 168 { ··· 176 176 f->do_crc = 0; 177 177 f->skip_hash = 0; 178 178 179 - f->algop = unsafe_hash_algo(the_hash_algo); 179 + f->algop = unsafe_hash_algo(algop); 180 180 f->algop->init_fn(&f->ctx); 181 181 182 182 f->buffer_len = buffer_len; ··· 186 186 return f; 187 187 } 188 188 189 - struct hashfile *hashfd(int fd, const char *name) 189 + struct hashfile *hashfd(const struct git_hash_algo *algop, 190 + int fd, const char *name) 190 191 { 191 192 /* 192 193 * Since we are not going to use a progress meter to 193 194 * measure the rate of data passing through this hashfile, 194 195 * use a larger buffer size to reduce fsync() calls. 195 196 */ 196 - return hashfd_internal(fd, name, NULL, 128 * 1024); 197 + return hashfd_internal(algop, fd, name, NULL, 128 * 1024); 197 198 } 198 199 199 - struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp) 200 + struct hashfile *hashfd_throughput(const struct git_hash_algo *algop, 201 + int fd, const char *name, struct progress *tp) 200 202 { 201 203 /* 202 204 * Since we are expecting to report progress of the ··· 204 206 * size so the progress indicators arrive at a more 205 207 * frequent rate. 206 208 */ 207 - return hashfd_internal(fd, name, tp, 8 * 1024); 209 + return hashfd_internal(algop, fd, name, tp, 8 * 1024); 208 210 } 209 211 210 212 void hashfile_checkpoint_init(struct hashfile *f, ··· 246 248 return f->crc32; 247 249 } 248 250 249 - int hashfile_checksum_valid(const unsigned char *data, size_t total_len) 251 + int hashfile_checksum_valid(const struct git_hash_algo *algop, 252 + const unsigned char *data, size_t total_len) 250 253 { 251 254 unsigned char got[GIT_MAX_RAWSZ]; 252 255 struct git_hash_ctx ctx; 253 - const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo); 254 256 size_t data_len = total_len - algop->rawsz; 257 + 258 + algop = unsafe_hash_algo(algop); 255 259 256 260 if (total_len < algop->rawsz) 257 261 return 0; /* say "too short"? */
+8 -4
csum-file.h
··· 45 45 #define CSUM_FSYNC 2 46 46 #define CSUM_HASH_IN_STREAM 4 47 47 48 - struct hashfile *hashfd(int fd, const char *name); 49 - struct hashfile *hashfd_check(const char *name); 50 - struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp); 48 + struct hashfile *hashfd(const struct git_hash_algo *algop, 49 + int fd, const char *name); 50 + struct hashfile *hashfd_check(const struct git_hash_algo *algop, 51 + const char *name); 52 + struct hashfile *hashfd_throughput(const struct git_hash_algo *algop, 53 + int fd, const char *name, struct progress *tp); 51 54 52 55 /* 53 56 * Free the hashfile without flushing its contents to disk. This only ··· 66 69 uint32_t crc32_end(struct hashfile *); 67 70 68 71 /* Verify checksum validity while reading. Returns non-zero on success. */ 69 - int hashfile_checksum_valid(const unsigned char *data, size_t len); 72 + int hashfile_checksum_valid(const struct git_hash_algo *algop, 73 + const unsigned char *data, size_t len); 70 74 71 75 /* 72 76 * Returns the total number of bytes fed to the hashfile so far (including ones
+4 -2
midx-write.c
··· 1342 1342 return -1; 1343 1343 } 1344 1344 1345 - f = hashfd(get_tempfile_fd(incr), get_tempfile_path(incr)); 1345 + f = hashfd(r->hash_algo, get_tempfile_fd(incr), 1346 + get_tempfile_path(incr)); 1346 1347 } else { 1347 1348 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR); 1348 - f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); 1349 + f = hashfd(r->hash_algo, get_lock_file_fd(&lk), 1350 + get_lock_file_path(&lk)); 1349 1351 } 1350 1352 1351 1353 cf = init_chunkfile(f);
+2 -1
midx.c
··· 747 747 748 748 int midx_checksum_valid(struct multi_pack_index *m) 749 749 { 750 - return hashfile_checksum_valid(m->data, m->data_len); 750 + return hashfile_checksum_valid(m->repo->hash_algo, 751 + m->data, m->data_len); 751 752 } 752 753 753 754 struct clear_midx_data {
+1 -1
pack-bitmap-write.c
··· 1030 1030 if (writer->pseudo_merges_nr) 1031 1031 options |= BITMAP_OPT_PSEUDO_MERGES; 1032 1032 1033 - f = hashfd(fd, tmp_file.buf); 1033 + f = hashfd(the_repository->hash_algo, fd, tmp_file.buf); 1034 1034 1035 1035 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)); 1036 1036 header.version = htons(default_version);
+5 -4
pack-bitmap.c
··· 3024 3024 return 0; 3025 3025 } 3026 3026 3027 - static int verify_bitmap_file(const char *name) 3027 + static int verify_bitmap_file(const struct git_hash_algo *algop, 3028 + const char *name) 3028 3029 { 3029 3030 struct stat st; 3030 3031 unsigned char *data; ··· 3040 3041 3041 3042 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 3042 3043 close(fd); 3043 - if (!hashfile_checksum_valid(data, st.st_size)) 3044 + if (!hashfile_checksum_valid(algop, data, st.st_size)) 3044 3045 res = error(_("bitmap file '%s' has invalid checksum"), 3045 3046 name); 3046 3047 ··· 3055 3056 for (struct multi_pack_index *m = get_multi_pack_index(r); 3056 3057 m; m = m->next) { 3057 3058 char *midx_bitmap_name = midx_bitmap_filename(m); 3058 - res |= verify_bitmap_file(midx_bitmap_name); 3059 + res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name); 3059 3060 free(midx_bitmap_name); 3060 3061 } 3061 3062 3062 3063 for (struct packed_git *p = get_all_packs(r); 3063 3064 p; p = p->next) { 3064 3065 char *pack_bitmap_name = pack_bitmap_filename(p); 3065 - res |= verify_bitmap_file(pack_bitmap_name); 3066 + res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name); 3066 3067 free(pack_bitmap_name); 3067 3068 } 3068 3069
+1 -1
pack-check.c
··· 180 180 return error("packfile %s index not opened", p->pack_name); 181 181 182 182 /* Verify SHA1 sum of the index file */ 183 - if (!hashfile_checksum_valid(p->index_data, p->index_size)) 183 + if (!hashfile_checksum_valid(the_repository->hash_algo, p->index_data, p->index_size)) 184 184 err = error("Packfile index for %s hash mismatch", 185 185 p->pack_name); 186 186 return err;
+2 -1
pack-revindex.c
··· 322 322 if (!p->revindex_map || !p->revindex_data) 323 323 return res; 324 324 325 - if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) { 325 + if (!hashfile_checksum_valid(the_repository->hash_algo, 326 + (const unsigned char *)p->revindex_map, p->revindex_size)) { 326 327 error(_("invalid checksum")); 327 328 res = -1; 328 329 }
+6 -6
pack-write.c
··· 82 82 83 83 if (opts->flags & WRITE_IDX_VERIFY) { 84 84 assert(index_name); 85 - f = hashfd_check(index_name); 85 + f = hashfd_check(the_repository->hash_algo, index_name); 86 86 } else { 87 87 if (!index_name) { 88 88 struct strbuf tmp_file = STRBUF_INIT; ··· 92 92 unlink(index_name); 93 93 fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); 94 94 } 95 - f = hashfd(fd, index_name); 95 + f = hashfd(the_repository->hash_algo, fd, index_name); 96 96 } 97 97 98 98 /* if last object's offset is >= 2^31 we should use index V2 */ ··· 268 268 fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600); 269 269 path = xstrdup(rev_name); 270 270 } 271 - f = hashfd(fd, path); 271 + f = hashfd(the_repository->hash_algo, fd, path); 272 272 } else if (flags & WRITE_REV_VERIFY) { 273 273 struct stat statbuf; 274 274 if (stat(rev_name, &statbuf)) { ··· 278 278 } else 279 279 die_errno(_("could not stat: %s"), rev_name); 280 280 } 281 - f = hashfd_check(rev_name); 281 + f = hashfd_check(the_repository->hash_algo, rev_name); 282 282 path = xstrdup(rev_name); 283 283 } else { 284 284 return NULL; ··· 346 346 347 347 fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX"); 348 348 mtimes_name = strbuf_detach(&tmp_file, NULL); 349 - f = hashfd(fd, mtimes_name); 349 + f = hashfd(the_repository->hash_algo, fd, mtimes_name); 350 350 351 351 write_mtimes_header(hash_algo, f); 352 352 write_mtimes_objects(f, to_pack, objects, nr_objects); ··· 534 534 535 535 fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX"); 536 536 *pack_tmp_name = strbuf_detach(&tmpname, NULL); 537 - return hashfd(fd, *pack_tmp_name); 537 + return hashfd(the_repository->hash_algo, fd, *pack_tmp_name); 538 538 } 539 539 540 540 static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
+1 -1
read-cache.c
··· 2848 2848 struct strbuf sb = STRBUF_INIT; 2849 2849 int nr, nr_threads, ret; 2850 2850 2851 - f = hashfd(tempfile->fd, tempfile->filename.buf); 2851 + f = hashfd(the_repository->hash_algo, tempfile->fd, tempfile->filename.buf); 2852 2852 2853 2853 prepare_repo_settings(r); 2854 2854 f->skip_hash = r->settings.index_skip_hash;