pack-objects: abstract away hash algorithm

Instead of using hard-coded instances of the constant 20, use the_hash_algo to look up the correct constant. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 2, 2018 at 00:25 UTC 411791009bac88114fa7c53667ea2eff75bf09f7
1 file changed +16 -14
builtin/pack-objects.c
+16 -14
@@ -264,6 +264,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
264 enum object_type type;
265 void *buf;
266 struct git_istream *st = NULL;
267 + const unsigned hashsz = the_hash_algo->rawsz;
268
269 if (!usable_delta) {
270 if (entry->type == OBJ_BLOB &&
@@ -320,7 +321,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
321 dheader[pos] = ofs & 127;
322 while (ofs >>= 7)
323 dheader[--pos] = 128 | (--ofs & 127);
323 - if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
324 + if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
325 if (st)
326 close_istream(st);
327 free(buf);
@@ -332,19 +333,19 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
333 } else if (type == OBJ_REF_DELTA) {
334 /*
335 * Deltas with a base reference contain
335 - * an additional 20 bytes for the base sha1.
336 + * additional bytes for the base object ID.
337 */
337 - if (limit && hdrlen + 20 + datalen + 20 >= limit) {
338 + if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
339 if (st)
340 close_istream(st);
341 free(buf);
342 return 0;
343 }
344 hashwrite(f, header, hdrlen);
344 - hashwrite(f, entry->delta->idx.oid.hash, 20);
345 - hdrlen += 20;
345 + hashwrite(f, entry->delta->idx.oid.hash, hashsz);
346 + hdrlen += hashsz;
347 } else {
347 - if (limit && hdrlen + datalen + 20 >= limit) {
348 + if (limit && hdrlen + datalen + hashsz >= limit) {
349 if (st)
350 close_istream(st);
351 free(buf);
@@ -376,6 +377,7 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
377 unsigned char header[MAX_PACK_OBJECT_HEADER],
378 dheader[MAX_PACK_OBJECT_HEADER];
379 unsigned hdrlen;
380 + const unsigned hashsz = the_hash_algo->rawsz;
381
382 if (entry->delta)
383 type = (allow_ofs_delta && entry->delta->idx.offset) ?
@@ -411,7 +413,7 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
413 dheader[pos] = ofs & 127;
414 while (ofs >>= 7)
415 dheader[--pos] = 128 | (--ofs & 127);
414 - if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
416 + if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
417 unuse_pack(&w_curs);
418 return 0;
419 }
@@ -420,16 +422,16 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
422 hdrlen += sizeof(dheader) - pos;
423 reused_delta++;
424 } else if (type == OBJ_REF_DELTA) {
423 - if (limit && hdrlen + 20 + datalen + 20 >= limit) {
425 + if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
426 unuse_pack(&w_curs);
427 return 0;
428 }
429 hashwrite(f, header, hdrlen);
428 - hashwrite(f, entry->delta->idx.oid.hash, 20);
429 - hdrlen += 20;
430 + hashwrite(f, entry->delta->idx.oid.hash, hashsz);
431 + hdrlen += hashsz;
432 reused_delta++;
433 } else {
432 - if (limit && hdrlen + datalen + 20 >= limit) {
434 + if (limit && hdrlen + datalen + hashsz >= limit) {
435 unuse_pack(&w_curs);
436 return 0;
437 }
@@ -752,7 +754,7 @@ static off_t write_reused_pack(struct hashfile *f)
754 die_errno("unable to seek in reused packfile");
755
756 if (reuse_packfile_offset < 0)
755 - reuse_packfile_offset = reuse_packfile->pack_size - 20;
757 + reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz;
758
759 total = to_write = reuse_packfile_offset - sizeof(struct pack_header);
760
@@ -1438,7 +1440,7 @@ static void check_object(struct object_entry *entry)
1440 if (reuse_delta && !entry->preferred_base)
1441 base_ref = use_pack(p, &w_curs,
1442 entry->in_pack_offset + used, NULL);
1441 - entry->in_pack_header_size = used + 20;
1443 + entry->in_pack_header_size = used + the_hash_algo->rawsz;
1444 break;
1445 case OBJ_OFS_DELTA:
1446 buf = use_pack(p, &w_curs,
@@ -1850,7 +1852,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
1852 /* Now some size filtering heuristics. */
1853 trg_size = trg_entry->size;
1854 if (!trg_entry->delta) {
1853 - max_size = trg_size/2 - 20;
1855 + max_size = trg_size/2 - the_hash_algo->rawsz;
1856 ref_depth = 1;
1857 } else {
1858 max_size = trg_entry->delta_size;