t09-tree.c 6.39 KB
Newer Older
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
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "test_lib.h"
#include "test_helpers.h"

#include "tree.h"

static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";

32 33 34
static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *first_tree  = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
35
static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
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
#if 0
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_close(tree);
				return GIT_ERROR;
			}
		}
	}

	git_tree_close(tree);
	return GIT_SUCCESS;
}
#endif

67
BEGIN_TEST(read0, "acces randomly the entries on a loaded tree")
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	git_oid id;
	git_repository *repo;
	git_tree *tree;

	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));

	git_oid_mkstr(&id, tree_oid);

	must_pass(git_tree_lookup(&tree, repo, &id));

	must_be_true(git_tree_entry_byname(tree, "README") != NULL);
	must_be_true(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
	must_be_true(git_tree_entry_byname(tree, "") == NULL);
	must_be_true(git_tree_entry_byindex(tree, 0) != NULL);
	must_be_true(git_tree_entry_byindex(tree, 2) != NULL);
	must_be_true(git_tree_entry_byindex(tree, 3) == NULL);
	must_be_true(git_tree_entry_byindex(tree, -1) == NULL);

86
	git_tree_close(tree);
87 88 89
	git_repository_free(repo);
END_TEST

90
BEGIN_TEST(read1, "read a tree from the repository")
91 92 93
	git_oid id;
	git_repository *repo;
	git_tree *tree;
94
	const git_tree_entry *entry;
95 96 97 98 99 100 101 102 103 104
	git_object *obj;

	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));

	git_oid_mkstr(&id, tree_oid);

	must_pass(git_tree_lookup(&tree, repo, &id));

	must_be_true(git_tree_entrycount(tree) == 3);

105 106
	/* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
	must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_TREE) == 0);
107
	git_object_close(obj);
108
	must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE);
109
	git_object_close(obj);
110

111 112 113 114 115
	entry = git_tree_entry_byname(tree, "README");
	must_be_true(entry != NULL);

	must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);

Vicent Marti committed
116
	must_pass(git_tree_entry_2object(&obj, repo, entry));
117

118 119
	git_object_close(obj);
	git_tree_close(tree);
120 121 122
	git_repository_free(repo);
END_TEST

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#if 0
BEGIN_TEST(write0, "write a tree from an index")
	git_repository *repo;
	git_index *index;
	git_oid tree_oid;

	must_pass(git_repository_open(&repo, "/tmp/redtmp/.git"));
	must_pass(git_repository_index(&index, repo));

	must_pass(git_tree_create_fromindex(&tree_oid, index));
	must_pass(print_tree(repo, &tree_oid, 0));

	git_repository_free(repo);
END_TEST
#endif

139 140 141 142
BEGIN_TEST(write2, "write a tree from a memory")
	git_repository *repo;
	git_treebuilder *builder;
	git_tree *tree;
143
	git_oid id, bid, rid, id2;
144

145
	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
146
	git_oid_mkstr(&id, first_tree);
147
	git_oid_mkstr(&id2, second_tree);
148 149 150 151 152 153 154 155
	git_oid_mkstr(&bid, blob_oid);

	//create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
	must_pass(git_tree_lookup(&tree, repo, &id));
	must_pass(git_treebuilder_create(&builder, tree));
	must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
	must_pass(git_treebuilder_write(&rid,repo,builder));

156
	must_be_true(git_oid_cmp(&rid, &id2) == 0);
157 158 159

	git_treebuilder_free(builder);
	git_tree_close(tree);
160
	close_temp_repo(repo);
161 162
END_TEST

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
BEGIN_TEST(write3, "write a hierarchical tree from a memory")
	git_repository *repo;
	git_treebuilder *builder;
	git_tree *tree;
	git_oid id, bid, subtree_id, id2, id3;
	git_oid id_hiearar;

	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
	git_oid_mkstr(&id, first_tree);
	git_oid_mkstr(&id2, second_tree);
	git_oid_mkstr(&id3, third_tree);
	git_oid_mkstr(&bid, blob_oid);

	//create subtree
	must_pass(git_treebuilder_create(&builder, NULL));
	must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
	must_pass(git_treebuilder_write(&subtree_id,repo,builder));
	git_treebuilder_free(builder);

        // create parent tree
        must_pass(git_tree_lookup(&tree, repo, &id));
	must_pass(git_treebuilder_create(&builder, tree));
	must_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
	must_pass(git_treebuilder_write(&id_hiearar,repo,builder));
	git_treebuilder_free(builder);
	git_tree_close(tree);

        must_be_true(git_oid_cmp(&id_hiearar, &id3) == 0);

        // check data is correct
        must_pass(git_tree_lookup(&tree, repo, &id_hiearar));
        must_be_true(2 == git_tree_entrycount(tree));
	git_tree_close(tree);

        close_temp_repo(repo);

END_TEST

201
BEGIN_SUITE(tree)
202
	//ADD_TEST(print0);
203 204
	ADD_TEST(read0);
	ADD_TEST(read1);
205 206
	//ADD_TEST(write0);
	//ADD_TEST(write1);
207
	ADD_TEST(write2);
208
	ADD_TEST(write3);
209
END_SUITE
210