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

#include <fcntl.h>

static git_repository *repo;
static git_signature *signature;

12 13 14 15 16 17 18 19 20 21
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);
}

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

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

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

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

	git_oid_fromstr(&master_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00");
	git_oid_fromstr(&beef_id, "b146bd7608eac53d9bf9e1a6963543588b555c64");
51 52 53 54

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

55 56
	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));
57

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

60 61 62 63 64 65
	cl_assert_equal_s("refs/heads/beef", git_rebase_orig_head_name(rebase));
	cl_assert_equal_oid(&beef_id, git_rebase_orig_head_id(rebase));

	cl_assert_equal_s("master", git_rebase_onto_name(rebase));
	cl_assert_equal_oid(&master_id, git_rebase_onto_id(rebase));

66
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
67

68 69
	git_oid_fromstr(&pick_id, "da9c51a23d02d931a486f45ad18cda05cf5d2b94");

70 71
	cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, rebase_operation->type);
	cl_assert_equal_oid(&pick_id, &rebase_operation->id);
72 73 74 75 76 77 78 79 80 81 82 83 84
	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);
85 86
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
87 88
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
89
	git_rebase_free(rebase);
90
}
91 92 93

void test_rebase_merge__next_with_conflicts(void)
{
94
	git_rebase *rebase;
95
	git_reference *branch_ref, *upstream_ref;
96
	git_annotated_commit *branch_head, *upstream_head;
97
	git_rebase_operation *rebase_operation;
98 99
	git_status_list *status_list;
	const git_status_entry *status_entry;
100
	git_oid pick_id, commit_id;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

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

125 126
	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));
127

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

130
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
131 132

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

134 135
	cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, rebase_operation->type);
	cl_assert_equal_oid(&pick_id, &rebase_operation->id);
136 137 138 139 140 141 142 143 144 145 146
	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");

147 148
	cl_git_fail_with(GIT_EUNMERGED, git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));

149
	git_status_list_free(status_list);
150 151
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
152 153
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
154
	git_rebase_free(rebase);
155 156
}

157 158
void test_rebase_merge__next_stops_with_iterover(void)
{
159
	git_rebase *rebase;
160
	git_reference *branch_ref, *upstream_ref;
161
	git_annotated_commit *branch_head, *upstream_head;
162
	git_rebase_operation *rebase_operation;
163 164 165 166 167 168
	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"));

169 170
	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));
171

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

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

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

182
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
183
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
184 185
		NULL, NULL));

186
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
187
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
188 189
		NULL, NULL));

190
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
191
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
192 193
		NULL, NULL));

194
	cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
195 196 197 198 199
	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");

200 201
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
202 203
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
204
	git_rebase_free(rebase);
205 206
}

207 208
void test_rebase_merge__commit(void)
{
209
	git_rebase *rebase;
210
	git_reference *branch_ref, *upstream_ref;
211
	git_annotated_commit *branch_head, *upstream_head;
212
	git_rebase_operation *rebase_operation;
213 214 215
	git_oid commit_id, tree_id, parent_id;
	git_signature *author;
	git_commit *commit;
216 217
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;
218 219 220 221

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

222 223
	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));
224

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

227
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
228
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		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)));

249 250 251 252 253 254
	/* 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));
255

256
	git_reflog_free(reflog);
257 258
	git_signature_free(author);
	git_commit_free(commit);
259 260
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
261 262
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
263
	git_rebase_free(rebase);
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
void test_rebase_merge__commit_with_id(void)
{
	git_rebase *rebase;
	git_oid branch_id, upstream_id;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_operation *rebase_operation;
	git_oid commit_id, tree_id, parent_id;
	git_signature *author;
	git_commit *commit;
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;

	cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
	cl_git_pass(git_oid_fromstr(&upstream_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));

	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
	cl_git_pass(git_annotated_commit_lookup(&upstream_head, repo, &upstream_id));

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

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		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)));

	/* 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));

	git_reflog_free(reflog);
	git_signature_free(author);
	git_commit_free(commit);
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
	git_rebase_free(rebase);
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
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);
}

358 359
void test_rebase_merge__commit_updates_rewritten(void)
{
360
	git_rebase *rebase;
361
	git_reference *branch_ref, *upstream_ref;
362
	git_annotated_commit *branch_head, *upstream_head;
363
	git_rebase_operation *rebase_operation;
364 365 366 367 368
	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"));

369 370
	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));
371

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

374
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
375
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
376 377
		NULL, NULL));

378
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
379
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
380 381 382 383 384 385 386
		NULL, NULL));

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

387 388
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
389 390
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
391
	git_rebase_free(rebase);
392 393
}

394 395
void test_rebase_merge__commit_drops_already_applied(void)
{
396
	git_rebase *rebase;
397
	git_reference *branch_ref, *upstream_ref;
398
	git_annotated_commit *branch_head, *upstream_head;
399
	git_rebase_operation *rebase_operation;
400 401 402 403 404 405
	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"));

406 407
	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));
408

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

411
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
412
	cl_git_fail(error = git_rebase_commit(&commit_id, rebase, NULL, signature,
413 414 415 416
		NULL, NULL));

	cl_assert_equal_i(GIT_EAPPLIED, error);

417
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
418
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
419 420 421 422 423 424
		NULL, NULL));

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

425 426
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
427 428
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
429
	git_rebase_free(rebase);
430 431
}

432 433
void test_rebase_merge__finish(void)
{
434
	git_rebase *rebase;
435
	git_reference *branch_ref, *upstream_ref, *head_ref;
436
	git_annotated_commit *branch_head, *upstream_head;
437
	git_rebase_operation *rebase_operation;
438 439 440 441 442 443 444 445
	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"));

446 447
	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));
448

449
	cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
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_fail(error = git_rebase_next(&rebase_operation, rebase));
456 457
	cl_assert_equal_i(GIT_ITEROVER, error);

458
	cl_git_pass(git_rebase_finish(rebase, signature));
459 460 461 462

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
463
	cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head_ref));
464 465 466 467 468 469 470 471 472 473 474 475
	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));
476
	cl_assert_equal_oid(git_annotated_commit_id(branch_head), git_reflog_entry_id_old(reflog_entry));
477 478 479 480
	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);
481 482
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
483 484 485
	git_reference_free(head_ref);
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
486
	git_rebase_free(rebase);
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 513 514 515 516 517 518 519 520 521 522 523 524
void test_rebase_merge__detached_finish(void)
{
	git_rebase *rebase;
	git_reference *branch_ref, *upstream_ref, *head_ref;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_operation *rebase_operation;
	git_oid commit_id;
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	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"));

	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_repository_set_head_detached_from_annotated(repo, branch_head));
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	git_checkout_head(repo, &opts);

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

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

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

	cl_git_pass(git_rebase_finish(rebase, signature));

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
525
	cl_assert_equal_i(GIT_REFERENCE_DIRECT, git_reference_type(head_ref));
526 527 528 529 530 531 532 533 534 535

	/* 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(git_annotated_commit_id(upstream_head), git_reflog_entry_id_old(reflog_entry));
	cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));

	git_reflog_free(reflog);
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
536
	git_reference_free(head_ref);
537 538 539 540 541
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
	git_rebase_free(rebase);
}

542 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 568 569 570 571 572 573
void test_rebase_merge__finish_with_ids(void)
{
	git_rebase *rebase;
	git_reference *head_ref;
	git_oid branch_id, upstream_id;
	git_annotated_commit *branch_head, *upstream_head;
	git_rebase_operation *rebase_operation;
	git_oid commit_id;
	git_reflog *reflog;
	const git_reflog_entry *reflog_entry;
	int error;

	cl_git_pass(git_oid_fromstr(&branch_id, "d616d97082eb7bb2dc6f180a7cca940993b7a56f"));
	cl_git_pass(git_oid_fromstr(&upstream_id, "f87d14a4a236582a0278a916340a793714256864"));

	cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
	cl_git_pass(git_annotated_commit_lookup(&upstream_head, repo, &upstream_id));

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

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

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

	cl_git_pass(git_rebase_finish(rebase, signature));

	cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));

	cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
574
	cl_assert_equal_i(GIT_REFERENCE_DIRECT, git_reference_type(head_ref));
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	cl_assert_equal_oid(&commit_id, git_reference_target(head_ref));

	/* reflogs are not updated as if we were operating on proper
	 * branches.  check that the last reflog entry is the rebase.
	 */
	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_new(reflog_entry));
	cl_assert_equal_s("rebase: Modification 3 to gravy", git_reflog_entry_message(reflog_entry));
	git_reflog_free(reflog);

	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
	git_reference_free(head_ref);
	git_rebase_free(rebase);
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
void test_rebase_merge__no_common_ancestor(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, expected_final_id;

	cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/barley"));
	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));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_finish(rebase, signature));

	git_oid_fromstr(&expected_final_id, "71e7ee8d4fe7d8bf0d107355197e0a953dfdb7f3");
	cl_assert_equal_oid(&expected_final_id, &commit_id);

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

640 641 642 643
static void test_copy_note(
	const git_rebase_options *opts,
	bool should_exist)
{
644
	git_rebase *rebase;
645
	git_reference *branch_ref, *upstream_ref;
646
	git_annotated_commit *branch_head, *upstream_head;
647
	git_commit *branch_commit;
648
	git_rebase_operation *rebase_operation;
649 650 651 652 653 654 655
	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"));

656 657
	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));
658 659

	cl_git_pass(git_reference_peel((git_object **)&branch_commit,
660
		branch_ref, GIT_OBJECT_COMMIT));
661 662

	/* Add a note to a commit */
663
	cl_git_pass(git_note_create(&note_id, repo, "refs/notes/test",
664
		git_commit_author(branch_commit), git_commit_committer(branch_commit),
665
		git_commit_id(branch_commit),
666 667
		"This is a commit note.", 0));

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

670
	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
671
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
672 673
		NULL, NULL));

674
	cl_git_pass(git_rebase_finish(rebase, signature));
675 676 677 678 679 680 681 682 683 684 685 686 687 688

	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);
689 690
	git_annotated_commit_free(branch_head);
	git_annotated_commit_free(upstream_head);
691 692
	git_reference_free(branch_ref);
	git_reference_free(upstream_ref);
693
	git_rebase_free(rebase);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
}

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

732 733 734 735 736 737 738
void rebase_checkout_progress_cb(
	const char *path,
	size_t completed_steps,
	size_t total_steps,
	void *payload)
{
	int *called = payload;
739 740 741 742 743

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

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	*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;

760 761
	memcpy(&rebase_options.checkout_options, &checkout_options,
		sizeof(git_checkout_options));
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

	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);
}
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

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

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
void test_rebase_merge__with_directories(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, tree_id;
	git_commit *commit;

	git_oid_fromstr(&tree_id, "a4d6d9c3d57308fd8e320cf2525bae8f1adafa57");

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

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

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

	cl_git_pass(git_rebase_next(&rebase_operation, rebase));
	cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
		NULL, NULL));

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

	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
	cl_assert_equal_oid(&tree_id, git_commit_tree_id(commit));

	git_commit_free(commit);
	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);
}