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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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
+