Commit 72139ef2 by Edward Thomson

shallow: don't assume SHA1

parent 3853ba8d
...@@ -3670,13 +3670,14 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro ...@@ -3670,13 +3670,14 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro
{ {
git_filebuf file = GIT_FILEBUF_INIT; git_filebuf file = GIT_FILEBUF_INIT;
git_str path = GIT_STR_INIT; git_str path = GIT_STR_INIT;
char oid_str[GIT_OID_MAX_HEXSIZE + 1];
size_t idx; size_t idx;
git_oid *oid; git_oid *oid;
int filebuf_hash, error = 0; int filebuf_hash, error = 0;
GIT_ASSERT_ARG(repo); GIT_ASSERT_ARG(repo);
filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1)); filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(repo->oid_type));
GIT_ASSERT(filebuf_hash); GIT_ASSERT(filebuf_hash);
if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) 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 ...@@ -3686,7 +3687,8 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro
goto on_error; goto on_error;
git_array_foreach(roots, idx, oid) { git_array_foreach(roots, idx, oid) {
git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_SHA1_HEXSIZE); git_oid_tostr(oid_str, sizeof(oid_str), oid);
git_filebuf_write(&file, oid_str, git_oid_hexsize(repo->oid_type));
git_filebuf_write(&file, "\n", 1); git_filebuf_write(&file, "\n", 1);
} }
......
...@@ -44,9 +44,14 @@ static int flush_pkt(git_pkt **out) ...@@ -44,9 +44,14 @@ static int flush_pkt(git_pkt **out)
} }
/* the rest of the line will be useful for multi_ack and multi_ack_detailed */ /* the rest of the line will be useful for multi_ack and multi_ack_detailed */
static int ack_pkt(git_pkt **out, const char *line, size_t len) static int ack_pkt(
git_pkt **out,
const char *line,
size_t len,
git_pkt_parse_data *data)
{ {
git_pkt_ack *pkt; git_pkt_ack *pkt;
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
pkt = git__calloc(1, sizeof(git_pkt_ack)); pkt = git__calloc(1, sizeof(git_pkt_ack));
GIT_ERROR_CHECK_ALLOC(pkt); GIT_ERROR_CHECK_ALLOC(pkt);
...@@ -57,11 +62,11 @@ static int ack_pkt(git_pkt **out, const char *line, size_t len) ...@@ -57,11 +62,11 @@ static int ack_pkt(git_pkt **out, const char *line, size_t len)
line += 4; line += 4;
len -= 4; len -= 4;
if (len < GIT_OID_SHA1_HEXSIZE || if (len < oid_hexsize ||
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1) < 0) git_oid__fromstr(&pkt->oid, line, data->oid_type) < 0)
goto out_err; goto out_err;
line += GIT_OID_SHA1_HEXSIZE; line += oid_hexsize;
len -= GIT_OID_SHA1_HEXSIZE; len -= oid_hexsize;
if (len && line[0] == ' ') { if (len && line[0] == ' ') {
line++; line++;
...@@ -436,9 +441,14 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len) ...@@ -436,9 +441,14 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len)
return 0; return 0;
} }
static int shallow_pkt(git_pkt **out, const char *line, size_t len) static int shallow_pkt(
git_pkt **out,
const char *line,
size_t len,
git_pkt_parse_data *data)
{ {
git_pkt_shallow *pkt; git_pkt_shallow *pkt;
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
pkt = git__calloc(1, sizeof(git_pkt_shallow)); pkt = git__calloc(1, sizeof(git_pkt_shallow));
GIT_ERROR_CHECK_ALLOC(pkt); GIT_ERROR_CHECK_ALLOC(pkt);
...@@ -451,13 +461,14 @@ static int shallow_pkt(git_pkt **out, const char *line, size_t len) ...@@ -451,13 +461,14 @@ static int shallow_pkt(git_pkt **out, const char *line, size_t len)
line += 8; line += 8;
len -= 8; len -= 8;
if (len >= GIT_OID_SHA1_HEXSIZE) { if (len != oid_hexsize)
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1); goto out_err;
line += GIT_OID_SHA1_HEXSIZE + 1;
len -= GIT_OID_SHA1_HEXSIZE + 1;
}
*out = (git_pkt *) pkt; git_oid__fromstr(&pkt->oid, line, data->oid_type);
line += oid_hexsize + 1;
len -= oid_hexsize + 1;
*out = (git_pkt *)pkt;
return 0; return 0;
...@@ -467,9 +478,14 @@ out_err: ...@@ -467,9 +478,14 @@ out_err:
return -1; return -1;
} }
static int unshallow_pkt(git_pkt **out, const char *line, size_t len) static int unshallow_pkt(
git_pkt **out,
const char *line,
size_t len,
git_pkt_parse_data *data)
{ {
git_pkt_shallow *pkt; git_pkt_shallow *pkt;
size_t oid_hexsize = git_oid_hexsize(data->oid_type);
pkt = git__calloc(1, sizeof(git_pkt_shallow)); pkt = git__calloc(1, sizeof(git_pkt_shallow));
GIT_ERROR_CHECK_ALLOC(pkt); GIT_ERROR_CHECK_ALLOC(pkt);
...@@ -482,11 +498,12 @@ static int unshallow_pkt(git_pkt **out, const char *line, size_t len) ...@@ -482,11 +498,12 @@ static int unshallow_pkt(git_pkt **out, const char *line, size_t len)
line += 10; line += 10;
len -= 10; len -= 10;
if (len >= GIT_OID_SHA1_HEXSIZE) { if (len != oid_hexsize)
git_oid__fromstr(&pkt->oid, line, GIT_OID_SHA1); goto out_err;
line += GIT_OID_SHA1_HEXSIZE + 1;
len -= GIT_OID_SHA1_HEXSIZE + 1; git_oid__fromstr(&pkt->oid, line, data->oid_type);
} line += oid_hexsize + 1;
len -= oid_hexsize + 1;
*out = (git_pkt *) pkt; *out = (git_pkt *) pkt;
...@@ -615,7 +632,7 @@ int git_pkt_parse_line( ...@@ -615,7 +632,7 @@ int git_pkt_parse_line(
else if (*line == GIT_SIDE_BAND_ERROR) else if (*line == GIT_SIDE_BAND_ERROR)
error = sideband_error_pkt(pkt, line, len); error = sideband_error_pkt(pkt, line, len);
else if (!git__prefixncmp(line, len, "ACK")) else if (!git__prefixncmp(line, len, "ACK"))
error = ack_pkt(pkt, line, len); error = ack_pkt(pkt, line, len, data);
else if (!git__prefixncmp(line, len, "NAK")) else if (!git__prefixncmp(line, len, "NAK"))
error = nak_pkt(pkt); error = nak_pkt(pkt);
else if (!git__prefixncmp(line, len, "ERR")) else if (!git__prefixncmp(line, len, "ERR"))
...@@ -629,9 +646,9 @@ int git_pkt_parse_line( ...@@ -629,9 +646,9 @@ int git_pkt_parse_line(
else if (!git__prefixncmp(line, len, "unpack")) else if (!git__prefixncmp(line, len, "unpack"))
error = unpack_pkt(pkt, line, len); error = unpack_pkt(pkt, line, len);
else if (!git__prefixcmp(line, "shallow")) else if (!git__prefixcmp(line, "shallow"))
error = shallow_pkt(pkt, line, len); error = shallow_pkt(pkt, line, len, data);
else if (!git__prefixcmp(line, "unshallow")) else if (!git__prefixcmp(line, "unshallow"))
error = unshallow_pkt(pkt, line, len); error = unshallow_pkt(pkt, line, len, data);
else else
error = ref_pkt(pkt, line, len, data); error = ref_pkt(pkt, line, len, data);
...@@ -788,12 +805,13 @@ int git_pkt_buffer_wants( ...@@ -788,12 +805,13 @@ int git_pkt_buffer_wants(
/* Tell the server about our shallow objects */ /* Tell the server about our shallow objects */
for (i = 0; i < git_shallowarray_count(wants->shallow_roots); i++) { for (i = 0; i < git_shallowarray_count(wants->shallow_roots); i++) {
char oid[GIT_OID_SHA1_HEXSIZE]; char oid[GIT_OID_MAX_HEXSIZE + 1];
git_str shallow_buf = GIT_STR_INIT; git_str shallow_buf = GIT_STR_INIT;
git_oid_fmt(oid, git_shallowarray_get(wants->shallow_roots, i)); git_oid_tostr(oid, GIT_OID_MAX_HEXSIZE + 1,
git_shallowarray_get(wants->shallow_roots, i));
git_str_puts(&shallow_buf, "shallow "); git_str_puts(&shallow_buf, "shallow ");
git_str_put(&shallow_buf, oid, GIT_OID_SHA1_HEXSIZE); git_str_puts(&shallow_buf, oid);
git_str_putc(&shallow_buf, '\n'); git_str_putc(&shallow_buf, '\n');
git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf)); git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment