Commit d1e446550a966a1dbc5d765aa79fe9bc47a1c1a3

Edward Thomson 2017-11-30T15:52:47

object: introduce git_object_stringn2type Introduce an internal API to get the object type based on a length-specified (not null terminated) string representation. This can be used to compare the (space terminated) object type name in a loose object. Reimplement `git_object_string2type` based on this API.

diff --git a/src/object.c b/src/object.c
index 4d069a3..48f5613 100644
--- a/src/object.c
+++ b/src/object.c
@@ -236,13 +236,22 @@ const char *git_object_type2string(git_otype type)
 
 git_otype git_object_string2type(const char *str)
 {
+	if (!str)
+		return GIT_OBJ_BAD;
+
+	return git_object_stringn2type(str, strlen(str));
+}
+
+git_otype git_object_stringn2type(const char *str, size_t len)
+{
 	size_t i;
 
-	if (!str || !*str)
+	if (!str || !len || !*str)
 		return GIT_OBJ_BAD;
 
 	for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
-		if (!strcmp(str, git_objects_table[i].str))
+		if (*git_objects_table[i].str &&
+			!git__prefixncmp(str, len, git_objects_table[i].str))
 			return (git_otype)i;
 
 	return GIT_OBJ_BAD;
diff --git a/src/object.h b/src/object.h
index ff61c1d..e46c9ca 100644
--- a/src/object.h
+++ b/src/object.h
@@ -30,6 +30,8 @@ int git_object__from_odb_object(
 
 int git_object__resolve_to_type(git_object **obj, git_otype type);
 
+git_otype 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);
 
 void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid);