]> git.proxmox.com Git - libgit2.git/blob - tests/index/collision.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / index / collision.c
1 #include "clar_libgit2.h"
2 #include "git2/repository.h"
3 #include "git2/index.h"
4
5 static git_repository *g_repo;
6 static git_odb *g_odb;
7 static git_index *g_index;
8 static git_oid g_empty_id;
9
10 void test_index_collision__initialize(void)
11 {
12 g_repo = cl_git_sandbox_init("empty_standard_repo");
13 cl_git_pass(git_repository_odb(&g_odb, g_repo));
14 cl_git_pass(git_repository_index(&g_index, g_repo));
15
16 cl_git_pass(git_odb_write(&g_empty_id, g_odb, "", 0, GIT_OBJECT_BLOB));
17 }
18
19 void test_index_collision__cleanup(void)
20 {
21 git_index_free(g_index);
22 git_odb_free(g_odb);
23 cl_git_sandbox_cleanup();
24 }
25
26 void test_index_collision__add_blob_with_conflicting_file(void)
27 {
28 git_index_entry entry;
29 git_tree_entry *tentry;
30 git_oid tree_id;
31 git_tree *tree;
32
33 memset(&entry, 0, sizeof(entry));
34 entry.ctime.seconds = 12346789;
35 entry.mtime.seconds = 12346789;
36 entry.mode = 0100644;
37 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 /* Check a/b exists here */
44 cl_git_pass(git_index_write_tree(&tree_id, g_index));
45 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
46 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
47 git_tree_entry_free(tentry);
48 git_tree_free(tree);
49
50 /* create a tree/blob collision */
51 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 cl_git_pass(git_index_write_tree(&tree_id, g_index));
56 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
57 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
58 git_tree_entry_free(tentry);
59 git_tree_free(tree);
60 }
61
62 void test_index_collision__add_blob_with_conflicting_dir(void)
63 {
64 git_index_entry entry;
65 git_tree_entry *tentry;
66 git_oid tree_id;
67 git_tree *tree;
68
69 memset(&entry, 0, sizeof(entry));
70 entry.ctime.seconds = 12346789;
71 entry.mtime.seconds = 12346789;
72 entry.mode = 0100644;
73 entry.file_size = 0;
74 git_oid_cpy(&entry.id, &g_empty_id);
75
76 entry.path = "a/b/c";
77 cl_git_pass(git_index_add(g_index, &entry));
78
79 /* Check a/b/c exists here */
80 cl_git_pass(git_index_write_tree(&tree_id, g_index));
81 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
82 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
83 git_tree_entry_free(tentry);
84 git_tree_free(tree);
85
86 /* create a blob/tree collision */
87 entry.path = "a/b";
88 cl_git_pass(git_index_add(g_index, &entry));
89
90 /* a/b should now be a tree and a/b/c a blob */
91 cl_git_pass(git_index_write_tree(&tree_id, g_index));
92 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
93 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
94 cl_git_fail(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
95 git_tree_entry_free(tentry);
96 git_tree_free(tree);
97 }
98
99 void test_index_collision__add_with_highstage_1(void)
100 {
101 git_index_entry entry;
102
103 memset(&entry, 0, sizeof(entry));
104 entry.ctime.seconds = 12346789;
105 entry.mtime.seconds = 12346789;
106 entry.mode = 0100644;
107 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 /* create a blob beneath the previous tree entry */
115 entry.path = "a/b/c";
116 entry.flags = 0;
117 cl_git_pass(git_index_add(g_index, &entry));
118
119 /* create another tree entry above the blob */
120 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 void test_index_collision__add_with_highstage_2(void)
126 {
127 git_index_entry entry;
128
129 memset(&entry, 0, sizeof(entry));
130 entry.ctime.seconds = 12346789;
131 entry.mtime.seconds = 12346789;
132 entry.mode = 0100644;
133 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 /* create a blob beneath the previous tree entry */
141 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 /* create another tree entry above the blob */
146 entry.path = "a/b";
147 GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
148 cl_git_pass(git_index_add(g_index, &entry));
149 }