Skip to content

Commit d1e4465

Browse files
committed
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.
1 parent dacc329 commit d1e4465

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/object.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,23 @@ const char *git_object_type2string(git_otype type)
235235
}
236236

237237
git_otype git_object_string2type(const char *str)
238+
{
239+
if (!str)
240+
return GIT_OBJ_BAD;
241+
242+
return git_object_stringn2type(str, strlen(str));
243+
}
244+
245+
git_otype git_object_stringn2type(const char *str, size_t len)
238246
{
239247
size_t i;
240248

241-
if (!str || !*str)
249+
if (!str || !len || !*str)
242250
return GIT_OBJ_BAD;
243251

244252
for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
245-
if (!strcmp(str, git_objects_table[i].str))
253+
if (*git_objects_table[i].str &&
254+
!git__prefixncmp(str, len, git_objects_table[i].str))
246255
return (git_otype)i;
247256

248257
return GIT_OBJ_BAD;

src/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ int git_object__from_odb_object(
3030

3131
int git_object__resolve_to_type(git_object **obj, git_otype type);
3232

33+
git_otype git_object_stringn2type(const char *str, size_t len);
34+
3335
int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
3436

3537
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid);

0 commit comments

Comments
 (0)