]> git.proxmox.com Git - libgit2.git/blob - tests/t09-tree.c
Fix compilation warnings in MSVC
[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_mkstr(&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_mkstr(&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 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 entry = git_tree_entry_byname(tree, "README");
112 must_be_true(entry != NULL);
113
114 must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);
115
116 must_pass(git_tree_entry_2object(&obj, repo, entry));
117
118 git_object_close(obj);
119 git_tree_close(tree);
120 git_repository_free(repo);
121 END_TEST
122
123 #if 0
124 BEGIN_TEST(write0, "write a tree from an index")
125 git_repository *repo;
126 git_index *index;
127 git_oid tree_oid;
128
129 must_pass(git_repository_open(&repo, "/tmp/redtmp/.git"));
130 must_pass(git_repository_index(&index, repo));
131
132 must_pass(git_tree_create_fromindex(&tree_oid, index));
133 must_pass(print_tree(repo, &tree_oid, 0));
134
135 git_repository_free(repo);
136 END_TEST
137 #endif
138
139 BEGIN_TEST(write2, "write a tree from a memory")
140 git_repository *repo;
141 git_treebuilder *builder;
142 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 git_oid_mkstr(&bid, blob_oid);
149
150 //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
151 must_pass(git_tree_lookup(&tree, repo, &id));
152 must_pass(git_treebuilder_create(&builder, tree));
153 must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
154 must_pass(git_treebuilder_write(&rid,repo,builder));
155
156 must_be_true(git_oid_cmp(&rid, &id2) == 0);
157
158 git_treebuilder_free(builder);
159 git_tree_close(tree);
160 close_temp_repo(repo);
161 END_TEST
162
163 BEGIN_TEST(write3, "write a hierarchical tree from a memory")
164 git_repository *repo;
165 git_treebuilder *builder;
166 git_tree *tree;
167 git_oid id, bid, subtree_id, id2, id3;
168 git_oid id_hiearar;
169
170 must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
171 git_oid_mkstr(&id, first_tree);
172 git_oid_mkstr(&id2, second_tree);
173 git_oid_mkstr(&id3, third_tree);
174 git_oid_mkstr(&bid, blob_oid);
175
176 //create subtree
177 must_pass(git_treebuilder_create(&builder, NULL));
178 must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
179 must_pass(git_treebuilder_write(&subtree_id,repo,builder));
180 git_treebuilder_free(builder);
181
182 // create parent tree
183 must_pass(git_tree_lookup(&tree, repo, &id));
184 must_pass(git_treebuilder_create(&builder, tree));
185 must_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
186 must_pass(git_treebuilder_write(&id_hiearar,repo,builder));
187 git_treebuilder_free(builder);
188 git_tree_close(tree);
189
190 must_be_true(git_oid_cmp(&id_hiearar, &id3) == 0);
191
192 // check data is correct
193 must_pass(git_tree_lookup(&tree, repo, &id_hiearar));
194 must_be_true(2 == git_tree_entrycount(tree));
195 git_tree_close(tree);
196
197 close_temp_repo(repo);
198
199 END_TEST
200
201 BEGIN_SUITE(tree)
202 //ADD_TEST(print0);
203 ADD_TEST(read0);
204 ADD_TEST(read1);
205 //ADD_TEST(write0);
206 //ADD_TEST(write1);
207 ADD_TEST(write2);
208 ADD_TEST(write3);
209 END_SUITE
210