initial 'got rebase' implementation
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 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
diff --git a/got/got.1 b/got/got.1
index 892d74a..0bcca9e 100644
--- a/got/got.1
+++ b/got/got.1
@@ -529,6 +529,72 @@ conflicts must be resolved first.
.It Cm bo
Short alias for
.Cm backout .
+.It Cm rebase Ar branch
+Rebase commits on the specified
+.Ar branch
+onto the tip of the current branch of the work tree.
+The
+.Ar branch
+must share common ancestry with the work tree's current branch.
+Rebasing begins with the first descendent of the youngest common
+ancestor commit of
+.Ar branch
+and the work tree's current branch, and stops once the tip commit
+of
+.Ar branch
+has been reached.
+.Pp
+Rebased commits are accumulated on a temporary branch and represent
+the same changes and log messages as their counterparts on the original
+.Ar branch ,
+but with different commit IDs.
+Once rebasing has completed successfully, the temporary branch becomes
+the new version of
+.Ar branch
+and the work tree is automatically switched to it.
+.Pp
+While rebasing commits, show the status of each affected file,
+using the following status codes:
+.Bl -column YXZ description
+.It G Ta file was merged
+.It C Ta file was merged and conflicts occurred during merge
+.It ! Ta changes destined for a missing file were not merged
+.It D Ta file was deleted
+.It d Ta file's deletion was obstructed by local modifications
+.It A Ta new file was added
+.It ~ Ta changes destined for a non-regular file were not merged
+.El
+.Pp
+If merge conflicts occur, the rebase operation will be interrupted and
+may be continued once conflicts have been resolved.
+Alternatively, the rebase operation may be aborted which will leave
+.Ar branch
+unmodified and the work tree switched back to its original branch.
+.Pp
+.Cm got rebase
+will refuse to run if certain preconditions are not met.
+If the work tree contains multiple base commits it must first be updated
+to a single base commit with
+.Cm got update .
+If the work tree contains local changes, these changes must be committed
+or reverted first.
+.Pp
+Some
+.Nm
+commands may refuse to run while a rebase operation is in progress.
+.Pp
+The options for
+.Cm got rebase
+are as follows:
+.Bl -tag -width Ds
+.It Fl a
+Abort an interrupted rebase operation.
+.It Fl c
+Continue an interrupted current rebase operation.
+.El
+.It Cm rb
+Short alias for
+.Cm rebase .
.El
.Sh ENVIRONMENT
.Bl -tag -width GOT_AUTHOR
@@ -619,20 +685,9 @@ Rebase the
branch on top of the new head commit of the
.Dq master
branch.
-This step currently requires
-.Xr git 1 :
.Pp
-.Dl $ git clone /var/git/src.git ~/src-git-wt
-.Dl $ cd ~/src-git-wt
-.Dl $ git checkout unified-buffer-cache
-.Dl $ git rebase master
-.Dl $ git push -f
-.Pp
-Update the work tree to the newly rebased
-.Dq unified-buffer-cache
-branch:
-.Pp
-.Dl $ got update -b unified-buffer-cache
+.Dl $ got update -b master
+.Dl $ got rebase unified-buffer-cache
.Sh SEE ALSO
.Xr git-repository 5
.Xr got-worktree 5
diff --git a/got/got.c b/got/got.c
index 9ab824b..e6bdb5c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -25,6 +25,7 @@
#include <err.h>
#include <errno.h>
#include <locale.h>
+#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -90,6 +91,7 @@ __dead static void usage_revert(void);
__dead static void usage_commit(void);
__dead static void usage_cherrypick(void);
__dead static void usage_backout(void);
+__dead static void usage_rebase(void);
static const struct got_error* cmd_init(int, char *[]);
static const struct got_error* cmd_checkout(int, char *[]);
@@ -107,6 +109,7 @@ static const struct got_error* cmd_revert(int, char *[]);
static const struct got_error* cmd_commit(int, char *[]);
static const struct got_error* cmd_cherrypick(int, char *[]);
static const struct got_error* cmd_backout(int, char *[]);
+static const struct got_error* cmd_rebase(int, char *[]);
static struct got_cmd got_commands[] = {
{ "init", cmd_init, usage_init, "" },
@@ -125,6 +128,7 @@ static struct got_cmd got_commands[] = {
{ "commit", cmd_commit, usage_commit, "ci" },
{ "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
{ "backout", cmd_backout, usage_backout, "bo" },
+ { "rebase", cmd_rebase, usage_rebase, "rb" },
};
static void
@@ -3182,3 +3186,361 @@ done:
got_repo_close(repo);
return error;
}
+
+__dead static void
+usage_rebase(void)
+{
+ fprintf(stderr, "usage: %s rebase [-a] [-c] | branch\n", getprogname());
+ exit(1);
+}
+
+static const struct got_error *
+show_rebase_progress(struct got_commit_object *commit,
+ struct got_object_id *old_id, struct got_object_id *new_id)
+{
+ const struct got_error *err;
+ char *old_id_str = NULL, *new_id_str = NULL;
+ char *logmsg0 = NULL, *logmsg, *nl;
+ size_t len;
+
+ err = got_object_id_str(&old_id_str, old_id);
+ if (err)
+ goto done;
+
+ err = got_object_id_str(&new_id_str, new_id);
+ if (err)
+ goto done;
+
+ logmsg0 = strdup(got_object_commit_get_logmsg(commit));
+ if (logmsg0 == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ logmsg = logmsg0;
+
+ while (isspace(logmsg[0]))
+ logmsg++;
+
+ old_id_str[12] = '\0';
+ new_id_str[12] = '\0';
+ len = strlen(logmsg);
+ if (len > 42)
+ len = 42;
+ logmsg[len] = '\0';
+ nl = strchr(logmsg, '\n');
+ if (nl)
+ *nl = '\0';
+ printf("%s -> %s: %s\n", old_id_str, new_id_str, logmsg);
+done:
+ free(old_id_str);
+ free(new_id_str);
+ free(logmsg0);
+ return err;
+}
+
+static void
+rebase_progress(void *arg, unsigned char status, const char *path)
+{
+ unsigned char *rebase_status = arg;
+
+ while (path[0] == '/')
+ path++;
+ printf("%c %s\n", status, path);
+
+ if (*rebase_status == GOT_STATUS_CONFLICT)
+ return;
+ if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
+ *rebase_status = status;
+}
+
+static const struct got_error *
+rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
+ struct got_reference *new_base_branch, struct got_reference *tmp_branch,
+ struct got_repository *repo)
+{
+ printf("Switching work tree to %s\n", got_ref_get_name(branch));
+ return got_worktree_rebase_complete(worktree,
+ new_base_branch, tmp_branch, branch, repo);
+}
+
+static const struct got_error *
+cmd_rebase(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_worktree *worktree = NULL;
+ struct got_repository *repo = NULL;
+ char *cwd = NULL;
+ struct got_reference *branch = NULL;
+ struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
+ struct got_object_id *commit_id = NULL, *parent_id = NULL;
+ struct got_object_id *resume_commit_id = NULL;
+ struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
+ struct got_commit_graph *graph = NULL;
+ struct got_commit_object *commit = NULL;
+ int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
+ unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
+ struct got_object_id_queue commits;
+ const struct got_object_id_queue *parent_ids;
+ struct got_object_qid *qid, *pid;
+
+ SIMPLEQ_INIT(&commits);
+
+ while ((ch = getopt(argc, argv, "ac")) != -1) {
+ switch (ch) {
+ case 'a':
+ abort_rebase = 1;
+ break;
+ case 'c':
+ continue_rebase = 1;
+ break;
+ default:
+ usage_rebase();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (abort_rebase && continue_rebase)
+ usage_rebase();
+ else if (abort_rebase || continue_rebase) {
+ if (argc != 0)
+ usage_rebase();
+ } else if (argc != 1)
+ usage_rebase();
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
+ error = got_error_from_errno("getcwd");
+ goto done;
+ }
+ error = got_worktree_open(&worktree, cwd);
+ if (error)
+ goto done;
+
+
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(got_repo_get_path(repo), 0,
+ got_worktree_get_root_path(worktree), 0);
+ if (error)
+ goto done;
+
+ error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
+ if (error)
+ goto done;
+
+ if (rebase_in_progress && abort_rebase) {
+ int did_something;
+ error = got_worktree_rebase_continue(&resume_commit_id,
+ &new_base_branch, &tmp_branch, &branch, worktree, repo);
+ if (error)
+ goto done;
+ printf("Switching work tree to %s\n",
+ got_ref_get_symref_target(new_base_branch));
+ error = got_worktree_rebase_abort(worktree, repo,
+ new_base_branch, update_progress, &did_something);
+ if (error)
+ goto done;
+ printf("Rebase of %s aborted\n", got_ref_get_name(branch));
+ goto done; /* nothing else to do */
+ } else if (abort_rebase) {
+ error = got_error(GOT_ERR_NOT_REBASING);
+ goto done;
+ }
+
+ if (continue_rebase) {
+ struct got_object_id *new_commit_id;
+
+ error = got_worktree_rebase_continue(&resume_commit_id,
+ &new_base_branch, &tmp_branch, &branch, worktree, repo);
+ if (error)
+ goto done;
+ error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
+ got_worktree_get_base_commit_id(worktree),
+ resume_commit_id, repo);
+ if (error)
+ goto done;
+ if (yca_id == NULL) {
+ error = got_error_msg(GOT_ERR_ANCESTRY,
+ "cannot determine common ancestor commit for "
+ "continued rebase operation");
+ goto done;
+ }
+ error = got_object_open_as_commit(&commit, repo,
+ resume_commit_id);
+ if (error)
+ goto done;
+
+ error = got_worktree_rebase_commit(&new_commit_id, worktree,
+ tmp_branch, commit, resume_commit_id, repo);
+ if (error)
+ goto done;
+ error = show_rebase_progress(commit, resume_commit_id,
+ new_commit_id);
+ free(new_commit_id);
+ free(resume_commit_id);
+
+ resume_commit_id = got_object_id_dup(SIMPLEQ_FIRST(
+ got_object_commit_get_parent_ids(commit))->id);
+ if (resume_commit_id == NULL) {
+ error = got_error_from_errno("got_object_id_dup");
+ goto done;
+ }
+ got_object_commit_close(commit);
+ commit = NULL;
+ commit_id = resume_commit_id;
+ if (got_object_id_cmp(resume_commit_id, yca_id) == 0) {
+ error = rebase_complete(worktree, branch,
+ new_base_branch, tmp_branch, repo);
+ /* YCA has been reached; we are done. */
+ goto done;
+ }
+ } else {
+ error = got_ref_open(&branch, repo, argv[0], 0);
+ if (error != NULL)
+ goto done;
+
+ error = check_same_branch(
+ got_worktree_get_base_commit_id(worktree), branch, repo);
+ if (error) {
+ if (error->code != GOT_ERR_ANCESTRY)
+ goto done;
+ error = NULL;
+ } else {
+ error = got_error_msg(GOT_ERR_SAME_BRANCH,
+ "specified branch resolves to a commit which "
+ "is already contained in work tree's branch");
+ goto done;
+ }
+
+ error = got_ref_resolve(&branch_head_commit_id, repo, branch);
+ if (error)
+ goto done;
+ error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
+ got_worktree_get_base_commit_id(worktree),
+ branch_head_commit_id, repo);
+ if (error)
+ goto done;
+ if (yca_id == NULL) {
+ error = got_error_msg(GOT_ERR_ANCESTRY,
+ "specified branch shares no common ancestry "
+ "with work tree's branch");
+ goto done;
+ }
+
+ error = got_worktree_rebase_prepare(&new_base_branch,
+ &tmp_branch, worktree, branch, repo);
+ if (error)
+ goto done;
+ commit_id = branch_head_commit_id;
+ }
+
+ error = got_object_open_as_commit(&commit, repo, commit_id);
+ if (error)
+ goto done;
+
+ error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
+ if (error)
+ goto done;
+ parent_ids = got_object_commit_get_parent_ids(commit);
+ pid = SIMPLEQ_FIRST(parent_ids);
+ error = got_commit_graph_iter_start(graph, pid->id, repo);
+ got_object_commit_close(commit);
+ commit = NULL;
+ if (error)
+ goto done;
+ while (got_object_id_cmp(commit_id, yca_id) != 0) {
+ error = got_commit_graph_iter_next(&parent_id, graph);
+ if (error) {
+ if (error->code == GOT_ERR_ITER_COMPLETED) {
+ error = got_error_msg(GOT_ERR_ANCESTRY,
+ "ran out of commits to rebase before "
+ "youngest common ancestor commit has "
+ "been reached?!?");
+ goto done;
+ } else if (error->code != GOT_ERR_ITER_NEED_MORE)
+ goto done;
+ error = got_commit_graph_fetch_commits(graph, 1, repo);
+ if (error)
+ goto done;
+ } else {
+ error = got_object_qid_alloc(&qid, commit_id);
+ if (error)
+ goto done;
+ SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
+ commit_id = parent_id;
+ }
+ }
+
+ if (SIMPLEQ_EMPTY(&commits)) {
+ error = got_error(GOT_ERR_EMPTY_REBASE);
+ goto done;
+ }
+
+ pid = NULL;
+ SIMPLEQ_FOREACH(qid, &commits, entry) {
+ struct got_object_id *new_commit_id;
+
+ commit_id = qid->id;
+ parent_id = pid ? pid->id : yca_id;
+ pid = qid;
+
+ error = got_worktree_rebase_merge_files(worktree, parent_id,
+ commit_id, repo, rebase_progress, &rebase_status,
+ check_cancelled, NULL);
+ if (error)
+ goto done;
+
+ if (rebase_status == GOT_STATUS_CONFLICT)
+ break;
+
+ error = got_object_open_as_commit(&commit, repo, commit_id);
+ if (error)
+ goto done;
+ error = got_worktree_rebase_commit(&new_commit_id, worktree,
+ tmp_branch, commit, commit_id, repo);
+ if (error)
+ goto done;
+ error = show_rebase_progress(commit, commit_id, new_commit_id);
+ free(new_commit_id);
+ got_object_commit_close(commit);
+ commit = NULL;
+ if (error)
+ goto done;
+
+ }
+
+ if (rebase_status == GOT_STATUS_CONFLICT) {
+ error = got_worktree_rebase_postpone(worktree);
+ if (error)
+ goto done;
+ error = got_error_msg(GOT_ERR_CONFLICTS,
+ "conflicts must be resolved before rebase can be resumed");
+ } else
+ error = rebase_complete(worktree, branch, new_base_branch,
+ tmp_branch, repo);
+done:
+ got_object_id_queue_free(&commits);
+ free(branch_head_commit_id);
+ free(resume_commit_id);
+ free(yca_id);
+ if (graph)
+ got_commit_graph_close(graph);
+ if (commit)
+ got_object_commit_close(commit);
+ if (branch)
+ got_ref_close(branch);
+ if (new_base_branch)
+ got_ref_close(new_base_branch);
+ if (tmp_branch)
+ got_ref_close(tmp_branch);
+ if (worktree)
+ got_worktree_close(worktree);
+ if (repo)
+ got_repo_close(repo);
+ return error;
+}
diff --git a/include/got_error.h b/include/got_error.h
index 83b7c99..441dea9 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -97,6 +97,10 @@
#define GOT_ERR_MIXED_COMMITS 81
#define GOT_ERR_CONFLICTS 82
#define GOT_ERR_BRANCH_EXISTS 83
+#define GOT_ERR_MODIFIED 84
+#define GOT_ERR_NOT_REBASING 85
+#define GOT_ERR_EMPTY_REBASE 86
+#define GOT_ERR_REBASE_COMMITID 87
static const struct got_error {
int code;
@@ -189,6 +193,11 @@ static const struct got_error {
{ GOT_ERR_CONFLICTS, "work tree contains conflicted files; these "
"conflicts must be resolved first" },
{ GOT_ERR_BRANCH_EXISTS,"specified branch already exists" },
+ { GOT_ERR_MODIFIED, "work tree contains local changes; these "
+ "changes must be committed or reverted first" },
+ { GOT_ERR_NOT_REBASING, "rebase operation not in progress" },
+ { GOT_ERR_EMPTY_REBASE, "no commits to rebase" },
+ { GOT_ERR_REBASE_COMMITID,"rebase commit ID mismatch" },
};
/*
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 2dd6e8c..c61b327 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -16,6 +16,7 @@
struct got_worktree;
struct got_commitable;
+struct got_commit_object;
/* status codes */
#define GOT_STATUS_NO_CHANGE ' '
@@ -208,3 +209,66 @@ const char *got_commitable_get_path(struct got_commitable *);
/* Get the status of a commitable worktree item. */
unsigned int got_commitable_get_status(struct got_commitable *);
+
+/*
+ * Prepare for rebasing a branch onto the work tree's current branch.
+ * This function creates references to a temporary branch, the branch
+ * being rebased, and the work tree's current branch, under the
+ * "got/worktree/rebase/" namespace. These references are used to
+ * keep track of rebase operation state and are used as input and/or
+ * output arguments with other rebase-related functions.
+ */
+const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
+ struct got_reference **, struct got_worktree *, struct got_reference *,
+ struct got_repository *);
+
+/*
+ * Continue an interrupted rebase operation.
+ * This function returns existing references created when rebase was prepared,
+ * and the ID of the commit currently being rebased. This should be called
+ * before either resuming or aborting a rebase operation.
+ */
+const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
+ struct got_reference **, struct got_reference **, struct got_reference **,
+ struct got_worktree *, struct got_repository *);
+
+/* Check whether a, potentially interrupted, rebase operation is in progress. */
+const struct got_error *got_worktree_rebase_in_progress(int *,
+ struct got_worktree *);
+
+/*
+ * Merge changes from the commit currently being rebased into the work tree.
+ * Report affected files, including merge conflicts, via the specified
+ * progress callback.
+ */
+const struct got_error *got_worktree_rebase_merge_files(
+ struct got_worktree *, struct got_object_id *, struct got_object_id *,
+ struct got_repository *, got_worktree_checkout_cb, void *,
+ got_worktree_cancel_cb, void *);
+
+/*
+ * Commit merged rebased changes to a temporary branch and return the
+ * ID of the newly created commit.
+ */
+const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
+ struct got_worktree *, struct got_reference *, struct got_commit_object *,
+ struct got_object_id *, struct got_repository *);
+
+/* Postpone the rebase operation. Should be called after a merge conflict. */
+const struct got_error *got_worktree_rebase_postpone(struct got_worktree *);
+
+/*
+ * Complete the current rebase operation. This should be called once all
+ * commits have been rebased successfully.
+ */
+const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
+ struct got_reference *, struct got_reference *, struct got_reference *,
+ struct got_repository *);
+
+/*
+ * Abort the current rebase operation.
+ * Report reverted files via the specified progress callback.
+ */
+const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
+ struct got_repository *, struct got_reference *,
+ got_worktree_checkout_cb, void *);
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index df3ae1a..05aa9d3 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -473,7 +473,8 @@ fetch_commits_from_open_branches(int *nfetched,
int i, ntips;
*nfetched = 0;
- *changed_id = NULL;
+ if (changed_id)
+ *changed_id = NULL;
ntips = got_object_idset_num_elements(graph->open_branches);
if (ntips == 0)
@@ -517,7 +518,7 @@ fetch_commits_from_open_branches(int *nfetched,
commit, repo);
if (err)
break;
- if (changed && *changed_id == NULL)
+ if (changed && changed_id && *changed_id == NULL)
*changed_id = commit_id;
}
done:
@@ -578,8 +579,16 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
int changed;
start_node = got_object_idset_get(graph->node_ids, id);
- if (start_node == NULL)
- return got_error_no_obj(id);
+ while (start_node == NULL) {
+ int ncommits;
+ err = fetch_commits_from_open_branches(&ncommits, NULL, graph,
+ repo);
+ if (err)
+ return err;
+ if (ncommits == 0)
+ return got_error_no_obj(id);
+ start_node = got_object_idset_get(graph->node_ids, id);
+ }
err = got_object_open_as_commit(&commit, repo, &start_node->id);
if (err)
diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index 9809abf..317d0fd 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -65,3 +65,15 @@ struct got_commitable {
const struct got_error *got_worktree_get_base_ref_name(char **,
struct got_worktree *worktree);
+
+/* Temporary branch which accumulates commits during a rebase operation. */
+#define GOT_WORKTREE_REBASE_TMP_REF_PREFIX "refs/got/worktree/rebase/tmp"
+
+/* Symbolic reference pointing at the name of the new base branch. */
+#define GOT_WORKTREE_NEWBASE_REF_PREFIX "refs/got/worktree/rebase/newbase"
+
+/* Symbolic reference pointing at the name of the branch being rebased. */
+#define GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX "refs/got/worktree/rebase/branch"
+
+/* Reference pointing at the ID of the current commit being rebased. */
+#define GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX "refs/got/worktree/rebase/commit"
diff --git a/lib/worktree.c b/lib/worktree.c
index 6c06691..4d551f4 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -725,6 +725,7 @@ static const struct got_error *
merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
struct got_blob_object *blob_orig, const char *ondisk_path,
const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
+ struct got_object_id *deriv_base_commit_id,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
void *progress_arg)
{
@@ -734,7 +735,7 @@ merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
char *blob_deriv_path = NULL, *blob_orig_path = NULL;
char *merged_path = NULL, *base_path = NULL;
char *id_str = NULL;
- char *label1 = NULL;
+ char *label_deriv = NULL;
int overlapcnt = 0;
char *parent;
@@ -789,16 +790,16 @@ merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
*/
}
- err = got_object_id_str(&id_str, worktree->base_commit_id);
+ err = got_object_id_str(&id_str, deriv_base_commit_id);
if (err)
goto done;
- if (asprintf(&label1, "commit %s", id_str) == -1) {
+ if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
- blob_orig_path, ondisk_path, label1, path);
+ blob_orig_path, ondisk_path, label_deriv, path);
if (err)
goto done;
@@ -848,7 +849,7 @@ done:
free(blob_orig_path);
}
free(id_str);
- free(label1);
+ free(label_deriv);
return err;
}
@@ -1183,7 +1184,8 @@ update_blob(struct got_worktree *worktree,
goto done;
}
err = merge_blob(&update_timestamps, worktree, blob2,
- ondisk_path, path, sb.st_mode, blob, repo,
+ ondisk_path, path, sb.st_mode, blob,
+ worktree->base_commit_id, repo,
progress_cb, progress_arg);
if (blob2)
got_object_blob_close(blob2);
@@ -1348,8 +1350,8 @@ diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
return err;
}
-const struct got_error *
-got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
+static const struct got_error *
+get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
{
const struct got_error *err = NULL;
char *uuidstr = NULL;
@@ -1361,7 +1363,7 @@ got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
if (uuid_status != uuid_s_ok)
return got_error_uuid(uuid_status);
- if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
+ if (asprintf(refname, "%s-%s", prefix, uuidstr)
== -1) {
err = got_error_from_errno("asprintf");
*refname = NULL;
@@ -1370,6 +1372,40 @@ got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
return err;
}
+const struct got_error *
+got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
+{
+ return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
+}
+
+static const struct got_error *
+get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
+{
+ return get_ref_name(refname, worktree,
+ GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
+}
+
+static const struct got_error *
+get_newbase_symref_name(char **refname, struct got_worktree *worktree)
+{
+ return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
+}
+
+static const struct got_error *
+get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
+{
+ return get_ref_name(refname, worktree,
+ GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
+}
+
+static const struct got_error *
+get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
+{
+ return get_ref_name(refname, worktree,
+ GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
+}
+
+
/*
* Prevent Git's garbage collector from deleting our base commit by
* setting a reference to our base commit's ID.
@@ -1660,6 +1696,7 @@ struct merge_file_cb_arg {
void *progress_arg;
got_worktree_cancel_cb cancel_cb;
void *cancel_arg;
+ struct got_object_id *commit_id2;
};
static const struct got_error *
@@ -1706,7 +1743,7 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
}
err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
- ondisk_path, path2, sb.st_mode, blob2, repo,
+ ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
a->progress_cb, a->progress_arg);
} else if (blob1) {
ie = got_fileindex_entry_get(a->fileindex, path1);
@@ -1772,7 +1809,8 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
goto done;
}
err = merge_blob(&local_changes_subsumed, a->worktree,
- NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
+ NULL, ondisk_path, path2, sb.st_mode, blob2,
+ a->commit_id2, repo,
a->progress_cb, a->progress_arg);
if (status == GOT_STATUS_DELETE) {
err = update_blob_fileindex_entry(a->worktree,
@@ -1839,34 +1877,17 @@ check_merge_ok(void *arg, struct got_fileindex_entry *ie)
return NULL;
}
-const struct got_error *
-got_worktree_merge_files(struct got_worktree *worktree,
- struct got_object_id *commit_id1, struct got_object_id *commit_id2,
- struct got_repository *repo, got_worktree_checkout_cb progress_cb,
- void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+static const struct got_error *
+merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
+ const char *fileindex_path, struct got_object_id *commit_id1,
+ struct got_object_id *commit_id2, struct got_repository *repo,
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
- const struct got_error *err = NULL, *sync_err, *unlockerr;
+ const struct got_error *err = NULL, *sync_err;
struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
struct got_tree_object *tree1 = NULL, *tree2 = NULL;
struct merge_file_cb_arg arg;
- char *fileindex_path = NULL;
- struct got_fileindex *fileindex = NULL;
- struct check_merge_ok_arg mok_arg;
-
- err = lock_worktree(worktree, LOCK_EX);
- if (err)
- return err;
-
- err = open_fileindex(&fileindex, &fileindex_path, worktree);
- if (err)
- goto done;
-
- mok_arg.worktree = worktree;
- mok_arg.repo = repo;
- err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
- &mok_arg);
- if (err)
- goto done;
if (commit_id1) {
err = got_object_id_by_path(&tree_id1, repo, commit_id1,
@@ -1894,17 +1915,51 @@ got_worktree_merge_files(struct got_worktree *worktree,
arg.progress_arg = progress_arg;
arg.cancel_cb = cancel_cb;
arg.cancel_arg = cancel_arg;
+ arg.commit_id2 = commit_id2;
err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
sync_err = sync_fileindex(fileindex, fileindex_path);
if (sync_err && err == NULL)
err = sync_err;
done:
- got_fileindex_free(fileindex);
if (tree1)
got_object_tree_close(tree1);
if (tree2)
got_object_tree_close(tree2);
+ return err;
+}
+
+const struct got_error *
+got_worktree_merge_files(struct got_worktree *worktree,
+ struct got_object_id *commit_id1, struct got_object_id *commit_id2,
+ struct got_repository *repo, got_worktree_checkout_cb progress_cb,
+ void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err, *unlockerr;
+ char *fileindex_path = NULL;
+ struct got_fileindex *fileindex = NULL;
+ struct check_merge_ok_arg mok_arg;
+
+ err = lock_worktree(worktree, LOCK_EX);
+ if (err)
+ return err;
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ goto done;
+
+ mok_arg.worktree = worktree;
+ mok_arg.repo = repo;
+ err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
+ &mok_arg);
+ if (err)
+ goto done;
+ err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
+ commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
+done:
+ if (fileindex)
+ got_fileindex_free(fileindex);
+ free(fileindex_path);
unlockerr = lock_worktree(worktree, LOCK_SH);
if (unlockerr && err == NULL)
err = unlockerr;
@@ -3400,3 +3455,524 @@ got_commitable_get_status(struct got_commitable *ct)
{
return ct->status;
}
+
+struct check_rebase_ok_arg {
+ struct got_worktree *worktree;
+ struct got_repository *repo;
+ int rebase_in_progress;
+};
+
+static const struct got_error *
+check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
+{
+ const struct got_error *err = NULL;
+ struct check_rebase_ok_arg *a = arg;
+ unsigned char status;
+ struct stat sb;
+ char *ondisk_path;
+
+ if (!a->rebase_in_progress) {
+ /* Reject rebase of a work tree with mixed base commits. */
+ if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
+ SHA1_DIGEST_LENGTH))
+ return got_error(GOT_ERR_MIXED_COMMITS);
+ }
+
+ if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
+ == -1)
+ return got_error_from_errno("asprintf");
+
+ /* Reject rebase of a work tree with modified or conflicted files. */
+ err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
+ free(ondisk_path);
+ if (err)
+ return err;
+
+ if (a->rebase_in_progress) {
+ if (status == GOT_STATUS_CONFLICT)
+ return got_error(GOT_ERR_CONFLICTS);
+ } else if (status != GOT_STATUS_NO_CHANGE)
+ return got_error(GOT_ERR_MODIFIED);
+
+ return NULL;
+}
+
+const struct got_error *
+got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
+ struct got_reference **tmp_branch, struct got_worktree *worktree,
+ struct got_reference *branch, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
+ char *branch_ref_name = NULL;
+ struct got_fileindex *fileindex = NULL;
+ char *fileindex_path = NULL;
+ struct check_rebase_ok_arg ok_arg;
+ struct got_reference *wt_branch = NULL, *branch_ref = NULL;
+
+ *new_base_branch_ref = NULL;
+ *tmp_branch = NULL;
+
+ err = lock_worktree(worktree, LOCK_EX);
+ if (err)
+ return err;
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ goto done;
+
+ ok_arg.worktree = worktree;
+ ok_arg.repo = repo;
+ ok_arg.rebase_in_progress = 0;
+ err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
+ &ok_arg);
+ if (err)
+ goto done;
+
+ err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
+ if (err)
+ goto done;
+
+ err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
+ if (err)
+ goto done;
+
+ err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
+ if (err)
+ goto done;
+
+ err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
+ 0);
+ if (err)
+ goto done;
+
+ err = got_ref_alloc_symref(new_base_branch_ref,
+ new_base_branch_ref_name, wt_branch);
+ if (err)
+ goto done;
+ err = got_ref_write(*new_base_branch_ref, repo);
+ if (err)
+ goto done;
+
+ /* TODO Lock original branch's ref while rebasing? */
+
+ err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
+ if (err)
+ goto done;
+
+ err = got_ref_write(branch_ref, repo);
+ if (err)
+ goto done;
+
+ err = got_ref_alloc(tmp_branch, tmp_branch_name,
+ worktree->base_commit_id);
+ if (err)
+ goto done;
+ err = got_ref_write(*tmp_branch, repo);
+ if (err)
+ goto done;
+
+ err = got_worktree_set_head_ref(worktree, *tmp_branch);
+ if (err)
+ goto done;
+done:
+ free(fileindex_path);
+ if (fileindex)
+ got_fileindex_free(fileindex);
+ free(tmp_branch_name);
+ free(new_base_branch_ref_name);
+ free(branch_ref_name);
+ if (branch_ref)
+ got_ref_close(branch_ref);
+ if (wt_branch)
+ got_ref_close(wt_branch);
+ if (err) {
+ if (*new_base_branch_ref) {
+ got_ref_close(*new_base_branch_ref);
+ *new_base_branch_ref = NULL;
+ }
+ if (*tmp_branch) {
+ got_ref_close(*tmp_branch);
+ *tmp_branch = NULL;
+ }
+ lock_worktree(worktree, LOCK_SH);
+ }
+ return err;
+}
+
+const struct got_error *
+got_worktree_rebase_continue(struct got_object_id **commit_id,
+ struct got_reference **new_base_branch, struct got_reference **tmp_branch,
+ struct got_reference **branch, struct got_worktree *worktree,
+ struct got_repository *repo)
+{
+ const struct got_error *err;
+ char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
+ char *tmp_branch_name = NULL, *branch_ref_name = NULL;
+ struct got_reference *commit_ref = NULL, *branch_ref = NULL;
+
+ *commit_id = NULL;
+
+ err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
+ if (err)
+ return err;
+
+ err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
+ if (err)
+ goto done;
+
+ err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
+ if (err)
+ goto done;
+
+ err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
+ if (err)
+ goto done;
+
+ err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
+ if (err)
+ goto done;
+
+ err = got_ref_open(branch, repo,
+ got_ref_get_symref_target(branch_ref), 0);
+ if (err)
+ goto done;
+
+ err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
+ if (err)
+ goto done;
+
+ err = got_ref_resolve(commit_id, repo, commit_ref);
+ if (err)
+ goto done;
+
+ err = got_ref_open(new_base_branch, repo,
+ new_base_branch_ref_name, 0);
+ if (err)
+ goto done;
+
+ err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
+ if (err)
+ goto done;
+done:
+ free(commit_ref_name);
+ free(branch_ref_name);
+ if (commit_ref)
+ got_ref_close(commit_ref);
+ if (branch_ref)
+ got_ref_close(branch_ref);
+ if (err) {
+ free(*commit_id);
+ *commit_id = NULL;
+ if (*tmp_branch) {
+ got_ref_close(*tmp_branch);
+ *tmp_branch = NULL;
+ }
+ if (*new_base_branch) {
+ got_ref_close(*new_base_branch);
+ *new_base_branch = NULL;
+ }
+ if (*branch) {
+ got_ref_close(*branch);
+ *branch = NULL;
+ }
+ }
+ return err;
+}
+
+const struct got_error *
+got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
+{
+ const struct got_error *err;
+ char *tmp_branch_name = NULL;
+
+ err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
+ if (err)
+ return err;
+
+ *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
+ free(tmp_branch_name);
+ return NULL;
+}
+
+static const struct got_error *
+collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
+ char **logmsg, void *arg)
+{
+ struct got_commit_object *commit = arg;
+
+ *logmsg = strdup(got_object_commit_get_logmsg(commit));
+ if (*logmsg == NULL)
+ return got_error_from_errno("strdup");
+
+ return NULL;
+}
+
+static const struct got_error *
+rebase_status(void *arg, unsigned char status, const char *path,
+ struct got_object_id *blob_id, struct got_object_id *commit_id)
+{
+ return NULL;
+}
+
+const struct got_error *
+got_worktree_rebase_merge_files(struct got_worktree *worktree,
+ struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
+ struct got_repository *repo, got_worktree_checkout_cb progress_cb,
+ void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err;
+ struct got_fileindex *fileindex;
+ char *fileindex_path, *commit_ref_name = NULL;
+ struct got_reference *commit_ref = NULL;
+
+ /* Work tree is locked/unlocked during rebase prepartion/teardown. */
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ return err;
+
+ err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
+ if (err)
+ goto done;
+ err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
+ if (err) {
+ if (err->code != GOT_ERR_NOT_REF)
+ goto done;
+ err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
+ if (err)
+ goto done;
+ err = got_ref_write(commit_ref, repo);
+ if (err)
+ goto done;
+ } else {
+ struct got_object_id *stored_id;
+ int cmp;
+
+ err = got_ref_resolve(&stored_id, repo, commit_ref);
+ if (err)
+ goto done;
+ cmp = got_object_id_cmp(commit_id, stored_id);
+ free(stored_id);
+ if (cmp != 0) {
+ err = got_error(GOT_ERR_REBASE_COMMITID);
+ goto done;
+ }
+ }
+
+ err = merge_files(worktree, fileindex, fileindex_path,
+ parent_commit_id, commit_id, repo, progress_cb, progress_arg,
+ cancel_cb, cancel_arg);
+done:
+ got_fileindex_free(fileindex);
+ free(fileindex_path);
+ if (commit_ref)
+ got_ref_close(commit_ref);
+ return err;
+}
+
+const struct got_error *
+got_worktree_rebase_commit(struct got_object_id **new_commit_id,
+ struct got_worktree *worktree, struct got_reference *tmp_branch,
+ struct got_commit_object *orig_commit,
+ struct got_object_id *orig_commit_id, struct got_repository *repo)
+{
+ const struct got_error *err;
+ char *commit_ref_name = NULL;
+ struct got_reference *commit_ref = NULL;
+ struct got_object_id *commit_id = NULL;
+
+ /* Work tree is locked/unlocked during rebase prepartion/teardown. */
+
+ err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
+ if (err)
+ goto done;
+ err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
+ if (err)
+ goto done;
+ err = got_ref_resolve(&commit_id, repo, commit_ref);
+ if (err)
+ goto done;
+ if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
+ err = got_error(GOT_ERR_REBASE_COMMITID);
+ goto done;
+ }
+
+ err = got_worktree_commit(new_commit_id, worktree, NULL,
+ got_object_commit_get_author(orig_commit),
+ got_object_commit_get_committer(orig_commit),
+ collect_rebase_commit_msg, orig_commit,
+ rebase_status, NULL, repo);
+ if (err)
+ goto done;
+
+ err = got_ref_delete(commit_ref, repo);
+ if (err)
+ goto done;
+
+ err = got_ref_change_ref(tmp_branch, *new_commit_id);
+done:
+ free(commit_ref_name);
+ if (commit_ref)
+ got_ref_close(commit_ref);
+ if (err) {
+ free(*new_commit_id);
+ *new_commit_id = NULL;
+ }
+ return err;
+}
+
+const struct got_error *
+got_worktree_rebase_postpone(struct got_worktree *worktree)
+{
+ return lock_worktree(worktree, LOCK_SH);
+}
+
+const struct got_error *
+got_worktree_rebase_complete(struct got_worktree *worktree,
+ struct got_reference *new_base_branch, struct got_reference *tmp_branch,
+ struct got_reference *rebased_branch,
+ struct got_repository *repo)
+{
+ const struct got_error *err, *unlockerr;
+ struct got_object_id *new_head_commit_id = NULL;
+
+ err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
+ if (err)
+ return err;
+
+ err = got_ref_change_ref(rebased_branch, new_head_commit_id);
+ if (err)
+ goto done;
+
+ err = got_ref_write(rebased_branch, repo);
+ if (err)
+ goto done;
+
+ err = got_worktree_set_head_ref(worktree, rebased_branch);
+ if (err)
+ goto done;
+
+ err = got_ref_delete(tmp_branch, repo);
+ if (err)
+ goto done;
+
+ err = got_ref_delete(new_base_branch, repo);
+ if (err)
+ goto done;
+done:
+ free(new_head_commit_id);
+ unlockerr = lock_worktree(worktree, LOCK_SH);
+ if (unlockerr && err == NULL)
+ err = unlockerr;
+ return err;
+}
+
+struct collect_revertible_paths_arg {
+ struct got_pathlist_head *revertible_paths;
+ struct got_worktree *worktree;
+};
+
+static const struct got_error *
+collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
+ struct got_object_id *blob_id, struct got_object_id *commit_id)
+{
+ struct collect_revertible_paths_arg *a = arg;
+ const struct got_error *err = NULL;
+ struct got_pathlist_entry *new = NULL;
+ char *path = NULL;
+
+ if (status != GOT_STATUS_ADD &&
+ status != GOT_STATUS_DELETE &&
+ status != GOT_STATUS_MODIFY &&
+ status != GOT_STATUS_CONFLICT &&
+ status != GOT_STATUS_MISSING)
+ return NULL;
+
+ if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
+ return got_error_from_errno("asprintf");
+
+ err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
+ if (err || new == NULL)
+ free(path);
+ return err;
+}
+
+const struct got_error *
+got_worktree_rebase_abort(struct got_worktree *worktree,
+ struct got_repository *repo, struct got_reference *new_base_branch,
+ got_worktree_checkout_cb progress_cb, void *progress_arg)
+{
+ const struct got_error *err, *unlockerr;
+ struct got_reference *resolved = NULL;
+ struct got_object_id *commit_id = NULL;
+ struct got_fileindex *fileindex = NULL;
+ char *fileindex_path = NULL;
+ struct got_pathlist_head revertible_paths;
+ struct got_pathlist_entry *pe;
+ struct collect_revertible_paths_arg crp_arg;
+
+ TAILQ_INIT(&revertible_paths);
+
+ err = lock_worktree(worktree, LOCK_EX);
+ if (err)
+ return err;
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ goto done;
+
+ err = got_ref_open(&resolved, repo,
+ got_ref_get_symref_target(new_base_branch), 0);
+ if (err)
+ goto done;
+
+ err = got_worktree_set_head_ref(worktree, resolved);
+ if (err)
+ goto done;
+
+ /*
+ * XXX commits to the base branch could have happened while
+ * we were busy rebasing; should we store the original commit ID
+ * when rebase begins and read it back here?
+ */
+ err = got_ref_resolve(&commit_id, repo, resolved);
+ if (err)
+ goto done;
+
+ err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
+ if (err)
+ goto done;
+
+ err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
+ progress_arg, NULL, NULL);
+ if (err)
+ goto done;
+
+ crp_arg.revertible_paths = &revertible_paths;
+ crp_arg.worktree = worktree;
+ err = got_worktree_status(worktree, "", repo,
+ collect_revertible_paths, &crp_arg, NULL, NULL);
+ if (err)
+ goto done;
+
+ TAILQ_FOREACH(pe, &revertible_paths, entry) {
+ err = revert_file(worktree, fileindex, pe->path,
+ progress_cb, progress_arg, repo);
+ if (err)
+ break;
+ }
+done:
+ got_ref_close(resolved);
+ free(commit_id);
+ got_fileindex_free(fileindex);
+ free(fileindex_path);
+ TAILQ_FOREACH(pe, &revertible_paths, entry)
+ free((char *)pe->path);
+ got_pathlist_free(&revertible_paths);
+
+ unlockerr = lock_worktree(worktree, LOCK_SH);
+ if (unlockerr && err == NULL)
+ err = unlockerr;
+ return err;
+}
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index 55653a3..cc53c40 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -1,4 +1,5 @@
-REGRESS_TARGETS=checkout update status log add rm diff commit cherrypick backout
+REGRESS_TARGETS=checkout update status log add rm diff commit \
+ cherrypick backout rebase
NOOBJ=Yes
checkout:
@@ -31,4 +32,7 @@ cherrypick:
backout:
./backout.sh
+rebase:
+ ./rebase.sh
+
.include <bsd.regress.mk>
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index 4135c2b..05565fe 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -44,12 +44,32 @@ function git_show_head
(cd $repo && git show --no-patch --pretty='format:%H')
}
+function git_show_parent_commit
+{
+ local repo="$1"
+ (cd $repo && git show --no-patch --pretty='format:%P')
+}
+
function git_show_tree
{
local repo="$1"
(cd $repo && git show --no-patch --pretty='format:%T')
}
+function trim_obj_id
+{
+ let trimcount=$1
+ id=$2
+
+ pat=""
+ while [ trimcount -gt 0 ]; do
+ pat="[0-9a-f]$pat"
+ let trimcount--
+ done
+
+ echo ${id%$pat}
+}
+
function git_commit_tree
{
local repo="$1"
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
new file mode 100755
index 0000000..ca79164
--- /dev/null
+++ b/regress/cmdline/rebase.sh
@@ -0,0 +1,393 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_rebase_basic {
+ local testroot=`test_init rebase_basic`
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified delta on branch" > $testroot/repo/gamma/delta
+ git_commit $testroot/repo -m "committing to delta on newbranch"
+
+ echo "modified alpha on branch" > $testroot/repo/alpha
+ (cd $testroot/repo && git rm -q beta)
+ echo "new file on branch" > $testroot/repo/epsilon/new
+ (cd $testroot/repo && git add epsilon/new)
+ git_commit $testroot/repo -m "committing more changes on newbranch"
+
+ local orig_commit1=`git_show_parent_commit $testroot/repo`
+ local orig_commit2=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q master)
+ echo "modified zeta on master" > $testroot/repo/epsilon/zeta
+ git_commit $testroot/repo -m "committing to zeta on master"
+ local master_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rebase newbranch > $testroot/stdout)
+
+ (cd $testroot/repo && git checkout -q newbranch)
+ local new_commit1=`git_show_parent_commit $testroot/repo`
+ local new_commit2=`git_show_head $testroot/repo`
+
+ local short_orig_commit1=`trim_obj_id 28 $orig_commit1`
+ local short_orig_commit2=`trim_obj_id 28 $orig_commit2`
+ local short_new_commit1=`trim_obj_id 28 $new_commit1`
+ local short_new_commit2=`trim_obj_id 28 $new_commit2`
+
+ echo "G gamma/delta" >> $testroot/stdout.expected
+ echo -n "$short_orig_commit1 -> $short_new_commit1" \
+ >> $testroot/stdout.expected
+ echo ": committing to delta on newbranch" >> $testroot/stdout.expected
+ echo "G alpha" >> $testroot/stdout.expected
+ echo "D beta" >> $testroot/stdout.expected
+ echo "A epsilon/new" >> $testroot/stdout.expected
+ echo -n "$short_orig_commit2 -> $short_new_commit2" \
+ >> $testroot/stdout.expected
+ echo ": committing more changes on newbranch" \
+ >> $testroot/stdout.expected
+ echo "Switching work tree to refs/heads/newbranch" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified delta on branch" > $testroot/content.expected
+ cat $testroot/wt/gamma/delta > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified alpha on branch" > $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ if [ -e $testroot/wt/beta ]; then
+ echo "removed file beta still exists on disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo "new file on branch" > $testroot/content.expected
+ cat $testroot/wt/epsilon/new > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ echo -n > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got log -l3 | grep ^commit > $testroot/stdout)
+ echo "commit $new_commit2 (newbranch)" > $testroot/stdout.expected
+ echo "commit $new_commit1" >> $testroot/stdout.expected
+ echo "commit $master_commit (master)" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_rebase_ancestry_check {
+ local testroot=`test_init rebase_ancestry_check`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified delta on branch" > $testroot/repo/gamma/delta
+ git_commit $testroot/repo -m "committing to delta on newbranch"
+
+ (cd $testroot/wt && got rebase newbranch > $testroot/stdout \
+ 2> $testroot/stderr)
+
+ echo -n > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo -n "got: specified branch resolves to a commit " \
+ > $testroot/stderr.expected
+ echo "which is already contained in work tree's branch" \
+ >> $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_rebase_continue {
+ local testroot=`test_init rebase_continue`
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified alpha on branch" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on newbranch"
+ local orig_commit1=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q master)
+ echo "modified alpha on master" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on master"
+ local master_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rebase newbranch > $testroot/stdout \
+ 2> $testroot/stderr)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: conflicts must be resolved before rebase can be resumed" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected
+ echo "modified alpha on branch" >> $testroot/content.expected
+ echo "=======" >> $testroot/content.expected
+ echo "modified alpha on master" >> $testroot/content.expected
+ echo '>>>>>>> alpha' >> $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # resolve the conflict
+ echo "modified alpha on branch and master" > $testroot/wt/alpha
+
+ (cd $testroot/wt && got rebase -c > $testroot/stdout)
+
+ (cd $testroot/repo && git checkout -q newbranch)
+ local new_commit1=`git_show_head $testroot/repo`
+
+ local short_orig_commit1=`trim_obj_id 28 $orig_commit1`
+ local short_new_commit1=`trim_obj_id 28 $new_commit1`
+
+ echo -n "$short_orig_commit1 -> $short_new_commit1" \
+ > $testroot/stdout.expected
+ echo ": committing to alpha on newbranch" >> $testroot/stdout.expected
+ echo "Switching work tree to refs/heads/newbranch" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+
+ (cd $testroot/wt && got log -l2 | grep ^commit > $testroot/stdout)
+ echo "commit $new_commit1 (newbranch)" > $testroot/stdout.expected
+ echo "commit $master_commit (master)" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_rebase_abort {
+ local testroot=`test_init rebase_abort`
+
+ local init_commit=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified alpha on branch" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on newbranch"
+ local orig_commit1=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q master)
+ echo "modified alpha on master" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on master"
+ local master_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rebase newbranch > $testroot/stdout \
+ 2> $testroot/stderr)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: conflicts must be resolved before rebase can be resumed" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected
+ echo "modified alpha on branch" >> $testroot/content.expected
+ echo "=======" >> $testroot/content.expected
+ echo "modified alpha on master" >> $testroot/content.expected
+ echo '>>>>>>> alpha' >> $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rebase -a > $testroot/stdout)
+
+ (cd $testroot/repo && git checkout -q newbranch)
+
+ echo "Switching work tree to refs/heads/master" \
+ > $testroot/stdout.expected
+ echo 'R alpha' >> $testroot/stdout.expected
+ echo "Rebase of refs/heads/newbranch aborted" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified alpha on master" > $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got log -l3 -c newbranch \
+ | grep ^commit > $testroot/stdout)
+ echo "commit $orig_commit1 (newbranch)" > $testroot/stdout.expected
+ echo "commit $init_commit" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+run_test test_rebase_basic
+run_test test_rebase_ancestry_check
+run_test test_rebase_continue
+run_test test_rebase_abort