]> git.proxmox.com Git - libgit2.git/blob - tests/t09-tree.c
Merge pull request #405 from carlosmn/http-ls
[libgit2.git] / tests / t09-tree.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25 #include "test_lib.h"
26 #include "test_helpers.h"
27
28 #include "tree.h"
29
30 static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
31
32 static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
33 static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
34 static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
35 static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
36
37 #if 0
38 static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
39 {
40 static const char *indent = " ";
41 git_tree *tree;
42 unsigned int i;
43
44 if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
45 return GIT_ERROR;
46
47 for (i = 0; i < git_tree_entrycount(tree); ++i) {
48 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
49 char entry_oid[40];
50
51 git_oid_fmt(entry_oid, &entry->oid);
52 printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);
53
54 if (entry->attr == S_IFDIR) {
55 if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
56 git_tree_close(tree);
57 return GIT_ERROR;
58 }
59 }
60 }
61
62 git_tree_close(tree);
63 return GIT_SUCCESS;
64 }
65 #endif
66
67 BEGIN_TEST(read0, "acces randomly the entries on a loaded tree")
68 git_oid id;
69 git_repository *repo;
70 git_tree *tree;
71
72 must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
73
74 git_oid_fromstr(&id, tree_oid);
75
76 must_pass(git_tree_lookup(&tree, repo, &id));
77
78 must_be_true(git_tree_entry_byname(tree, "README") != NULL);
79 must_be_true(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
80 must_be_true(git_tree_entry_byname(tree, "") == NULL);
81 must_be_true(git_tree_entry_byindex(tree, 0) != NULL);
82 must_be_true(git_tree_entry_byindex(tree, 2) != NULL);
83 must_be_true(git_tree_entry_byindex(tree, 3) == NULL);
84 must_be_true(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL);
85
86 git_tree_close(tree);
87 git_repository_free(repo);
88 END_TEST
89
90 BEGIN_TEST(read1, "read a tree from the repository")
91 git_oid id;
92 git_repository *repo;
93 git_tree *tree;
94 const git_tree_entry *entry;
95 git_object *obj;
96
97 must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
98
99 git_oid_fromstr(&id, tree_oid);
100
101 must_pass(git_tree_lookup(&tree, repo, &id));
102
103 must_be_true(git_tree_entrycount(tree) == 3);
104
105 /* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
106 must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_TREE) == 0);
107 must_be_true(obj != NULL);
108 git_object_close(obj);
109 obj = NULL;
110 must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE);
111 must_be_true(obj == NULL);
112
113 entry = git_tree_entry_byname(tree, "README");
114 must_be_true(entry != NULL);
115
116 must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);
117
118 must_pass(git_tree_entry_2object(&obj, repo, entry));
119 must_be_true(obj != NULL);
120
121 git_object_close(obj);
122 git_tree_close(tree);
123 git_repository_free(repo);
124 END_TEST
125
126 #if 0
127 BEGIN_TEST(write0, "write a tree from an index")
128 git_repository *repo;
129 git_index *index;
130 git_oid tree_oid;
131
132 must_pass(git_repository_open(&repo, "/tmp/redtmp/.git"));
133 must_pass(git_repository_index(&index, repo));
134
135 must_pass(git_tree_create_fromindex(&tree_oid, index));
136 must_pass(print_tree(repo, &tree_oid, 0));
137
138 git_repository_free(repo);
139 END_TEST
140 #endif
141
142 BEGIN_TEST(write2, "write a tree from a memory")
143 git_repository *repo;
144 git_treebuilder *builder;
145 git_tree *tree;
146 git_oid id, bid, rid, id2;
147
148 must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
149 git_oid_fromstr(&id, first_tree);
150 git_oid_fromstr(&id2, second_tree);
151 git_oid_fromstr(&bid, blob_oid);
152
153 //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
154 must_pass(git_tree_lookup(&tree, repo, &id));
155 must_pass(git_treebuilder_create(&builder, tree));
156
157 must_fail(git_treebuilder_insert(NULL, builder, "", &bid, 0100644));
158 must_fail(git_treebuilder_insert(NULL, builder, "/", &bid, 0100644));
159 must_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt", &bid, 0100644));
160
161 must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
162 must_pass(git_treebuilder_write(&rid,repo,builder));
163
164 must_be_true(git_oid_cmp(&rid, &id2) == 0);
165
166 git_treebuilder_free(builder);
167 git_tree_close(tree);
168 close_temp_repo(repo);
169 END_TEST
170
171 BEGIN_TEST(write3, "write a hierarchical tree from a memory")
172 git_repository *repo;
173 git_treebuilder *builder;
174 git_tree *tree;
175 git_oid id, bid, subtree_id, id2, id3;
176 git_oid id_hiearar;
177
178 must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
179 git_oid_fromstr(&id, first_tree);
180 git_oid_fromstr(&id2, second_tree);
181 git_oid_fromstr(&id3, third_tree);
182 git_oid_fromstr(&bid, blob_oid);
183
184 //create subtree
185 must_pass(git_treebuilder_create(&builder, NULL));
186 must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
187 must_pass(git_treebuilder_write(&subtree_id,repo,builder));
188 git_treebuilder_free(builder);
189
190 // create parent tree
191 must_pass(git_tree_lookup(&tree, repo, &id));
192 must_pass(git_treebuilder_create(&builder, tree));
193 must_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
194 must_pass(git_treebuilder_write(&id_hiearar,repo,builder));
195 git_treebuilder_free(builder);
196 git_tree_close(tree);
197
198 must_be_true(git_oid_cmp(&id_hiearar, &id3) == 0);
199
200 // check data is correct
201 must_pass(git_tree_lookup(&tree, repo, &id_hiearar));
202 must_be_true(2 == git_tree_entrycount(tree));
203 git_tree_close(tree);
204
205 close_temp_repo(repo);
206
207 END_TEST
208
209 BEGIN_SUITE(tree)
210 //ADD_TEST(print0);
211 ADD_TEST(read0);
212 ADD_TEST(read1);
213 //ADD_TEST(write0);
214 //ADD_TEST(write1);
215 ADD_TEST(write2);
216 ADD_TEST(write3);
217 END_SUITE
218