Commit 9a39a36424904213103b01ecd7a54dbdf3b21103

Ben Straub 2012-03-30T07:04:52

t09-tree.c ported.

diff --git a/tests-clar/object/tree/read.c b/tests-clar/object/tree/read.c
new file mode 100644
index 0000000..7abc723
--- /dev/null
+++ b/tests-clar/object/tree/read.c
@@ -0,0 +1,75 @@
+#include "clar_libgit2.h"
+
+#include "tree.h"
+
+static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
+
+static git_repository *g_repo;
+
+// Fixture setup and teardown
+void test_object_tree_read__initialize(void)
+{
+   g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_object_tree_read__cleanup(void)
+{
+   cl_git_sandbox_cleanup();
+}
+
+
+
+void test_object_tree_read__loaded(void)
+{
+   // acces randomly the entries on a loaded tree
+	git_oid id;
+	git_tree *tree;
+
+	git_oid_fromstr(&id, tree_oid);
+
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+
+	cl_assert(git_tree_entry_byname(tree, "README") != NULL);
+	cl_assert(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
+	cl_assert(git_tree_entry_byname(tree, "") == NULL);
+	cl_assert(git_tree_entry_byindex(tree, 0) != NULL);
+	cl_assert(git_tree_entry_byindex(tree, 2) != NULL);
+	cl_assert(git_tree_entry_byindex(tree, 3) == NULL);
+	cl_assert(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL);
+
+	git_tree_free(tree);
+}
+
+void test_object_tree_read__two(void)
+{
+   // read a tree from the repository
+	git_oid id;
+	git_tree *tree;
+	const git_tree_entry *entry;
+	git_object *obj;
+
+	git_oid_fromstr(&id, tree_oid);
+
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+
+	cl_assert(git_tree_entrycount(tree) == 3);
+
+	/* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
+	cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_TREE) == 0);
+	cl_assert(obj != NULL);
+	git_object_free(obj);
+	obj = NULL;
+	cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE);
+	cl_assert(obj == NULL);
+
+	entry = git_tree_entry_byname(tree, "README");
+	cl_assert(entry != NULL);
+
+	cl_assert(strcmp(git_tree_entry_name(entry), "README") == 0);
+
+	cl_git_pass(git_tree_entry_2object(&obj, g_repo, entry));
+	cl_assert(obj != NULL);
+
+	git_object_free(obj);
+	git_tree_free(tree);
+}
diff --git a/tests-clar/object/tree/write.c b/tests-clar/object/tree/write.c
new file mode 100644
index 0000000..80a5c89
--- /dev/null
+++ b/tests-clar/object/tree/write.c
@@ -0,0 +1,133 @@
+#include "clar_libgit2.h"
+
+#include "tree.h"
+
+static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
+static const char *first_tree  = "181037049a54a1eb5fab404658a3a250b44335d7";
+static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
+static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
+
+static git_repository *g_repo;
+
+
+// Helpers
+static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
+{
+	static const char *indent = "                              ";
+	git_tree *tree;
+	unsigned int i;
+
+	if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
+		return GIT_ERROR;
+
+	for (i = 0; i < git_tree_entrycount(tree); ++i) {
+		const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
+		char entry_oid[40];
+
+		git_oid_fmt(entry_oid, &entry->oid);
+		printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);
+
+		if (entry->attr == S_IFDIR) {
+			if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
+				git_tree_free(tree);
+				return GIT_ERROR;
+			}
+		}
+	}
+
+	git_tree_free(tree);
+	return GIT_SUCCESS;
+}
+
+
+// Fixture setup and teardown
+void test_object_tree_write__initialize(void)
+{
+   g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_object_tree_write__cleanup(void)
+{
+   cl_git_sandbox_cleanup();
+}
+
+
+
+void _test_object_tree_write__print(void)
+{
+   // write a tree from an index
+	git_index *index;
+	git_oid tree_oid;
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	cl_git_pass(git_tree_create_fromindex(&tree_oid, index));
+	cl_git_pass(print_tree(g_repo, &tree_oid, 0));
+}
+
+void test_object_tree_write__from_memory(void)
+{
+   // write a tree from a memory
+	git_treebuilder *builder;
+	git_tree *tree;
+	git_oid id, bid, rid, id2;
+
+	git_oid_fromstr(&id, first_tree);
+	git_oid_fromstr(&id2, second_tree);
+	git_oid_fromstr(&bid, blob_oid);
+
+	//create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+	cl_git_pass(git_treebuilder_create(&builder, tree));
+
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "", &bid, 0100644));
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "/", &bid, 0100644));
+	cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt", &bid, 0100644));
+
+	cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
+	cl_git_pass(git_treebuilder_write(&rid, g_repo, builder));
+
+	cl_assert(git_oid_cmp(&rid, &id2) == 0);
+
+	git_treebuilder_free(builder);
+	git_tree_free(tree);
+}
+
+void test_object_tree_write__subtree(void)
+{
+   // write a hierarchical tree from a memory
+	git_treebuilder *builder;
+	git_tree *tree;
+	git_oid id, bid, subtree_id, id2, id3;
+	git_oid id_hiearar;
+
+	git_oid_fromstr(&id, first_tree);
+	git_oid_fromstr(&id2, second_tree);
+	git_oid_fromstr(&id3, third_tree);
+	git_oid_fromstr(&bid, blob_oid);
+
+	//create subtree
+	cl_git_pass(git_treebuilder_create(&builder, NULL));
+	cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
+	cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder));
+	git_treebuilder_free(builder);
+
+	// create parent tree
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+	cl_git_pass(git_treebuilder_create(&builder, tree));
+	cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
+	cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder));
+	git_treebuilder_free(builder);
+	git_tree_free(tree);
+
+	cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0);
+
+	// check data is correct
+	cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar));
+	cl_assert(2 == git_tree_entrycount(tree));
+#ifndef GIT_WIN32
+	cl_assert((loose_object_dir_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_DIR_MODE);
+	cl_assert((loose_object_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_FILE_MODE);
+#endif
+	git_tree_free(tree);
+}