Commit 7b6e8067ec96acef9a4184b43210d583b6d2f99a

Ramsay Jones 2008-12-10T18:31:28

Add some git_otype string conversion and testing routines Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

diff --git a/src/git/odb.h b/src/git/odb.h
index 07f4f5f..e8b1744 100644
--- a/src/git/odb.h
+++ b/src/git/odb.h
@@ -110,6 +110,34 @@ GIT_INLINE(void) git_obj_close(git_obj *obj)
 	obj->data = NULL;
 }
 
+/**
+ * Convert an object type to it's string representation.
+ *
+ * The result is a pointer to a string in static memory and
+ * should not be free()'ed.
+ *
+ * @param type object type to convert.
+ * @return the corresponding string representation.
+ */
+GIT_EXTERN(const char *) git_obj_type_to_string(git_otype type);
+
+/**
+ * Convert a string object type representation to it's git_otype.
+ *
+ * @param str the string to convert.
+ * @return the corresponding git_otype.
+ */
+GIT_EXTERN(git_otype) git_obj_string_to_type(const char *str);
+
+/**
+ * Determine if the given git_otype is a valid loose object type.
+ *
+ * @param type object type to test.
+ * @return true if the type represents a valid loose object type,
+ * false otherwise.
+ */
+GIT_EXTERN(int) git_obj__loose_object_type(git_otype type);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/odb.c b/src/odb.c
index 64c993e..3dd02e2 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -24,6 +24,7 @@
  */
 
 #include "git/odb.h"
+#include "util.h"
 
 struct git_odb {
 	/** Path to the "objects" directory. */
@@ -33,6 +34,48 @@ struct git_odb {
 	git_odb **alternates;
 };
 
+static struct {
+	const char *str;   /* type name string */
+	int        loose;  /* valid loose object type flag */
+} obj_type_table [] = {
+	{ "",          0 },  /* 0 = GIT_OBJ__EXT1     */
+	{ "commit",    1 },  /* 1 = GIT_OBJ_COMMIT    */
+	{ "tree",      1 },  /* 2 = GIT_OBJ_TREE      */
+	{ "blob",      1 },  /* 3 = GIT_OBJ_BLOB      */
+	{ "tag",       1 },  /* 4 = GIT_OBJ_TAG       */
+	{ "",          0 },  /* 5 = GIT_OBJ__EXT2     */
+	{ "OFS_DELTA", 0 },  /* 6 = GIT_OBJ_OFS_DELTA */
+	{ "REF_DELTA", 0 }   /* 7 = GIT_OBJ_REF_DELTA */
+};
+
+const char *git_obj_type_to_string(git_otype type)
+{
+	if (type < 0 || type >= ARRAY_SIZE(obj_type_table))
+		return "";
+	return obj_type_table[type].str;
+}
+
+git_otype git_obj_string_to_type(const char *str)
+{
+	int i;
+
+	if (!str || !*str)
+		return GIT_OBJ_BAD;
+
+	for (i = 0; i < ARRAY_SIZE(obj_type_table); i++)
+		if (!strcmp(str, obj_type_table[i].str))
+			return (git_otype) i;
+
+	return GIT_OBJ_BAD;
+}
+
+int git_obj__loose_object_type(git_otype type)
+{
+	if (type < 0 || type >= ARRAY_SIZE(obj_type_table))
+		return 0;
+	return obj_type_table[type].loose;
+}
+
 static int open_alternates(git_odb *db)
 {
 	unsigned n = 0;
@@ -95,3 +138,14 @@ attempt:
 	out->data = NULL;
 	return GIT_ENOTFOUND;
 }
+
+int git_odb__read_loose(git_obj *out, git_odb *db, const git_oid *id)
+{
+	return GIT_SUCCESS;
+}
+
+int git_odb__read_packed(git_obj *out, git_odb *db, const git_oid *id)
+{
+	return GIT_SUCCESS;
+}
+
diff --git a/tests/t0000-obj.c b/tests/t0000-obj.c
new file mode 100644
index 0000000..425b3cd
--- /dev/null
+++ b/tests/t0000-obj.c
@@ -0,0 +1,50 @@
+
+#include "test_lib.h"
+#include <git/odb.h>
+
+BEGIN_TEST(type_to_string)
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_BAD),""));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ__EXT1),""));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_COMMIT),"commit"));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_TREE),"tree"));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_BLOB),"blob"));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_TAG),"tag"));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ__EXT2),""));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_OFS_DELTA),"OFS_DELTA"));
+	must_be_true(!strcmp(git_obj_type_to_string(GIT_OBJ_REF_DELTA),"REF_DELTA"));
+
+	must_be_true(!strcmp(git_obj_type_to_string(-2),""));
+	must_be_true(!strcmp(git_obj_type_to_string(8),""));
+	must_be_true(!strcmp(git_obj_type_to_string(1234),""));
+END_TEST
+
+BEGIN_TEST(string_to_type)
+	must_be_true(git_obj_string_to_type(NULL) == GIT_OBJ_BAD);
+	must_be_true(git_obj_string_to_type("") == GIT_OBJ_BAD);
+	must_be_true(git_obj_string_to_type("commit") == GIT_OBJ_COMMIT);
+	must_be_true(git_obj_string_to_type("tree") == GIT_OBJ_TREE);
+	must_be_true(git_obj_string_to_type("blob") == GIT_OBJ_BLOB);
+	must_be_true(git_obj_string_to_type("tag") == GIT_OBJ_TAG);
+	must_be_true(git_obj_string_to_type("OFS_DELTA") == GIT_OBJ_OFS_DELTA);
+	must_be_true(git_obj_string_to_type("REF_DELTA") == GIT_OBJ_REF_DELTA);
+
+	must_be_true(git_obj_string_to_type("CoMmIt") == GIT_OBJ_BAD);
+	must_be_true(git_obj_string_to_type("hohoho") == GIT_OBJ_BAD);
+END_TEST
+
+BEGIN_TEST(loose_object)
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_BAD) == 0);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ__EXT1) == 0);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_COMMIT) == 1);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_TREE) == 1);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_BLOB) == 1);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_TAG) == 1);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ__EXT2) == 0);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_OFS_DELTA) == 0);
+	must_be_true(git_obj__loose_object_type(GIT_OBJ_REF_DELTA) == 0);
+
+	must_be_true(git_obj__loose_object_type(-2) == 0);
+	must_be_true(git_obj__loose_object_type(8) == 0);
+	must_be_true(git_obj__loose_object_type(1234) == 0);
+END_TEST
+