summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2017-11-30 15:52:47 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2017-12-20 16:08:03 +0000
commitd1e446550a966a1dbc5d765aa79fe9bc47a1c1a3 (patch)
treec02306aee18ab6d2658b3d2f69ad685c39a78f9b
parentdacc32910e36e79ba108bef507e3aec9b0626e3c (diff)
downloadlibgit2-d1e446550a966a1dbc5d765aa79fe9bc47a1c1a3.tar.gz
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.
-rw-r--r--src/object.c13
-rw-r--r--src/object.h2
2 files changed, 13 insertions, 2 deletions
diff --git a/src/object.c b/src/object.c
index 4d069a34c..48f561384 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 ff61c1d33..e46c9cafa 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);