pack-write: stop depending on `the_repository` and `the_hash_algo`
There are a couple of functions in "pack-write.c" that implicitly depend on `the_repository` or `the_hash_algo`. Remove this dependency by injecting the repository via a parameter and adapt callers accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 10, 2025 at 08:13 UTC
2582846f2fe21b23fe7c567e030510960f135160
7 files changed
+41
-41
builtin/fast-import.c
+1
-1
@@ -798,7 +798,7 @@ static const char *create_index(void)
798
if (c != last)
799
die("internal consistency error creating the index");
800
801
- tmpfile = write_idx_file(the_hash_algo, NULL, idx, object_count,
801
+ tmpfile = write_idx_file(the_repository, NULL, idx, object_count,
802
&pack_idx_opts, pack_data->hash);
803
free(idx);
804
return tmpfile;
builtin/index-pack.c
+2
-2
@@ -2088,10 +2088,10 @@ int cmd_index_pack(int argc,
2088
ALLOC_ARRAY(idx_objects, nr_objects);
2089
for (i = 0; i < nr_objects; i++)
2090
idx_objects[i] = &objects[i].idx;
2091
- curr_index = write_idx_file(the_hash_algo, index_name, idx_objects,
2091
+ curr_index = write_idx_file(the_repository, index_name, idx_objects,
2092
nr_objects, &opts, pack_hash);
2093
if (rev_index)
2094
- curr_rev_index = write_rev_file(the_hash_algo, rev_index_name,
2094
+ curr_rev_index = write_rev_file(the_repository, rev_index_name,
2095
idx_objects, nr_objects,
2096
pack_hash, opts.flags);
2097
free(idx_objects);
builtin/pack-objects.c
+2
-2
@@ -1314,7 +1314,7 @@ static void write_pack_file(void)
1314
f = hashfd_throughput(the_repository->hash_algo, 1,
1315
"<stdout>", progress_state);
1316
else
1317
- f = create_tmp_packfile(&pack_tmp_name);
1317
+ f = create_tmp_packfile(the_repository, &pack_tmp_name);
1318
1319
offset = write_pack_header(f, nr_remaining);
1320
@@ -1407,7 +1407,7 @@ static void write_pack_file(void)
1407
if (cruft)
1408
pack_idx_opts.flags |= WRITE_MTIMES;
1409
1410
- stage_tmp_packfiles(the_hash_algo, &tmpname,
1410
+ stage_tmp_packfiles(the_repository, &tmpname,
1411
pack_tmp_name, written_list,
1412
nr_written, &to_pack,
1413
&pack_idx_opts, hash,
bulk-checkin.c
+2
-2
@@ -44,7 +44,7 @@ static void finish_tmp_packfile(struct strbuf *basename,
44
{
45
char *idx_tmp_name = NULL;
46
47
- stage_tmp_packfiles(the_hash_algo, basename, pack_tmp_name,
47
+ stage_tmp_packfiles(the_repository, basename, pack_tmp_name,
48
written_list, nr_written, NULL, pack_idx_opts, hash,
49
&idx_tmp_name);
50
rename_tmp_packfile_idx(basename, &idx_tmp_name);
@@ -244,7 +244,7 @@ static void prepare_to_stream(struct bulk_checkin_packfile *state,
244
if (!(flags & HASH_WRITE_OBJECT) || state->f)
245
return;
246
247
- state->f = create_tmp_packfile(&state->pack_tmp_name);
247
+ state->f = create_tmp_packfile(the_repository, &state->pack_tmp_name);
248
reset_pack_idx_option(&state->pack_idx_opts);
249
250
/* Pretend we are going to write only one object */
midx-write.c
+1
-1
@@ -658,7 +658,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
658
strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
659
ctx->repo->hash_algo));
660
661
- tmp_file = write_rev_file_order(ctx->repo->hash_algo, NULL, ctx->pack_order,
661
+ tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
662
ctx->entries_nr, midx_hash, WRITE_REV);
663
664
if (finalize_object_file(tmp_file, buf.buf))
pack-write.c
+27
-28
@@ -1,5 +1,3 @@
1
-#define USE_THE_REPOSITORY_VARIABLE
2
-
1
#include "git-compat-util.h"
2
#include "environment.h"
3
#include "gettext.h"
@@ -56,7 +54,7 @@ static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
54
* The *sha1 contains the pack content SHA1 hash.
55
* The objects array passed in will be sorted by SHA1 on exit.
56
*/
59
-const char *write_idx_file(const struct git_hash_algo *hash_algo,
57
+const char *write_idx_file(struct repository *repo,
58
const char *index_name, struct pack_idx_entry **objects,
59
int nr_objects, const struct pack_idx_option *opts,
60
const unsigned char *sha1)
@@ -82,7 +80,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
80
81
if (opts->flags & WRITE_IDX_VERIFY) {
82
assert(index_name);
85
- f = hashfd_check(the_repository->hash_algo, index_name);
83
+ f = hashfd_check(repo->hash_algo, index_name);
84
} else {
85
if (!index_name) {
86
struct strbuf tmp_file = STRBUF_INIT;
@@ -92,7 +90,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
90
unlink(index_name);
91
fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
92
}
95
- f = hashfd(the_repository->hash_algo, fd, index_name);
93
+ f = hashfd(repo->hash_algo, fd, index_name);
94
}
95
96
/* if last object's offset is >= 2^31 we should use index V2 */
@@ -131,7 +129,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
129
struct pack_idx_entry *obj = *list++;
130
if (index_version < 2)
131
hashwrite_be32(f, obj->offset);
134
- hashwrite(f, obj->oid.hash, hash_algo->rawsz);
132
+ hashwrite(f, obj->oid.hash, repo->hash_algo->rawsz);
133
if ((opts->flags & WRITE_IDX_STRICT) &&
134
(i && oideq(&list[-2]->oid, &obj->oid)))
135
die("The same object %s appears twice in the pack",
@@ -173,7 +171,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
171
}
172
}
173
176
- hashwrite(f, sha1, hash_algo->rawsz);
174
+ hashwrite(f, sha1, repo->hash_algo->rawsz);
175
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
176
CSUM_HASH_IN_STREAM | CSUM_CLOSE |
177
((opts->flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC));
@@ -217,7 +215,7 @@ static void write_rev_trailer(const struct git_hash_algo *hash_algo,
215
hashwrite(f, hash, hash_algo->rawsz);
216
}
217
220
-char *write_rev_file(const struct git_hash_algo *hash_algo,
218
+char *write_rev_file(struct repository *repo,
219
const char *rev_name,
220
struct pack_idx_entry **objects,
221
uint32_t nr_objects,
@@ -236,7 +234,7 @@ char *write_rev_file(const struct git_hash_algo *hash_algo,
234
pack_order[i] = i;
235
QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
236
239
- ret = write_rev_file_order(hash_algo, rev_name, pack_order, nr_objects,
237
+ ret = write_rev_file_order(repo, rev_name, pack_order, nr_objects,
238
hash, flags);
239
240
free(pack_order);
@@ -244,7 +242,7 @@ char *write_rev_file(const struct git_hash_algo *hash_algo,
242
return ret;
243
}
244
247
-char *write_rev_file_order(const struct git_hash_algo *hash_algo,
245
+char *write_rev_file_order(struct repository *repo,
246
const char *rev_name,
247
uint32_t *pack_order,
248
uint32_t nr_objects,
@@ -268,7 +266,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
266
fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
267
path = xstrdup(rev_name);
268
}
271
- f = hashfd(the_repository->hash_algo, fd, path);
269
+ f = hashfd(repo->hash_algo, fd, path);
270
} else if (flags & WRITE_REV_VERIFY) {
271
struct stat statbuf;
272
if (stat(rev_name, &statbuf)) {
@@ -278,18 +276,18 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
276
} else
277
die_errno(_("could not stat: %s"), rev_name);
278
}
281
- f = hashfd_check(the_repository->hash_algo, rev_name);
279
+ f = hashfd_check(repo->hash_algo, rev_name);
280
path = xstrdup(rev_name);
281
} else {
282
return NULL;
283
}
284
287
- write_rev_header(hash_algo, f);
285
+ write_rev_header(repo->hash_algo, f);
286
287
write_rev_index_positions(f, pack_order, nr_objects);
290
- write_rev_trailer(hash_algo, f, hash);
288
+ write_rev_trailer(repo->hash_algo, f, hash);
289
292
- if (adjust_shared_perm(the_repository, path) < 0)
290
+ if (adjust_shared_perm(repo, path) < 0)
291
die(_("failed to make %s readable"), path);
292
293
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@@ -330,7 +328,7 @@ static void write_mtimes_trailer(const struct git_hash_algo *hash_algo,
328
hashwrite(f, hash, hash_algo->rawsz);
329
}
330
333
-static char *write_mtimes_file(const struct git_hash_algo *hash_algo,
331
+static char *write_mtimes_file(struct repository *repo,
332
struct packing_data *to_pack,
333
struct pack_idx_entry **objects,
334
uint32_t nr_objects,
@@ -346,13 +344,13 @@ static char *write_mtimes_file(const struct git_hash_algo *hash_algo,
344
345
fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX");
346
mtimes_name = strbuf_detach(&tmp_file, NULL);
349
- f = hashfd(the_repository->hash_algo, fd, mtimes_name);
347
+ f = hashfd(repo->hash_algo, fd, mtimes_name);
348
351
- write_mtimes_header(hash_algo, f);
349
+ write_mtimes_header(repo->hash_algo, f);
350
write_mtimes_objects(f, to_pack, objects, nr_objects);
353
- write_mtimes_trailer(hash_algo, f, hash);
351
+ write_mtimes_trailer(repo->hash_algo, f, hash);
352
355
- if (adjust_shared_perm(the_repository, mtimes_name) < 0)
353
+ if (adjust_shared_perm(repo, mtimes_name) < 0)
354
die(_("failed to make %s readable"), mtimes_name);
355
356
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@@ -527,14 +525,15 @@ int encode_in_pack_object_header(unsigned char *hdr, int hdr_len,
525
return n;
526
}
527
530
-struct hashfile *create_tmp_packfile(char **pack_tmp_name)
528
+struct hashfile *create_tmp_packfile(struct repository *repo,
529
+ char **pack_tmp_name)
530
{
531
struct strbuf tmpname = STRBUF_INIT;
532
int fd;
533
534
fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX");
535
*pack_tmp_name = strbuf_detach(&tmpname, NULL);
537
- return hashfd(the_repository->hash_algo, fd, *pack_tmp_name);
536
+ return hashfd(repo->hash_algo, fd, *pack_tmp_name);
537
}
538
539
static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
@@ -555,7 +554,7 @@ void rename_tmp_packfile_idx(struct strbuf *name_buffer,
554
rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
555
}
556
558
-void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
557
+void stage_tmp_packfiles(struct repository *repo,
558
struct strbuf *name_buffer,
559
const char *pack_tmp_name,
560
struct pack_idx_entry **written_list,
@@ -568,19 +567,19 @@ void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
567
char *rev_tmp_name = NULL;
568
char *mtimes_tmp_name = NULL;
569
571
- if (adjust_shared_perm(the_repository, pack_tmp_name))
570
+ if (adjust_shared_perm(repo, pack_tmp_name))
571
die_errno("unable to make temporary pack file readable");
572
574
- *idx_tmp_name = (char *)write_idx_file(hash_algo, NULL, written_list,
573
+ *idx_tmp_name = (char *)write_idx_file(repo, NULL, written_list,
574
nr_written, pack_idx_opts, hash);
576
- if (adjust_shared_perm(the_repository, *idx_tmp_name))
575
+ if (adjust_shared_perm(repo, *idx_tmp_name))
576
die_errno("unable to make temporary index file readable");
577
579
- rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written,
578
+ rev_tmp_name = write_rev_file(repo, NULL, written_list, nr_written,
579
hash, pack_idx_opts->flags);
580
581
if (pack_idx_opts->flags & WRITE_MTIMES) {
583
- mtimes_tmp_name = write_mtimes_file(hash_algo, to_pack,
582
+ mtimes_tmp_name = write_mtimes_file(repo, to_pack,
583
written_list, nr_written,
584
hash);
585
}
pack.h
+6
-5
@@ -87,7 +87,7 @@ struct progress;
87
/* Note, the data argument could be NULL if object type is blob */
88
typedef int (*verify_fn)(const struct object_id *, enum object_type, unsigned long, void*, int*);
89
90
-const char *write_idx_file(const struct git_hash_algo *hash_algo,
90
+const char *write_idx_file(struct repository *repo,
91
const char *index_name,
92
struct pack_idx_entry **objects,
93
int nr_objects,
@@ -106,13 +106,13 @@ struct ref;
106
107
void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought);
108
109
-char *write_rev_file(const struct git_hash_algo *hash_algo,
109
+char *write_rev_file(struct repository *repo,
110
const char *rev_name,
111
struct pack_idx_entry **objects,
112
uint32_t nr_objects,
113
const unsigned char *hash,
114
unsigned flags);
115
-char *write_rev_file_order(const struct git_hash_algo *hash_algo,
115
+char *write_rev_file_order(struct repository *repo,
116
const char *rev_name,
117
uint32_t *pack_order,
118
uint32_t nr_objects,
@@ -134,8 +134,9 @@ int read_pack_header(int fd, struct pack_header *);
134
135
struct packing_data;
136
137
-struct hashfile *create_tmp_packfile(char **pack_tmp_name);
138
-void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
137
+struct hashfile *create_tmp_packfile(struct repository *repo,
138
+ char **pack_tmp_name);
139
+void stage_tmp_packfiles(struct repository *repo,
140
struct strbuf *name_buffer,
141
const char *pack_tmp_name,
142
struct pack_idx_entry **written_list,