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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 1, 2018 at 02:18 UTC
454253f059746e8c9cc96cf3af525d4827a45aa4
1 file changed
+45
-45
builtin/index-pack.c
+45
-45
@@ -91,7 +91,7 @@ static unsigned int input_offset, input_len;
91
static off_t consumed_bytes;
92
static off_t max_input_size;
93
static unsigned deepest_delta;
94
-static git_SHA_CTX input_ctx;
94
+static git_hash_ctx input_ctx;
95
static uint32_t input_crc32;
96
static int input_fd, output_fd;
97
static const char *curr_pack;
@@ -253,7 +253,7 @@ static void flush(void)
253
if (input_offset) {
254
if (output_fd >= 0)
255
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);
257
memmove(input_buffer, input_buffer + input_offset, input_len);
258
input_offset = 0;
259
}
@@ -326,7 +326,7 @@ static const char *open_pack_file(const char *pack_name)
326
output_fd = -1;
327
nothread_data.pack_fd = input_fd;
328
}
329
- git_SHA1_Init(&input_ctx);
329
+ the_hash_algo->init_fn(&input_ctx);
330
return pack_name;
331
}
332
@@ -437,22 +437,22 @@ static int is_delta_type(enum object_type type)
437
}
438
439
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)
441
{
442
static char fixed_buf[8192];
443
int status;
444
git_zstream stream;
445
void *buf;
446
- git_SHA_CTX c;
446
+ git_hash_ctx c;
447
char hdr[32];
448
int hdrlen;
449
450
if (!is_delta_type(type)) {
451
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);
454
} else
455
- sha1 = NULL;
455
+ oid = NULL;
456
if (type == OBJ_BLOB && size > big_file_threshold)
457
buf = fixed_buf;
458
else
@@ -469,8 +469,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
469
stream.avail_in = input_len;
470
status = git_inflate(&stream, 0);
471
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);
474
if (buf == fixed_buf) {
475
stream.next_out = buf;
476
stream.avail_out = sizeof(fixed_buf);
@@ -479,15 +479,15 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
479
if (stream.total_out != size || status != Z_STREAM_END)
480
bad_object(offset, _("inflate returned %d"), status);
481
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);
484
return buf == fixed_buf ? NULL : buf;
485
}
486
487
static void *unpack_raw_entry(struct object_entry *obj,
488
off_t *ofs_offset,
489
- unsigned char *ref_sha1,
490
- unsigned char *sha1)
489
+ struct object_id *ref_oid,
490
+ struct object_id *oid)
491
{
492
unsigned char *p;
493
unsigned long size, c;
@@ -515,8 +515,8 @@ static void *unpack_raw_entry(struct object_entry *obj,
515
516
switch (obj->type) {
517
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);
520
break;
521
case OBJ_OFS_DELTA:
522
p = fill(1);
@@ -546,7 +546,7 @@ static void *unpack_raw_entry(struct object_entry *obj,
546
}
547
obj->hdr_size = consumed_bytes - obj->idx.offset;
548
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);
550
obj->idx.crc32 = input_crc32;
551
return data;
552
}
@@ -1119,11 +1119,11 @@ static void *threaded_second_pass(void *data)
1119
* - calculate SHA1 of all non-delta objects;
1120
* - remember base (SHA1 or offset) for all deltas.
1121
*/
1122
-static void parse_pack_objects(unsigned char *sha1)
1122
+static void parse_pack_objects(unsigned char *hash)
1123
{
1124
int i, nr_delays = 0;
1125
struct ofs_delta_entry *ofs_delta = ofs_deltas;
1126
- unsigned char ref_delta_sha1[20];
1126
+ struct object_id ref_delta_oid;
1127
struct stat st;
1128
1129
if (verbose)
@@ -1133,8 +1133,8 @@ static void parse_pack_objects(unsigned char *sha1)
1133
for (i = 0; i < nr_objects; i++) {
1134
struct object_entry *obj = &objects[i];
1135
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);
1138
obj->real_type = obj->type;
1139
if (obj->type == OBJ_OFS_DELTA) {
1140
nr_ofs_deltas++;
@@ -1142,7 +1142,7 @@ static void parse_pack_objects(unsigned char *sha1)
1142
ofs_delta++;
1143
} else if (obj->type == OBJ_REF_DELTA) {
1144
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);
1146
ref_deltas[nr_ref_deltas].obj_no = i;
1147
nr_ref_deltas++;
1148
} else if (!data) {
@@ -1160,10 +1160,10 @@ static void parse_pack_objects(unsigned char *sha1)
1160
1161
/* Check pack integrity */
1162
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))
1165
die(_("pack is corrupted (SHA1 mismatch)"));
1166
- use(20);
1166
+ use(the_hash_algo->rawsz);
1167
1168
/* If input_fd is a file, we should have reached its end now. */
1169
if (fstat(input_fd, &st))
@@ -1239,21 +1239,21 @@ static void resolve_deltas(void)
1239
/*
1240
* Third pass:
1241
* - 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
1243
*/
1244
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)
1246
{
1247
if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
1248
stop_progress(&progress);
1249
- /* Flush remaining pack final 20-byte SHA1. */
1249
+ /* Flush remaining pack final hash. */
1250
flush();
1251
return;
1252
}
1253
1254
if (fix_thin_pack) {
1255
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];
1257
struct strbuf msg = STRBUF_INIT;
1258
int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
1259
int nr_objects_initial = nr_objects;
@@ -1270,12 +1270,12 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
1270
nr_objects - nr_objects_initial);
1271
stop_progress_msg(&progress, msg.buf);
1272
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,
1276
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)
1279
die(_("Unexpected tail checksum for %s "
1280
"(disk corruption?)"), curr_pack);
1281
}
@@ -1392,7 +1392,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
1392
static void final(const char *final_pack_name, const char *curr_pack_name,
1393
const char *final_index_name, const char *curr_index_name,
1394
const char *keep_name, const char *keep_msg,
1395
- unsigned char *sha1)
1395
+ unsigned char *hash)
1396
{
1397
const char *report = "pack";
1398
struct strbuf pack_name = STRBUF_INIT;
@@ -1413,7 +1413,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1413
int keep_fd, keep_msg_len = strlen(keep_msg);
1414
1415
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");
1417
1418
keep_fd = odb_pack_keep(keep_name);
1419
if (keep_fd < 0) {
@@ -1434,7 +1434,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1434
1435
if (final_pack_name != curr_pack_name) {
1436
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");
1438
if (finalize_object_file(curr_pack_name, final_pack_name))
1439
die(_("cannot store pack file"));
1440
} else if (from_stdin)
@@ -1442,18 +1442,18 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1442
1443
if (final_index_name != curr_index_name) {
1444
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");
1446
if (finalize_object_file(curr_index_name, final_index_name))
1447
die(_("cannot store index file"));
1448
} else
1449
chmod(final_index_name, 0444);
1450
1451
if (!from_stdin) {
1452
- printf("%s\n", sha1_to_hex(sha1));
1452
+ printf("%s\n", sha1_to_hex(hash));
1453
} else {
1454
struct strbuf buf = STRBUF_INIT;
1455
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));
1457
write_or_die(1, buf.buf, buf.len);
1458
strbuf_release(&buf);
1459
@@ -1637,7 +1637,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1637
keep_name_buf = STRBUF_INIT;
1638
struct pack_idx_entry **idx_objects;
1639
struct pack_idx_option opts;
1640
- unsigned char pack_sha1[20];
1640
+ unsigned char pack_hash[GIT_MAX_RAWSZ];
1641
unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
1642
int report_end_of_input = 0;
1643
@@ -1768,11 +1768,11 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1768
if (show_stat)
1769
obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
1770
ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1771
- parse_pack_objects(pack_sha1);
1771
+ parse_pack_objects(pack_hash);
1772
if (report_end_of_input)
1773
write_in_full(2, "\0", 1);
1774
resolve_deltas();
1775
- conclude_pack(fix_thin_pack, curr_pack, pack_sha1);
1775
+ conclude_pack(fix_thin_pack, curr_pack, pack_hash);
1776
free(ofs_deltas);
1777
free(ref_deltas);
1778
if (strict)
@@ -1784,14 +1784,14 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1784
ALLOC_ARRAY(idx_objects, nr_objects);
1785
for (i = 0; i < nr_objects; i++)
1786
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);
1788
free(idx_objects);
1789
1790
if (!verify)
1791
final(pack_name, curr_pack,
1792
index_name, curr_index,
1793
keep_name, keep_msg,
1794
- pack_sha1);
1794
+ pack_hash);
1795
else
1796
close(input_fd);
1797
free(objects);