1 #include "clar_libgit2.h"
2 #include "git2/repository.h"
3 #include "git2/merge.h"
8 static git_repository
*repo
;
9 static git_index
*repo_index
;
11 #define TEST_REPO_PATH "merge-resolve"
12 #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
14 #define ORIG_HEAD "bd593285fc7fe4ca18ccdbabf027f5d689101452"
16 #define THEIRS_SIMPLE_BRANCH "branch"
17 #define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520"
19 #define OCTO1_BRANCH "octo1"
20 #define OCTO1_OID "16f825815cfd20a07a75c71554e82d8eede0b061"
22 #define OCTO2_BRANCH "octo2"
23 #define OCTO2_OID "158dc7bedb202f5b26502bf3574faa7f4238d56c"
25 #define OCTO3_BRANCH "octo3"
26 #define OCTO3_OID "50ce7d7d01217679e26c55939eef119e0c93e272"
28 #define OCTO4_BRANCH "octo4"
29 #define OCTO4_OID "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae"
31 #define OCTO5_BRANCH "octo5"
32 #define OCTO5_OID "e4f618a2c3ed0669308735727df5ebf2447f022f"
34 /* Fixture setup and teardown */
35 void test_merge_workdir_setup__initialize(void)
37 repo
= cl_git_sandbox_init(TEST_REPO_PATH
);
38 git_repository_index(&repo_index
, repo
);
41 void test_merge_workdir_setup__cleanup(void)
43 git_index_free(repo_index
);
44 cl_git_sandbox_cleanup();
47 static bool test_file_contents(const char *filename
, const char *expected
)
49 git_buf file_path_buf
= GIT_BUF_INIT
, file_buf
= GIT_BUF_INIT
;
52 git_buf_joinpath(&file_path_buf
, git_repository_path(repo
), filename
);
54 cl_git_pass(git_futils_readbuffer(&file_buf
, file_path_buf
.ptr
));
55 equals
= (strcmp(file_buf
.ptr
, expected
) == 0);
57 git_buf_dispose(&file_path_buf
);
58 git_buf_dispose(&file_buf
);
63 static void write_file_contents(const char *filename
, const char *output
)
65 git_buf file_path_buf
= GIT_BUF_INIT
;
67 git_buf_joinpath(&file_path_buf
, git_repository_path(repo
),
69 cl_git_rewritefile(file_path_buf
.ptr
, output
);
71 git_buf_dispose(&file_path_buf
);
74 /* git merge --no-ff octo1 */
75 void test_merge_workdir_setup__one_branch(void)
78 git_reference
*octo1_ref
;
79 git_annotated_commit
*our_head
, *their_heads
[1];
81 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
82 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
87 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 1));
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"));
94 git_reference_free(octo1_ref
);
96 git_annotated_commit_free(our_head
);
97 git_annotated_commit_free(their_heads
[0]);
100 /* git merge --no-ff 16f825815cfd20a07a75c71554e82d8eede0b061 */
101 void test_merge_workdir_setup__one_oid(void)
105 git_annotated_commit
*our_head
, *their_heads
[1];
107 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
108 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
113 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 1));
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"));
120 git_annotated_commit_free(our_head
);
121 git_annotated_commit_free(their_heads
[0]);
124 /* git merge octo1 octo2 */
125 void test_merge_workdir_setup__two_branches(void)
128 git_reference
*octo1_ref
;
129 git_reference
*octo2_ref
;
130 git_annotated_commit
*our_head
, *their_heads
[2];
132 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
133 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
141 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
148 git_reference_free(octo1_ref
);
149 git_reference_free(octo2_ref
);
151 git_annotated_commit_free(our_head
);
152 git_annotated_commit_free(their_heads
[0]);
153 git_annotated_commit_free(their_heads
[1]);
156 /* git merge octo1 octo2 octo3 */
157 void test_merge_workdir_setup__three_branches(void)
160 git_reference
*octo1_ref
;
161 git_reference
*octo2_ref
;
162 git_reference
*octo3_ref
;
163 git_annotated_commit
*our_head
, *their_heads
[3];
165 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
166 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
177 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
184 git_reference_free(octo1_ref
);
185 git_reference_free(octo2_ref
);
186 git_reference_free(octo3_ref
);
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]);
194 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 158dc7bedb202f5b26502bf3574faa7f4238d56c 50ce7d7d01217679e26c55939eef119e0c93e272 */
195 void test_merge_workdir_setup__three_oids(void)
201 git_annotated_commit
*our_head
, *their_heads
[3];
203 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
204 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
215 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
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]);
228 /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c */
229 void test_merge_workdir_setup__branches_and_oids_1(void)
232 git_reference
*octo1_ref
;
234 git_annotated_commit
*our_head
, *their_heads
[2];
236 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
237 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
245 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
252 git_reference_free(octo1_ref
);
254 git_annotated_commit_free(our_head
);
255 git_annotated_commit_free(their_heads
[0]);
256 git_annotated_commit_free(their_heads
[1]);
259 /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c octo3 54269b3f6ec3d7d4ede24dd350dd5d605495c3ae */
260 void test_merge_workdir_setup__branches_and_oids_2(void)
263 git_reference
*octo1_ref
;
265 git_reference
*octo3_ref
;
267 git_annotated_commit
*our_head
, *their_heads
[4];
269 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
270 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
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
));
284 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 4));
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"));
291 git_reference_free(octo1_ref
);
292 git_reference_free(octo3_ref
);
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]);
301 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 */
302 void test_merge_workdir_setup__branches_and_oids_3(void)
306 git_reference
*octo2_ref
;
308 git_reference
*octo4_ref
;
309 git_annotated_commit
*our_head
, *their_heads
[4];
311 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
312 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
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
));
326 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 4));
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"));
333 git_reference_free(octo2_ref
);
334 git_reference_free(octo4_ref
);
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]);
343 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 octo5 */
344 void test_merge_workdir_setup__branches_and_oids_4(void)
348 git_reference
*octo2_ref
;
350 git_reference
*octo4_ref
;
351 git_reference
*octo5_ref
;
352 git_annotated_commit
*our_head
, *their_heads
[5];
354 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
355 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
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
));
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
));
372 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 5));
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"));
379 git_reference_free(octo2_ref
);
380 git_reference_free(octo4_ref
);
381 git_reference_free(octo5_ref
);
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]);
391 /* git merge octo1 octo1 octo1 */
392 void test_merge_workdir_setup__three_same_branches(void)
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];
400 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
401 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
412 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
419 git_reference_free(octo1_1_ref
);
420 git_reference_free(octo1_2_ref
);
421 git_reference_free(octo1_3_ref
);
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]);
429 /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 */
430 void test_merge_workdir_setup__three_same_oids(void)
436 git_annotated_commit
*our_head
, *their_heads
[3];
438 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
439 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
450 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
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]);
463 static int create_remote_tracking_branch(const char *branch_name
, const char *oid_str
)
467 git_buf remotes_path
= GIT_BUF_INIT
,
468 origin_path
= GIT_BUF_INIT
,
469 filename
= GIT_BUF_INIT
,
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)
476 if (!git_path_exists(git_buf_cstr(&remotes_path
)) &&
477 (error
= p_mkdir(git_buf_cstr(&remotes_path
), 0777)) < 0)
480 if ((error
= git_buf_puts(&origin_path
, git_buf_cstr(&remotes_path
))) < 0 ||
481 (error
= git_buf_puts(&origin_path
, "origin")) < 0)
484 if (!git_path_exists(git_buf_cstr(&origin_path
)) &&
485 (error
= p_mkdir(git_buf_cstr(&origin_path
), 0777)) < 0)
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)
495 cl_git_rewritefile(git_buf_cstr(&filename
), git_buf_cstr(&data
));
498 git_buf_dispose(&remotes_path
);
499 git_buf_dispose(&origin_path
);
500 git_buf_dispose(&filename
);
501 git_buf_dispose(&data
);
506 /* git merge refs/remotes/origin/octo1 */
507 void test_merge_workdir_setup__remote_tracking_one_branch(void)
510 git_reference
*octo1_ref
;
511 git_annotated_commit
*our_head
, *their_heads
[1];
513 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH
, OCTO1_OID
));
515 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
516 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
521 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 1));
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"));
528 git_reference_free(octo1_ref
);
530 git_annotated_commit_free(our_head
);
531 git_annotated_commit_free(their_heads
[0]);
534 /* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 */
535 void test_merge_workdir_setup__remote_tracking_two_branches(void)
538 git_reference
*octo1_ref
;
539 git_reference
*octo2_ref
;
540 git_annotated_commit
*our_head
, *their_heads
[2];
542 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH
, OCTO1_OID
));
543 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH
, OCTO2_OID
));
545 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
546 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
554 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
561 git_reference_free(octo1_ref
);
562 git_reference_free(octo2_ref
);
564 git_annotated_commit_free(our_head
);
565 git_annotated_commit_free(their_heads
[0]);
566 git_annotated_commit_free(their_heads
[1]);
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)
573 git_reference
*octo1_ref
;
574 git_reference
*octo2_ref
;
575 git_reference
*octo3_ref
;
576 git_annotated_commit
*our_head
, *their_heads
[3];
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
));
582 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
583 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
594 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
601 git_reference_free(octo1_ref
);
602 git_reference_free(octo2_ref
);
603 git_reference_free(octo3_ref
);
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]);
611 /* git merge octo1 refs/remotes/origin/octo2 */
612 void test_merge_workdir_setup__normal_branch_and_remote_tracking_branch(void)
615 git_reference
*octo1_ref
;
616 git_reference
*octo2_ref
;
617 git_annotated_commit
*our_head
, *their_heads
[2];
619 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH
, OCTO2_OID
));
621 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
622 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
630 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
637 git_reference_free(octo1_ref
);
638 git_reference_free(octo2_ref
);
640 git_annotated_commit_free(our_head
);
641 git_annotated_commit_free(their_heads
[0]);
642 git_annotated_commit_free(their_heads
[1]);
645 /* git merge refs/remotes/origin/octo1 octo2 */
646 void test_merge_workdir_setup__remote_tracking_branch_and_normal_branch(void)
649 git_reference
*octo1_ref
;
650 git_reference
*octo2_ref
;
651 git_annotated_commit
*our_head
, *their_heads
[2];
653 cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH
, OCTO1_OID
));
655 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
656 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
664 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
671 git_reference_free(octo1_ref
);
672 git_reference_free(octo2_ref
);
674 git_annotated_commit_free(our_head
);
675 git_annotated_commit_free(their_heads
[0]);
676 git_annotated_commit_free(their_heads
[1]);
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)
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];
689 cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH
, OCTO2_OID
));
690 cl_git_pass(create_remote_tracking_branch(OCTO4_BRANCH
, OCTO4_OID
));
692 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
693 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
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
));
707 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 4));
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"));
714 git_reference_free(octo1_ref
);
715 git_reference_free(octo2_ref
);
716 git_reference_free(octo3_ref
);
717 git_reference_free(octo4_ref
);
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]);
726 /* git pull origin branch octo1 */
727 void test_merge_workdir_setup__pull_one(void)
731 git_annotated_commit
*our_head
, *their_heads
[1];
733 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
734 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
739 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 1));
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"));
746 git_annotated_commit_free(our_head
);
747 git_annotated_commit_free(their_heads
[0]);
750 /* git pull origin octo1 octo2 */
751 void test_merge_workdir_setup__pull_two(void)
756 git_annotated_commit
*our_head
, *their_heads
[2];
758 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
759 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
767 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 2));
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"));
774 git_annotated_commit_free(our_head
);
775 git_annotated_commit_free(their_heads
[0]);
776 git_annotated_commit_free(their_heads
[1]);
779 /* git pull origin octo1 octo2 octo3 */
780 void test_merge_workdir_setup__pull_three(void)
786 git_annotated_commit
*our_head
, *their_heads
[3];
788 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
789 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
800 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
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]);
813 void test_merge_workdir_setup__three_remotes(void)
819 git_annotated_commit
*our_head
, *their_heads
[3];
821 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
822 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
833 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 3));
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"));
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]);
846 void test_merge_workdir_setup__two_remotes(void)
853 git_annotated_commit
*our_head
, *their_heads
[4];
855 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
856 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
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
));
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
));
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
));
870 cl_git_pass(git_merge__setup(repo
, our_head
, (const git_annotated_commit
**)their_heads
, 4));
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"));
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]);
884 void test_merge_workdir_setup__id_from_head(void)
889 git_annotated_commit
*heads
[3];
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
));
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
));
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
));
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]);
911 struct annotated_commit_cb_data
{
912 const char **oid_str
;
918 static int annotated_commit_foreach_cb(const git_oid
*oid
, void *payload
)
920 git_oid expected_oid
;
921 struct annotated_commit_cb_data
*cb_data
= payload
;
923 git_oid_fromstr(&expected_oid
, cb_data
->oid_str
[cb_data
->i
]);
924 cl_assert(git_oid_cmp(&expected_oid
, oid
) == 0);
929 void test_merge_workdir_setup__head_notfound(void)
933 cl_git_fail((error
= git_repository_mergehead_foreach(repo
,
934 annotated_commit_foreach_cb
, NULL
)));
935 cl_assert(error
== GIT_ENOTFOUND
);
938 void test_merge_workdir_setup__head_invalid_oid(void)
942 write_file_contents(GIT_MERGE_HEAD_FILE
, "invalid-oid\n");
944 cl_git_fail((error
= git_repository_mergehead_foreach(repo
,
945 annotated_commit_foreach_cb
, NULL
)));
946 cl_assert(error
== -1);
949 void test_merge_workdir_setup__head_foreach_nonewline(void)
953 write_file_contents(GIT_MERGE_HEAD_FILE
, THEIRS_SIMPLE_OID
);
955 cl_git_fail((error
= git_repository_mergehead_foreach(repo
,
956 annotated_commit_foreach_cb
, NULL
)));
957 cl_assert(error
== -1);
960 void test_merge_workdir_setup__head_foreach_one(void)
962 const char *expected
= THEIRS_SIMPLE_OID
;
964 struct annotated_commit_cb_data cb_data
= { &expected
, 1 };
966 write_file_contents(GIT_MERGE_HEAD_FILE
, THEIRS_SIMPLE_OID
"\n");
968 cl_git_pass(git_repository_mergehead_foreach(repo
,
969 annotated_commit_foreach_cb
, &cb_data
));
971 cl_assert(cb_data
.i
== cb_data
.len
);
974 void test_merge_workdir_setup__head_foreach_octopus(void)
976 const char *expected
[] = { THEIRS_SIMPLE_OID
,
977 OCTO1_OID
, OCTO2_OID
, OCTO3_OID
, OCTO4_OID
, OCTO5_OID
};
979 struct annotated_commit_cb_data cb_data
= { expected
, 6 };
981 write_file_contents(GIT_MERGE_HEAD_FILE
,
982 THEIRS_SIMPLE_OID
"\n"
989 cl_git_pass(git_repository_mergehead_foreach(repo
,
990 annotated_commit_foreach_cb
, &cb_data
));
992 cl_assert(cb_data
.i
== cb_data
.len
);
995 void test_merge_workdir_setup__retained_after_success(void)
998 git_reference
*octo1_ref
;
999 git_annotated_commit
*our_head
, *their_heads
[1];
1001 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
1002 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
1004 cl_git_pass(git_reference_lookup(&octo1_ref
, repo
, GIT_REFS_HEADS_DIR OCTO1_BRANCH
));
1006 cl_git_pass(git_annotated_commit_from_ref(&their_heads
[0], repo
, octo1_ref
));
1008 cl_git_pass(git_merge(repo
, (const git_annotated_commit
**)&their_heads
[0], 1, NULL
, NULL
));
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"));
1015 git_reference_free(octo1_ref
);
1017 git_annotated_commit_free(our_head
);
1018 git_annotated_commit_free(their_heads
[0]);
1022 void test_merge_workdir_setup__removed_after_failure(void)
1025 git_reference
*octo1_ref
;
1026 git_annotated_commit
*our_head
, *their_heads
[1];
1028 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
1029 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
1034 cl_git_write2file("merge-resolve/.git/index.lock", "foo\n", 4, O_RDWR
|O_CREAT
, 0666);
1036 cl_git_fail(git_merge(
1037 repo
, (const git_annotated_commit
**)&their_heads
[0], 1, NULL
, NULL
));
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
));
1043 git_reference_free(octo1_ref
);
1045 git_annotated_commit_free(our_head
);
1046 git_annotated_commit_free(their_heads
[0]);
1049 void test_merge_workdir_setup__unlocked_after_success(void)
1052 git_reference
*octo1_ref
;
1053 git_annotated_commit
*our_head
, *their_heads
[1];
1055 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
1056 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
1061 cl_git_pass(git_merge(
1062 repo
, (const git_annotated_commit
**)&their_heads
[0], 1, NULL
, NULL
));
1064 cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
1066 git_reference_free(octo1_ref
);
1068 git_annotated_commit_free(our_head
);
1069 git_annotated_commit_free(their_heads
[0]);
1072 void test_merge_workdir_setup__unlocked_after_conflict(void)
1075 git_reference
*octo1_ref
;
1076 git_annotated_commit
*our_head
, *their_heads
[1];
1078 cl_git_pass(git_oid_fromstr(&our_oid
, ORIG_HEAD
));
1079 cl_git_pass(git_annotated_commit_lookup(&our_head
, repo
, &our_oid
));
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
));
1084 cl_git_rewritefile("merge-resolve/new-in-octo1.txt",
1085 "Conflicting file!\n\nMerge will fail!\n");
1087 cl_git_fail(git_merge(
1088 repo
, (const git_annotated_commit
**)&their_heads
[0], 1, NULL
, NULL
));
1090 cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
1092 git_reference_free(octo1_ref
);
1094 git_annotated_commit_free(our_head
);
1095 git_annotated_commit_free(their_heads
[0]);