Skip to content

Commit 228457c

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e969bc8 commit 228457c

14 files changed

+56
-39
lines changed

builtin/fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ static void start_packfile(void)
770770
p->pack_fd = pack_fd;
771771
p->do_not_close = 1;
772772
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);
774774

775775
pack_data = p;
776776
pack_size = write_pack_header(pack_file, 0);

builtin/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
13811381
REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
13821382
memset(objects + nr_objects + 1, 0,
13831383
nr_unresolved * sizeof(*objects));
1384-
f = hashfd(output_fd, curr_pack);
1384+
f = hashfd(the_repository->hash_algo, output_fd, curr_pack);
13851385
fix_unresolved_deltas(f);
13861386
strbuf_addf(&msg, Q_("completed with %d local object",
13871387
"completed with %d local objects",

builtin/pack-objects.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,8 @@ static void write_pack_file(void)
13111311
char *pack_tmp_name = NULL;
13121312

13131313
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);
13151316
else
13161317
f = create_tmp_packfile(&pack_tmp_name);
13171318

commit-graph.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,11 +2090,13 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
20902090
return -1;
20912091
}
20922092

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));
20942095
} else {
20952096
hold_lock_file_for_update_mode(&lk, ctx->graph_name,
20962097
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));
20982100
}
20992101

21002102
cf = init_chunkfile(f);
@@ -2716,7 +2718,8 @@ static void graph_report(const char *fmt, ...)
27162718

27172719
static int commit_graph_checksum_valid(struct commit_graph *g)
27182720
{
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);
27202723
}
27212724

27222725
static int verify_one_commit_graph(struct repository *r,

csum-file.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
* able to verify hasn't been messed with afterwards.
99
*/
1010

11-
#define USE_THE_REPOSITORY_VARIABLE
12-
1311
#include "git-compat-util.h"
1412
#include "csum-file.h"
1513
#include "git-zlib.h"
@@ -148,21 +146,23 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
148146
}
149147
}
150148

151-
struct hashfile *hashfd_check(const char *name)
149+
struct hashfile *hashfd_check(const struct git_hash_algo *algop,
150+
const char *name)
152151
{
153152
int sink, check;
154153
struct hashfile *f;
155154

156155
sink = xopen("/dev/null", O_WRONLY);
157156
check = xopen(name, O_RDONLY);
158-
f = hashfd(sink, name);
157+
f = hashfd(algop, sink, name);
159158
f->check_fd = check;
160159
f->check_buffer = xmalloc(f->buffer_len);
161160

162161
return f;
163162
}
164163

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,
166166
struct progress *tp,
167167
size_t buffer_len)
168168
{
@@ -176,7 +176,7 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
176176
f->do_crc = 0;
177177
f->skip_hash = 0;
178178

179-
f->algop = unsafe_hash_algo(the_hash_algo);
179+
f->algop = unsafe_hash_algo(algop);
180180
f->algop->init_fn(&f->ctx);
181181

182182
f->buffer_len = buffer_len;
@@ -186,25 +186,27 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
186186
return f;
187187
}
188188

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)
190191
{
191192
/*
192193
* Since we are not going to use a progress meter to
193194
* measure the rate of data passing through this hashfile,
194195
* use a larger buffer size to reduce fsync() calls.
195196
*/
196-
return hashfd_internal(fd, name, NULL, 128 * 1024);
197+
return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
197198
}
198199

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)
200202
{
201203
/*
202204
* Since we are expecting to report progress of the
203205
* write into this hashfile, use a smaller buffer
204206
* size so the progress indicators arrive at a more
205207
* frequent rate.
206208
*/
207-
return hashfd_internal(fd, name, tp, 8 * 1024);
209+
return hashfd_internal(algop, fd, name, tp, 8 * 1024);
208210
}
209211

210212
void hashfile_checkpoint_init(struct hashfile *f,
@@ -246,13 +248,15 @@ uint32_t crc32_end(struct hashfile *f)
246248
return f->crc32;
247249
}
248250

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)
250253
{
251254
unsigned char got[GIT_MAX_RAWSZ];
252255
struct git_hash_ctx ctx;
253-
const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo);
254256
size_t data_len = total_len - algop->rawsz;
255257

258+
algop = unsafe_hash_algo(algop);
259+
256260
if (total_len < algop->rawsz)
257261
return 0; /* say "too short"? */
258262

csum-file.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
4545
#define CSUM_FSYNC 2
4646
#define CSUM_HASH_IN_STREAM 4
4747

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);
5154

5255
/*
5356
* Free the hashfile without flushing its contents to disk. This only
@@ -66,7 +69,8 @@ void crc32_begin(struct hashfile *);
6669
uint32_t crc32_end(struct hashfile *);
6770

6871
/* 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);
7074

7175
/*
7276
* Returns the total number of bytes fed to the hashfile so far (including ones

midx-write.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13421342
return -1;
13431343
}
13441344

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));
13461347
} else {
13471348
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));
13491351
}
13501352

13511353
cf = init_chunkfile(f);

midx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
747747

748748
int midx_checksum_valid(struct multi_pack_index *m)
749749
{
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);
751752
}
752753

753754
struct clear_midx_data {

pack-bitmap-write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
10301030
if (writer->pseudo_merges_nr)
10311031
options |= BITMAP_OPT_PSEUDO_MERGES;
10321032

1033-
f = hashfd(fd, tmp_file.buf);
1033+
f = hashfd(the_repository->hash_algo, fd, tmp_file.buf);
10341034

10351035
memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
10361036
header.version = htons(default_version);

pack-bitmap.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,7 +3024,8 @@ int bitmap_is_preferred_refname(struct repository *r, const char *refname)
30243024
return 0;
30253025
}
30263026

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)
30283029
{
30293030
struct stat st;
30303031
unsigned char *data;
@@ -3040,7 +3041,7 @@ static int verify_bitmap_file(const char *name)
30403041

30413042
data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
30423043
close(fd);
3043-
if (!hashfile_checksum_valid(data, st.st_size))
3044+
if (!hashfile_checksum_valid(algop, data, st.st_size))
30443045
res = error(_("bitmap file '%s' has invalid checksum"),
30453046
name);
30463047

@@ -3055,14 +3056,14 @@ int verify_bitmap_files(struct repository *r)
30553056
for (struct multi_pack_index *m = get_multi_pack_index(r);
30563057
m; m = m->next) {
30573058
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);
30593060
free(midx_bitmap_name);
30603061
}
30613062

30623063
for (struct packed_git *p = get_all_packs(r);
30633064
p; p = p->next) {
30643065
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);
30663067
free(pack_bitmap_name);
30673068
}
30683069

0 commit comments

Comments
 (0)