merge.c 20.9 KB
Newer Older
1 2 3
#include "clar_libgit2.h"
#include "git2/rebase.h"
#include "posix.h"
4
#include "signature.h"
5 6 7 8 9 10

#include <fcntl.h>

static git_repository *repo;
static git_signature *signature;

11 12 13 14 15 16 17 18 19 20
static void set_core_autocrlf_to(git_repository *repo, bool value)
{
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value));

	git_config_free(cfg);
}

21 22 23 24
// Fixture setup and teardown
void test_rebase_merge__initialize(void)
{
	repo = cl_git_sandbox_init("rebase");
25 26
	cl_git_pass(git_signature_new(&signature,
		"Rebaser", "rebaser@rebaser.rb", 1405694510, 0));
27 28

	set_core_autocrlf_to(repo, false);
29 30 31 32
}

void test_rebase_merge__cleanup(void)
{
33
	git_signature_free(signature);
34 35 36 37 38
	cl_git_sandbox_cleanup();
}

void test_rebase_merge__next(void)
{
39
	git_rebase *rebase;
40
	git_reference *branch_ref, *upstream_ref;
41
	git_annotated_commit *branch_head, *upstream_head;
42
	git_rebase_operation *rebase_operation;
43 44
	git_status_list *status_list;
	const git_status_entry *status_entry;
45
	git_oid pick_id, file1_id;
46 47 48 49

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

50 51
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
52

53
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
54

55
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
56

57 58
	git_oid_fromstr(&pick_id, "da9c51a23d02d931a486f45ad18cda05cf5d2b94");

59 60
	cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, rebase_operation->type);
	cl_assert_equal_oid(&pick_id, &rebase_operation->id);
61 62 63 64 65 66 67 68 69 70 71 72 73
	cl_assert_equal_file("da9c51a23d02d931a486f45ad18cda05cf5d2b94\n", 41, "rebase/.git/rebase-merge/current");
	cl_assert_equal_file("1\n", 2, "rebase/.git/rebase-merge/msgnum");

	cl_git_pass(git_status_list_new(&status_list, repo, NULL));
	cl_assert_equal_i(1, git_status_list_entrycount(status_list));
	cl_assert(status_entry = git_status_byindex(status_list, 0));

	cl_assert_equal_s("beef.txt", status_entry->head_to_index->new_file.path);

	git_oid_fromstr(&file1_id, "8d95ea62e621f1d38d230d9e7d206e41096d76af");
	cl_assert_equal_oid(&file1_id, &status_entry->head_to_index->new_file.id);

	git_status_list_free(status_list);
74 75
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
76 77
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
78
	git_rebase_free(rebase);
79
}
80 81 82

void test_rebase_merge__next_with_conflicts(void)
{
83
	git_rebase *rebase;
84
	git_reference *branch_ref, *upstream_ref;
85
	git_annotated_commit *branch_head, *upstream_head;
86
	git_rebase_operation *rebase_operation;
87 88
	git_status_list *status_list;
	const git_status_entry *status_entry;
89
	git_oid pick_id, commit_id;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

	const char *expected_merge =
"ASPARAGUS SOUP.\n"
"\n"
"<<<<<<< master\n"
"TAKE FOUR LARGE BUNCHES of asparagus, scrape it nicely, cut off one inch\n"
"OF THE TOPS, and lay them in water, chop the stalks and put them on the\n"
"FIRE WITH A PIECE OF BACON, a large onion cut up, and pepper and salt;\n"
"ADD TWO QUARTS OF WATER, boil them till the stalks are quite soft, then\n"
"PULP THEM THROUGH A SIEVE, and strain the water to it, which must be put\n"
"=======\n"
"Take four large bunches of asparagus, scrape it nicely, CUT OFF ONE INCH\n"
"of the tops, and lay them in water, chop the stalks and PUT THEM ON THE\n"
"fire with a piece of bacon, a large onion cut up, and pepper and salt;\n"
"add two quarts of water, boil them till the stalks are quite soft, then\n"
"pulp them through a sieve, and strain the water to it, which must be put\n"
">>>>>>> Conflicting modification 1 to asparagus\n"
"back in the pot; put into it a chicken cut up, with the tops of\n"
"asparagus which had been laid by, boil it until these last articles are\n"
"sufficiently done, thicken with flour, butter and milk, and serve it up.\n";

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/asparagus"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

114 115
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
116

117
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
118

119
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
120 121

	git_oid_fromstr(&pick_id, "33f915f9e4dbd9f4b24430e48731a59b45b15500");
122

123 124
	cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, rebase_operation->type);
	cl_assert_equal_oid(&pick_id, &rebase_operation->id);
125 126 127 128 129 130 131 132 133 134 135
	cl_assert_equal_file("33f915f9e4dbd9f4b24430e48731a59b45b15500\n", 41, "rebase/.git/rebase-merge/current");
	cl_assert_equal_file("1\n", 2, "rebase/.git/rebase-merge/msgnum");

	cl_git_pass(git_status_list_new(&status_list, repo, NULL));
	cl_assert_equal_i(1, git_status_list_entrycount(status_list));
	cl_assert(status_entry = git_status_byindex(status_list, 0));

	cl_assert_equal_s("asparagus.txt", status_entry->head_to_index->new_file.path);

	cl_assert_equal_file(expected_merge, strlen(expected_merge), "rebase/asparagus.txt");

136 137
	cl_git_fail_with(GIT_EUNMERGED, git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));

138
	git_status_list_free(status_list);
139 140
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
141 142
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
143
	git_rebase_free(rebase);
144 145
}

146 147
void test_rebase_merge__next_stops_with_iterover(void)
{
148
	git_rebase *rebase;
149
	git_reference *branch_ref, *upstream_ref;
150
	git_annotated_commit *branch_head, *upstream_head;
151
	git_rebase_operation *rebase_operation;
152 153 154 155 156 157
	git_oid commit_id;
	int error;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

158 159
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
160

161
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
162

163
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
164
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
165 166
		NULL, NULL));

167
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
168
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
169 170
		NULL, NULL));

171
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
172
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
173 174
		NULL, NULL));

175
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
176
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
177 178
		NULL, NULL));

179
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
180
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
181 182
		NULL, NULL));

183
	cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
184 185 186 187 188
	cl_assert_equal_i(GIT_ITEROVER, error);

	cl_assert_equal_file("5\n", 2, "rebase/.git/rebase-merge/end");
	cl_assert_equal_file("5\n", 2, "rebase/.git/rebase-merge/msgnum");

189 190
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
191 192
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
193
	git_rebase_free(rebase);
194 195
}

196 197
void test_rebase_merge__commit(void)
{
198
	git_rebase *rebase;
199
	git_reference *branch_ref, *upstream_ref;
200
	git_annotated_commit *branch_head, *upstream_head;
201
	git_rebase_operation *rebase_operation;
202 203 204
	git_oid commit_id, tree_id, parent_id;
	git_signature *author;
	git_commit *commit;
205 206
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;
207 208 209 210

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

211 212
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
213

214
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
215

216
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
217
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		NULL, NULL));

	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));

	git_oid_fromstr(&parent_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00");
	cl_assert_equal_i(1, git_commit_parentcount(commit));
	cl_assert_equal_oid(&parent_id, git_commit_parent_id(commit, 0));

	git_oid_fromstr(&tree_id, "4461379789c777d2a6c1f2ee0e9d6c86731b9992");
	cl_assert_equal_oid(&tree_id, git_commit_tree_id(commit));

	cl_assert_equal_s(NULL, git_commit_message_encoding(commit));
	cl_assert_equal_s("Modification 1 to beef\n", git_commit_message(commit));

	cl_git_pass(git_signature_new(&author,
		"Edward Thomson", "ethomson@edwardthomson.com", 1405621769, 0-(4*60)));
	cl_assert(git_signature__equal(author, git_commit_author(commit)));

	cl_assert(git_signature__equal(signature, git_commit_committer(commit)));

238 239 240 241 242 243
	/* Make sure the reflogs are updated appropriately */
	cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
	cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
	cl_assert_equal_oid(&parent_id, git_reflog_entry_id_old(reflog_entry));
	cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
	cl_assert_equal_s("rebase: Modification 1 to beef", git_reflog_entry_message(reflog_entry));
244

245
	git_reflog_free(reflog);
246 247
	git_signature_free(author);
	git_commit_free(commit);
248 249
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
250 251
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
252
	git_rebase_free(rebase);
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
void test_rebase_merge__blocked_when_dirty(void)
{
	git_rebase *rebase;
	git_reference *branch_ref, *upstream_ref;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_operation *rebase_operation;
	git_oid commit_id;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));

	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));

	/* Allow untracked files */
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_mkfile("rebase/untracked_file.txt", "This is untracked\n");
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	/* Do not allow unstaged */
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_mkfile("rebase/veal.txt", "This is an unstaged change\n");
	cl_git_fail_with(GIT_EUNMERGED, git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
	git_rebase_free(rebase);
}

290 291
void test_rebase_merge__commit_updates_rewritten(void)
{
292
	git_rebase *rebase;
293
	git_reference *branch_ref, *upstream_ref;
294
	git_annotated_commit *branch_head, *upstream_head;
295
	git_rebase_operation *rebase_operation;
296 297 298 299 300
	git_oid commit_id;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

301 302
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
303

304
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
305

306
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
307
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
308 309
		NULL, NULL));

310
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
311
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
312 313 314 315 316 317 318
		NULL, NULL));

	cl_assert_equal_file(
		"da9c51a23d02d931a486f45ad18cda05cf5d2b94 776e4c48922799f903f03f5f6e51da8b01e4cce0\n"
		"8d1f13f93c4995760ac07d129246ac1ff64c0be9 ba1f9b4fd5cf8151f7818be2111cc0869f1eb95a\n",
		164, "rebase/.git/rebase-merge/rewritten");

319 320
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
321 322
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
323
	git_rebase_free(rebase);
324 325
}

326 327
void test_rebase_merge__commit_drops_already_applied(void)
{
328
	git_rebase *rebase;
329
	git_reference *branch_ref, *upstream_ref;
330
	git_annotated_commit *branch_head, *upstream_head;
331
	git_rebase_operation *rebase_operation;
332 333 334 335 336 337
	git_oid commit_id;
	int error;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/green_pea"));

338 339
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
340

341
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
342

343
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
344
	cl_git_fail(error = git_rebase_commit(&commit_id, rebase, NULL, signature,
345 346 347 348
		NULL, NULL));

	cl_assert_equal_i(GIT_EAPPLIED, error);

349
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
350
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
351 352 353 354 355 356
		NULL, NULL));

	cl_assert_equal_file(
		"8d1f13f93c4995760ac07d129246ac1ff64c0be9 2ac4fb7b74c1287f6c792acad759e1ec01e18dae\n",
		82, "rebase/.git/rebase-merge/rewritten");

357 358
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
359 360
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
361
	git_rebase_free(rebase);
362 363
}

364 365
void test_rebase_merge__finish(void)
{
366
	git_rebase *rebase;
367
	git_reference *branch_ref, *upstream_ref, *head_ref;
368
	git_annotated_commit *branch_head, *upstream_head;
369
	git_rebase_operation *rebase_operation;
370 371 372 373 374 375 376 377
	git_oid commit_id;
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;
	int error;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));

378 379
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
380

381
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
382

383
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
384
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
385 386
		NULL, NULL));

387
	cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
388 389
	cl_assert_equal_i(GIT_ITEROVER, error);

390
	cl_git_pass(git_rebase_finish(rebase, signature));
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head_ref));
	cl_assert_equal_s("refs/heads/gravy", git_reference_symbolic_target(head_ref));

	/* Make sure the reflogs are updated appropriately */
	cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
	cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
	cl_assert_equal_oid(&commit_id, git_reflog_entry_id_old(reflog_entry));
	cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
	cl_assert_equal_s("rebase finished: returning to refs/heads/gravy", git_reflog_entry_message(reflog_entry));
	git_reflog_free(reflog);

	cl_git_pass(git_reflog_read(&reflog, repo, "refs/heads/gravy"));
	cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
408
	cl_assert_equal_oid(git_annotated_commit_id(branch_head), git_reflog_entry_id_old(reflog_entry));
409 410 411 412
	cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
	cl_assert_equal_s("rebase finished: refs/heads/gravy onto f87d14a4a236582a0278a916340a793714256864", git_reflog_entry_message(reflog_entry));

	git_reflog_free(reflog);
413 414
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
415 416 417
	git_reference_free(head_ref);
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
418
	git_rebase_free(rebase);
419 420
}

421 422 423 424
static void test_copy_note(
	const git_rebase_options *opts,
	bool should_exist)
{
425
	git_rebase *rebase;
426
	git_reference *branch_ref, *upstream_ref;
427
	git_annotated_commit *branch_head, *upstream_head;
428
	git_commit *branch_commit;
429
	git_rebase_operation *rebase_operation;
430 431 432 433 434 435 436
	git_oid note_id, commit_id;
	git_note *note = NULL;
	int error;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));

437 438
	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
439 440 441 442 443

	cl_git_pass(git_reference_peel((git_object **)&branch_commit,
		branch_ref, GIT_OBJ_COMMIT));

	/* Add a note to a commit */
444
	cl_git_pass(git_note_create(&note_id, repo, "refs/notes/test",
445
		git_commit_author(branch_commit), git_commit_committer(branch_commit),
446
		git_commit_id(branch_commit),
447 448
		"This is a commit note.", 0));

449
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, opts));
450

451
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
452
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
453 454
		NULL, NULL));

455
	cl_git_pass(git_rebase_finish(rebase, signature));
456 457 458 459 460 461 462 463 464 465 466 467 468 469

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	if (should_exist) {
		cl_git_pass(git_note_read(&note, repo, "refs/notes/test", &commit_id));
		cl_assert_equal_s("This is a commit note.", git_note_message(note));
	} else {
		cl_git_fail(error =
			git_note_read(&note, repo, "refs/notes/test", &commit_id));
		cl_assert_equal_i(GIT_ENOTFOUND, error);
	}

	git_note_free(note);
	git_commit_free(branch_commit);
470 471
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
472 473
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
474
	git_rebase_free(rebase);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
}

void test_rebase_merge__copy_notes_off_by_default(void)
{
	test_copy_note(NULL, 0);
}

void test_rebase_merge__copy_notes_specified_in_options(void)
{
	git_rebase_options opts = GIT_REBASE_OPTIONS_INIT;
	opts.rewrite_notes_ref = "refs/notes/test";

	test_copy_note(&opts, 1);
}

void test_rebase_merge__copy_notes_specified_in_config(void)
{
	git_config *config;

	cl_git_pass(git_repository_config(&config, repo));
	cl_git_pass(git_config_set_string(config,
		"notes.rewriteRef", "refs/notes/test"));

	test_copy_note(NULL, 1);
}

void test_rebase_merge__copy_notes_disabled_in_config(void)
{
	git_config *config;

	cl_git_pass(git_repository_config(&config, repo));
	cl_git_pass(git_config_set_bool(config, "notes.rewrite.rebase", 0));
	cl_git_pass(git_config_set_string(config,
		"notes.rewriteRef", "refs/notes/test"));

	test_copy_note(NULL, 0);
}

513 514 515 516 517 518 519
void rebase_checkout_progress_cb(
	const char *path,
	size_t completed_steps,
	size_t total_steps,
	void *payload)
{
	int *called = payload;
520 521 522 523 524

	GIT_UNUSED(path);
	GIT_UNUSED(completed_steps);
	GIT_UNUSED(total_steps);

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	*called = 1;
}

void test_rebase_merge__custom_checkout_options(void)
{
	git_rebase *rebase;
	git_reference *branch_ref, *upstream_ref;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_options rebase_options = GIT_REBASE_OPTIONS_INIT;
	git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
	git_rebase_operation *rebase_operation;
	int called = 0;

	checkout_options.progress_cb = rebase_checkout_progress_cb;
	checkout_options.progress_payload = &called;

541 542
	memcpy(&rebase_options.checkout_options, &checkout_options,
		sizeof(git_checkout_options));
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));

	called = 0;
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_options));
	cl_assert_equal_i(1, called);

	called = 0;
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_assert_equal_i(1, called);

	called = 0;
	cl_git_pass(git_rebase_abort(rebase));
	cl_assert_equal_i(1, called);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
	git_rebase_free(rebase);
}
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

void test_rebase_merge__custom_merge_options(void)
{
	git_rebase *rebase;
	git_reference *branch_ref, *upstream_ref;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_options rebase_options = GIT_REBASE_OPTIONS_INIT;
	git_rebase_operation *rebase_operation;

	rebase_options.merge_options.flags |=
		GIT_MERGE_FAIL_ON_CONFLICT |
		GIT_MERGE_SKIP_REUC;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/asparagus"));
	cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));

	cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
	cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));

	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_options));

	cl_git_fail_with(GIT_EMERGECONFLICT, git_rebase_next(&rebase_operation, rebase));

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
	git_rebase_free(rebase);
}