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

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
static bool test_file_contents(const char *filename, const char *expected)
{
49
	git_str file_path_buf = GIT_STR_INIT, file_buf = GIT_STR_INIT;
Edward Thomson committed
50
	bool equals;
51

52
	git_str_joinpath(&file_path_buf, git_repository_path(repo), filename);
53

Edward Thomson committed
54 55 56
	cl_git_pass(git_futils_readbuffer(&file_buf, file_path_buf.ptr));
	equals = (strcmp(file_buf.ptr, expected) == 0);

57 58
	git_str_dispose(&file_path_buf);
	git_str_dispose(&file_buf);
59

Edward Thomson committed
60 61 62
	return equals;
}

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

67
	git_str_joinpath(&file_path_buf, git_repository_path(repo),
Edward Thomson committed
68
		filename);
69 70
	cl_git_rewritefile(file_path_buf.ptr, output);

71
	git_str_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];
80

81
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
82
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
83

Edward Thomson committed
84
	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
85
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
86

87
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
88 89 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];
106

107
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
108
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
109

110
	cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1));
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];
131

132
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
133
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
134

Edward Thomson committed
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));
142

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "'\n"));
147

Edward Thomson committed
148 149
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
150

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];
164

165
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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));
170

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "'\n"));
183

Edward Thomson committed
184 185 186 187
	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];
202

203
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
207
	cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
208

209
	cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO2_OID "'; commit '" OCTO3_OID "'\n"));
221

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];
235

236
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'; commit '" OCTO2_OID "'\n"));
251

Edward Thomson committed
252 253
	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, GIT_OID_SHA1));
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));
274

275
	cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1));
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));
280

281
	cl_git_pass(git_oid__fromstr(&octo4_oid, OCTO4_OID, GIT_OID_SHA1));
282
	cl_git_pass(git_annotated_commit_lookup(&their_heads[3], repo, &octo4_oid));
283

284
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "'; commit '" OCTO2_OID "'; commit '" OCTO4_OID "'\n"));
290

Edward Thomson committed
291 292 293
	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];
310

311
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
321
	cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid));
322

Edward Thomson committed
323
	cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH));
324
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref));
325

326
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "'; commit '" OCTO3_OID "'\n"));
332

Edward Thomson committed
333 334 335
	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];
353

354
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
355
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
356

357
	cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1));
358
	cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid));
359

Edward Thomson committed
360
	cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
361
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref));
362

363
	cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1));
364
	cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid));
365

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "', '" OCTO4_BRANCH "' and '" OCTO5_BRANCH "'; commit '" OCTO3_OID "'\n"));
378

Edward Thomson committed
379 380 381 382
	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];
399

400
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
401
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
402

Edward Thomson committed
403
	cl_git_pass(git_reference_lookup(&octo1_1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
404
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_1_ref));
405

Edward Thomson committed
406
	cl_git_pass(git_reference_lookup(&octo1_2_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
407
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo1_2_ref));
408

Edward Thomson committed
409
	cl_git_pass(git_reference_lookup(&octo1_3_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
410
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo1_3_ref));
411

412
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO1_BRANCH "' and '" OCTO1_BRANCH "'\n"));
418

Edward Thomson committed
419 420 421
	git_reference_free(octo1_1_ref);
	git_reference_free(octo1_2_ref);
	git_reference_free(octo1_3_ref);
422

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];
437

438
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
439
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
440

441
	cl_git_pass(git_oid__fromstr(&octo1_1_oid, OCTO1_OID, GIT_OID_SHA1));
442
	cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_1_oid));
443

444
	cl_git_pass(git_oid__fromstr(&octo1_2_oid, OCTO1_OID, GIT_OID_SHA1));
445
	cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo1_2_oid));
446

447
	cl_git_pass(git_oid__fromstr(&octo1_3_oid, OCTO1_OID, GIT_OID_SHA1));
448
	cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo1_3_oid));
449

450
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3));
451

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO1_OID "'; commit '" OCTO1_OID "'\n"));
456

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
}

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

467 468 469 470
	git_str remotes_path = GIT_STR_INIT,
		origin_path = GIT_STR_INIT,
		filename = GIT_STR_INIT,
		data = GIT_STR_INIT;
Edward Thomson committed
471

472 473
	if ((error = git_str_puts(&remotes_path, git_repository_path(repo))) < 0 ||
		(error = git_str_puts(&remotes_path, GIT_REFS_REMOTES_DIR)) < 0)
Edward Thomson committed
474 475
		goto done;

476
	if (!git_fs_path_exists(git_str_cstr(&remotes_path)) &&
477
		(error = p_mkdir(git_str_cstr(&remotes_path), 0777)) < 0)
Edward Thomson committed
478 479
		goto done;

480 481
	if ((error = git_str_puts(&origin_path, git_str_cstr(&remotes_path))) < 0 ||
		(error = git_str_puts(&origin_path, "origin")) < 0)
Edward Thomson committed
482 483
		goto done;

484
	if (!git_fs_path_exists(git_str_cstr(&origin_path)) &&
485
		(error = p_mkdir(git_str_cstr(&origin_path), 0777)) < 0)
Edward Thomson committed
486 487
		goto done;

488 489 490 491 492
	if ((error = git_str_puts(&filename, git_str_cstr(&origin_path))) < 0 ||
		(error = git_str_puts(&filename, "/")) < 0 ||
		(error = git_str_puts(&filename, branch_name)) < 0 ||
		(error = git_str_puts(&data, oid_str)) < 0 ||
		(error = git_str_puts(&data, "\n")) < 0)
Edward Thomson committed
493 494
		goto done;

495
	cl_git_rewritefile(git_str_cstr(&filename), git_str_cstr(&data));
Edward Thomson committed
496 497

done:
498 499 500 501
	git_str_dispose(&remotes_path);
	git_str_dispose(&origin_path);
	git_str_dispose(&filename);
	git_str_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

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));

515
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
516
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
517

Edward Thomson committed
518
	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
519
	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
520

521
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));

	git_reference_free(octo1_ref);
529

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

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

545
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
546
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
547

Edward Thomson committed
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));
555

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "' and 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
560

Edward Thomson committed
561 562
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
563

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

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

582
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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));
587

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "', 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO3_BRANCH "'\n"));
600

Edward Thomson committed
601 602 603 604
	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

	cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));

621
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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));
631

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
636

Edward Thomson committed
637 638
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
639

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

	cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));

655
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
656
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
657

Edward Thomson committed
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));
665

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO2_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
670

Edward Thomson committed
671 672
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
673

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

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

692
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
693
	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
694

Edward Thomson committed
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));
708

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "', remote-tracking branches 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO4_BRANCH "'\n"));
713

Edward Thomson committed
714 715 716 717
	git_reference_free(octo1_ref);
	git_reference_free(octo2_ref);
	git_reference_free(octo3_ref);
	git_reference_free(octo4_ref);
718

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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
737
	cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_1_oid));
738

739
	cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1));
740

Edward Thomson committed
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch 'octo1' of http://remote.url/repo.git\n"));
745

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];
757

758
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "' of http://remote.url/repo.git\n"));
773

774 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];
787

788
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.url/repo.git\n"));
806

807 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];
820

821
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "' of http://remote.first/repo.git, branch '" OCTO2_BRANCH "' of http://remote.second/repo.git, branch '" OCTO3_BRANCH "' of http://remote.third/repo.git\n"));
839

840 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];
854

855
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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
	cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.first/repo.git, branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "' of http://remote.second/repo.git\n"));
876

877 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, GIT_OID_SHA1));
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
	git_oid__fromstr(&expected_oid, cb_data->oid_str[cb_data->i], GIT_OID_SHA1);
924 925 926 927 928
	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, GIT_OID_SHA1));
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, GIT_OID_SHA1));
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_fs_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE));
	cl_assert(!git_fs_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE));
	cl_assert(!git_fs_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

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];

1055
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
1056 1057 1058 1059 1060 1061 1062 1063
	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));

1064
	cl_assert(!git_fs_path_exists("merge-resolve/.git/index.lock"));
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

	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];

1078
	cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1));
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	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));

1090
	cl_assert(!git_fs_path_exists("merge-resolve/.git/index.lock"));
1091 1092 1093 1094 1095 1096

	git_reference_free(octo1_ref);

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