Skip to content

Commit fe8e3b7

Browse files
dschogitster
authored andcommitted
Refactor type_from_string() to allow continuing after detecting an error
In the next commits, we will enhance the fsck_tag() function to check tag objects more thoroughly. To this end, we need a function to verify that a given string is a valid object type, but that does not die() in the negative case. While at it, prepare type_from_string() for counted strings, i.e. strings with an explicitly specified length rather than a NUL termination. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c72b98 commit fe8e3b7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

object.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ const char *typename(unsigned int type)
3333
return object_type_strings[type];
3434
}
3535

36-
int type_from_string(const char *str)
36+
int type_from_string_gently(const char *str, ssize_t len, int gentle)
3737
{
3838
int i;
3939

40+
if (len < 0)
41+
len = strlen(str);
42+
4043
for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
41-
if (!strcmp(str, object_type_strings[i]))
44+
if (!strncmp(str, object_type_strings[i], len))
4245
return i;
46+
47+
if (gentle)
48+
return -1;
49+
4350
die("invalid object type \"%s\"", str);
4451
}
4552

object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ struct object {
5353
};
5454

5555
extern const char *typename(unsigned int type);
56-
extern int type_from_string(const char *str);
56+
extern int type_from_string_gently(const char *str, ssize_t, int gentle);
57+
#define type_from_string(str) type_from_string_gently(str, -1, 0)
5758

5859
/*
5960
* Return the current number of buckets in the object hashmap.

0 commit comments

Comments
 (0)