]>
Commit | Line | Data |
---|---|---|
bec65a5e ET |
1 | #include "clar_libgit2.h" |
2 | #include "git2/repository.h" | |
3 | #include "git2/merge.h" | |
4 | #include "merge.h" | |
5 | #include "../merge_helpers.h" | |
6 | #include "refs.h" | |
22a2d3d5 | 7 | #include "futils.h" |
75d1c8c6 | 8 | #include "git2/sys/index.h" |
bec65a5e ET |
9 | |
10 | static git_repository *repo; | |
11 | ||
12 | #define TEST_REPO_PATH "merge-resolve" | |
13 | #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" | |
14 | ||
15 | ||
ac3d33df | 16 | /* Fixture setup and teardown */ |
bec65a5e ET |
17 | void test_merge_trees_trivial__initialize(void) |
18 | { | |
19 | repo = cl_git_sandbox_init(TEST_REPO_PATH); | |
20 | } | |
21 | ||
22 | void test_merge_trees_trivial__cleanup(void) | |
23 | { | |
24 | cl_git_sandbox_cleanup(); | |
25 | } | |
26 | ||
27 | ||
0e1ba46c | 28 | static int merge_trivial(git_index **index, const char *ours, const char *theirs) |
bec65a5e ET |
29 | { |
30 | git_commit *our_commit, *their_commit, *ancestor_commit; | |
31 | git_tree *our_tree, *their_tree, *ancestor_tree; | |
32 | git_oid our_oid, their_oid, ancestor_oid; | |
e579e0f7 | 33 | git_str branch_buf = GIT_STR_INIT; |
5aa2ac6d | 34 | git_merge_options opts = GIT_MERGE_OPTIONS_INIT; |
bec65a5e | 35 | |
e579e0f7 | 36 | git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours); |
bec65a5e ET |
37 | cl_git_pass(git_reference_name_to_id(&our_oid, repo, branch_buf.ptr)); |
38 | cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); | |
39 | ||
e579e0f7 MB |
40 | git_str_clear(&branch_buf); |
41 | git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs); | |
bec65a5e ET |
42 | cl_git_pass(git_reference_name_to_id(&their_oid, repo, branch_buf.ptr)); |
43 | cl_git_pass(git_commit_lookup(&their_commit, repo, &their_oid)); | |
44 | ||
45 | cl_git_pass(git_merge_base(&ancestor_oid, repo, git_commit_id(our_commit), git_commit_id(their_commit))); | |
46 | cl_git_pass(git_commit_lookup(&ancestor_commit, repo, &ancestor_oid)); | |
1fed6b07 | 47 | |
bec65a5e ET |
48 | cl_git_pass(git_commit_tree(&ancestor_tree, ancestor_commit)); |
49 | cl_git_pass(git_commit_tree(&our_tree, our_commit)); | |
50 | cl_git_pass(git_commit_tree(&their_tree, their_commit)); | |
51 | ||
52 | cl_git_pass(git_merge_trees(index, repo, ancestor_tree, our_tree, their_tree, &opts)); | |
53 | ||
e579e0f7 | 54 | git_str_dispose(&branch_buf); |
bec65a5e ET |
55 | git_tree_free(our_tree); |
56 | git_tree_free(their_tree); | |
57 | git_tree_free(ancestor_tree); | |
58 | git_commit_free(our_commit); | |
59 | git_commit_free(their_commit); | |
60 | git_commit_free(ancestor_commit); | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | static int merge_trivial_conflict_entrycount(git_index *index) | |
66 | { | |
67 | const git_index_entry *entry; | |
0f938c6b | 68 | int count = 0; |
bec65a5e ET |
69 | size_t i; |
70 | ||
71 | for (i = 0; i < git_index_entrycount(index); i++) { | |
72 | cl_assert(entry = git_index_get_byindex(index, i)); | |
73 | ||
9f545b9d | 74 | if (git_index_entry_is_conflict(entry)) |
bec65a5e ET |
75 | count++; |
76 | } | |
77 | ||
78 | return count; | |
79 | } | |
80 | ||
81 | /* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote */ | |
82 | void test_merge_trees_trivial__2alt(void) | |
83 | { | |
84 | git_index *result; | |
85 | const git_index_entry *entry; | |
86 | ||
0e1ba46c | 87 | cl_git_pass(merge_trivial(&result, "trivial-2alt", "trivial-2alt-branch")); |
bec65a5e ET |
88 | |
89 | cl_assert(entry = git_index_get_bypath(result, "new-in-branch.txt", 0)); | |
90 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
91 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
92 | ||
93 | git_index_free(result); | |
94 | } | |
95 | ||
96 | /* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head */ | |
97 | void test_merge_trees_trivial__3alt(void) | |
98 | { | |
99 | git_index *result; | |
100 | const git_index_entry *entry; | |
101 | ||
0e1ba46c | 102 | cl_git_pass(merge_trivial(&result, "trivial-3alt", "trivial-3alt-branch")); |
bec65a5e ET |
103 | |
104 | cl_assert(entry = git_index_get_bypath(result, "new-in-3alt.txt", 0)); | |
105 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
106 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 107 | |
bec65a5e ET |
108 | git_index_free(result); |
109 | } | |
110 | ||
111 | /* 4: ancest:(empty)^, head:head, remote:remote = result:no merge */ | |
112 | void test_merge_trees_trivial__4(void) | |
113 | { | |
114 | git_index *result; | |
115 | const git_index_entry *entry; | |
116 | ||
0e1ba46c | 117 | cl_git_pass(merge_trivial(&result, "trivial-4", "trivial-4-branch")); |
bec65a5e ET |
118 | |
119 | cl_assert((entry = git_index_get_bypath(result, "new-and-different.txt", 0)) == NULL); | |
120 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
121 | ||
122 | cl_assert(merge_trivial_conflict_entrycount(result) == 2); | |
123 | cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 2)); | |
124 | cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 3)); | |
1fed6b07 | 125 | |
bec65a5e ET |
126 | git_index_free(result); |
127 | } | |
128 | ||
129 | /* 5ALT: ancest:*, head:head, remote:head = result:head */ | |
130 | void test_merge_trees_trivial__5alt_1(void) | |
131 | { | |
132 | git_index *result; | |
133 | const git_index_entry *entry; | |
134 | ||
0e1ba46c | 135 | cl_git_pass(merge_trivial(&result, "trivial-5alt-1", "trivial-5alt-1-branch")); |
bec65a5e ET |
136 | |
137 | cl_assert(entry = git_index_get_bypath(result, "new-and-same.txt", 0)); | |
138 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
139 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 140 | |
bec65a5e ET |
141 | git_index_free(result); |
142 | } | |
143 | ||
144 | /* 5ALT: ancest:*, head:head, remote:head = result:head */ | |
145 | void test_merge_trees_trivial__5alt_2(void) | |
146 | { | |
147 | git_index *result; | |
148 | const git_index_entry *entry; | |
149 | ||
0e1ba46c | 150 | cl_git_pass(merge_trivial(&result, "trivial-5alt-2", "trivial-5alt-2-branch")); |
bec65a5e ET |
151 | |
152 | cl_assert(entry = git_index_get_bypath(result, "modified-to-same.txt", 0)); | |
153 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
154 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 155 | |
bec65a5e ET |
156 | git_index_free(result); |
157 | } | |
158 | ||
159 | /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */ | |
160 | void test_merge_trees_trivial__6(void) | |
bec65a5e ET |
161 | { |
162 | git_index *result; | |
163 | const git_index_entry *entry; | |
164 | const git_index_reuc_entry *reuc; | |
165 | ||
0e1ba46c | 166 | cl_git_pass(merge_trivial(&result, "trivial-6", "trivial-6-branch")); |
bec65a5e ET |
167 | |
168 | cl_assert((entry = git_index_get_bypath(result, "removed-in-both.txt", 0)) == NULL); | |
169 | cl_assert(git_index_reuc_entrycount(result) == 1); | |
170 | cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-both.txt")); | |
171 | ||
172 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 173 | |
bec65a5e ET |
174 | git_index_free(result); |
175 | } | |
176 | ||
177 | /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */ | |
178 | void test_merge_trees_trivial__8(void) | |
bec65a5e ET |
179 | { |
180 | git_index *result; | |
181 | const git_index_entry *entry; | |
182 | const git_index_reuc_entry *reuc; | |
183 | ||
0e1ba46c | 184 | cl_git_pass(merge_trivial(&result, "trivial-8", "trivial-8-branch")); |
bec65a5e ET |
185 | |
186 | cl_assert((entry = git_index_get_bypath(result, "removed-in-8.txt", 0)) == NULL); | |
187 | ||
188 | cl_assert(git_index_reuc_entrycount(result) == 1); | |
189 | cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-8.txt")); | |
190 | ||
191 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 192 | |
bec65a5e ET |
193 | git_index_free(result); |
194 | } | |
195 | ||
196 | /* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge */ | |
197 | void test_merge_trees_trivial__7(void) | |
198 | { | |
199 | git_index *result; | |
200 | const git_index_entry *entry; | |
201 | ||
0e1ba46c | 202 | cl_git_pass(merge_trivial(&result, "trivial-7", "trivial-7-branch")); |
bec65a5e ET |
203 | |
204 | cl_assert((entry = git_index_get_bypath(result, "removed-in-7.txt", 0)) == NULL); | |
205 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
206 | ||
207 | cl_assert(merge_trivial_conflict_entrycount(result) == 2); | |
208 | cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 1)); | |
209 | cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 3)); | |
1fed6b07 | 210 | |
bec65a5e ET |
211 | git_index_free(result); |
212 | } | |
213 | ||
214 | /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */ | |
215 | void test_merge_trees_trivial__10(void) | |
bec65a5e ET |
216 | { |
217 | git_index *result; | |
218 | const git_index_entry *entry; | |
219 | const git_index_reuc_entry *reuc; | |
220 | ||
0e1ba46c | 221 | cl_git_pass(merge_trivial(&result, "trivial-10", "trivial-10-branch")); |
bec65a5e ET |
222 | |
223 | cl_assert((entry = git_index_get_bypath(result, "removed-in-10-branch.txt", 0)) == NULL); | |
224 | ||
225 | cl_assert(git_index_reuc_entrycount(result) == 1); | |
226 | cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-10-branch.txt")); | |
227 | ||
228 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 229 | |
bec65a5e ET |
230 | git_index_free(result); |
231 | } | |
232 | ||
233 | /* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge */ | |
234 | void test_merge_trees_trivial__9(void) | |
235 | { | |
236 | git_index *result; | |
237 | const git_index_entry *entry; | |
238 | ||
0e1ba46c | 239 | cl_git_pass(merge_trivial(&result, "trivial-9", "trivial-9-branch")); |
bec65a5e ET |
240 | |
241 | cl_assert((entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 0)) == NULL); | |
242 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
243 | ||
244 | cl_assert(merge_trivial_conflict_entrycount(result) == 2); | |
245 | cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 1)); | |
246 | cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 2)); | |
1fed6b07 | 247 | |
bec65a5e ET |
248 | git_index_free(result); |
249 | } | |
250 | ||
251 | /* 13: ancest:ancest+, head:head, remote:ancest = result:head */ | |
252 | void test_merge_trees_trivial__13(void) | |
253 | { | |
254 | git_index *result; | |
255 | const git_index_entry *entry; | |
256 | git_oid expected_oid; | |
257 | ||
0e1ba46c | 258 | cl_git_pass(merge_trivial(&result, "trivial-13", "trivial-13-branch")); |
bec65a5e ET |
259 | |
260 | cl_assert(entry = git_index_get_bypath(result, "modified-in-13.txt", 0)); | |
261 | cl_git_pass(git_oid_fromstr(&expected_oid, "1cff9ec6a47a537380dedfdd17c9e76d74259a2b")); | |
0cee70eb | 262 | cl_assert_equal_oid(&expected_oid, &entry->id); |
bec65a5e ET |
263 | |
264 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
265 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 266 | |
bec65a5e ET |
267 | git_index_free(result); |
268 | } | |
269 | ||
270 | /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */ | |
271 | void test_merge_trees_trivial__14(void) | |
272 | { | |
273 | git_index *result; | |
274 | const git_index_entry *entry; | |
275 | git_oid expected_oid; | |
276 | ||
0e1ba46c | 277 | cl_git_pass(merge_trivial(&result, "trivial-14", "trivial-14-branch")); |
bec65a5e ET |
278 | |
279 | cl_assert(entry = git_index_get_bypath(result, "modified-in-14-branch.txt", 0)); | |
280 | cl_git_pass(git_oid_fromstr(&expected_oid, "26153a3ff3649b6c2bb652d3f06878c6e0a172f9")); | |
d541170c | 281 | cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); |
bec65a5e ET |
282 | |
283 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
284 | cl_assert(merge_trivial_conflict_entrycount(result) == 0); | |
1fed6b07 | 285 | |
bec65a5e ET |
286 | git_index_free(result); |
287 | } | |
288 | ||
289 | /* 11: ancest:ancest+, head:head, remote:remote = result:no merge */ | |
290 | void test_merge_trees_trivial__11(void) | |
291 | { | |
292 | git_index *result; | |
293 | const git_index_entry *entry; | |
294 | ||
0e1ba46c | 295 | cl_git_pass(merge_trivial(&result, "trivial-11", "trivial-11-branch")); |
bec65a5e ET |
296 | |
297 | cl_assert((entry = git_index_get_bypath(result, "modified-in-both.txt", 0)) == NULL); | |
298 | cl_assert(git_index_reuc_entrycount(result) == 0); | |
299 | ||
300 | cl_assert(merge_trivial_conflict_entrycount(result) == 3); | |
301 | cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 1)); | |
302 | cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 2)); | |
303 | cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 3)); | |
1fed6b07 | 304 | |
bec65a5e ET |
305 | git_index_free(result); |
306 | } |