Skip to content

Commit da5d847

Browse files
KarthikNayakgitster
authored andcommitted
pack-write: pass hash_algo to write_rev_file()
The `write_rev_file()` function uses the global `the_hash_algo` variable to access the repository's hash function. To avoid global variable usage, let's pass the hash function from the layers above. Altough the layers above could have access to the hash function internally, simply pass in `the_hash_algo`. This avoids any compatibility issues and bubbles up global variable usage to upper layers which can be eventually resolved. However, in `midx-write.c`, since all usage of global variables is removed, don't reintroduce them and instead use the `repo` available in the context. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3155cd commit da5d847

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

builtin/index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,9 +2096,9 @@ int cmd_index_pack(int argc,
20962096
curr_index = write_idx_file(the_hash_algo, index_name, idx_objects,
20972097
nr_objects, &opts, pack_hash);
20982098
if (rev_index)
2099-
curr_rev_index = write_rev_file(rev_index_name, idx_objects,
2100-
nr_objects, pack_hash,
2101-
opts.flags);
2099+
curr_rev_index = write_rev_file(the_hash_algo, rev_index_name,
2100+
idx_objects, nr_objects,
2101+
pack_hash, opts.flags);
21022102
free(idx_objects);
21032103

21042104
if (!verify)

midx-write.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,8 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
658658
strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
659659
ctx->repo->hash_algo));
660660

661-
tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
662-
midx_hash, WRITE_REV);
661+
tmp_file = write_rev_file_order(ctx->repo->hash_algo, NULL, ctx->pack_order,
662+
ctx->entries_nr, midx_hash, WRITE_REV);
663663

664664
if (finalize_object_file(tmp_file, buf.buf))
665665
die(_("cannot store reverse index file"));

pack-write.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ static int pack_order_cmp(const void *va, const void *vb, void *ctx)
194194
return 0;
195195
}
196196

197-
static void write_rev_header(struct hashfile *f)
197+
static void write_rev_header(const struct git_hash_algo *hash_algo,
198+
struct hashfile *f)
198199
{
199200
hashwrite_be32(f, RIDX_SIGNATURE);
200201
hashwrite_be32(f, RIDX_VERSION);
201-
hashwrite_be32(f, oid_version(the_hash_algo));
202+
hashwrite_be32(f, oid_version(hash_algo));
202203
}
203204

204205
static void write_rev_index_positions(struct hashfile *f,
@@ -215,7 +216,8 @@ static void write_rev_trailer(struct hashfile *f, const unsigned char *hash)
215216
hashwrite(f, hash, the_hash_algo->rawsz);
216217
}
217218

218-
char *write_rev_file(const char *rev_name,
219+
char *write_rev_file(const struct git_hash_algo *hash_algo,
220+
const char *rev_name,
219221
struct pack_idx_entry **objects,
220222
uint32_t nr_objects,
221223
const unsigned char *hash,
@@ -233,15 +235,16 @@ char *write_rev_file(const char *rev_name,
233235
pack_order[i] = i;
234236
QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
235237

236-
ret = write_rev_file_order(rev_name, pack_order, nr_objects, hash,
237-
flags);
238+
ret = write_rev_file_order(hash_algo, rev_name, pack_order, nr_objects,
239+
hash, flags);
238240

239241
free(pack_order);
240242

241243
return ret;
242244
}
243245

244-
char *write_rev_file_order(const char *rev_name,
246+
char *write_rev_file_order(const struct git_hash_algo *hash_algo,
247+
const char *rev_name,
245248
uint32_t *pack_order,
246249
uint32_t nr_objects,
247250
const unsigned char *hash,
@@ -280,7 +283,7 @@ char *write_rev_file_order(const char *rev_name,
280283
return NULL;
281284
}
282285

283-
write_rev_header(f);
286+
write_rev_header(hash_algo, f);
284287

285288
write_rev_index_positions(f, pack_order, nr_objects);
286289
write_rev_trailer(f, hash);
@@ -568,8 +571,8 @@ void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
568571
if (adjust_shared_perm(*idx_tmp_name))
569572
die_errno("unable to make temporary index file readable");
570573

571-
rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
572-
pack_idx_opts->flags);
574+
rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written,
575+
hash, pack_idx_opts->flags);
573576

574577
if (pack_idx_opts->flags & WRITE_MTIMES) {
575578
mtimes_tmp_name = write_mtimes_file(to_pack, written_list,

pack.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,18 @@ struct ref;
105105

106106
void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought);
107107

108-
char *write_rev_file(const char *rev_name, struct pack_idx_entry **objects, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
109-
char *write_rev_file_order(const char *rev_name, uint32_t *pack_order, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
108+
char *write_rev_file(const struct git_hash_algo *hash_algo,
109+
const char *rev_name,
110+
struct pack_idx_entry **objects,
111+
uint32_t nr_objects,
112+
const unsigned char *hash,
113+
unsigned flags);
114+
char *write_rev_file_order(const struct git_hash_algo *hash_algo,
115+
const char *rev_name,
116+
uint32_t *pack_order,
117+
uint32_t nr_objects,
118+
const unsigned char *hash,
119+
unsigned flags);
110120

111121
/*
112122
* The "hdr" output buffer should be at least this big, which will handle sizes

0 commit comments

Comments
 (0)