Commit b7a46fa8505f6b1d9a4c0a844b6e8f91522257bf

Edward Thomson 2022-01-23T12:25:03

object: move oid header parsing to object

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));
 	}
 }