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