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 32 33 34 35 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
#include "clar_libgit2.h"
#include "tree.h"
static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
static git_repository *g_repo;
// Fixture setup and teardown
void test_object_tree_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_write__from_memory(void)
{
// write a tree from a memory
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, rid, id2;
git_oid_fromstr(&id, first_tree);
git_oid_fromstr(&id2, second_tree);
git_oid_fromstr(&bid, blob_oid);
//create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
cl_git_fail(git_treebuilder_insert(NULL, builder, "",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "/",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "..",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt",
&bid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_write(&rid, g_repo, builder));
cl_assert(git_oid_cmp(&rid, &id2) == 0);
git_treebuilder_free(builder);
git_tree_free(tree);
}
void test_object_tree_write__subtree(void)
{
// write a hierarchical tree from a memory
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, subtree_id, id2, id3;
git_oid id_hiearar;
git_oid_fromstr(&id, first_tree);
git_oid_fromstr(&id2, second_tree);
git_oid_fromstr(&id3, third_tree);
git_oid_fromstr(&bid, blob_oid);
//create subtree
cl_git_pass(git_treebuilder_create(&builder, NULL));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); //-V536
cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder));
git_treebuilder_free(builder);
// create parent tree
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); //-V536
cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder));
git_treebuilder_free(builder);
git_tree_free(tree);
cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0);
// check data is correct
cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar));
cl_assert(2 == git_tree_entrycount(tree));
git_tree_free(tree);
}
/*
* And the Lord said: Is this tree properly sorted?
*/
void test_object_tree_write__sorted_subtrees(void)
{
git_treebuilder *builder;
unsigned int i;
int position_c = -1, position_cake = -1, position_config = -1;
struct {
unsigned int attr;
const char *filename;
} entries[] = {
{ GIT_FILEMODE_BLOB, ".gitattributes" },
{ GIT_FILEMODE_BLOB, ".gitignore" },
{ GIT_FILEMODE_BLOB, ".htaccess" },
{ GIT_FILEMODE_BLOB, "Capfile" },
{ GIT_FILEMODE_BLOB, "Makefile"},
{ GIT_FILEMODE_BLOB, "README"},
{ GIT_FILEMODE_TREE, "app"},
{ GIT_FILEMODE_TREE, "cake"},
{ GIT_FILEMODE_TREE, "config"},
{ GIT_FILEMODE_BLOB, "c"},
{ GIT_FILEMODE_BLOB, "git_test.txt"},
{ GIT_FILEMODE_BLOB, "htaccess.htaccess"},
{ GIT_FILEMODE_BLOB, "index.php"},
{ GIT_FILEMODE_TREE, "plugins"},
{ GIT_FILEMODE_TREE, "schemas"},
{ GIT_FILEMODE_TREE, "ssl-certs"},
{ GIT_FILEMODE_TREE, "vendors"}
};
git_oid blank_oid, tree_oid;
memset(&blank_oid, 0x0, sizeof(blank_oid));
cl_git_pass(git_treebuilder_create(&builder, NULL));
for (i = 0; i < ARRAY_SIZE(entries); ++i) {
cl_git_pass(git_treebuilder_insert(NULL,
builder, entries[i].filename, &blank_oid, entries[i].attr));
}
cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder));
for (i = 0; i < builder->entries.length; ++i) {
git_tree_entry *entry = git_vector_get(&builder->entries, i);
if (strcmp(entry->filename, "c") == 0)
position_c = i;
if (strcmp(entry->filename, "cake") == 0)
position_cake = i;
if (strcmp(entry->filename, "config") == 0)
position_config = i;
}
cl_assert(position_c != -1);
cl_assert(position_cake != -1);
cl_assert(position_config != -1);
cl_assert(position_c < position_cake);
cl_assert(position_cake < position_config);
git_treebuilder_free(builder);
}
void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
{
git_treebuilder *builder;
int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
git_oid blank_oid, tree_oid;
git_tree *tree;
struct {
unsigned int attr;
const char *filename;
} entries[] = {
{ GIT_FILEMODE_BLOB, "aardvark" },
{ GIT_FILEMODE_BLOB, ".first" },
{ GIT_FILEMODE_BLOB, "apple" },
{ GIT_FILEMODE_BLOB, "last"},
{ GIT_FILEMODE_BLOB, "apple_after"},
{ GIT_FILEMODE_BLOB, "after_aardvark"},
{ 0, NULL },
};
memset(&blank_oid, 0x0, sizeof(blank_oid));
cl_git_pass(git_treebuilder_create(&builder, NULL));
cl_assert_equal_i(0, (int)git_treebuilder_entrycount(builder));
for (i = 0; entries[i].filename; ++i)
cl_git_pass(git_treebuilder_insert(NULL,
builder, entries[i].filename, &blank_oid, entries[i].attr));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "apple"));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "apple_after"));
cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "before_last", &blank_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
/* reinsert apple_after */
cl_git_pass(git_treebuilder_insert(
NULL, builder, "apple_after", &blank_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "last"));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
/* reinsert last */
cl_git_pass(git_treebuilder_insert(
NULL, builder, "last", &blank_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "apple_extra", &blank_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(7, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder));
git_treebuilder_free(builder);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
cl_assert_equal_i(7, (int)git_tree_entrycount(tree));
cl_assert(git_tree_entry_byname(tree, ".first") != NULL);
cl_assert(git_tree_entry_byname(tree, "apple") == NULL);
cl_assert(git_tree_entry_byname(tree, "apple_after") != NULL);
cl_assert(git_tree_entry_byname(tree, "apple_extra") != NULL);
cl_assert(git_tree_entry_byname(tree, "last") != NULL);
aardvark_i = apple_i = apple_after_i = apple_extra_i = last_i = -1;
for (i = 0; i < 7; ++i) {
const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
if (!strcmp(entry->filename, "aardvark"))
aardvark_i = i;
else if (!strcmp(entry->filename, "apple"))
apple_i = i;
else if (!strcmp(entry->filename, "apple_after"))
apple_after_i = i;
else if (!strcmp(entry->filename, "apple_extra"))
apple_extra_i = i;
else if (!strcmp(entry->filename, "last"))
last_i = i;
}
cl_assert_equal_i(-1, apple_i);
cl_assert_equal_i(6, last_i);
cl_assert(aardvark_i < apple_after_i);
cl_assert(apple_after_i < apple_extra_i);
git_tree_free(tree);
}