]> git.proxmox.com Git - libgit2.git/blame - tests/t09-tree.c
Fix path normalization tests
[libgit2.git] / tests / t09-tree.c
CommitLineData
2a1732b4
VM
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
30static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
31
98ac6780
ST
32static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
33static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
34static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
35
0ad6efa1
VM
36#if 0
37static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
38{
39 static const char *indent = " ";
40 git_tree *tree;
41 unsigned int i;
42
43 if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
44 return GIT_ERROR;
45
46 for (i = 0; i < git_tree_entrycount(tree); ++i) {
47 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
48 char entry_oid[40];
49
50 git_oid_fmt(entry_oid, &entry->oid);
51 printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);
52
53 if (entry->attr == S_IFDIR) {
54 if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
55 git_tree_close(tree);
56 return GIT_ERROR;
57 }
58 }
59 }
60
61 git_tree_close(tree);
62 return GIT_SUCCESS;
63}
64#endif
65
3dccfed1 66BEGIN_TEST(read0, "acces randomly the entries on a loaded tree")
2a1732b4
VM
67 git_oid id;
68 git_repository *repo;
69 git_tree *tree;
70
71 must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
72
73 git_oid_mkstr(&id, tree_oid);
74
75 must_pass(git_tree_lookup(&tree, repo, &id));
76
77 must_be_true(git_tree_entry_byname(tree, "README") != NULL);
78 must_be_true(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
79 must_be_true(git_tree_entry_byname(tree, "") == NULL);
80 must_be_true(git_tree_entry_byindex(tree, 0) != NULL);
81 must_be_true(git_tree_entry_byindex(tree, 2) != NULL);
82 must_be_true(git_tree_entry_byindex(tree, 3) == NULL);
83 must_be_true(git_tree_entry_byindex(tree, -1) == NULL);
84
85 git_repository_free(repo);
86END_TEST
87
3dccfed1 88BEGIN_TEST(read1, "read a tree from the repository")
2a1732b4
VM
89 git_oid id;
90 git_repository *repo;
91 git_tree *tree;
0ad6efa1 92 const git_tree_entry *entry;
2a1732b4
VM
93 git_object *obj;
94
95 must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
96
97 git_oid_mkstr(&id, tree_oid);
98
99 must_pass(git_tree_lookup(&tree, repo, &id));
100
101 must_be_true(git_tree_entrycount(tree) == 3);
102
1ee32c6d
BN
103 /* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
104 must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_TREE) == 0);
105 must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE);
106
2a1732b4
VM
107 entry = git_tree_entry_byname(tree, "README");
108 must_be_true(entry != NULL);
109
110 must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);
111
72a3fe42 112 must_pass(git_tree_entry_2object(&obj, repo, entry));
2a1732b4
VM
113
114 git_repository_free(repo);
115END_TEST
116
0ad6efa1
VM
117#if 0
118BEGIN_TEST(write0, "write a tree from an index")
119 git_repository *repo;
120 git_index *index;
121 git_oid tree_oid;
122
123 must_pass(git_repository_open(&repo, "/tmp/redtmp/.git"));
124 must_pass(git_repository_index(&index, repo));
125
126 must_pass(git_tree_create_fromindex(&tree_oid, index));
127 must_pass(print_tree(repo, &tree_oid, 0));
128
129 git_repository_free(repo);
130END_TEST
131#endif
132
98ac6780
ST
133BEGIN_TEST(write2, "write a tree from a memory")
134 git_repository *repo;
98ac6780
ST
135 git_treebuilder *builder;
136 git_tree *tree;
b918ae40 137 git_oid id, bid, rid, id2;
98ac6780 138
a6359408 139 must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
98ac6780 140 git_oid_mkstr(&id, first_tree);
b918ae40 141 git_oid_mkstr(&id2, second_tree);
98ac6780
ST
142 git_oid_mkstr(&bid, blob_oid);
143
144 //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
145 must_pass(git_tree_lookup(&tree, repo, &id));
146 must_pass(git_treebuilder_create(&builder, tree));
147 must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
148 must_pass(git_treebuilder_write(&rid,repo,builder));
149
b918ae40 150 must_be_true(git_oid_cmp(&rid, &id2) == 0);
a6359408 151 close_temp_repo(repo);
98ac6780
ST
152END_TEST
153
3dccfed1 154BEGIN_SUITE(tree)
0ad6efa1 155 //ADD_TEST(print0);
3dccfed1
VM
156 ADD_TEST(read0);
157 ADD_TEST(read1);
0ad6efa1
VM
158 //ADD_TEST(write0);
159 //ADD_TEST(write1);
98ac6780 160 ADD_TEST(write2);
3dccfed1 161END_SUITE
2a1732b4 162