]>
Commit | Line | Data |
---|---|---|
f45ec1a0 ET |
1 | #include "clar_libgit2.h" |
2 | #include "index.h" | |
3 | #include "git2/repository.h" | |
4 | ||
5 | static git_repository *repo; | |
6 | static git_index *repo_index; | |
7 | ||
8 | #define TEST_REPO_PATH "mergedrepo" | |
9 | #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" | |
10 | ||
11 | #define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" | |
12 | #define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399" | |
13 | #define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da" | |
14 | ||
15 | #define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c" | |
16 | #define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2" | |
17 | #define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e" | |
18 | ||
19 | #define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f" | |
20 | #define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b" | |
21 | #define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567" | |
22 | ||
23 | // Fixture setup and teardown | |
24 | void test_index_conflicts__initialize(void) | |
25 | { | |
26 | repo = cl_git_sandbox_init("mergedrepo"); | |
27 | git_repository_index(&repo_index, repo); | |
28 | } | |
29 | ||
30 | void test_index_conflicts__cleanup(void) | |
31 | { | |
32 | git_index_free(repo_index); | |
9094d30b SC |
33 | repo_index = NULL; |
34 | ||
f45ec1a0 ET |
35 | cl_git_sandbox_cleanup(); |
36 | } | |
37 | ||
38 | void test_index_conflicts__add(void) | |
39 | { | |
40 | git_index_entry ancestor_entry, our_entry, their_entry; | |
41 | ||
42 | cl_assert(git_index_entrycount(repo_index) == 8); | |
43 | ||
44 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
45 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
46 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
47 | ||
48 | ancestor_entry.path = "test-one.txt"; | |
49 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 50 | git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); |
f45ec1a0 ET |
51 | |
52 | our_entry.path = "test-one.txt"; | |
53 | ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 54 | git_oid_fromstr(&our_entry.id, TEST_OUR_OID); |
f45ec1a0 ET |
55 | |
56 | their_entry.path = "test-one.txt"; | |
57 | ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 58 | git_oid_fromstr(&their_entry.id, TEST_THEIR_OID); |
f45ec1a0 ET |
59 | |
60 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); | |
61 | ||
62 | cl_assert(git_index_entrycount(repo_index) == 11); | |
63 | } | |
64 | ||
65 | void test_index_conflicts__add_fixes_incorrect_stage(void) | |
66 | { | |
67 | git_index_entry ancestor_entry, our_entry, their_entry; | |
0e0108f7 | 68 | const git_index_entry *conflict_entry[3]; |
f45ec1a0 ET |
69 | |
70 | cl_assert(git_index_entrycount(repo_index) == 8); | |
71 | ||
72 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
73 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
74 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
75 | ||
76 | ancestor_entry.path = "test-one.txt"; | |
77 | ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 78 | git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); |
f45ec1a0 ET |
79 | |
80 | our_entry.path = "test-one.txt"; | |
81 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 82 | git_oid_fromstr(&our_entry.id, TEST_OUR_OID); |
f45ec1a0 ET |
83 | |
84 | their_entry.path = "test-one.txt"; | |
85 | ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 86 | git_oid_fromstr(&their_entry.id, TEST_THEIR_OID); |
f45ec1a0 ET |
87 | |
88 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); | |
89 | ||
90 | cl_assert(git_index_entrycount(repo_index) == 11); | |
91 | ||
92 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt")); | |
93 | ||
94 | cl_assert(git_index_entry_stage(conflict_entry[0]) == 1); | |
95 | cl_assert(git_index_entry_stage(conflict_entry[1]) == 2); | |
96 | cl_assert(git_index_entry_stage(conflict_entry[2]) == 3); | |
97 | } | |
98 | ||
99 | void test_index_conflicts__get(void) | |
100 | { | |
0e0108f7 | 101 | const git_index_entry *conflict_entry[3]; |
f45ec1a0 ET |
102 | git_oid oid; |
103 | ||
104 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
105 | &conflict_entry[2], repo_index, "conflicts-one.txt")); | |
106 | ||
a7f8065f | 107 | cl_assert_equal_s("conflicts-one.txt", conflict_entry[0]->path); |
f45ec1a0 ET |
108 | |
109 | git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID); | |
d541170c | 110 | cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0); |
f45ec1a0 ET |
111 | |
112 | git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID); | |
d541170c | 113 | cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0); |
f45ec1a0 ET |
114 | |
115 | git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID); | |
d541170c | 116 | cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0); |
f45ec1a0 ET |
117 | |
118 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
119 | &conflict_entry[2], repo_index, "conflicts-two.txt")); | |
120 | ||
a7f8065f | 121 | cl_assert_equal_s("conflicts-two.txt", conflict_entry[0]->path); |
f45ec1a0 ET |
122 | |
123 | git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID); | |
d541170c | 124 | cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0); |
f45ec1a0 ET |
125 | |
126 | git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID); | |
d541170c | 127 | cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0); |
f45ec1a0 ET |
128 | |
129 | git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID); | |
d541170c | 130 | cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0); |
f45ec1a0 ET |
131 | } |
132 | ||
0e0108f7 ET |
133 | void test_index_conflicts__iterate(void) |
134 | { | |
135 | git_index_conflict_iterator *iterator; | |
136 | const git_index_entry *conflict_entry[3]; | |
137 | git_oid oid; | |
138 | ||
139 | cl_git_pass(git_index_conflict_iterator_new(&iterator, repo_index)); | |
140 | ||
141 | cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator)); | |
142 | ||
143 | git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID); | |
d541170c | 144 | cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0); |
0e0108f7 ET |
145 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0); |
146 | ||
147 | git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID); | |
d541170c | 148 | cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0); |
0e0108f7 ET |
149 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0); |
150 | ||
151 | git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID); | |
d541170c | 152 | cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0); |
0e0108f7 ET |
153 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0); |
154 | ||
155 | cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator)); | |
156 | ||
157 | git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID); | |
d541170c | 158 | cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0); |
0e0108f7 ET |
159 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0); |
160 | ||
161 | git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID); | |
d541170c | 162 | cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0); |
0e0108f7 ET |
163 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0); |
164 | ||
165 | git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID); | |
d541170c | 166 | cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0); |
0e0108f7 ET |
167 | cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0); |
168 | ||
169 | cl_assert(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator) == GIT_ITEROVER); | |
170 | ||
171 | cl_assert(conflict_entry[0] == NULL); | |
172 | cl_assert(conflict_entry[2] == NULL); | |
173 | cl_assert(conflict_entry[2] == NULL); | |
174 | ||
175 | git_index_conflict_iterator_free(iterator); | |
176 | } | |
177 | ||
f45ec1a0 ET |
178 | void test_index_conflicts__remove(void) |
179 | { | |
f45d51ff | 180 | const git_index_entry *entry; |
f45ec1a0 | 181 | size_t i; |
a8122b5d | 182 | |
f45ec1a0 ET |
183 | cl_assert(git_index_entrycount(repo_index) == 8); |
184 | ||
185 | cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt")); | |
186 | cl_assert(git_index_entrycount(repo_index) == 5); | |
187 | ||
188 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
189 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
190 | cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0); | |
191 | } | |
192 | ||
193 | cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt")); | |
194 | cl_assert(git_index_entrycount(repo_index) == 2); | |
195 | ||
196 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
197 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
198 | cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0); | |
199 | } | |
200 | } | |
201 | ||
25743bd7 | 202 | void test_index_conflicts__moved_to_reuc_on_add(void) |
f45ec1a0 | 203 | { |
f45d51ff | 204 | const git_index_entry *entry; |
f45ec1a0 ET |
205 | size_t i; |
206 | ||
207 | cl_assert(git_index_entrycount(repo_index) == 8); | |
208 | ||
209 | cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n"); | |
210 | ||
25743bd7 | 211 | cl_git_pass(git_index_add_bypath(repo_index, "conflicts-one.txt")); |
f45ec1a0 ET |
212 | |
213 | cl_assert(git_index_entrycount(repo_index) == 6); | |
214 | ||
215 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
216 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
217 | ||
218 | if (strcmp(entry->path, "conflicts-one.txt") == 0) | |
219 | cl_assert(git_index_entry_stage(entry) == 0); | |
220 | } | |
221 | } | |
222 | ||
25743bd7 ET |
223 | void test_index_conflicts__moved_to_reuc_on_remove(void) |
224 | { | |
225 | const git_index_entry *entry; | |
226 | size_t i; | |
227 | ||
228 | cl_assert(git_index_entrycount(repo_index) == 8); | |
229 | ||
230 | cl_git_pass(p_unlink("./mergedrepo/conflicts-one.txt")); | |
231 | ||
232 | cl_git_pass(git_index_remove_bypath(repo_index, "conflicts-one.txt")); | |
233 | ||
234 | cl_assert(git_index_entrycount(repo_index) == 5); | |
235 | ||
236 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
237 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
238 | cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0); | |
239 | } | |
240 | } | |
241 | ||
f45ec1a0 ET |
242 | void test_index_conflicts__remove_all_conflicts(void) |
243 | { | |
244 | size_t i; | |
f45d51ff | 245 | const git_index_entry *entry; |
f45ec1a0 ET |
246 | |
247 | cl_assert(git_index_entrycount(repo_index) == 8); | |
248 | ||
7cc1bf0f | 249 | cl_assert_equal_i(true, git_index_has_conflicts(repo_index)); |
250 | ||
f45ec1a0 ET |
251 | git_index_conflict_cleanup(repo_index); |
252 | ||
7cc1bf0f | 253 | cl_assert_equal_i(false, git_index_has_conflicts(repo_index)); |
254 | ||
f45ec1a0 ET |
255 | cl_assert(git_index_entrycount(repo_index) == 2); |
256 | ||
257 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
258 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
259 | cl_assert(git_index_entry_stage(entry) == 0); | |
260 | } | |
261 | } | |
262 | ||
263 | void test_index_conflicts__partial(void) | |
264 | { | |
265 | git_index_entry ancestor_entry, our_entry, their_entry; | |
0e0108f7 | 266 | const git_index_entry *conflict_entry[3]; |
f45ec1a0 ET |
267 | |
268 | cl_assert(git_index_entrycount(repo_index) == 8); | |
269 | ||
270 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
271 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
272 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
273 | ||
274 | ancestor_entry.path = "test-one.txt"; | |
275 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
d541170c | 276 | git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); |
f45ec1a0 ET |
277 | |
278 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL)); | |
279 | cl_assert(git_index_entrycount(repo_index) == 9); | |
280 | ||
281 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
282 | &conflict_entry[2], repo_index, "test-one.txt")); | |
283 | ||
d541170c | 284 | cl_assert(git_oid_cmp(&ancestor_entry.id, &conflict_entry[0]->id) == 0); |
f45ec1a0 ET |
285 | cl_assert(conflict_entry[1] == NULL); |
286 | cl_assert(conflict_entry[2] == NULL); | |
287 | } |