]>
git.proxmox.com Git - libgit2.git/blob - tests/t09-tree.c
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.
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.)
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.
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.
26 #include "test_helpers.h"
30 static const char *tree_oid
= "1810dff58d8a660512d4832e740f692884338ccd";
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";
38 static int print_tree(git_repository
*repo
, const git_oid
*tree_oid
, int depth
)
40 static const char *indent
= " ";
44 if (git_tree_lookup(&tree
, repo
, tree_oid
) < GIT_SUCCESS
)
47 for (i
= 0; i
< git_tree_entrycount(tree
); ++i
) {
48 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
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
);
54 if (entry
->attr
== S_IFDIR
) {
55 if (print_tree(repo
, &entry
->oid
, depth
+ 1) < GIT_SUCCESS
) {
67 BEGIN_TEST(read0
, "acces randomly the entries on a loaded tree")
72 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
74 git_oid_mkstr(&id
, tree_oid
);
76 must_pass(git_tree_lookup(&tree
, repo
, &id
));
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
);
87 git_repository_free(repo
);
90 BEGIN_TEST(read1
, "read a tree from the repository")
94 const git_tree_entry
*entry
;
97 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
99 git_oid_mkstr(&id
, tree_oid
);
101 must_pass(git_tree_lookup(&tree
, repo
, &id
));
103 must_be_true(git_tree_entrycount(tree
) == 3);
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
);
111 entry
= git_tree_entry_byname(tree
, "README");
112 must_be_true(entry
!= NULL
);
114 must_be_true(strcmp(git_tree_entry_name(entry
), "README") == 0);
116 must_pass(git_tree_entry_2object(&obj
, repo
, entry
));
118 git_object_close(obj
);
119 git_tree_close(tree
);
120 git_repository_free(repo
);
124 BEGIN_TEST(write0
, "write a tree from an index")
125 git_repository
*repo
;
129 must_pass(git_repository_open(&repo
, "/tmp/redtmp/.git"));
130 must_pass(git_repository_index(&index
, repo
));
132 must_pass(git_tree_create_fromindex(&tree_oid
, index
));
133 must_pass(print_tree(repo
, &tree_oid
, 0));
135 git_repository_free(repo
);
139 BEGIN_TEST(write2
, "write a tree from a memory")
140 git_repository
*repo
;
141 git_treebuilder
*builder
;
143 git_oid id
, bid
, rid
, id2
;
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
);
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
));
156 must_be_true(git_oid_cmp(&rid
, &id2
) == 0);
158 git_treebuilder_free(builder
);
159 git_tree_close(tree
);
160 close_temp_repo(repo
);
163 BEGIN_TEST(write3
, "write a hierarchical tree from a memory")
164 git_repository
*repo
;
165 git_treebuilder
*builder
;
167 git_oid id
, bid
, subtree_id
, id2
, id3
;
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
);
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
);
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
);
190 must_be_true(git_oid_cmp(&id_hiearar
, &id3
) == 0);
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
);
197 close_temp_repo(repo
);