Skip to content

Commit 72139ef

Browse files
committed
shallow: don't assume SHA1
1 parent 3853ba8 commit 72139ef

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

src/libgit2/repository.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,13 +3670,14 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro
36703670
{
36713671
git_filebuf file = GIT_FILEBUF_INIT;
36723672
git_str path = GIT_STR_INIT;
3673+
char oid_str[GIT_OID_MAX_HEXSIZE + 1];
36733674
size_t idx;
36743675
git_oid *oid;
36753676
int filebuf_hash, error = 0;
36763677

36773678
GIT_ASSERT_ARG(repo);
36783679

3679-
filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1));
3680+
filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(repo->oid_type));
36803681
GIT_ASSERT(filebuf_hash);
36813682

36823683
if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0)
@@ -3686,7 +3687,8 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro
36863687
goto on_error;
36873688

36883689
git_array_foreach(roots, idx, oid) {
3689-
git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_SHA1_HEXSIZE);
3690+
git_oid_tostr(oid_str, sizeof(oid_str), oid);
3691+
git_filebuf_write(&file, oid_str, git_oid_hexsize(repo->oid_type));
36903692
git_filebuf_write(&file, "\n", 1);
36913693
}
36923694

src/libgit2/transports/smart_pkt.c

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ static int flush_pkt(git_pkt **out)
4444
}
4545

4646
/* the rest of the line will be useful for multi_ack and multi_ack_detailed */
47-
static int ack_pkt(git_pkt **out, const char *line, size_t len)
47+
static int ack_pkt(
48+
git_pkt **out,
49+
const char *line,
50+
size_t len,
51+
git_pkt_parse_data *data)
4852
{
4953
git_pkt_ack *pkt;
54+
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
5055

5156
pkt = git__calloc(1, sizeof(git_pkt_ack));
5257
GIT_ERROR_CHECK_ALLOC(pkt);
@@ -57,11 +62,11 @@ static int ack_pkt(git_pkt **out, const char *line, size_t len)
5762
line += 4;
5863
len -= 4;
5964

60-
if (len < GIT_OID_SHA1_HEXSIZE ||
61-
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1) < 0)
65+
if (len < oid_hexsize ||
66+
git_oid__fromstr(&pkt->oid, line, data->oid_type) < 0)
6267
goto out_err;
63-
line += GIT_OID_SHA1_HEXSIZE;
64-
len -= GIT_OID_SHA1_HEXSIZE;
68+
line += oid_hexsize;
69+
len -= oid_hexsize;
6570

6671
if (len && line[0] == ' ') {
6772
line++;
@@ -436,9 +441,14 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len)
436441
return 0;
437442
}
438443

439-
static int shallow_pkt(git_pkt **out, const char *line, size_t len)
444+
static int shallow_pkt(
445+
git_pkt **out,
446+
const char *line,
447+
size_t len,
448+
git_pkt_parse_data *data)
440449
{
441450
git_pkt_shallow *pkt;
451+
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
442452

443453
pkt = git__calloc(1, sizeof(git_pkt_shallow));
444454
GIT_ERROR_CHECK_ALLOC(pkt);
@@ -451,13 +461,14 @@ static int shallow_pkt(git_pkt **out, const char *line, size_t len)
451461
line += 8;
452462
len -= 8;
453463

454-
if (len >= GIT_OID_SHA1_HEXSIZE) {
455-
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1);
456-
line += GIT_OID_SHA1_HEXSIZE + 1;
457-
len -= GIT_OID_SHA1_HEXSIZE + 1;
458-
}
464+
if (len != oid_hexsize)
465+
goto out_err;
459466

460-
*out = (git_pkt *) pkt;
467+
git_oid__fromstr(&pkt->oid, line, data->oid_type);
468+
line += oid_hexsize + 1;
469+
len -= oid_hexsize + 1;
470+
471+
*out = (git_pkt *)pkt;
461472

462473
return 0;
463474

@@ -467,9 +478,14 @@ static int shallow_pkt(git_pkt **out, const char *line, size_t len)
467478
return -1;
468479
}
469480

470-
static int unshallow_pkt(git_pkt **out, const char *line, size_t len)
481+
static int unshallow_pkt(
482+
git_pkt **out,
483+
const char *line,
484+
size_t len,
485+
git_pkt_parse_data *data)
471486
{
472487
git_pkt_shallow *pkt;
488+
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
473489

474490
pkt = git__calloc(1, sizeof(git_pkt_shallow));
475491
GIT_ERROR_CHECK_ALLOC(pkt);
@@ -482,11 +498,12 @@ static int unshallow_pkt(git_pkt **out, const char *line, size_t len)
482498
line += 10;
483499
len -= 10;
484500

485-
if (len >= GIT_OID_SHA1_HEXSIZE) {
486-
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1);
487-
line += GIT_OID_SHA1_HEXSIZE + 1;
488-
len -= GIT_OID_SHA1_HEXSIZE + 1;
489-
}
501+
if (len != oid_hexsize)
502+
goto out_err;
503+
504+
git_oid__fromstr(&pkt->oid, line, data->oid_type);
505+
line += oid_hexsize + 1;
506+
len -= oid_hexsize + 1;
490507

491508
*out = (git_pkt *) pkt;
492509

@@ -615,7 +632,7 @@ int git_pkt_parse_line(
615632
else if (*line == GIT_SIDE_BAND_ERROR)
616633
error = sideband_error_pkt(pkt, line, len);
617634
else if (!git__prefixncmp(line, len, "ACK"))
618-
error = ack_pkt(pkt, line, len);
635+
error = ack_pkt(pkt, line, len, data);
619636
else if (!git__prefixncmp(line, len, "NAK"))
620637
error = nak_pkt(pkt);
621638
else if (!git__prefixncmp(line, len, "ERR"))
@@ -629,9 +646,9 @@ int git_pkt_parse_line(
629646
else if (!git__prefixncmp(line, len, "unpack"))
630647
error = unpack_pkt(pkt, line, len);
631648
else if (!git__prefixcmp(line, "shallow"))
632-
error = shallow_pkt(pkt, line, len);
649+
error = shallow_pkt(pkt, line, len, data);
633650
else if (!git__prefixcmp(line, "unshallow"))
634-
error = unshallow_pkt(pkt, line, len);
651+
error = unshallow_pkt(pkt, line, len, data);
635652
else
636653
error = ref_pkt(pkt, line, len, data);
637654

@@ -788,12 +805,13 @@ int git_pkt_buffer_wants(
788805

789806
/* Tell the server about our shallow objects */
790807
for (i = 0; i < git_shallowarray_count(wants->shallow_roots); i++) {
791-
char oid[GIT_OID_SHA1_HEXSIZE];
808+
char oid[GIT_OID_MAX_HEXSIZE + 1];
792809
git_str shallow_buf = GIT_STR_INIT;
793810

794-
git_oid_fmt(oid, git_shallowarray_get(wants->shallow_roots, i));
811+
git_oid_tostr(oid, GIT_OID_MAX_HEXSIZE + 1,
812+
git_shallowarray_get(wants->shallow_roots, i));
795813
git_str_puts(&shallow_buf, "shallow ");
796-
git_str_put(&shallow_buf, oid, GIT_OID_SHA1_HEXSIZE);
814+
git_str_puts(&shallow_buf, oid);
797815
git_str_putc(&shallow_buf, '\n');
798816

799817
git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf));

0 commit comments

Comments
 (0)