]>
Commit | Line | Data |
---|---|---|
1 | #include "clar_libgit2.h" | |
2 | #include "git2/repository.h" | |
3 | #include "git2/merge.h" | |
4 | #include "git2/sys/index.h" | |
5 | #include "merge.h" | |
6 | #include "../merge_helpers.h" | |
7 | #include "refs.h" | |
8 | #include "futils.h" | |
9 | ||
10 | static git_repository *repo; | |
11 | static git_index *repo_index; | |
12 | ||
13 | #define TEST_REPO_PATH "merge-resolve" | |
14 | #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" | |
15 | ||
16 | ||
17 | /* Fixture setup and teardown */ | |
18 | void test_merge_workdir_trivial__initialize(void) | |
19 | { | |
20 | repo = cl_git_sandbox_init(TEST_REPO_PATH); | |
21 | git_repository_index(&repo_index, repo); | |
22 | } | |
23 | ||
24 | void test_merge_workdir_trivial__cleanup(void) | |
25 | { | |
26 | git_index_free(repo_index); | |
27 | cl_git_sandbox_cleanup(); | |
28 | } | |
29 | ||
30 | ||
31 | static int merge_trivial(const char *ours, const char *theirs) | |
32 | { | |
33 | git_str branch_buf = GIT_STR_INIT; | |
34 | git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; | |
35 | git_reference *our_ref, *their_ref; | |
36 | git_annotated_commit *their_heads[1]; | |
37 | ||
38 | checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
39 | ||
40 | git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours); | |
41 | cl_git_pass(git_reference_symbolic_create(&our_ref, repo, "HEAD", branch_buf.ptr, 1, NULL)); | |
42 | ||
43 | cl_git_pass(git_checkout_head(repo, &checkout_opts)); | |
44 | ||
45 | git_str_clear(&branch_buf); | |
46 | git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs); | |
47 | cl_git_pass(git_reference_lookup(&their_ref, repo, branch_buf.ptr)); | |
48 | cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, their_ref)); | |
49 | ||
50 | cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, NULL, NULL)); | |
51 | ||
52 | git_str_dispose(&branch_buf); | |
53 | git_reference_free(our_ref); | |
54 | git_reference_free(their_ref); | |
55 | git_annotated_commit_free(their_heads[0]); | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
60 | static size_t merge_trivial_conflict_entrycount(void) | |
61 | { | |
62 | const git_index_entry *entry; | |
63 | size_t count = 0; | |
64 | size_t i; | |
65 | ||
66 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
67 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
68 | ||
69 | if (git_index_entry_is_conflict(entry)) | |
70 | count++; | |
71 | } | |
72 | ||
73 | return count; | |
74 | } | |
75 | ||
76 | /* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote */ | |
77 | void test_merge_workdir_trivial__2alt(void) | |
78 | { | |
79 | const git_index_entry *entry; | |
80 | ||
81 | cl_git_pass(merge_trivial("trivial-2alt", "trivial-2alt-branch")); | |
82 | ||
83 | cl_assert(entry = git_index_get_bypath(repo_index, "new-in-branch.txt", 0)); | |
84 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
85 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
86 | } | |
87 | ||
88 | /* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head */ | |
89 | void test_merge_workdir_trivial__3alt(void) | |
90 | { | |
91 | const git_index_entry *entry; | |
92 | ||
93 | cl_git_pass(merge_trivial("trivial-3alt", "trivial-3alt-branch")); | |
94 | ||
95 | cl_assert(entry = git_index_get_bypath(repo_index, "new-in-3alt.txt", 0)); | |
96 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
97 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
98 | } | |
99 | ||
100 | /* 4: ancest:(empty)^, head:head, remote:remote = result:no merge */ | |
101 | void test_merge_workdir_trivial__4(void) | |
102 | { | |
103 | const git_index_entry *entry; | |
104 | ||
105 | cl_git_pass(merge_trivial("trivial-4", "trivial-4-branch")); | |
106 | ||
107 | cl_assert((entry = git_index_get_bypath(repo_index, "new-and-different.txt", 0)) == NULL); | |
108 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
109 | ||
110 | cl_assert(merge_trivial_conflict_entrycount() == 2); | |
111 | cl_assert(entry = git_index_get_bypath(repo_index, "new-and-different.txt", 2)); | |
112 | cl_assert(entry = git_index_get_bypath(repo_index, "new-and-different.txt", 3)); | |
113 | } | |
114 | ||
115 | /* 5ALT: ancest:*, head:head, remote:head = result:head */ | |
116 | void test_merge_workdir_trivial__5alt_1(void) | |
117 | { | |
118 | const git_index_entry *entry; | |
119 | ||
120 | cl_git_pass(merge_trivial("trivial-5alt-1", "trivial-5alt-1-branch")); | |
121 | ||
122 | cl_assert(entry = git_index_get_bypath(repo_index, "new-and-same.txt", 0)); | |
123 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
124 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
125 | } | |
126 | ||
127 | /* 5ALT: ancest:*, head:head, remote:head = result:head */ | |
128 | void test_merge_workdir_trivial__5alt_2(void) | |
129 | { | |
130 | const git_index_entry *entry; | |
131 | ||
132 | cl_git_pass(merge_trivial("trivial-5alt-2", "trivial-5alt-2-branch")); | |
133 | ||
134 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-to-same.txt", 0)); | |
135 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
136 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
137 | } | |
138 | ||
139 | /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */ | |
140 | void test_merge_workdir_trivial__6(void) | |
141 | { | |
142 | const git_index_entry *entry; | |
143 | const git_index_reuc_entry *reuc; | |
144 | ||
145 | cl_git_pass(merge_trivial("trivial-6", "trivial-6-branch")); | |
146 | ||
147 | cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-both.txt", 0)) == NULL); | |
148 | cl_assert(git_index_reuc_entrycount(repo_index) == 1); | |
149 | cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-both.txt")); | |
150 | ||
151 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
152 | } | |
153 | ||
154 | /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */ | |
155 | void test_merge_workdir_trivial__8(void) | |
156 | { | |
157 | const git_index_entry *entry; | |
158 | const git_index_reuc_entry *reuc; | |
159 | ||
160 | cl_git_pass(merge_trivial("trivial-8", "trivial-8-branch")); | |
161 | ||
162 | cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-8.txt", 0)) == NULL); | |
163 | ||
164 | cl_assert(git_index_reuc_entrycount(repo_index) == 1); | |
165 | cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-8.txt")); | |
166 | ||
167 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
168 | } | |
169 | ||
170 | /* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge */ | |
171 | void test_merge_workdir_trivial__7(void) | |
172 | { | |
173 | const git_index_entry *entry; | |
174 | ||
175 | cl_git_pass(merge_trivial("trivial-7", "trivial-7-branch")); | |
176 | ||
177 | cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 0)) == NULL); | |
178 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
179 | ||
180 | cl_assert(merge_trivial_conflict_entrycount() == 2); | |
181 | cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 1)); | |
182 | cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 3)); | |
183 | } | |
184 | ||
185 | /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */ | |
186 | void test_merge_workdir_trivial__10(void) | |
187 | { | |
188 | const git_index_entry *entry; | |
189 | const git_index_reuc_entry *reuc; | |
190 | ||
191 | cl_git_pass(merge_trivial("trivial-10", "trivial-10-branch")); | |
192 | ||
193 | cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-10-branch.txt", 0)) == NULL); | |
194 | ||
195 | cl_assert(git_index_reuc_entrycount(repo_index) == 1); | |
196 | cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-10-branch.txt")); | |
197 | ||
198 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
199 | } | |
200 | ||
201 | /* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge */ | |
202 | void test_merge_workdir_trivial__9(void) | |
203 | { | |
204 | const git_index_entry *entry; | |
205 | ||
206 | cl_git_pass(merge_trivial("trivial-9", "trivial-9-branch")); | |
207 | ||
208 | cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 0)) == NULL); | |
209 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
210 | ||
211 | cl_assert(merge_trivial_conflict_entrycount() == 2); | |
212 | cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 1)); | |
213 | cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 2)); | |
214 | } | |
215 | ||
216 | /* 13: ancest:ancest+, head:head, remote:ancest = result:head */ | |
217 | void test_merge_workdir_trivial__13(void) | |
218 | { | |
219 | const git_index_entry *entry; | |
220 | git_oid expected_oid; | |
221 | ||
222 | cl_git_pass(merge_trivial("trivial-13", "trivial-13-branch")); | |
223 | ||
224 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-13.txt", 0)); | |
225 | cl_git_pass(git_oid_fromstr(&expected_oid, "1cff9ec6a47a537380dedfdd17c9e76d74259a2b")); | |
226 | cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); | |
227 | ||
228 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
229 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
230 | } | |
231 | ||
232 | /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */ | |
233 | void test_merge_workdir_trivial__14(void) | |
234 | { | |
235 | const git_index_entry *entry; | |
236 | git_oid expected_oid; | |
237 | ||
238 | cl_git_pass(merge_trivial("trivial-14", "trivial-14-branch")); | |
239 | ||
240 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-14-branch.txt", 0)); | |
241 | cl_git_pass(git_oid_fromstr(&expected_oid, "26153a3ff3649b6c2bb652d3f06878c6e0a172f9")); | |
242 | cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); | |
243 | ||
244 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
245 | cl_assert(merge_trivial_conflict_entrycount() == 0); | |
246 | } | |
247 | ||
248 | /* 11: ancest:ancest+, head:head, remote:remote = result:no merge */ | |
249 | void test_merge_workdir_trivial__11(void) | |
250 | { | |
251 | const git_index_entry *entry; | |
252 | ||
253 | cl_git_pass(merge_trivial("trivial-11", "trivial-11-branch")); | |
254 | ||
255 | cl_assert((entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 0)) == NULL); | |
256 | cl_assert(git_index_reuc_entrycount(repo_index) == 0); | |
257 | ||
258 | cl_assert(merge_trivial_conflict_entrycount() == 3); | |
259 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 1)); | |
260 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 2)); | |
261 | cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 3)); | |
262 | } |