setup.c 42.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "merge.h"
#include "refs.h"
#include "fileops.h"

static git_repository *repo;
static git_index *repo_index;

Edward Thomson committed
11 12
#define TEST_REPO_PATH		"merge-resolve"
#define TEST_INDEX_PATH TEST_REPO_PATH	"/.git/index"
13

Edward Thomson committed
14
#define ORIG_HEAD			"bd593285fc7fe4ca18ccdbabf027f5d689101452"
15

Edward Thomson committed
16 17
#define THEIRS_SIMPLE_BRANCH	"branch"
#define THEIRS_SIMPLE_OID	"7cb63eed597130ba4abb87b3e544b85021905520"
18

Edward Thomson committed
19 20
#define OCTO1_BRANCH		"octo1"
#define OCTO1_OID			"16f825815cfd20a07a75c71554e82d8eede0b061"
21

Edward Thomson committed
22 23
#define OCTO2_BRANCH		"octo2"
#define OCTO2_OID			"158dc7bedb202f5b26502bf3574faa7f4238d56c"
24

Edward Thomson committed
25 26
#define OCTO3_BRANCH		"octo3"
#define OCTO3_OID			"50ce7d7d01217679e26c55939eef119e0c93e272"
27

Edward Thomson committed
28 29
#define OCTO4_BRANCH		"octo4"
#define OCTO4_OID			"54269b3f6ec3d7d4ede24dd350dd5d605495c3ae"
30

Edward Thomson committed
31 32
#define OCTO5_BRANCH		"octo5"
#define OCTO5_OID			"e4f618a2c3ed0669308735727df5ebf2447f022f"
33

34
/* Fixture setup and teardown */
Edward Thomson committed
35
void test_merge_workdir_setup__initialize(void)
36 37
{
	repo = cl_git_sandbox_init(TEST_REPO_PATH);
Edward Thomson committed
38
	git_repository_index(&repo_index, repo);
39 40
}

Edward Thomson committed
41
void test_merge_workdir_setup__cleanup(void)
42
{
Edward Thomson committed
43
	git_index_free(repo_index);
44 45 46
	cl_git_sandbox_cleanup();
}

Edward Thomson committed
47 48 49 50 51 52 53 54 55 56
static bool test_file_contents(const char *filename, const char *expected)
{
	git_buf file_path_buf = GIT_BUF_INIT, file_buf = GIT_BUF_INIT;
	bool equals;
	
	git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo), filename);
	
	cl_git_pass(git_futils_readbuffer(&file_buf, file_path_buf.ptr));
	equals = (strcmp(file_buf.ptr, expected) == 0);

57 58
	git_buf_dispose(&file_path_buf);
	git_buf_dispose(&file_buf);
Edward Thomson committed
59 60 61 62
	
	return equals;
}

63 64 65 66
static void write_file_contents(const char *filename, const char *output)
{
	git_buf file_path_buf = GIT_BUF_INIT;

Edward Thomson committed
67 68
	git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo),
		filename);
69 70
	cl_git_rewritefile(file_path_buf.ptr, output);

71
	git_buf_dispose(&file_path_buf);
72 73
}

74
/* git merge --no-ff octo1 */
Edward Thomson committed
75 76 77 78
void test_merge_workdir_setup__one_branch(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
79
	git_annotated_commit *our_head, *their_heads[1];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
86
	
87
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
88 89 90 91 92 93 94

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n"));

	git_reference_free(octo1_ref);
95
	
96 97
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
98 99
}

100
/* git merge --no-ff 16f825815cfd20a07a75c71554e82d8eede0b061 */
Edward Thomson committed
101 102 103 104
void test_merge_workdir_setup__one_oid(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
105
	git_annotated_commit *our_head, *their_heads[1];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
112

113
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
Edward Thomson committed
114 115 116

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
117
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
118 119
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'\n"));

120 121
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
Edward Thomson committed
122 123 124 125 126 127 128 129
}

/* git merge octo1 octo2 */
void test_merge_workdir_setup__two_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
130
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
140

141
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
142 143 144
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
145
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
146 147 148 149 150
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "'\n"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	
151 152 153
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
154 155 156 157 158 159 160 161 162
}

/* git merge octo1 octo2 octo3 */
void test_merge_workdir_setup__three_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
	git_reference *octo3_ref;
163
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
176

177
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
178 179 180

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
181
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
182 183 184 185 186 187
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "'\n"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	git_reference_free(octo3_ref);

188 189 190 191
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
192 193 194 195 196 197 198 199 200
}

/* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 158dc7bedb202f5b26502bf3574faa7f4238d56c 50ce7d7d01217679e26c55939eef119e0c93e272 */
void test_merge_workdir_setup__three_oids(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_oid octo2_oid;
	git_oid octo3_oid;
201
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
214

215
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
216 217 218

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
219
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
220 221
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO2_OID "'; commit '" OCTO3_OID "'\n"));
	
222 223 224 225
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
226 227 228 229 230 231 232 233
}

/* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c */
void test_merge_workdir_setup__branches_and_oids_1(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_oid octo2_oid;
234
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
244

245
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
246 247 248

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
249
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
250 251 252 253
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'; commit '" OCTO2_OID "'\n"));
	
	git_reference_free(octo1_ref);

254 255 256
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
257 258 259 260 261 262 263 264 265 266
}

/* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c octo3 54269b3f6ec3d7d4ede24dd350dd5d605495c3ae */
void test_merge_workdir_setup__branches_and_oids_2(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_oid octo2_oid;
	git_reference *octo3_ref;
	git_oid octo4_oid;
267
	git_annotated_commit *our_head, *their_heads[4];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
283
	
284
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
Edward Thomson committed
285 286 287

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
288
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
289 290 291 292 293
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "'; commit '" OCTO2_OID "'; commit '" OCTO4_OID "'\n"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo3_ref);

294 295 296 297 298
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
	git_annotated_commit_free(their_heads[3]);
Edward Thomson committed
299 300 301 302 303 304 305 306 307 308
}

/* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 */
void test_merge_workdir_setup__branches_and_oids_3(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_reference *octo2_ref;
	git_oid octo3_oid;
	git_reference *octo4_ref;
309
	git_annotated_commit *our_head, *their_heads[4];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
325
	
326
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
Edward Thomson committed
327 328 329

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
330
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
331 332 333 334 335
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "'; commit '" OCTO3_OID "'\n"));
	
	git_reference_free(octo2_ref);
	git_reference_free(octo4_ref);

336 337 338 339 340
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
	git_annotated_commit_free(their_heads[3]);
Edward Thomson committed
341 342 343 344 345 346 347 348 349 350 351
}

/* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 octo5 */
void test_merge_workdir_setup__branches_and_oids_4(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_reference *octo2_ref;
	git_oid octo3_oid;
	git_reference *octo4_ref;
	git_reference *octo5_ref;
352
	git_annotated_commit *our_head, *their_heads[5];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
371

372
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 5));
Edward Thomson committed
373 374 375

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n" OCTO5_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
376
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
377 378 379 380 381 382
	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"));
	
	git_reference_free(octo2_ref);
	git_reference_free(octo4_ref);
	git_reference_free(octo5_ref);

383 384 385 386 387 388
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
	git_annotated_commit_free(their_heads[3]);
	git_annotated_commit_free(their_heads[4]);
Edward Thomson committed
389 390 391 392 393 394 395 396 397
}

/* git merge octo1 octo1 octo1 */
void test_merge_workdir_setup__three_same_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_1_ref;
	git_reference *octo1_2_ref;
	git_reference *octo1_3_ref;
398
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
411
	
412
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
413 414 415

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
416
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
417 418 419 420 421 422
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO1_BRANCH "' and '" OCTO1_BRANCH "'\n"));
	
	git_reference_free(octo1_1_ref);
	git_reference_free(octo1_2_ref);
	git_reference_free(octo1_3_ref);
	
423 424 425 426
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
427 428 429 430 431 432 433 434 435
}

/* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 */
void test_merge_workdir_setup__three_same_oids(void)
{
	git_oid our_oid;
	git_oid octo1_1_oid;
	git_oid octo1_2_oid;
	git_oid octo1_3_oid;
436
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
449
	
450
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
451 452 453
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
454
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
455 456
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO1_OID "'; commit '" OCTO1_OID "'\n"));
	
457 458 459 460
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
}

static int create_remote_tracking_branch(const char *branch_name, const char *oid_str)
{
	int error = 0;

	git_buf remotes_path = GIT_BUF_INIT,
		origin_path = GIT_BUF_INIT,
		filename = GIT_BUF_INIT,
		data = GIT_BUF_INIT;

	if ((error = git_buf_puts(&remotes_path, git_repository_path(repo))) < 0 ||
		(error = git_buf_puts(&remotes_path, GIT_REFS_REMOTES_DIR)) < 0)
		goto done;

	if (!git_path_exists(git_buf_cstr(&remotes_path)) &&
		(error = p_mkdir(git_buf_cstr(&remotes_path), 0777)) < 0)
		goto done;

	if ((error = git_buf_puts(&origin_path, git_buf_cstr(&remotes_path))) < 0 ||
		(error = git_buf_puts(&origin_path, "origin")) < 0)
		goto done;

	if (!git_path_exists(git_buf_cstr(&origin_path)) &&
		(error = p_mkdir(git_buf_cstr(&origin_path), 0777)) < 0)
		goto done;

	if ((error = git_buf_puts(&filename, git_buf_cstr(&origin_path))) < 0 ||
		(error = git_buf_puts(&filename, "/")) < 0 ||
		(error = git_buf_puts(&filename, branch_name)) < 0 ||
		(error = git_buf_puts(&data, oid_str)) < 0 ||
		(error = git_buf_puts(&data, "\n")) < 0)
		goto done;

	cl_git_rewritefile(git_buf_cstr(&filename), git_buf_cstr(&data));

done:
498 499 500 501
	git_buf_dispose(&remotes_path);
	git_buf_dispose(&origin_path);
	git_buf_dispose(&filename);
	git_buf_dispose(&data);
Edward Thomson committed
502 503 504 505 506 507 508 509 510

	return error;
}

/* git merge refs/remotes/origin/octo1 */
void test_merge_workdir_setup__remote_tracking_one_branch(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
511
	git_annotated_commit *our_head, *their_heads[1];
Edward Thomson committed
512 513 514 515

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
516
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
520
	
521
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
Edward Thomson committed
522 523 524

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
525
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
526 527 528 529
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));

	git_reference_free(octo1_ref);
	
530 531
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
Edward Thomson committed
532 533 534 535 536 537 538 539
}

/* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 */
void test_merge_workdir_setup__remote_tracking_two_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
540
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
541 542 543 544 545

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
	cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
546
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
553

554
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
555 556 557
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
558
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
559 560 561 562 563
	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"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	
564 565 566
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
567 568 569 570 571 572 573 574 575
}

/* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 refs/remotes/origin/octo3 */
void test_merge_workdir_setup__remote_tracking_three_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
	git_reference *octo3_ref;
576
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
577 578 579 580 581 582

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
	cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
	cl_git_pass(create_remote_tracking_branch(OCTO3_BRANCH, OCTO3_OID));
	
	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
583
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
593

594
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
595 596 597

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
598
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
599 600 601 602 603 604
	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"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	git_reference_free(octo3_ref);

605 606 607 608
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
609 610 611 612 613 614 615 616
}

/* git merge octo1 refs/remotes/origin/octo2 */
void test_merge_workdir_setup__normal_branch_and_remote_tracking_branch(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
617
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
618 619 620 621

	cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
622
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
629

630
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
631 632 633
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
634
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
635 636 637 638 639
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	
640 641 642
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
643 644 645 646 647 648 649 650
}

/* git merge refs/remotes/origin/octo1 octo2 */
void test_merge_workdir_setup__remote_tracking_branch_and_normal_branch(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
651
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
652 653 654 655

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
656
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
663

664
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
665 666 667
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
668
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
669 670 671 672 673
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO2_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	
674 675 676
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
677 678 679 680 681 682 683 684 685 686
}

/* git merge octo1 refs/remotes/origin/octo2 octo3 refs/remotes/origin/octo4 */
void test_merge_workdir_setup__two_remote_tracking_branch_and_two_normal_branches(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_reference *octo2_ref;
	git_reference *octo3_ref;
	git_reference *octo4_ref;
687
	git_annotated_commit *our_head, *their_heads[4];
Edward Thomson committed
688 689 690 691 692

	cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
	cl_git_pass(create_remote_tracking_branch(OCTO4_BRANCH, OCTO4_OID));

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
693
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
706

707
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
Edward Thomson committed
708 709 710
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
711
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
712 713 714 715 716 717 718
	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"));
	
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	git_reference_free(octo3_ref);
	git_reference_free(octo4_ref);
	
719 720 721 722 723
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
	git_annotated_commit_free(their_heads[3]);
Edward Thomson committed
724 725 726 727 728 729 730
}

/* git pull origin branch octo1 */
void test_merge_workdir_setup__pull_one(void)
{
	git_oid our_oid;
	git_oid octo1_1_oid;
731
	git_annotated_commit *our_head, *their_heads[1];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
738
	
739
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
Edward Thomson committed
740 741 742
	
	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
743
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
744 745
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch 'octo1' of http://remote.url/repo.git\n"));
	
746 747
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
Edward Thomson committed
748 749 750 751 752 753 754 755
}

/* git pull origin octo1 octo2 */
void test_merge_workdir_setup__pull_two(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_oid octo2_oid;
756
	git_annotated_commit *our_head, *their_heads[2];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
766

767
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2));
Edward Thomson committed
768 769 770

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
771
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
772 773
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "' of http://remote.url/repo.git\n"));
	
774 775 776
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
Edward Thomson committed
777 778 779 780 781 782 783 784 785
}

/* git pull origin octo1 octo2 octo3 */
void test_merge_workdir_setup__pull_three(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_oid octo2_oid;
	git_oid octo3_oid;
786
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
799

800
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
801 802 803

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
804
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
805 806
	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 808 809 810
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
811 812 813 814 815 816 817 818
}

void test_merge_workdir_setup__three_remotes(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_oid octo2_oid;
	git_oid octo3_oid;
819
	git_annotated_commit *our_head, *their_heads[3];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
832

833
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
Edward Thomson committed
834 835 836

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
837
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
838 839
	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 841 842 843
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
Edward Thomson committed
844 845 846 847 848 849 850 851 852
}

void test_merge_workdir_setup__two_remotes(void)
{
	git_oid our_oid;
	git_oid octo1_oid;
	git_oid octo2_oid;
	git_oid octo3_oid;
	git_oid octo4_oid;
853
	git_annotated_commit *our_head, *their_heads[4];
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
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));
Edward Thomson committed
869

870
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
Edward Thomson committed
871 872 873

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
874
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
Edward Thomson committed
875 876
	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 878 879 880 881
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
	git_annotated_commit_free(their_heads[1]);
	git_annotated_commit_free(their_heads[2]);
	git_annotated_commit_free(their_heads[3]);
Edward Thomson committed
882 883
}

884 885 886 887 888
void test_merge_workdir_setup__id_from_head(void)
{
	git_oid expected_id;
	const git_oid *id;
	git_reference *ref;
889
	git_annotated_commit *heads[3];
890 891

	cl_git_pass(git_oid_fromstr(&expected_id, OCTO1_OID));
892 893
	cl_git_pass(git_annotated_commit_from_fetchhead(&heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &expected_id));
	id = git_annotated_commit_id(heads[0]);
894 895
	cl_assert_equal_i(1, git_oid_equal(id, &expected_id));

896 897
	cl_git_pass(git_annotated_commit_lookup(&heads[1], repo, &expected_id));
	id = git_annotated_commit_id(heads[1]);
898 899 900
	cl_assert_equal_i(1, git_oid_equal(id, &expected_id));

	cl_git_pass(git_reference_lookup(&ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
901 902
	cl_git_pass(git_annotated_commit_from_ref(&heads[2], repo, ref));
	id = git_annotated_commit_id(heads[2]);
903 904 905
	cl_assert_equal_i(1, git_oid_equal(id, &expected_id));

	git_reference_free(ref);
906 907 908
	git_annotated_commit_free(heads[0]);
	git_annotated_commit_free(heads[1]);
	git_annotated_commit_free(heads[2]);
909 910
}

911
struct annotated_commit_cb_data {
912 913 914 915 916 917
	const char **oid_str;
	unsigned int len;

	unsigned int i;
};

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 924 925 926 927 928

	git_oid_fromstr(&expected_oid, cb_data->oid_str[cb_data->i]);
	cl_assert(git_oid_cmp(&expected_oid, oid) == 0);
	cb_data->i++;
	return 0;
}

Edward Thomson committed
929
void test_merge_workdir_setup__head_notfound(void)
930 931 932 933
{
	int error;

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
934
		annotated_commit_foreach_cb, NULL)));
935 936 937
	cl_assert(error == GIT_ENOTFOUND);
}

Edward Thomson committed
938
void test_merge_workdir_setup__head_invalid_oid(void)
939 940 941 942 943 944
{
	int error;

	write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n");

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
945
		annotated_commit_foreach_cb, NULL)));
946 947 948
	cl_assert(error == -1);
}

Edward Thomson committed
949
void test_merge_workdir_setup__head_foreach_nonewline(void)
950 951 952 953 954 955
{
	int error;

	write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID);

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
956
		annotated_commit_foreach_cb, NULL)));
957 958 959
	cl_assert(error == -1);
}

Edward Thomson committed
960
void test_merge_workdir_setup__head_foreach_one(void)
961 962 963
{
	const char *expected = THEIRS_SIMPLE_OID;

964
	struct annotated_commit_cb_data cb_data = { &expected, 1 };
965 966 967 968

	write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n");

	cl_git_pass(git_repository_mergehead_foreach(repo,
969
		annotated_commit_foreach_cb, &cb_data));
970 971 972 973

	cl_assert(cb_data.i == cb_data.len);
}

Edward Thomson committed
974
void test_merge_workdir_setup__head_foreach_octopus(void)
975 976 977 978
{
	const char *expected[] = { THEIRS_SIMPLE_OID,
		OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID };

979
	struct annotated_commit_cb_data cb_data = { expected, 6 };
980 981 982 983 984 985 986 987 988 989

	write_file_contents(GIT_MERGE_HEAD_FILE,
		THEIRS_SIMPLE_OID "\n"
		OCTO1_OID "\n"
		OCTO2_OID "\n"
		OCTO3_OID "\n"
		OCTO4_OID "\n"
		OCTO5_OID "\n");

	cl_git_pass(git_repository_mergehead_foreach(repo,
990
		annotated_commit_foreach_cb, &cb_data));
991 992 993

	cl_assert(cb_data.i == cb_data.len);
}
994 995 996 997 998

void test_merge_workdir_setup__retained_after_success(void)
{
	git_oid our_oid;
	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 1005

	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));
1007

1008
	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1009 1010 1011 1012 1013 1014 1015 1016

	cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n"));
	cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n"));
	cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff"));
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n"));

	git_reference_free(octo1_ref);

1017 1018
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
1019 1020
}

1021

1022 1023 1024 1025
void test_merge_workdir_setup__removed_after_failure(void)
{
	git_oid our_oid;
	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

Russell Belfer committed
1036
	cl_git_fail(git_merge(
1037
		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
1038

1039 1040 1041
	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE));
	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE));
	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MSG_FILE));
1042 1043 1044

	git_reference_free(octo1_ref);

1045 1046
	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
1047
}
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

void test_merge_workdir_setup__unlocked_after_success(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_annotated_commit *our_head, *their_heads[1];

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));

	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));

	cl_git_pass(git_merge(
		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));

	cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));

	git_reference_free(octo1_ref);

	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
}

void test_merge_workdir_setup__unlocked_after_conflict(void)
{
	git_oid our_oid;
	git_reference *octo1_ref;
	git_annotated_commit *our_head, *their_heads[1];

	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));

	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));

	cl_git_rewritefile("merge-resolve/new-in-octo1.txt",
		"Conflicting file!\n\nMerge will fail!\n");

	cl_git_fail(git_merge(
		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));

	cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));

	git_reference_free(octo1_ref);

	git_annotated_commit_free(our_head);
	git_annotated_commit_free(their_heads[0]);
}