Skip to content

Commit 4117910

Browse files
bk2204gitster
authored andcommitted
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>
1 parent 37fec86 commit 4117910

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

builtin/pack-objects.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
264264
enum object_type type;
265265
void *buf;
266266
struct git_istream *st = NULL;
267+
const unsigned hashsz = the_hash_algo->rawsz;
267268

268269
if (!usable_delta) {
269270
if (entry->type == OBJ_BLOB &&
@@ -320,7 +321,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
320321
dheader[pos] = ofs & 127;
321322
while (ofs >>= 7)
322323
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) {
324325
if (st)
325326
close_istream(st);
326327
free(buf);
@@ -332,19 +333,19 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
332333
} else if (type == OBJ_REF_DELTA) {
333334
/*
334335
* Deltas with a base reference contain
335-
* an additional 20 bytes for the base sha1.
336+
* additional bytes for the base object ID.
336337
*/
337-
if (limit && hdrlen + 20 + datalen + 20 >= limit) {
338+
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
338339
if (st)
339340
close_istream(st);
340341
free(buf);
341342
return 0;
342343
}
343344
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;
346347
} else {
347-
if (limit && hdrlen + datalen + 20 >= limit) {
348+
if (limit && hdrlen + datalen + hashsz >= limit) {
348349
if (st)
349350
close_istream(st);
350351
free(buf);
@@ -376,6 +377,7 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
376377
unsigned char header[MAX_PACK_OBJECT_HEADER],
377378
dheader[MAX_PACK_OBJECT_HEADER];
378379
unsigned hdrlen;
380+
const unsigned hashsz = the_hash_algo->rawsz;
379381

380382
if (entry->delta)
381383
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,
411413
dheader[pos] = ofs & 127;
412414
while (ofs >>= 7)
413415
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) {
415417
unuse_pack(&w_curs);
416418
return 0;
417419
}
@@ -420,16 +422,16 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
420422
hdrlen += sizeof(dheader) - pos;
421423
reused_delta++;
422424
} else if (type == OBJ_REF_DELTA) {
423-
if (limit && hdrlen + 20 + datalen + 20 >= limit) {
425+
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
424426
unuse_pack(&w_curs);
425427
return 0;
426428
}
427429
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;
430432
reused_delta++;
431433
} else {
432-
if (limit && hdrlen + datalen + 20 >= limit) {
434+
if (limit && hdrlen + datalen + hashsz >= limit) {
433435
unuse_pack(&w_curs);
434436
return 0;
435437
}
@@ -752,7 +754,7 @@ static off_t write_reused_pack(struct hashfile *f)
752754
die_errno("unable to seek in reused packfile");
753755

754756
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;
756758

757759
total = to_write = reuse_packfile_offset - sizeof(struct pack_header);
758760

@@ -1438,7 +1440,7 @@ static void check_object(struct object_entry *entry)
14381440
if (reuse_delta && !entry->preferred_base)
14391441
base_ref = use_pack(p, &w_curs,
14401442
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;
14421444
break;
14431445
case OBJ_OFS_DELTA:
14441446
buf = use_pack(p, &w_curs,
@@ -1850,7 +1852,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
18501852
/* Now some size filtering heuristics. */
18511853
trg_size = trg_entry->size;
18521854
if (!trg_entry->delta) {
1853-
max_size = trg_size/2 - 20;
1855+
max_size = trg_size/2 - the_hash_algo->rawsz;
18541856
ref_depth = 1;
18551857
} else {
18561858
max_size = trg_entry->delta_size;

0 commit comments

Comments
 (0)