Hash :
53412305
Author :
Date :
2016-05-19T15:29:53
tree: plug leaks in the tree updater
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
#include "clar_libgit2.h"
#include "tree.h"
static git_repository *g_repo;
void test_object_tree_update__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo2");
}
void test_object_tree_update__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_update__remove_blob(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "README";
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path},
};
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
cl_git_pass(git_index_remove(idx, path, 0));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__remove_blob_deeper(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "subdir/README";
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path},
};
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
cl_git_pass(git_index_remove(idx, path, 0));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__replace_blob(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "README";
git_index_entry entry = { {0} };
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, path},
};
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
entry.path = path;
cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92"));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(idx, &entry));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_oid_fromstr(&updates[0].id, "fa49b077972391ad58037050f2a75f74e3671e92"));
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__add_blobs(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
git_index_entry entry = { {0} };
int i;
const char *paths[] = {
"some/deep/path",
"some/other/path",
"a/path/elsewhere",
};
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[0]},
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[1]},
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[2]},
};
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92"));
for (i = 0; i < 3; i++) {
cl_git_pass(git_oid_fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92"));
}
for (i = 0; i < 2; i++) {
int j;
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
base_tree = NULL;
if (i == 1) {
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
cl_git_pass(git_index_read_tree(idx, base_tree));
}
for (j = 0; j < 3; j++) {
entry.path = paths[j];
cl_git_pass(git_index_add(idx, &entry));
}
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operations via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 3, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
}
git_tree_free(base_tree);
}
void test_object_tree_update__add_conflict(void)
{
int i;
git_oid tree_updater_id;
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, "a/dir/blob"},
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, "a/dir"},
};
for (i = 0; i < 2; i++) {
cl_git_pass(git_oid_fromstr(&updates[i].id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
}
cl_git_fail(git_tree_create_updated(&tree_updater_id, g_repo, NULL, 2, updates));
}
void test_object_tree_update__add_conflict2(void)
{
int i;
git_oid tree_updater_id;
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, "a/dir/blob"},
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_TREE, "a/dir/blob"},
};
for (i = 0; i < 2; i++) {
cl_git_pass(git_oid_fromstr(&updates[i].id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
}
cl_git_fail(git_tree_create_updated(&tree_updater_id, g_repo, NULL, 2, updates));
}