Skip to content

Commit 0fd90da

Browse files
committed
Merge branch 'bc/hash-algo'
More abstraction of hash function from the codepath. * bc/hash-algo: hash: update obsolete reference to SHA1_HEADER bulk-checkin: abstract SHA-1 usage csum-file: abstract uses of SHA-1 csum-file: rename sha1file to hashfile read-cache: abstract away uses of SHA-1 pack-write: switch various SHA-1 values to abstract forms pack-check: convert various uses of SHA-1 to abstract forms fast-import: switch various uses of SHA-1 to the_hash_algo sha1_file: switch uses of SHA-1 to the_hash_algo builtin/unpack-objects: switch uses of SHA-1 to the_hash_algo builtin/index-pack: improve hash function abstraction hash: create union for hash context allocation hash: move SHA-1 macros to hash.h
2 parents 0dbd562 + b212c0c commit 0fd90da

15 files changed

+348
-344
lines changed

builtin/index-pack.c

Lines changed: 57 additions & 57 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
}
@@ -1118,11 +1118,11 @@ static void *threaded_second_pass(void *data)
11181118
* - calculate SHA1 of all non-delta objects;
11191119
* - remember base (SHA1 or offset) for all deltas.
11201120
*/
1121-
static void parse_pack_objects(unsigned char *sha1)
1121+
static void parse_pack_objects(unsigned char *hash)
11221122
{
11231123
int i, nr_delays = 0;
11241124
struct ofs_delta_entry *ofs_delta = ofs_deltas;
1125-
unsigned char ref_delta_sha1[20];
1125+
struct object_id ref_delta_oid;
11261126
struct stat st;
11271127

11281128
if (verbose)
@@ -1132,16 +1132,16 @@ static void parse_pack_objects(unsigned char *sha1)
11321132
for (i = 0; i < nr_objects; i++) {
11331133
struct object_entry *obj = &objects[i];
11341134
void *data = unpack_raw_entry(obj, &ofs_delta->offset,
1135-
ref_delta_sha1,
1136-
obj->idx.oid.hash);
1135+
&ref_delta_oid,
1136+
&obj->idx.oid);
11371137
obj->real_type = obj->type;
11381138
if (obj->type == OBJ_OFS_DELTA) {
11391139
nr_ofs_deltas++;
11401140
ofs_delta->obj_no = i;
11411141
ofs_delta++;
11421142
} else if (obj->type == OBJ_REF_DELTA) {
11431143
ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
1144-
hashcpy(ref_deltas[nr_ref_deltas].sha1, ref_delta_sha1);
1144+
hashcpy(ref_deltas[nr_ref_deltas].sha1, ref_delta_oid.hash);
11451145
ref_deltas[nr_ref_deltas].obj_no = i;
11461146
nr_ref_deltas++;
11471147
} else if (!data) {
@@ -1159,10 +1159,10 @@ static void parse_pack_objects(unsigned char *sha1)
11591159

11601160
/* Check pack integrity */
11611161
flush();
1162-
git_SHA1_Final(sha1, &input_ctx);
1163-
if (hashcmp(fill(20), sha1))
1162+
the_hash_algo->final_fn(hash, &input_ctx);
1163+
if (hashcmp(fill(the_hash_algo->rawsz), hash))
11641164
die(_("pack is corrupted (SHA1 mismatch)"));
1165-
use(20);
1165+
use(the_hash_algo->rawsz);
11661166

11671167
/* If input_fd is a file, we should have reached its end now. */
11681168
if (fstat(input_fd, &st))
@@ -1238,21 +1238,21 @@ static void resolve_deltas(void)
12381238
/*
12391239
* Third pass:
12401240
* - append objects to convert thin pack to full pack if required
1241-
* - write the final 20-byte SHA-1
1241+
* - write the final pack hash
12421242
*/
1243-
static void fix_unresolved_deltas(struct sha1file *f);
1244-
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_sha1)
1243+
static void fix_unresolved_deltas(struct hashfile *f);
1244+
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
12451245
{
12461246
if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
12471247
stop_progress(&progress);
1248-
/* Flush remaining pack final 20-byte SHA1. */
1248+
/* Flush remaining pack final hash. */
12491249
flush();
12501250
return;
12511251
}
12521252

12531253
if (fix_thin_pack) {
1254-
struct sha1file *f;
1255-
unsigned char read_sha1[20], tail_sha1[20];
1254+
struct hashfile *f;
1255+
unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
12561256
struct strbuf msg = STRBUF_INIT;
12571257
int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
12581258
int nr_objects_initial = nr_objects;
@@ -1261,20 +1261,20 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
12611261
REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
12621262
memset(objects + nr_objects + 1, 0,
12631263
nr_unresolved * sizeof(*objects));
1264-
f = sha1fd(output_fd, curr_pack);
1264+
f = hashfd(output_fd, curr_pack);
12651265
fix_unresolved_deltas(f);
12661266
strbuf_addf(&msg, Q_("completed with %d local object",
12671267
"completed with %d local objects",
12681268
nr_objects - nr_objects_initial),
12691269
nr_objects - nr_objects_initial);
12701270
stop_progress_msg(&progress, msg.buf);
12711271
strbuf_release(&msg);
1272-
sha1close(f, tail_sha1, 0);
1273-
hashcpy(read_sha1, pack_sha1);
1274-
fixup_pack_header_footer(output_fd, pack_sha1,
1272+
hashclose(f, tail_hash, 0);
1273+
hashcpy(read_hash, pack_hash);
1274+
fixup_pack_header_footer(output_fd, pack_hash,
12751275
curr_pack, nr_objects,
1276-
read_sha1, consumed_bytes-20);
1277-
if (hashcmp(read_sha1, tail_sha1) != 0)
1276+
read_hash, consumed_bytes-the_hash_algo->rawsz);
1277+
if (hashcmp(read_hash, tail_hash) != 0)
12781278
die(_("Unexpected tail checksum for %s "
12791279
"(disk corruption?)"), curr_pack);
12801280
}
@@ -1285,7 +1285,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
12851285
nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
12861286
}
12871287

1288-
static int write_compressed(struct sha1file *f, void *in, unsigned int size)
1288+
static int write_compressed(struct hashfile *f, void *in, unsigned int size)
12891289
{
12901290
git_zstream stream;
12911291
int status;
@@ -1299,7 +1299,7 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)
12991299
stream.next_out = outbuf;
13001300
stream.avail_out = sizeof(outbuf);
13011301
status = git_deflate(&stream, Z_FINISH);
1302-
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
1302+
hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
13031303
} while (status == Z_OK);
13041304

13051305
if (status != Z_STREAM_END)
@@ -1309,7 +1309,7 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)
13091309
return size;
13101310
}
13111311

1312-
static struct object_entry *append_obj_to_pack(struct sha1file *f,
1312+
static struct object_entry *append_obj_to_pack(struct hashfile *f,
13131313
const unsigned char *sha1, void *buf,
13141314
unsigned long size, enum object_type type)
13151315
{
@@ -1326,15 +1326,15 @@ static struct object_entry *append_obj_to_pack(struct sha1file *f,
13261326
}
13271327
header[n++] = c;
13281328
crc32_begin(f);
1329-
sha1write(f, header, n);
1329+
hashwrite(f, header, n);
13301330
obj[0].size = size;
13311331
obj[0].hdr_size = n;
13321332
obj[0].type = type;
13331333
obj[0].real_type = type;
13341334
obj[1].idx.offset = obj[0].idx.offset + n;
13351335
obj[1].idx.offset += write_compressed(f, buf, size);
13361336
obj[0].idx.crc32 = crc32_end(f);
1337-
sha1flush(f);
1337+
hashflush(f);
13381338
hashcpy(obj->idx.oid.hash, sha1);
13391339
return obj;
13401340
}
@@ -1346,7 +1346,7 @@ static int delta_pos_compare(const void *_a, const void *_b)
13461346
return a->obj_no - b->obj_no;
13471347
}
13481348

1349-
static void fix_unresolved_deltas(struct sha1file *f)
1349+
static void fix_unresolved_deltas(struct hashfile *f)
13501350
{
13511351
struct ref_delta_entry **sorted_by_pos;
13521352
int i;
@@ -1402,7 +1402,7 @@ static const char *derive_filename(const char *pack_name, const char *suffix,
14021402
}
14031403

14041404
static void write_special_file(const char *suffix, const char *msg,
1405-
const char *pack_name, const unsigned char *sha1,
1405+
const char *pack_name, const unsigned char *hash,
14061406
const char **report)
14071407
{
14081408
struct strbuf name_buf = STRBUF_INIT;
@@ -1413,7 +1413,7 @@ static void write_special_file(const char *suffix, const char *msg,
14131413
if (pack_name)
14141414
filename = derive_filename(pack_name, suffix, &name_buf);
14151415
else
1416-
filename = odb_pack_name(&name_buf, sha1, suffix);
1416+
filename = odb_pack_name(&name_buf, hash, suffix);
14171417

14181418
fd = odb_pack_keep(filename);
14191419
if (fd < 0) {
@@ -1437,7 +1437,7 @@ static void write_special_file(const char *suffix, const char *msg,
14371437
static void final(const char *final_pack_name, const char *curr_pack_name,
14381438
const char *final_index_name, const char *curr_index_name,
14391439
const char *keep_msg, const char *promisor_msg,
1440-
unsigned char *sha1)
1440+
unsigned char *hash)
14411441
{
14421442
const char *report = "pack";
14431443
struct strbuf pack_name = STRBUF_INIT;
@@ -1454,34 +1454,34 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14541454
}
14551455

14561456
if (keep_msg)
1457-
write_special_file("keep", keep_msg, final_pack_name, sha1,
1457+
write_special_file("keep", keep_msg, final_pack_name, hash,
14581458
&report);
14591459
if (promisor_msg)
14601460
write_special_file("promisor", promisor_msg, final_pack_name,
1461-
sha1, NULL);
1461+
hash, NULL);
14621462

14631463
if (final_pack_name != curr_pack_name) {
14641464
if (!final_pack_name)
1465-
final_pack_name = odb_pack_name(&pack_name, sha1, "pack");
1465+
final_pack_name = odb_pack_name(&pack_name, hash, "pack");
14661466
if (finalize_object_file(curr_pack_name, final_pack_name))
14671467
die(_("cannot store pack file"));
14681468
} else if (from_stdin)
14691469
chmod(final_pack_name, 0444);
14701470

14711471
if (final_index_name != curr_index_name) {
14721472
if (!final_index_name)
1473-
final_index_name = odb_pack_name(&index_name, sha1, "idx");
1473+
final_index_name = odb_pack_name(&index_name, hash, "idx");
14741474
if (finalize_object_file(curr_index_name, final_index_name))
14751475
die(_("cannot store index file"));
14761476
} else
14771477
chmod(final_index_name, 0444);
14781478

14791479
if (!from_stdin) {
1480-
printf("%s\n", sha1_to_hex(sha1));
1480+
printf("%s\n", sha1_to_hex(hash));
14811481
} else {
14821482
struct strbuf buf = STRBUF_INIT;
14831483

1484-
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(sha1));
1484+
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(hash));
14851485
write_or_die(1, buf.buf, buf.len);
14861486
strbuf_release(&buf);
14871487

@@ -1652,7 +1652,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16521652
struct strbuf index_name_buf = STRBUF_INIT;
16531653
struct pack_idx_entry **idx_objects;
16541654
struct pack_idx_option opts;
1655-
unsigned char pack_sha1[20];
1655+
unsigned char pack_hash[GIT_MAX_RAWSZ];
16561656
unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
16571657
int report_end_of_input = 0;
16581658

@@ -1789,11 +1789,11 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
17891789
if (show_stat)
17901790
obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
17911791
ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1792-
parse_pack_objects(pack_sha1);
1792+
parse_pack_objects(pack_hash);
17931793
if (report_end_of_input)
17941794
write_in_full(2, "\0", 1);
17951795
resolve_deltas();
1796-
conclude_pack(fix_thin_pack, curr_pack, pack_sha1);
1796+
conclude_pack(fix_thin_pack, curr_pack, pack_hash);
17971797
free(ofs_deltas);
17981798
free(ref_deltas);
17991799
if (strict)
@@ -1805,14 +1805,14 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
18051805
ALLOC_ARRAY(idx_objects, nr_objects);
18061806
for (i = 0; i < nr_objects; i++)
18071807
idx_objects[i] = &objects[i].idx;
1808-
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
1808+
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
18091809
free(idx_objects);
18101810

18111811
if (!verify)
18121812
final(pack_name, curr_pack,
18131813
index_name, curr_index,
18141814
keep_msg, promisor_msg,
1815-
pack_sha1);
1815+
pack_hash);
18161816
else
18171817
close(input_fd);
18181818
free(objects);

0 commit comments

Comments
 (0)