Skip to content

Commit b7a46fa

Browse files
committed
object: move oid header parsing to object
1 parent c436011 commit b7a46fa

File tree

6 files changed

+44
-33
lines changed

6 files changed

+44
-33
lines changed

src/libgit2/commit.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
408408

409409
/* The tree is always the first field */
410410
if (!(flags & GIT_COMMIT_PARSE_QUICK)) {
411-
if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
411+
if (git_object__parse_oid_header(&commit->tree_id,
412+
&buffer, buffer_end, "tree ") < 0)
412413
goto bad_buffer;
413414
} else {
414415
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
421422
* TODO: commit grafts!
422423
*/
423424

424-
while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) {
425+
while (git_object__parse_oid_header(&parent_id,
426+
&buffer, buffer_end, "parent ") == 0) {
425427
git_oid *new_id = git_array_alloc(commit->parent_ids);
426428
GIT_ERROR_CHECK_ALLOC(new_id);
427429

@@ -566,7 +568,7 @@ const char *git_commit_summary(git_commit *commit)
566568
while (*next && git__isspace_nonlf(*next)) {
567569
++next;
568570
}
569-
if (!*next || *next == '\n')
571+
if (!*next || *next == '\n')
570572
break;
571573
}
572574
/* record the beginning of contiguous whitespace runs */

src/libgit2/object.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,31 @@ int git_object_rawcontent_is_valid(
600600

601601
return error;
602602
}
603+
604+
int git_object__parse_oid_header(
605+
git_oid *oid,
606+
const char **buffer_out,
607+
const char *buffer_end,
608+
const char *header)
609+
{
610+
const size_t sha_len = GIT_OID_SHA1_HEXSIZE;
611+
const size_t header_len = strlen(header);
612+
613+
const char *buffer = *buffer_out;
614+
615+
if (buffer + (header_len + sha_len + 1) > buffer_end)
616+
return -1;
617+
618+
if (memcmp(buffer, header, header_len) != 0)
619+
return -1;
620+
621+
if (buffer[header_len + sha_len] != '\n')
622+
return -1;
623+
624+
if (git_oid_fromstr(oid, buffer + header_len) < 0)
625+
return -1;
626+
627+
*buffer_out = buffer + (header_len + sha_len + 1);
628+
629+
return 0;
630+
}

src/libgit2/object.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ int git_object__resolve_to_type(git_object **obj, git_object_t type);
4545

4646
git_object_t git_object_stringn2type(const char *str, size_t len);
4747

48-
int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
48+
int git_object__parse_oid_header(
49+
git_oid *oid,
50+
const char **buffer_out,
51+
const char *buffer_end,
52+
const char *header);
4953

5054
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid);
5155

src/libgit2/oid.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,6 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
143143
return out;
144144
}
145145

146-
int git_oid__parse(
147-
git_oid *oid, const char **buffer_out,
148-
const char *buffer_end, const char *header)
149-
{
150-
const size_t sha_len = GIT_OID_SHA1_HEXSIZE;
151-
const size_t header_len = strlen(header);
152-
153-
const char *buffer = *buffer_out;
154-
155-
if (buffer + (header_len + sha_len + 1) > buffer_end)
156-
return -1;
157-
158-
if (memcmp(buffer, header, header_len) != 0)
159-
return -1;
160-
161-
if (buffer[header_len + sha_len] != '\n')
162-
return -1;
163-
164-
if (git_oid_fromstr(oid, buffer + header_len) < 0)
165-
return -1;
166-
167-
*buffer_out = buffer + (header_len + sha_len + 1);
168-
169-
return 0;
170-
}
171-
172146
void git_oid__writebuf(git_str *buf, const char *header, const git_oid *oid)
173147
{
174148
char hex_oid[GIT_OID_SHA1_HEXSIZE];

src/libgit2/tag.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end)
7575
unsigned int i;
7676
int error;
7777

78-
if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0)
78+
if (git_object__parse_oid_header(&tag->target,
79+
&buffer, buffer_end, "object ") < 0)
7980
return tag_error("object field invalid");
8081

8182
if (buffer + 5 >= buffer_end)

tests/libgit2/commit/parse.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void test_commit_parse__header(void)
5656
const char *line = testcase->line;
5757
const char *line_end = line + strlen(line);
5858

59-
cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
59+
cl_git_pass(git_object__parse_oid_header(&oid,
60+
&line, line_end, testcase->header));
6061
cl_assert(line == line_end);
6162
}
6263

@@ -65,7 +66,8 @@ void test_commit_parse__header(void)
6566
const char *line = testcase->line;
6667
const char *line_end = line + strlen(line);
6768

68-
cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
69+
cl_git_fail(git_object__parse_oid_header(&oid,
70+
&line, line_end, testcase->header));
6971
}
7072
}
7173

0 commit comments

Comments
 (0)