Subject: [PATCH 2/7] object.c: make type_from_string() return "enum object_type"

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Mon, 8 Mar 2021 21:04:21 +0100

Change the type_from_string*() functions to return an "enum
object_type", and refactor their callers to check for "== OBJ_BAD"
instead of "< 0".

This helps to distinguish code in object.c where we really do return
-1 from code that returns an "enum object_type", whose OBJ_BAD happens
to be -1.

Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
---
 fsck.c        |  2 +-
 object-file.c |  2 +-
 object.c      | 12 ++++++------
 object.h      |  4 ++--
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/fsck.c b/fsck.c
index 6cc4f9ea892..a6d00dfa2e6 100644
--- a/fsck.c
+++ b/fsck.c
@@ -958,7 +958,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
 		goto done;
 	}
 	*tagged_type = type_from_string_gently(buffer, eol - buffer);
-	if (*tagged_type < 0)
+	if (*tagged_type == OBJ_BAD)
 		ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
 	if (ret)
 		goto done;
diff --git a/object-file.c b/object-file.c
index 42bc579828d..cd30c2b5590 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1324,7 +1324,7 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
 	 */
 	if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
 		type = 0;
-	else if (type < 0)
+	else if (type == OBJ_BAD)
 		die(_("invalid object type"));
 	if (oi->typep)
 		*oi->typep = type;
diff --git a/object.c b/object.c
index c7586e46727..eebacc28847 100644
--- a/object.c
+++ b/object.c
@@ -35,22 +35,22 @@ const char *type_name(unsigned int type)
 	return object_type_strings[type];
 }
 
-int type_from_string_gently(const char *str, ssize_t len)
+enum object_type type_from_string_gently(const char *str, ssize_t len)
 {
-	int i;
+	enum object_type i;
 
 	for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
 		if (!strncmp(str, object_type_strings[i], len) &&
 		    object_type_strings[i][len] == '\0')
 			return i;
-	return -1;
+	return OBJ_BAD;
 }
 
-int type_from_string(const char *str)
+enum object_type type_from_string(const char *str)
 {
 	size_t len = strlen(str);
-	int ret = type_from_string_gently(str, len);
-	if (ret < 0)
+	enum object_type ret = type_from_string_gently(str, len);
+	if (ret == OBJ_BAD)
 		die(_("invalid object type \"%s\""), str);
 	return ret;
 }
diff --git a/object.h b/object.h
index ffdc1298300..5e7a523e858 100644
--- a/object.h
+++ b/object.h
@@ -93,8 +93,8 @@ struct object {
 };
 
 const char *type_name(unsigned int type);
-int type_from_string_gently(const char *str, ssize_t len);
-int type_from_string(const char *str);
+enum object_type type_from_string_gently(const char *str, ssize_t len);
+enum object_type type_from_string(const char *str);
 
 /*
  * Return the current number of buckets in the object hashmap.
-- 
2.31.0.rc1.210.g0f8085a843c