1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 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 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 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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 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 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 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 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "merge.h"
#include "refs.h"
#include "fileops.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "merge-resolve"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
#define ORIG_HEAD "bd593285fc7fe4ca18ccdbabf027f5d689101452"
#define THEIRS_SIMPLE_BRANCH "branch"
#define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520"
#define OCTO1_BRANCH "octo1"
#define OCTO1_OID "16f825815cfd20a07a75c71554e82d8eede0b061"
#define OCTO2_BRANCH "octo2"
#define OCTO2_OID "158dc7bedb202f5b26502bf3574faa7f4238d56c"
#define OCTO3_BRANCH "octo3"
#define OCTO3_OID "50ce7d7d01217679e26c55939eef119e0c93e272"
#define OCTO4_BRANCH "octo4"
#define OCTO4_OID "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae"
#define OCTO5_BRANCH "octo5"
#define OCTO5_OID "e4f618a2c3ed0669308735727df5ebf2447f022f"
// Fixture setup and teardown
void test_merge_workdir_setup__initialize(void)
{
repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&repo_index, repo);
}
void test_merge_workdir_setup__cleanup(void)
{
git_index_free(repo_index);
cl_git_sandbox_cleanup();
}
static bool test_file_contents(const char *filename, const char *expected)
{
git_buf file_path_buf = GIT_BUF_INIT, file_buf = GIT_BUF_INIT;
bool equals;
git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo), filename);
cl_git_pass(git_futils_readbuffer(&file_buf, file_path_buf.ptr));
equals = (strcmp(file_buf.ptr, expected) == 0);
git_buf_free(&file_path_buf);
git_buf_free(&file_buf);
return equals;
}
static void write_file_contents(const char *filename, const char *output)
{
git_buf file_path_buf = GIT_BUF_INIT;
git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo),
filename);
cl_git_rewritefile(file_path_buf.ptr, output);
git_buf_free(&file_path_buf);
}
/* git merge --no-ff octo1 */
void test_merge_workdir_setup__one_branch(void)
{
git_oid our_oid;
git_reference *octo1_ref;
git_merge_head *our_head, *their_heads[1];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 1, 0));
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, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
}
/* git merge --no-ff 16f825815cfd20a07a75c71554e82d8eede0b061 */
void test_merge_workdir_setup__one_oid(void)
{
git_oid our_oid;
git_oid octo1_oid;
git_merge_head *our_head, *their_heads[1];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &octo1_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 1, 0));
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, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'\n"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
}
/* git merge octo1 octo2 */
void test_merge_workdir_setup__two_branches(void)
{
git_oid our_oid;
git_reference *octo1_ref;
git_reference *octo2_ref;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[2], repo, octo3_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_reference_free(octo3_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &octo1_oid));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[1], repo, &octo2_oid));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[2], repo, &octo3_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO2_OID "'; commit '" OCTO3_OID "'\n"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
/* 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;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[1], repo, &octo2_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'; commit '" OCTO2_OID "'\n"));
git_reference_free(octo1_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[4];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[1], repo, &octo2_oid));
cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[2], repo, octo3_ref));
cl_git_pass(git_oid_fromstr(&octo4_oid, OCTO4_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[3], repo, &octo4_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 4, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "'; commit '" OCTO2_OID "'; commit '" OCTO4_OID "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo3_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
git_merge_head_free(their_heads[3]);
}
/* 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;
git_merge_head *our_head, *their_heads[4];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &octo1_oid));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[2], repo, &octo3_oid));
cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[3], repo, octo4_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 4, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "'; commit '" OCTO3_OID "'\n"));
git_reference_free(octo2_ref);
git_reference_free(octo4_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
git_merge_head_free(their_heads[3]);
}
/* 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;
git_merge_head *our_head, *their_heads[5];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &octo1_oid));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[2], repo, &octo3_oid));
cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[3], repo, octo4_ref));
cl_git_pass(git_reference_lookup(&octo5_ref, repo, GIT_REFS_HEADS_DIR OCTO5_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[4], repo, octo5_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 5, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "', '" OCTO4_BRANCH "' and '" OCTO5_BRANCH "'; commit '" OCTO3_OID "'\n"));
git_reference_free(octo2_ref);
git_reference_free(octo4_ref);
git_reference_free(octo5_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
git_merge_head_free(their_heads[3]);
git_merge_head_free(their_heads[4]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_1_ref));
cl_git_pass(git_reference_lookup(&octo1_2_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo1_2_ref));
cl_git_pass(git_reference_lookup(&octo1_3_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[2], repo, octo1_3_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO1_BRANCH "' and '" OCTO1_BRANCH "'\n"));
git_reference_free(octo1_1_ref);
git_reference_free(octo1_2_ref);
git_reference_free(octo1_3_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &octo1_1_oid));
cl_git_pass(git_oid_fromstr(&octo1_2_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[1], repo, &octo1_2_oid));
cl_git_pass(git_oid_fromstr(&octo1_3_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_oid(&their_heads[2], repo, &octo1_3_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO1_OID "'; commit '" OCTO1_OID "'\n"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
static int create_remote_tracking_branch(const char *branch_name, const char *oid_str)
{
int error = 0;
git_buf remotes_path = GIT_BUF_INIT,
origin_path = GIT_BUF_INIT,
filename = GIT_BUF_INIT,
data = GIT_BUF_INIT;
if ((error = git_buf_puts(&remotes_path, git_repository_path(repo))) < 0 ||
(error = git_buf_puts(&remotes_path, GIT_REFS_REMOTES_DIR)) < 0)
goto done;
if (!git_path_exists(git_buf_cstr(&remotes_path)) &&
(error = p_mkdir(git_buf_cstr(&remotes_path), 0777)) < 0)
goto done;
if ((error = git_buf_puts(&origin_path, git_buf_cstr(&remotes_path))) < 0 ||
(error = git_buf_puts(&origin_path, "origin")) < 0)
goto done;
if (!git_path_exists(git_buf_cstr(&origin_path)) &&
(error = p_mkdir(git_buf_cstr(&origin_path), 0777)) < 0)
goto done;
if ((error = git_buf_puts(&filename, git_buf_cstr(&origin_path))) < 0 ||
(error = git_buf_puts(&filename, "/")) < 0 ||
(error = git_buf_puts(&filename, branch_name)) < 0 ||
(error = git_buf_puts(&data, oid_str)) < 0 ||
(error = git_buf_puts(&data, "\n")) < 0)
goto done;
cl_git_rewritefile(git_buf_cstr(&filename), git_buf_cstr(&data));
done:
git_buf_free(&remotes_path);
git_buf_free(&origin_path);
git_buf_free(&filename);
git_buf_free(&data);
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;
git_merge_head *our_head, *their_heads[1];
cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 1, 0));
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, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
}
/* 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;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "' and 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
cl_git_pass(create_remote_tracking_branch(OCTO3_BRANCH, OCTO3_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO3_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[2], repo, octo3_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "', 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO3_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_reference_free(octo3_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
/* 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;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO2_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO2_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[4];
cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID));
cl_git_pass(create_remote_tracking_branch(OCTO4_BRANCH, OCTO4_OID));
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, octo1_ref));
cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[1], repo, octo2_ref));
cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[2], repo, octo3_ref));
cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO4_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_heads[3], repo, octo4_ref));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 4, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "', remote-tracking branches 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO4_BRANCH "'\n"));
git_reference_free(octo1_ref);
git_reference_free(octo2_ref);
git_reference_free(octo3_ref);
git_reference_free(octo4_ref);
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
git_merge_head_free(their_heads[3]);
}
/* git pull origin branch octo1 */
void test_merge_workdir_setup__pull_one(void)
{
git_oid our_oid;
git_oid octo1_1_oid;
git_merge_head *our_head, *their_heads[1];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_1_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 1, 0));
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, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch 'octo1' of http://remote.url/repo.git\n"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
}
/* 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;
git_merge_head *our_head, *their_heads[2];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 2, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "' of http://remote.url/repo.git\n"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
}
/* 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;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.url/repo.git", &octo3_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
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"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
void test_merge_workdir_setup__three_remotes(void)
{
git_oid our_oid;
git_oid octo1_oid;
git_oid octo2_oid;
git_oid octo3_oid;
git_merge_head *our_head, *their_heads[3];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.third/repo.git", &octo3_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 3, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
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"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
}
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;
git_merge_head *our_head, *their_heads[4];
cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
cl_git_pass(git_merge_head_from_oid(&our_head, repo, &our_oid));
cl_git_pass(git_oid_fromstr(&octo1_oid, OCTO1_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid));
cl_git_pass(git_oid_fromstr(&octo2_oid, OCTO2_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid));
cl_git_pass(git_oid_fromstr(&octo3_oid, OCTO3_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.first/repo.git", &octo3_oid));
cl_git_pass(git_oid_fromstr(&octo4_oid, OCTO4_OID));
cl_git_pass(git_merge_head_from_fetchhead(&their_heads[3], repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH, "http://remote.second/repo.git", &octo4_oid));
cl_git_pass(git_merge__setup(repo, our_head, (const git_merge_head **)their_heads, 4, 0));
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"));
cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, ""));
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"));
git_merge_head_free(our_head);
git_merge_head_free(their_heads[0]);
git_merge_head_free(their_heads[1]);
git_merge_head_free(their_heads[2]);
git_merge_head_free(their_heads[3]);
}
struct merge_head_cb_data {
const char **oid_str;
unsigned int len;
unsigned int i;
};
static int merge_head_foreach_cb(const git_oid *oid, void *payload)
{
git_oid expected_oid;
struct merge_head_cb_data *cb_data = payload;
git_oid_fromstr(&expected_oid, cb_data->oid_str[cb_data->i]);
cl_assert(git_oid_cmp(&expected_oid, oid) == 0);
cb_data->i++;
return 0;
}
void test_merge_workdir_setup__head_notfound(void)
{
int error;
cl_git_fail((error = git_repository_mergehead_foreach(repo,
merge_head_foreach_cb, NULL)));
cl_assert(error == GIT_ENOTFOUND);
}
void test_merge_workdir_setup__head_invalid_oid(void)
{
int error;
write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n");
cl_git_fail((error = git_repository_mergehead_foreach(repo,
merge_head_foreach_cb, NULL)));
cl_assert(error == -1);
}
void test_merge_workdir_setup__head_foreach_nonewline(void)
{
int error;
write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID);
cl_git_fail((error = git_repository_mergehead_foreach(repo,
merge_head_foreach_cb, NULL)));
cl_assert(error == -1);
}
void test_merge_workdir_setup__head_foreach_one(void)
{
const char *expected = THEIRS_SIMPLE_OID;
struct merge_head_cb_data cb_data = { &expected, 1 };
write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n");
cl_git_pass(git_repository_mergehead_foreach(repo,
merge_head_foreach_cb, &cb_data));
cl_assert(cb_data.i == cb_data.len);
}
void test_merge_workdir_setup__head_foreach_octopus(void)
{
const char *expected[] = { THEIRS_SIMPLE_OID,
OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID };
struct merge_head_cb_data cb_data = { expected, 6 };
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,
merge_head_foreach_cb, &cb_data));
cl_assert(cb_data.i == cb_data.len);
}