Skip to content

Commit c3155cd

Browse files
KarthikNayakgitster
authored andcommitted
pack-write: pass hash_algo to write_idx_file()
The `write_idx_file()` function uses the global `the_hash_algo` variable to access the repository's hash function. To avoid global variable usage, 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. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41a39af commit c3155cd

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

builtin/fast-import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,8 @@ static const char *create_index(void)
798798
if (c != last)
799799
die("internal consistency error creating the index");
800800

801-
tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts,
802-
pack_data->hash);
801+
tmpfile = write_idx_file(the_hash_algo, NULL, idx, object_count,
802+
&pack_idx_opts, pack_data->hash);
803803
free(idx);
804804
return tmpfile;
805805
}

builtin/index-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,8 @@ int cmd_index_pack(int argc,
20932093
ALLOC_ARRAY(idx_objects, nr_objects);
20942094
for (i = 0; i < nr_objects; i++)
20952095
idx_objects[i] = &objects[i].idx;
2096-
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
2096+
curr_index = write_idx_file(the_hash_algo, index_name, idx_objects,
2097+
nr_objects, &opts, pack_hash);
20972098
if (rev_index)
20982099
curr_rev_index = write_rev_file(rev_index_name, idx_objects,
20992100
nr_objects, pack_hash,

builtin/pack-objects.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,9 +1368,10 @@ static void write_pack_file(void)
13681368
if (cruft)
13691369
pack_idx_opts.flags |= WRITE_MTIMES;
13701370

1371-
stage_tmp_packfiles(&tmpname, pack_tmp_name,
1372-
written_list, nr_written,
1373-
&to_pack, &pack_idx_opts, hash,
1371+
stage_tmp_packfiles(the_hash_algo, &tmpname,
1372+
pack_tmp_name, written_list,
1373+
nr_written, &to_pack,
1374+
&pack_idx_opts, hash,
13741375
&idx_tmp_name);
13751376

13761377
if (write_bitmap_index) {

bulk-checkin.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ static void finish_tmp_packfile(struct strbuf *basename,
4444
{
4545
char *idx_tmp_name = NULL;
4646

47-
stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written,
48-
NULL, pack_idx_opts, hash, &idx_tmp_name);
47+
stage_tmp_packfiles(the_hash_algo, basename, pack_tmp_name,
48+
written_list, nr_written, NULL, pack_idx_opts, hash,
49+
&idx_tmp_name);
4950
rename_tmp_packfile_idx(basename, &idx_tmp_name);
5051

5152
free(idx_tmp_name);

pack-write.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
5656
* The *sha1 contains the pack content SHA1 hash.
5757
* The objects array passed in will be sorted by SHA1 on exit.
5858
*/
59-
const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
59+
const char *write_idx_file(const struct git_hash_algo *hash_algo,
60+
const char *index_name, struct pack_idx_entry **objects,
6061
int nr_objects, const struct pack_idx_option *opts,
6162
const unsigned char *sha1)
6263
{
@@ -130,7 +131,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
130131
struct pack_idx_entry *obj = *list++;
131132
if (index_version < 2)
132133
hashwrite_be32(f, obj->offset);
133-
hashwrite(f, obj->oid.hash, the_hash_algo->rawsz);
134+
hashwrite(f, obj->oid.hash, hash_algo->rawsz);
134135
if ((opts->flags & WRITE_IDX_STRICT) &&
135136
(i && oideq(&list[-2]->oid, &obj->oid)))
136137
die("The same object %s appears twice in the pack",
@@ -172,7 +173,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
172173
}
173174
}
174175

175-
hashwrite(f, sha1, the_hash_algo->rawsz);
176+
hashwrite(f, sha1, hash_algo->rawsz);
176177
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
177178
CSUM_HASH_IN_STREAM | CSUM_CLOSE |
178179
((opts->flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC));
@@ -546,7 +547,8 @@ void rename_tmp_packfile_idx(struct strbuf *name_buffer,
546547
rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
547548
}
548549

549-
void stage_tmp_packfiles(struct strbuf *name_buffer,
550+
void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
551+
struct strbuf *name_buffer,
550552
const char *pack_tmp_name,
551553
struct pack_idx_entry **written_list,
552554
uint32_t nr_written,
@@ -561,8 +563,8 @@ void stage_tmp_packfiles(struct strbuf *name_buffer,
561563
if (adjust_shared_perm(pack_tmp_name))
562564
die_errno("unable to make temporary pack file readable");
563565

564-
*idx_tmp_name = (char *)write_idx_file(NULL, written_list, nr_written,
565-
pack_idx_opts, hash);
566+
*idx_tmp_name = (char *)write_idx_file(hash_algo, NULL, written_list,
567+
nr_written, pack_idx_opts, hash);
566568
if (adjust_shared_perm(*idx_tmp_name))
567569
die_errno("unable to make temporary index file readable");
568570

pack.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ struct progress;
8686
/* Note, the data argument could be NULL if object type is blob */
8787
typedef int (*verify_fn)(const struct object_id *, enum object_type, unsigned long, void*, int*);
8888

89-
const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, int nr_objects, const struct pack_idx_option *, const unsigned char *sha1);
89+
const char *write_idx_file(const struct git_hash_algo *hash_algo,
90+
const char *index_name,
91+
struct pack_idx_entry **objects,
92+
int nr_objects,
93+
const struct pack_idx_option *,
94+
const unsigned char *sha1);
9095
int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
9196
int verify_pack_index(struct packed_git *);
9297
int verify_pack(struct repository *, struct packed_git *, verify_fn fn, struct progress *, uint32_t);
@@ -119,7 +124,8 @@ int read_pack_header(int fd, struct pack_header *);
119124
struct packing_data;
120125

121126
struct hashfile *create_tmp_packfile(char **pack_tmp_name);
122-
void stage_tmp_packfiles(struct strbuf *name_buffer,
127+
void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
128+
struct strbuf *name_buffer,
123129
const char *pack_tmp_name,
124130
struct pack_idx_entry **written_list,
125131
uint32_t nr_written,

0 commit comments

Comments
 (0)