collision.c 4 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/index.h"

5 6 7 8 9 10 11 12 13 14 15
static git_repository *g_repo;
static git_odb *g_odb;
static git_index *g_index;
static git_oid g_empty_id;

void test_index_collision__initialize(void)
{
	g_repo = cl_git_sandbox_init("empty_standard_repo");
	cl_git_pass(git_repository_odb(&g_odb, g_repo));
	cl_git_pass(git_repository_index(&g_index, g_repo));

16
	cl_git_pass(git_odb_write(&g_empty_id, g_odb, "", 0, GIT_OBJECT_BLOB));
17
}
18 19 20

void test_index_collision__cleanup(void)
{
21 22
	git_index_free(g_index);
	git_odb_free(g_odb);
23 24 25
	cl_git_sandbox_cleanup();
}

26
void test_index_collision__add_blob_with_conflicting_file(void)
27 28
{
	git_index_entry entry;
29
	git_tree_entry *tentry;
30 31 32 33 34 35 36 37
	git_oid tree_id;
	git_tree *tree;

	memset(&entry, 0, sizeof(entry));
	entry.ctime.seconds = 12346789;
	entry.mtime.seconds = 12346789;
	entry.mode  = 0100644;
	entry.file_size = 0;
38
	git_oid_cpy(&entry.id, &g_empty_id);
39 40

	entry.path = "a/b";
41
	cl_git_pass(git_index_add(g_index, &entry));
42

43 44 45 46 47 48 49
	/* Check a/b exists here */
	cl_git_pass(git_index_write_tree(&tree_id, g_index));
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
	cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
	git_tree_entry_free(tentry);
	git_tree_free(tree);

50 51
	/* create a tree/blob collision */
	entry.path = "a/b/c";
52
	cl_git_pass(git_index_add(g_index, &entry));
53

54
	/* a/b should now be a tree and a/b/c a blob */
55 56
	cl_git_pass(git_index_write_tree(&tree_id, g_index));
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
57 58 59 60 61 62 63 64 65 66 67
	cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
	git_tree_entry_free(tentry);
	git_tree_free(tree);
}

void test_index_collision__add_blob_with_conflicting_dir(void)
{
	git_index_entry entry;
	git_tree_entry *tentry;
	git_oid tree_id;
	git_tree *tree;
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
	memset(&entry, 0, sizeof(entry));
	entry.ctime.seconds = 12346789;
	entry.mtime.seconds = 12346789;
	entry.mode  = 0100644;
	entry.file_size = 0;
	git_oid_cpy(&entry.id, &g_empty_id);

	entry.path = "a/b/c";
	cl_git_pass(git_index_add(g_index, &entry));

	/* Check a/b/c exists here */
	cl_git_pass(git_index_write_tree(&tree_id, g_index));
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
	cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
	git_tree_entry_free(tentry);
	git_tree_free(tree);

	/* create a blob/tree collision */
	entry.path = "a/b";
	cl_git_pass(git_index_add(g_index, &entry));

	/* a/b should now be a tree and a/b/c a blob */
	cl_git_pass(git_index_write_tree(&tree_id, g_index));
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
	cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
	cl_git_fail(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
	git_tree_entry_free(tentry);
96 97
	git_tree_free(tree);
}
98 99 100 101 102 103 104 105 106 107

void test_index_collision__add_with_highstage_1(void)
{
	git_index_entry entry;

	memset(&entry, 0, sizeof(entry));
	entry.ctime.seconds = 12346789;
	entry.mtime.seconds = 12346789;
	entry.mode  = 0100644;
	entry.file_size = 0;
108
	git_oid_cpy(&entry.id, &g_empty_id);
109 110

	entry.path = "a/b";
111
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
112
	cl_git_pass(git_index_add(g_index, &entry));
113 114 115 116

	/* create a blob beneath the previous tree entry */
	entry.path = "a/b/c";
	entry.flags = 0;
117
	cl_git_pass(git_index_add(g_index, &entry));
118 119 120

	/* create another tree entry above the blob */
	entry.path = "a/b";
121
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
122
	cl_git_pass(git_index_add(g_index, &entry));
123 124 125 126 127 128 129 130 131 132 133
}

void test_index_collision__add_with_highstage_2(void)
{
	git_index_entry entry;

	memset(&entry, 0, sizeof(entry));
	entry.ctime.seconds = 12346789;
	entry.mtime.seconds = 12346789;
	entry.mode  = 0100644;
	entry.file_size = 0;
134
	git_oid_cpy(&entry.id, &g_empty_id);
135 136

	entry.path = "a/b/c";
137
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
138
	cl_git_pass(git_index_add(g_index, &entry));
139 140 141

	/* create a blob beneath the previous tree entry */
	entry.path = "a/b/c";
142
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
143
	cl_git_pass(git_index_add(g_index, &entry));
144 145 146

	/* create another tree entry above the blob */
	entry.path = "a/b";
147
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
148
	cl_git_pass(git_index_add(g_index, &entry));
149
}