]> git.proxmox.com Git - libgit2.git/blob - tests/merge/workdir/setup.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / merge / workdir / setup.c
1 #include "clar_libgit2.h"
2 #include "git2/repository.h"
3 #include "git2/merge.h"
4 #include "merge.h"
5 #include "refs.h"
6 #include "futils.h"
7
8 static git_repository *repo;
9 static git_index *repo_index;
10
11 #define TEST_REPO_PATH "merge-resolve"
12 #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
13
14 #define ORIG_HEAD "bd593285fc7fe4ca18ccdbabf027f5d689101452"
15
16 #define THEIRS_SIMPLE_BRANCH "branch"
17 #define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520"
18
19 #define OCTO1_BRANCH "octo1"
20 #define OCTO1_OID "16f825815cfd20a07a75c71554e82d8eede0b061"
21
22 #define OCTO2_BRANCH "octo2"
23 #define OCTO2_OID "158dc7bedb202f5b26502bf3574faa7f4238d56c"
24
25 #define OCTO3_BRANCH "octo3"
26 #define OCTO3_OID "50ce7d7d01217679e26c55939eef119e0c93e272"
27
28 #define OCTO4_BRANCH "octo4"
29 #define OCTO4_OID "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae"
30
31 #define OCTO5_BRANCH "octo5"
32 #define OCTO5_OID "e4f618a2c3ed0669308735727df5ebf2447f022f"
33
34 /* Fixture setup and teardown */
35 void test_merge_workdir_setup__initialize(void)
36 {
37 repo = cl_git_sandbox_init(TEST_REPO_PATH);
38 git_repository_index(&repo_index, repo);
39 }
40
41 void test_merge_workdir_setup__cleanup(void)
42 {
43 git_index_free(repo_index);
44 cl_git_sandbox_cleanup();
45 }
46
47 static bool test_file_contents(const char *filename, const char *expected)
48 {
49 git_buf file_path_buf = GIT_BUF_INIT, file_buf = GIT_BUF_INIT;
50 bool equals;
51
52 git_buf_joinpath(&file_path_buf, git_repository_path(repo), filename);
53
54 cl_git_pass(git_futils_readbuffer(&file_buf, file_path_buf.ptr));
55 equals = (strcmp(file_buf.ptr, expected) == 0);
56
57 git_buf_dispose(&file_path_buf);
58 git_buf_dispose(&file_buf);
59
60 return equals;
61 }
62
63 static void write_file_contents(const char *filename, const char *output)
64 {
65 git_buf file_path_buf = GIT_BUF_INIT;
66
67 git_buf_joinpath(&file_path_buf, git_repository_path(repo),
68 filename);
69 cl_git_rewritefile(file_path_buf.ptr, output);
70
71 git_buf_dispose(&file_path_buf);
72 }
73
74 /* git merge --no-ff octo1 */
75 void test_merge_workdir_setup__one_branch(void)
76 {
77 git_oid our_oid;
78 git_reference *octo1_ref;
79 git_annotated_commit *our_head, *their_heads[1];
80
81 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
82 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
83
84 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
85 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
86
87 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
88
89 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
90 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
91 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
92 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n"));
93
94 git_reference_free(octo1_ref);
95
96 git_annotated_commit_free(our_head);
97 git_annotated_commit_free(their_heads[0]);
98 }
99
100 /* git merge --no-ff 16f825815cfd20a07a75c71554e82d8eede0b061 */
101 void test_merge_workdir_setup__one_oid(void)
102 {
103 git_oid our_oid;
104 git_oid octo1_oid;
105 git_annotated_commit *our_head, *their_heads[1];
106
107 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
108 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
109
110 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
111 cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
112
113 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
114
115 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
116 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
117 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
118 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'\n"));
119
120 git_annotated_commit_free(our_head);
121 git_annotated_commit_free(their_heads[0]);
122 }
123
124 /* git merge octo1 octo2 */
125 void test_merge_workdir_setup__two_branches(void)
126 {
127 git_oid our_oid;
128 git_reference *octo1_ref;
129 git_reference *octo2_ref;
130 git_annotated_commit *our_head, *their_heads[2];
131
132 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
133 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
134
135 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
136 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
137
138 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
139 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
140
141 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
142
143 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
144 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
145 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
146 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "'\n"));
147
148 git_reference_free(octo1_ref);
149 git_reference_free(octo2_ref);
150
151 git_annotated_commit_free(our_head);
152 git_annotated_commit_free(their_heads[0]);
153 git_annotated_commit_free(their_heads[1]);
154 }
155
156 /* git merge octo1 octo2 octo3 */
157 void test_merge_workdir_setup__three_branches(void)
158 {
159 git_oid our_oid;
160 git_reference *octo1_ref;
161 git_reference *octo2_ref;
162 git_reference *octo3_ref;
163 git_annotated_commit *our_head, *their_heads[3];
164
165 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
166 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
167
168 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
169 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
170
171 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
172 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
173
174 cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
175 cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref));
176
177 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
178
179 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
180 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
181 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
182 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "'\n"));
183
184 git_reference_free(octo1_ref);
185 git_reference_free(octo2_ref);
186 git_reference_free(octo3_ref);
187
188 git_annotated_commit_free(our_head);
189 git_annotated_commit_free(their_heads[0]);
190 git_annotated_commit_free(their_heads[1]);
191 git_annotated_commit_free(their_heads[2]);
192 }
193
194 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 158dc7bedb202f5b26502bf3574faa7f4238d56c 50ce7d7d01217679e26c55939eef119e0c93e272 */
195 void test_merge_workdir_setup__three_oids(void)
196 {
197 git_oid our_oid;
198 git_oid octo1_oid;
199 git_oid octo2_oid;
200 git_oid octo3_oid;
201 git_annotated_commit *our_head, *their_heads[3];
202
203 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
204 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
205
206 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
207 cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
208
209 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
210 cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid));
211
212 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
213 cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid));
214
215 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
216
217 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
218 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
219 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
220 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO2_OID "'; commit '" OCTO3_OID "'\n"));
221
222 git_annotated_commit_free(our_head);
223 git_annotated_commit_free(their_heads[0]);
224 git_annotated_commit_free(their_heads[1]);
225 git_annotated_commit_free(their_heads[2]);
226 }
227
228 /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c */
229 void test_merge_workdir_setup__branches_and_oids_1(void)
230 {
231 git_oid our_oid;
232 git_reference *octo1_ref;
233 git_oid octo2_oid;
234 git_annotated_commit *our_head, *their_heads[2];
235
236 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
237 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
238
239 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
240 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
241
242 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
243 cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid));
244
245 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
246
247 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
248 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
249 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
250 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'; commit '" OCTO2_OID "'\n"));
251
252 git_reference_free(octo1_ref);
253
254 git_annotated_commit_free(our_head);
255 git_annotated_commit_free(their_heads[0]);
256 git_annotated_commit_free(their_heads[1]);
257 }
258
259 /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c octo3 54269b3f6ec3d7d4ede24dd350dd5d605495c3ae */
260 void test_merge_workdir_setup__branches_and_oids_2(void)
261 {
262 git_oid our_oid;
263 git_reference *octo1_ref;
264 git_oid octo2_oid;
265 git_reference *octo3_ref;
266 git_oid octo4_oid;
267 git_annotated_commit *our_head, *their_heads[4];
268
269 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
270 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
271
272 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
273 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
274
275 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
276 cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid));
277
278 cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
279 cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref));
280
281 cl_git_pass(git_oid_fromstr(&octo4_oid, OCTO4_OID));
282 cl_git_pass(git_annotated_commit_lookup(&their_heads[3], repo, &octo4_oid));
283
284 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
285
286 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
287 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
288 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
289 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "'; commit '" OCTO2_OID "'; commit '" OCTO4_OID "'\n"));
290
291 git_reference_free(octo1_ref);
292 git_reference_free(octo3_ref);
293
294 git_annotated_commit_free(our_head);
295 git_annotated_commit_free(their_heads[0]);
296 git_annotated_commit_free(their_heads[1]);
297 git_annotated_commit_free(their_heads[2]);
298 git_annotated_commit_free(their_heads[3]);
299 }
300
301 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 */
302 void test_merge_workdir_setup__branches_and_oids_3(void)
303 {
304 git_oid our_oid;
305 git_oid octo1_oid;
306 git_reference *octo2_ref;
307 git_oid octo3_oid;
308 git_reference *octo4_ref;
309 git_annotated_commit *our_head, *their_heads[4];
310
311 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
312 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
313
314 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
315 cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
316
317 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
318 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
319
320 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
321 cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid));
322
323 cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH));
324 cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref));
325
326 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
327
328 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
329 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
330 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
331 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "'; commit '" OCTO3_OID "'\n"));
332
333 git_reference_free(octo2_ref);
334 git_reference_free(octo4_ref);
335
336 git_annotated_commit_free(our_head);
337 git_annotated_commit_free(their_heads[0]);
338 git_annotated_commit_free(their_heads[1]);
339 git_annotated_commit_free(their_heads[2]);
340 git_annotated_commit_free(their_heads[3]);
341 }
342
343 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 octo5 */
344 void test_merge_workdir_setup__branches_and_oids_4(void)
345 {
346 git_oid our_oid;
347 git_oid octo1_oid;
348 git_reference *octo2_ref;
349 git_oid octo3_oid;
350 git_reference *octo4_ref;
351 git_reference *octo5_ref;
352 git_annotated_commit *our_head, *their_heads[5];
353
354 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
355 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
356
357 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
358 cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
359
360 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
361 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
362
363 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
364 cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid));
365
366 cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH));
367 cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref));
368
369 cl_git_pass(git_reference_lookup(&octo5_ref, repo, GIT_REFS_HEADS_DIR OCTO5_BRANCH));
370 cl_git_pass(git_annotated_commit_from_ref(&their_heads[4], repo, octo5_ref));
371
372 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 5));
373
374 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n" OCTO5_OID "\n"));
375 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
376 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
377 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "', '" OCTO4_BRANCH "' and '" OCTO5_BRANCH "'; commit '" OCTO3_OID "'\n"));
378
379 git_reference_free(octo2_ref);
380 git_reference_free(octo4_ref);
381 git_reference_free(octo5_ref);
382
383 git_annotated_commit_free(our_head);
384 git_annotated_commit_free(their_heads[0]);
385 git_annotated_commit_free(their_heads[1]);
386 git_annotated_commit_free(their_heads[2]);
387 git_annotated_commit_free(their_heads[3]);
388 git_annotated_commit_free(their_heads[4]);
389 }
390
391 /* git merge octo1 octo1 octo1 */
392 void test_merge_workdir_setup__three_same_branches(void)
393 {
394 git_oid our_oid;
395 git_reference *octo1_1_ref;
396 git_reference *octo1_2_ref;
397 git_reference *octo1_3_ref;
398 git_annotated_commit *our_head, *their_heads[3];
399
400 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
401 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
402
403 cl_git_pass(git_reference_lookup(&octo1_1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
404 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_1_ref));
405
406 cl_git_pass(git_reference_lookup(&octo1_2_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
407 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo1_2_ref));
408
409 cl_git_pass(git_reference_lookup(&octo1_3_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
410 cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo1_3_ref));
411
412 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
413
414 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n"));
415 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
416 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
417 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO1_BRANCH "' and '" OCTO1_BRANCH "'\n"));
418
419 git_reference_free(octo1_1_ref);
420 git_reference_free(octo1_2_ref);
421 git_reference_free(octo1_3_ref);
422
423 git_annotated_commit_free(our_head);
424 git_annotated_commit_free(their_heads[0]);
425 git_annotated_commit_free(their_heads[1]);
426 git_annotated_commit_free(their_heads[2]);
427 }
428
429 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 */
430 void test_merge_workdir_setup__three_same_oids(void)
431 {
432 git_oid our_oid;
433 git_oid octo1_1_oid;
434 git_oid octo1_2_oid;
435 git_oid octo1_3_oid;
436 git_annotated_commit *our_head, *their_heads[3];
437
438 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
439 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
440
441 cl_git_pass(git_oid_fromstr(&octo1_1_oid, OCTO1_OID));
442 cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_1_oid));
443
444 cl_git_pass(git_oid_fromstr(&octo1_2_oid, OCTO1_OID));
445 cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo1_2_oid));
446
447 cl_git_pass(git_oid_fromstr(&octo1_3_oid, OCTO1_OID));
448 cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo1_3_oid));
449
450 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
451
452 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n"));
453 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
454 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
455 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO1_OID "'; commit '" OCTO1_OID "'\n"));
456
457 git_annotated_commit_free(our_head);
458 git_annotated_commit_free(their_heads[0]);
459 git_annotated_commit_free(their_heads[1]);
460 git_annotated_commit_free(their_heads[2]);
461 }
462
463 static int create_remote_tracking_branch(const char *branch_name, const char *oid_str)
464 {
465 int error = 0;
466
467 git_buf remotes_path = GIT_BUF_INIT,
468 origin_path = GIT_BUF_INIT,
469 filename = GIT_BUF_INIT,
470 data = GIT_BUF_INIT;
471
472 if ((error = git_buf_puts(&remotes_path, git_repository_path(repo))) < 0 ||
473 (error = git_buf_puts(&remotes_path, GIT_REFS_REMOTES_DIR)) < 0)
474 goto done;
475
476 if (!git_path_exists(git_buf_cstr(&remotes_path)) &&
477 (error = p_mkdir(git_buf_cstr(&remotes_path), 0777)) < 0)
478 goto done;
479
480 if ((error = git_buf_puts(&origin_path, git_buf_cstr(&remotes_path))) < 0 ||
481 (error = git_buf_puts(&origin_path, "origin")) < 0)
482 goto done;
483
484 if (!git_path_exists(git_buf_cstr(&origin_path)) &&
485 (error = p_mkdir(git_buf_cstr(&origin_path), 0777)) < 0)
486 goto done;
487
488 if ((error = git_buf_puts(&filename, git_buf_cstr(&origin_path))) < 0 ||
489 (error = git_buf_puts(&filename, "/")) < 0 ||
490 (error = git_buf_puts(&filename, branch_name)) < 0 ||
491 (error = git_buf_puts(&data, oid_str)) < 0 ||
492 (error = git_buf_puts(&data, "\n")) < 0)
493 goto done;
494
495 cl_git_rewritefile(git_buf_cstr(&filename), git_buf_cstr(&data));
496
497 done:
498 git_buf_dispose(&remotes_path);
499 git_buf_dispose(&origin_path);
500 git_buf_dispose(&filename);
501 git_buf_dispose(&data);
502
503 return error;
504 }
505
506 /* git merge refs/remotes/origin/octo1 */
507 void test_merge_workdir_setup__remote_tracking_one_branch(void)
508 {
509 git_oid our_oid;
510 git_reference *octo1_ref;
511 git_annotated_commit *our_head, *their_heads[1];
512
513 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
514
515 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
516 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
517
518 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
519 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
520
521 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
522
523 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
524 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
525 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
526 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
527
528 git_reference_free(octo1_ref);
529
530 git_annotated_commit_free(our_head);
531 git_annotated_commit_free(their_heads[0]);
532 }
533
534 /* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 */
535 void test_merge_workdir_setup__remote_tracking_two_branches(void)
536 {
537 git_oid our_oid;
538 git_reference *octo1_ref;
539 git_reference *octo2_ref;
540 git_annotated_commit *our_head, *their_heads[2];
541
542 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
543 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
544
545 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
546 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
547
548 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
549 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
550
551 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
552 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
553
554 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
555
556 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
557 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
558 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
559 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "' and 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
560
561 git_reference_free(octo1_ref);
562 git_reference_free(octo2_ref);
563
564 git_annotated_commit_free(our_head);
565 git_annotated_commit_free(their_heads[0]);
566 git_annotated_commit_free(their_heads[1]);
567 }
568
569 /* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 refs/remotes/origin/octo3 */
570 void test_merge_workdir_setup__remote_tracking_three_branches(void)
571 {
572 git_oid our_oid;
573 git_reference *octo1_ref;
574 git_reference *octo2_ref;
575 git_reference *octo3_ref;
576 git_annotated_commit *our_head, *their_heads[3];
577
578 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
579 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
580 cl_git_pass(create_remote_tracking_branch(OCTO3_BRANCH, OCTO3_OID));
581
582 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
583 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
584
585 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
586 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
587
588 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
589 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
590
591 cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO3_BRANCH));
592 cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref));
593
594 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
595
596 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
597 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
598 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
599 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "', 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO3_BRANCH "'\n"));
600
601 git_reference_free(octo1_ref);
602 git_reference_free(octo2_ref);
603 git_reference_free(octo3_ref);
604
605 git_annotated_commit_free(our_head);
606 git_annotated_commit_free(their_heads[0]);
607 git_annotated_commit_free(their_heads[1]);
608 git_annotated_commit_free(their_heads[2]);
609 }
610
611 /* git merge octo1 refs/remotes/origin/octo2 */
612 void test_merge_workdir_setup__normal_branch_and_remote_tracking_branch(void)
613 {
614 git_oid our_oid;
615 git_reference *octo1_ref;
616 git_reference *octo2_ref;
617 git_annotated_commit *our_head, *their_heads[2];
618
619 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
620
621 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
622 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
623
624 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
625 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
626
627 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
628 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
629
630 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
631
632 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
633 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
634 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
635 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
636
637 git_reference_free(octo1_ref);
638 git_reference_free(octo2_ref);
639
640 git_annotated_commit_free(our_head);
641 git_annotated_commit_free(their_heads[0]);
642 git_annotated_commit_free(their_heads[1]);
643 }
644
645 /* git merge refs/remotes/origin/octo1 octo2 */
646 void test_merge_workdir_setup__remote_tracking_branch_and_normal_branch(void)
647 {
648 git_oid our_oid;
649 git_reference *octo1_ref;
650 git_reference *octo2_ref;
651 git_annotated_commit *our_head, *their_heads[2];
652
653 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
654
655 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
656 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
657
658 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
659 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
660
661 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
662 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
663
664 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
665
666 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
667 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
668 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
669 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO2_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
670
671 git_reference_free(octo1_ref);
672 git_reference_free(octo2_ref);
673
674 git_annotated_commit_free(our_head);
675 git_annotated_commit_free(their_heads[0]);
676 git_annotated_commit_free(their_heads[1]);
677 }
678
679 /* git merge octo1 refs/remotes/origin/octo2 octo3 refs/remotes/origin/octo4 */
680 void test_merge_workdir_setup__two_remote_tracking_branch_and_two_normal_branches(void)
681 {
682 git_oid our_oid;
683 git_reference *octo1_ref;
684 git_reference *octo2_ref;
685 git_reference *octo3_ref;
686 git_reference *octo4_ref;
687 git_annotated_commit *our_head, *their_heads[4];
688
689 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
690 cl_git_pass(create_remote_tracking_branch(OCTO4_BRANCH, OCTO4_OID));
691
692 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
693 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
694
695 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
696 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
697
698 cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
699 cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
700
701 cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
702 cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref));
703
704 cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO4_BRANCH));
705 cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref));
706
707 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
708
709 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
710 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
711 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
712 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "', remote-tracking branches 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO4_BRANCH "'\n"));
713
714 git_reference_free(octo1_ref);
715 git_reference_free(octo2_ref);
716 git_reference_free(octo3_ref);
717 git_reference_free(octo4_ref);
718
719 git_annotated_commit_free(our_head);
720 git_annotated_commit_free(their_heads[0]);
721 git_annotated_commit_free(their_heads[1]);
722 git_annotated_commit_free(their_heads[2]);
723 git_annotated_commit_free(their_heads[3]);
724 }
725
726 /* git pull origin branch octo1 */
727 void test_merge_workdir_setup__pull_one(void)
728 {
729 git_oid our_oid;
730 git_oid octo1_1_oid;
731 git_annotated_commit *our_head, *their_heads[1];
732
733 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
734 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
735
736 cl_git_pass(git_oid_fromstr(&octo1_1_oid, OCTO1_OID));
737 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_1_oid));
738
739 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
740
741 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
742 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
743 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
744 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch 'octo1' of http://remote.url/repo.git\n"));
745
746 git_annotated_commit_free(our_head);
747 git_annotated_commit_free(their_heads[0]);
748 }
749
750 /* git pull origin octo1 octo2 */
751 void test_merge_workdir_setup__pull_two(void)
752 {
753 git_oid our_oid;
754 git_oid octo1_oid;
755 git_oid octo2_oid;
756 git_annotated_commit *our_head, *their_heads[2];
757
758 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
759 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
760
761 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
762 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid));
763
764 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
765 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid));
766
767 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
768
769 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
770 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
771 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
772 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "' of http://remote.url/repo.git\n"));
773
774 git_annotated_commit_free(our_head);
775 git_annotated_commit_free(their_heads[0]);
776 git_annotated_commit_free(their_heads[1]);
777 }
778
779 /* git pull origin octo1 octo2 octo3 */
780 void test_merge_workdir_setup__pull_three(void)
781 {
782 git_oid our_oid;
783 git_oid octo1_oid;
784 git_oid octo2_oid;
785 git_oid octo3_oid;
786 git_annotated_commit *our_head, *their_heads[3];
787
788 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
789 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
790
791 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
792 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid));
793
794 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
795 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid));
796
797 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
798 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.url/repo.git", &octo3_oid));
799
800 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
801
802 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
803 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
804 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
805 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.url/repo.git\n"));
806
807 git_annotated_commit_free(our_head);
808 git_annotated_commit_free(their_heads[0]);
809 git_annotated_commit_free(their_heads[1]);
810 git_annotated_commit_free(their_heads[2]);
811 }
812
813 void test_merge_workdir_setup__three_remotes(void)
814 {
815 git_oid our_oid;
816 git_oid octo1_oid;
817 git_oid octo2_oid;
818 git_oid octo3_oid;
819 git_annotated_commit *our_head, *their_heads[3];
820
821 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
822 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
823
824 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
825 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid));
826
827 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
828 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid));
829
830 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
831 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.third/repo.git", &octo3_oid));
832
833 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
834
835 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
836 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
837 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
838 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "' of http://remote.first/repo.git, branch '" OCTO2_BRANCH "' of http://remote.second/repo.git, branch '" OCTO3_BRANCH "' of http://remote.third/repo.git\n"));
839
840 git_annotated_commit_free(our_head);
841 git_annotated_commit_free(their_heads[0]);
842 git_annotated_commit_free(their_heads[1]);
843 git_annotated_commit_free(their_heads[2]);
844 }
845
846 void test_merge_workdir_setup__two_remotes(void)
847 {
848 git_oid our_oid;
849 git_oid octo1_oid;
850 git_oid octo2_oid;
851 git_oid octo3_oid;
852 git_oid octo4_oid;
853 git_annotated_commit *our_head, *their_heads[4];
854
855 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
856 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
857
858 cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
859 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid));
860
861 cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
862 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid));
863
864 cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
865 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.first/repo.git", &octo3_oid));
866
867 cl_git_pass(git_oid_fromstr(&octo4_oid, OCTO4_OID));
868 cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[3], repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH, "http://remote.second/repo.git", &octo4_oid));
869
870 cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
871
872 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
873 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
874 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
875 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.first/repo.git, branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "' of http://remote.second/repo.git\n"));
876
877 git_annotated_commit_free(our_head);
878 git_annotated_commit_free(their_heads[0]);
879 git_annotated_commit_free(their_heads[1]);
880 git_annotated_commit_free(their_heads[2]);
881 git_annotated_commit_free(their_heads[3]);
882 }
883
884 void test_merge_workdir_setup__id_from_head(void)
885 {
886 git_oid expected_id;
887 const git_oid *id;
888 git_reference *ref;
889 git_annotated_commit *heads[3];
890
891 cl_git_pass(git_oid_fromstr(&expected_id, OCTO1_OID));
892 cl_git_pass(git_annotated_commit_from_fetchhead(&heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &expected_id));
893 id = git_annotated_commit_id(heads[0]);
894 cl_assert_equal_i(1, git_oid_equal(id, &expected_id));
895
896 cl_git_pass(git_annotated_commit_lookup(&heads[1], repo, &expected_id));
897 id = git_annotated_commit_id(heads[1]);
898 cl_assert_equal_i(1, git_oid_equal(id, &expected_id));
899
900 cl_git_pass(git_reference_lookup(&ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
901 cl_git_pass(git_annotated_commit_from_ref(&heads[2], repo, ref));
902 id = git_annotated_commit_id(heads[2]);
903 cl_assert_equal_i(1, git_oid_equal(id, &expected_id));
904
905 git_reference_free(ref);
906 git_annotated_commit_free(heads[0]);
907 git_annotated_commit_free(heads[1]);
908 git_annotated_commit_free(heads[2]);
909 }
910
911 struct annotated_commit_cb_data {
912 const char **oid_str;
913 unsigned int len;
914
915 unsigned int i;
916 };
917
918 static int annotated_commit_foreach_cb(const git_oid *oid, void *payload)
919 {
920 git_oid expected_oid;
921 struct annotated_commit_cb_data *cb_data = payload;
922
923 git_oid_fromstr(&expected_oid, cb_data->oid_str[cb_data->i]);
924 cl_assert(git_oid_cmp(&expected_oid, oid) == 0);
925 cb_data->i++;
926 return 0;
927 }
928
929 void test_merge_workdir_setup__head_notfound(void)
930 {
931 int error;
932
933 cl_git_fail((error = git_repository_mergehead_foreach(repo,
934 annotated_commit_foreach_cb, NULL)));
935 cl_assert(error == GIT_ENOTFOUND);
936 }
937
938 void test_merge_workdir_setup__head_invalid_oid(void)
939 {
940 int error;
941
942 write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n");
943
944 cl_git_fail((error = git_repository_mergehead_foreach(repo,
945 annotated_commit_foreach_cb, NULL)));
946 cl_assert(error == -1);
947 }
948
949 void test_merge_workdir_setup__head_foreach_nonewline(void)
950 {
951 int error;
952
953 write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID);
954
955 cl_git_fail((error = git_repository_mergehead_foreach(repo,
956 annotated_commit_foreach_cb, NULL)));
957 cl_assert(error == -1);
958 }
959
960 void test_merge_workdir_setup__head_foreach_one(void)
961 {
962 const char *expected = THEIRS_SIMPLE_OID;
963
964 struct annotated_commit_cb_data cb_data = { &expected, 1 };
965
966 write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n");
967
968 cl_git_pass(git_repository_mergehead_foreach(repo,
969 annotated_commit_foreach_cb, &cb_data));
970
971 cl_assert(cb_data.i == cb_data.len);
972 }
973
974 void test_merge_workdir_setup__head_foreach_octopus(void)
975 {
976 const char *expected[] = { THEIRS_SIMPLE_OID,
977 OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID };
978
979 struct annotated_commit_cb_data cb_data = { expected, 6 };
980
981 write_file_contents(GIT_MERGE_HEAD_FILE,
982 THEIRS_SIMPLE_OID "\n"
983 OCTO1_OID "\n"
984 OCTO2_OID "\n"
985 OCTO3_OID "\n"
986 OCTO4_OID "\n"
987 OCTO5_OID "\n");
988
989 cl_git_pass(git_repository_mergehead_foreach(repo,
990 annotated_commit_foreach_cb, &cb_data));
991
992 cl_assert(cb_data.i == cb_data.len);
993 }
994
995 void test_merge_workdir_setup__retained_after_success(void)
996 {
997 git_oid our_oid;
998 git_reference *octo1_ref;
999 git_annotated_commit *our_head, *their_heads[1];
1000
1001 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
1002 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
1003
1004 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
1005
1006 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
1007
1008 cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1009
1010 cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
1011 cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
1012 cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
1013 cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n"));
1014
1015 git_reference_free(octo1_ref);
1016
1017 git_annotated_commit_free(our_head);
1018 git_annotated_commit_free(their_heads[0]);
1019 }
1020
1021
1022 void test_merge_workdir_setup__removed_after_failure(void)
1023 {
1024 git_oid our_oid;
1025 git_reference *octo1_ref;
1026 git_annotated_commit *our_head, *their_heads[1];
1027
1028 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
1029 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
1030
1031 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
1032 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
1033
1034 cl_git_write2file("merge-resolve/.git/index.lock", "foo\n", 4, O_RDWR|O_CREAT, 0666);
1035
1036 cl_git_fail(git_merge(
1037 repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1038
1039 cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE));
1040 cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE));
1041 cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MSG_FILE));
1042
1043 git_reference_free(octo1_ref);
1044
1045 git_annotated_commit_free(our_head);
1046 git_annotated_commit_free(their_heads[0]);
1047 }
1048
1049 void test_merge_workdir_setup__unlocked_after_success(void)
1050 {
1051 git_oid our_oid;
1052 git_reference *octo1_ref;
1053 git_annotated_commit *our_head, *their_heads[1];
1054
1055 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
1056 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
1057
1058 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
1059 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
1060
1061 cl_git_pass(git_merge(
1062 repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1063
1064 cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
1065
1066 git_reference_free(octo1_ref);
1067
1068 git_annotated_commit_free(our_head);
1069 git_annotated_commit_free(their_heads[0]);
1070 }
1071
1072 void test_merge_workdir_setup__unlocked_after_conflict(void)
1073 {
1074 git_oid our_oid;
1075 git_reference *octo1_ref;
1076 git_annotated_commit *our_head, *their_heads[1];
1077
1078 cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
1079 cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
1080
1081 cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
1082 cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
1083
1084 cl_git_rewritefile("merge-resolve/new-in-octo1.txt",
1085 "Conflicting file!\n\nMerge will fail!\n");
1086
1087 cl_git_fail(git_merge(
1088 repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1089
1090 cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
1091
1092 git_reference_free(octo1_ref);
1093
1094 git_annotated_commit_free(our_head);
1095 git_annotated_commit_free(their_heads[0]);
1096 }