Commit b7a46fa8 by Edward Thomson

object: move oid header parsing to object

parent c4360116
......@@ -408,7 +408,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
/* The tree is always the first field */
if (!(flags & GIT_COMMIT_PARSE_QUICK)) {
if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
if (git_object__parse_oid_header(&commit->tree_id,
&buffer, buffer_end, "tree ") < 0)
goto bad_buffer;
} else {
size_t tree_len = strlen("tree ") + GIT_OID_SHA1_HEXSIZE + 1;
......@@ -421,7 +422,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
* TODO: commit grafts!
*/
while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) {
while (git_object__parse_oid_header(&parent_id,
&buffer, buffer_end, "parent ") == 0) {
git_oid *new_id = git_array_alloc(commit->parent_ids);
GIT_ERROR_CHECK_ALLOC(new_id);
......@@ -566,7 +568,7 @@ const char *git_commit_summary(git_commit *commit)
while (*next && git__isspace_nonlf(*next)) {
++next;
}
if (!*next || *next == '\n')
if (!*next || *next == '\n')
break;
}
/* record the beginning of contiguous whitespace runs */
......
......@@ -600,3 +600,31 @@ int git_object_rawcontent_is_valid(
return error;
}
int git_object__parse_oid_header(
git_oid *oid,
const char **buffer_out,
const char *buffer_end,
const char *header)
{
const size_t sha_len = GIT_OID_SHA1_HEXSIZE;
const size_t header_len = strlen(header);
const char *buffer = *buffer_out;
if (buffer + (header_len + sha_len + 1) > buffer_end)
return -1;
if (memcmp(buffer, header, header_len) != 0)
return -1;
if (buffer[header_len + sha_len] != '\n')
return -1;
if (git_oid_fromstr(oid, buffer + header_len) < 0)
return -1;
*buffer_out = buffer + (header_len + sha_len + 1);
return 0;
}
......@@ -45,7 +45,11 @@ int git_object__resolve_to_type(git_object **obj, git_object_t type);
git_object_t git_object_stringn2type(const char *str, size_t len);
int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
int git_object__parse_oid_header(
git_oid *oid,
const char **buffer_out,
const char *buffer_end,
const char *header);
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid);
......
......@@ -143,32 +143,6 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
return out;
}
int git_oid__parse(
git_oid *oid, const char **buffer_out,
const char *buffer_end, const char *header)
{
const size_t sha_len = GIT_OID_SHA1_HEXSIZE;
const size_t header_len = strlen(header);
const char *buffer = *buffer_out;
if (buffer + (header_len + sha_len + 1) > buffer_end)
return -1;
if (memcmp(buffer, header, header_len) != 0)
return -1;
if (buffer[header_len + sha_len] != '\n')
return -1;
if (git_oid_fromstr(oid, buffer + header_len) < 0)
return -1;
*buffer_out = buffer + (header_len + sha_len + 1);
return 0;
}
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid)
{
char hex_oid[GIT_OID_SHA1_HEXSIZE];
......
......@@ -75,7 +75,8 @@ static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end)
unsigned int i;
int error;
if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0)
if (git_object__parse_oid_header(&tag->target,
&buffer, buffer_end, "object ") < 0)
return tag_error("object field invalid");
if (buffer + 5 >= buffer_end)
......
......@@ -56,7 +56,8 @@ void test_commit_parse__header(void)
const char *line = testcase->line;
const char *line_end = line + strlen(line);
cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
cl_git_pass(git_object__parse_oid_header(&oid,
&line, line_end, testcase->header));
cl_assert(line == line_end);
}
......@@ -65,7 +66,8 @@ void test_commit_parse__header(void)
const char *line = testcase->line;
const char *line_end = line + strlen(line);
cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
cl_git_fail(git_object__parse_oid_header(&oid,
&line, line_end, testcase->header));
}
}
......
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