object: move oid header parsing to object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c
index 9516e92..d5606c0 100644
--- a/src/libgit2/commit.c
+++ b/src/libgit2/commit.c
@@ -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 */
diff --git a/src/libgit2/object.c b/src/libgit2/object.c
index d43f3f0..ecdca77 100644
--- a/src/libgit2/object.c
+++ b/src/libgit2/object.c
@@ -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;
+}
diff --git a/src/libgit2/object.h b/src/libgit2/object.h
index 66be575..1b24219 100644
--- a/src/libgit2/object.h
+++ b/src/libgit2/object.h
@@ -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);
diff --git a/src/libgit2/oid.c b/src/libgit2/oid.c
index a66047d..e038cc0 100644
--- a/src/libgit2/oid.c
+++ b/src/libgit2/oid.c
@@ -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];
diff --git a/src/libgit2/tag.c b/src/libgit2/tag.c
index 5734106..1ff9f97 100644
--- a/src/libgit2/tag.c
+++ b/src/libgit2/tag.c
@@ -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)
diff --git a/tests/libgit2/commit/parse.c b/tests/libgit2/commit/parse.c
index 04366d7..653ed08 100644
--- a/tests/libgit2/commit/parse.c
+++ b/tests/libgit2/commit/parse.c
@@ -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));
}
}