Skip to content

Commit 454253f

Browse files
bk2204gitster
authored andcommitted
builtin/index-pack: improve hash function abstraction
Convert several uses of unsigned char [20] to struct object_id and convert various hard-coded constants and uses of SHA-1 functions to use the_hash_algo. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac73ced commit 454253f

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

builtin/index-pack.c

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static unsigned int input_offset, input_len;
9191
static off_t consumed_bytes;
9292
static off_t max_input_size;
9393
static unsigned deepest_delta;
94-
static git_SHA_CTX input_ctx;
94+
static git_hash_ctx input_ctx;
9595
static uint32_t input_crc32;
9696
static int input_fd, output_fd;
9797
static const char *curr_pack;
@@ -253,7 +253,7 @@ static void flush(void)
253253
if (input_offset) {
254254
if (output_fd >= 0)
255255
write_or_die(output_fd, input_buffer, input_offset);
256-
git_SHA1_Update(&input_ctx, input_buffer, input_offset);
256+
the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
257257
memmove(input_buffer, input_buffer + input_offset, input_len);
258258
input_offset = 0;
259259
}
@@ -326,7 +326,7 @@ static const char *open_pack_file(const char *pack_name)
326326
output_fd = -1;
327327
nothread_data.pack_fd = input_fd;
328328
}
329-
git_SHA1_Init(&input_ctx);
329+
the_hash_algo->init_fn(&input_ctx);
330330
return pack_name;
331331
}
332332

@@ -437,22 +437,22 @@ static int is_delta_type(enum object_type type)
437437
}
438438

439439
static void *unpack_entry_data(off_t offset, unsigned long size,
440-
enum object_type type, unsigned char *sha1)
440+
enum object_type type, struct object_id *oid)
441441
{
442442
static char fixed_buf[8192];
443443
int status;
444444
git_zstream stream;
445445
void *buf;
446-
git_SHA_CTX c;
446+
git_hash_ctx c;
447447
char hdr[32];
448448
int hdrlen;
449449

450450
if (!is_delta_type(type)) {
451451
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), size) + 1;
452-
git_SHA1_Init(&c);
453-
git_SHA1_Update(&c, hdr, hdrlen);
452+
the_hash_algo->init_fn(&c);
453+
the_hash_algo->update_fn(&c, hdr, hdrlen);
454454
} else
455-
sha1 = NULL;
455+
oid = NULL;
456456
if (type == OBJ_BLOB && size > big_file_threshold)
457457
buf = fixed_buf;
458458
else
@@ -469,8 +469,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
469469
stream.avail_in = input_len;
470470
status = git_inflate(&stream, 0);
471471
use(input_len - stream.avail_in);
472-
if (sha1)
473-
git_SHA1_Update(&c, last_out, stream.next_out - last_out);
472+
if (oid)
473+
the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
474474
if (buf == fixed_buf) {
475475
stream.next_out = buf;
476476
stream.avail_out = sizeof(fixed_buf);
@@ -479,15 +479,15 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
479479
if (stream.total_out != size || status != Z_STREAM_END)
480480
bad_object(offset, _("inflate returned %d"), status);
481481
git_inflate_end(&stream);
482-
if (sha1)
483-
git_SHA1_Final(sha1, &c);
482+
if (oid)
483+
the_hash_algo->final_fn(oid->hash, &c);
484484
return buf == fixed_buf ? NULL : buf;
485485
}
486486

487487
static void *unpack_raw_entry(struct object_entry *obj,
488488
off_t *ofs_offset,
489-
unsigned char *ref_sha1,
490-
unsigned char *sha1)
489+
struct object_id *ref_oid,
490+
struct object_id *oid)
491491
{
492492
unsigned char *p;
493493
unsigned long size, c;
@@ -515,8 +515,8 @@ static void *unpack_raw_entry(struct object_entry *obj,
515515

516516
switch (obj->type) {
517517
case OBJ_REF_DELTA:
518-
hashcpy(ref_sha1, fill(20));
519-
use(20);
518+
hashcpy(ref_oid->hash, fill(the_hash_algo->rawsz));
519+
use(the_hash_algo->rawsz);
520520
break;
521521
case OBJ_OFS_DELTA:
522522
p = fill(1);
@@ -546,7 +546,7 @@ static void *unpack_raw_entry(struct object_entry *obj,
546546
}
547547
obj->hdr_size = consumed_bytes - obj->idx.offset;
548548

549-
data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, sha1);
549+
data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid);
550550
obj->idx.crc32 = input_crc32;
551551
return data;
552552
}
@@ -1119,11 +1119,11 @@ static void *threaded_second_pass(void *data)
11191119
* - calculate SHA1 of all non-delta objects;
11201120
* - remember base (SHA1 or offset) for all deltas.
11211121
*/
1122-
static void parse_pack_objects(unsigned char *sha1)
1122+
static void parse_pack_objects(unsigned char *hash)
11231123
{
11241124
int i, nr_delays = 0;
11251125
struct ofs_delta_entry *ofs_delta = ofs_deltas;
1126-
unsigned char ref_delta_sha1[20];
1126+
struct object_id ref_delta_oid;
11271127
struct stat st;
11281128

11291129
if (verbose)
@@ -1133,16 +1133,16 @@ static void parse_pack_objects(unsigned char *sha1)
11331133
for (i = 0; i < nr_objects; i++) {
11341134
struct object_entry *obj = &objects[i];
11351135
void *data = unpack_raw_entry(obj, &ofs_delta->offset,
1136-
ref_delta_sha1,
1137-
obj->idx.oid.hash);
1136+
&ref_delta_oid,
1137+
&obj->idx.oid);
11381138
obj->real_type = obj->type;
11391139
if (obj->type == OBJ_OFS_DELTA) {
11401140
nr_ofs_deltas++;
11411141
ofs_delta->obj_no = i;
11421142
ofs_delta++;
11431143
} else if (obj->type == OBJ_REF_DELTA) {
11441144
ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
1145-
hashcpy(ref_deltas[nr_ref_deltas].sha1, ref_delta_sha1);
1145+
hashcpy(ref_deltas[nr_ref_deltas].sha1, ref_delta_oid.hash);
11461146
ref_deltas[nr_ref_deltas].obj_no = i;
11471147
nr_ref_deltas++;
11481148
} else if (!data) {
@@ -1160,10 +1160,10 @@ static void parse_pack_objects(unsigned char *sha1)
11601160

11611161
/* Check pack integrity */
11621162
flush();
1163-
git_SHA1_Final(sha1, &input_ctx);
1164-
if (hashcmp(fill(20), sha1))
1163+
the_hash_algo->final_fn(hash, &input_ctx);
1164+
if (hashcmp(fill(the_hash_algo->rawsz), hash))
11651165
die(_("pack is corrupted (SHA1 mismatch)"));
1166-
use(20);
1166+
use(the_hash_algo->rawsz);
11671167

11681168
/* If input_fd is a file, we should have reached its end now. */
11691169
if (fstat(input_fd, &st))
@@ -1239,21 +1239,21 @@ static void resolve_deltas(void)
12391239
/*
12401240
* Third pass:
12411241
* - append objects to convert thin pack to full pack if required
1242-
* - write the final 20-byte SHA-1
1242+
* - write the final pack hash
12431243
*/
12441244
static void fix_unresolved_deltas(struct sha1file *f);
1245-
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_sha1)
1245+
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
12461246
{
12471247
if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
12481248
stop_progress(&progress);
1249-
/* Flush remaining pack final 20-byte SHA1. */
1249+
/* Flush remaining pack final hash. */
12501250
flush();
12511251
return;
12521252
}
12531253

12541254
if (fix_thin_pack) {
12551255
struct sha1file *f;
1256-
unsigned char read_sha1[20], tail_sha1[20];
1256+
unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
12571257
struct strbuf msg = STRBUF_INIT;
12581258
int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
12591259
int nr_objects_initial = nr_objects;
@@ -1270,12 +1270,12 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
12701270
nr_objects - nr_objects_initial);
12711271
stop_progress_msg(&progress, msg.buf);
12721272
strbuf_release(&msg);
1273-
sha1close(f, tail_sha1, 0);
1274-
hashcpy(read_sha1, pack_sha1);
1275-
fixup_pack_header_footer(output_fd, pack_sha1,
1273+
sha1close(f, tail_hash, 0);
1274+
hashcpy(read_hash, pack_hash);
1275+
fixup_pack_header_footer(output_fd, pack_hash,
12761276
curr_pack, nr_objects,
1277-
read_sha1, consumed_bytes-20);
1278-
if (hashcmp(read_sha1, tail_sha1) != 0)
1277+
read_hash, consumed_bytes-the_hash_algo->rawsz);
1278+
if (hashcmp(read_hash, tail_hash) != 0)
12791279
die(_("Unexpected tail checksum for %s "
12801280
"(disk corruption?)"), curr_pack);
12811281
}
@@ -1392,7 +1392,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
13921392
static void final(const char *final_pack_name, const char *curr_pack_name,
13931393
const char *final_index_name, const char *curr_index_name,
13941394
const char *keep_name, const char *keep_msg,
1395-
unsigned char *sha1)
1395+
unsigned char *hash)
13961396
{
13971397
const char *report = "pack";
13981398
struct strbuf pack_name = STRBUF_INIT;
@@ -1413,7 +1413,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14131413
int keep_fd, keep_msg_len = strlen(keep_msg);
14141414

14151415
if (!keep_name)
1416-
keep_name = odb_pack_name(&keep_name_buf, sha1, "keep");
1416+
keep_name = odb_pack_name(&keep_name_buf, hash, "keep");
14171417

14181418
keep_fd = odb_pack_keep(keep_name);
14191419
if (keep_fd < 0) {
@@ -1434,26 +1434,26 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14341434

14351435
if (final_pack_name != curr_pack_name) {
14361436
if (!final_pack_name)
1437-
final_pack_name = odb_pack_name(&pack_name, sha1, "pack");
1437+
final_pack_name = odb_pack_name(&pack_name, hash, "pack");
14381438
if (finalize_object_file(curr_pack_name, final_pack_name))
14391439
die(_("cannot store pack file"));
14401440
} else if (from_stdin)
14411441
chmod(final_pack_name, 0444);
14421442

14431443
if (final_index_name != curr_index_name) {
14441444
if (!final_index_name)
1445-
final_index_name = odb_pack_name(&index_name, sha1, "idx");
1445+
final_index_name = odb_pack_name(&index_name, hash, "idx");
14461446
if (finalize_object_file(curr_index_name, final_index_name))
14471447
die(_("cannot store index file"));
14481448
} else
14491449
chmod(final_index_name, 0444);
14501450

14511451
if (!from_stdin) {
1452-
printf("%s\n", sha1_to_hex(sha1));
1452+
printf("%s\n", sha1_to_hex(hash));
14531453
} else {
14541454
struct strbuf buf = STRBUF_INIT;
14551455

1456-
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(sha1));
1456+
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(hash));
14571457
write_or_die(1, buf.buf, buf.len);
14581458
strbuf_release(&buf);
14591459

@@ -1637,7 +1637,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16371637
keep_name_buf = STRBUF_INIT;
16381638
struct pack_idx_entry **idx_objects;
16391639
struct pack_idx_option opts;
1640-
unsigned char pack_sha1[20];
1640+
unsigned char pack_hash[GIT_MAX_RAWSZ];
16411641
unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
16421642
int report_end_of_input = 0;
16431643

@@ -1768,11 +1768,11 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
17681768
if (show_stat)
17691769
obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
17701770
ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1771-
parse_pack_objects(pack_sha1);
1771+
parse_pack_objects(pack_hash);
17721772
if (report_end_of_input)
17731773
write_in_full(2, "\0", 1);
17741774
resolve_deltas();
1775-
conclude_pack(fix_thin_pack, curr_pack, pack_sha1);
1775+
conclude_pack(fix_thin_pack, curr_pack, pack_hash);
17761776
free(ofs_deltas);
17771777
free(ref_deltas);
17781778
if (strict)
@@ -1784,14 +1784,14 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
17841784
ALLOC_ARRAY(idx_objects, nr_objects);
17851785
for (i = 0; i < nr_objects; i++)
17861786
idx_objects[i] = &objects[i].idx;
1787-
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
1787+
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
17881788
free(idx_objects);
17891789

17901790
if (!verify)
17911791
final(pack_name, curr_pack,
17921792
index_name, curr_index,
17931793
keep_name, keep_msg,
1794-
pack_sha1);
1794+
pack_hash);
17951795
else
17961796
close(input_fd);
17971797
free(objects);

0 commit comments

Comments
 (0)