Merge pull request #3139 from ethomson/diff_conflicts Include conflicts when diffing
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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1946342..99e3759 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,13 @@ support for HTTPS connections insead of OpenSSL.
the error message, which allows you to get the "repository not
found" messages.
+* `git_index_conflict_add()` will remove staged entries that exist for
+ conflicted paths.
+
+* The flags for a `git_diff_file` will now have the `GIT_DIFF_FLAG_EXISTS`
+ bit set when a file exists on that side of the diff. This is useful
+ for understanding whether a side of the diff exists in the presence of
+ a conflict.
### API additions
@@ -98,6 +105,18 @@ support for HTTPS connections insead of OpenSSL.
configuration of the server, and tools can use this to show messages
about failing to communicate with the server.
+* `git_diff_index_to_workdir()` and `git_diff_tree_to_index()` will now
+ produce deltas of type `GIT_DELTA_CONFLICTED` to indicate that the index
+ side of the delta is a conflict.
+
+* The `git_status` family of functions will now produce status of type
+ `GIT_STATUS_CONFLICTED` to indicate that a conflict exists for that file
+ in the index.
+
+* `git_index_entry_is_conflict()` is a utility function to determine if
+ a given index entry has a non-zero stage entry, indicating that it is
+ one side of a conflict.
+
### API removals
* `git_remote_save()` and `git_remote_clear_refspecs()` have been
diff --git a/include/git2/diff.h b/include/git2/diff.h
index cac3b26..c0d42e3 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -226,6 +226,7 @@ typedef enum {
GIT_DIFF_FLAG_BINARY = (1u << 0), /**< file(s) treated as binary data */
GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /**< file(s) treated as text data */
GIT_DIFF_FLAG_VALID_ID = (1u << 2), /**< `id` value is known correct */
+ GIT_DIFF_FLAG_EXISTS = (1u << 3), /**< file exists at this side of the delta */
} git_diff_flag_t;
/**
@@ -239,16 +240,17 @@ typedef enum {
* DELETED pairs).
*/
typedef enum {
- GIT_DELTA_UNMODIFIED = 0, /**< no changes */
- GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */
- GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */
- GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */
- GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */
- GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */
- GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */
- GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */
- GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */
- GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */
+ GIT_DELTA_UNMODIFIED = 0, /**< no changes */
+ GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */
+ GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */
+ GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */
+ GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */
+ GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */
+ GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */
+ GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */
+ GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */
+ GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */
+ GIT_DELTA_CONFLICTED = 10, /**< entry in the index is conflicted */
} git_delta_t;
/**
diff --git a/include/git2/index.h b/include/git2/index.h
index 52032f7..49bbe16 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -430,6 +430,15 @@ GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_en
*/
GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
+/**
+ * Return whether the given index entry is a conflict (has a high stage
+ * entry). This is simply shorthand for `git_index_entry_stage > 0`.
+ *
+ * @param entry The entry
+ * @return 1 if the entry is a conflict entry, 0 otherwise
+ */
+GIT_EXTERN(int) git_index_entry_is_conflict(const git_index_entry *entry);
+
/**@}*/
/** @name Workdir Index Entry Functions
@@ -631,7 +640,8 @@ GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *pat
/**@{*/
/**
- * Add or update index entries to represent a conflict
+ * Add or update index entries to represent a conflict. Any staged
+ * entries that exist at the given paths will be removed.
*
* The entries are the entries from the tree included in the merge. Any
* entry may be null to indicate that that file was not present in the
diff --git a/include/git2/status.h b/include/git2/status.h
index 5f21181..6711139 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -46,6 +46,7 @@ typedef enum {
GIT_STATUS_WT_UNREADABLE = (1u << 12),
GIT_STATUS_IGNORED = (1u << 14),
+ GIT_STATUS_CONFLICTED = (1u << 15),
} git_status_t;
/**
diff --git a/src/diff.c b/src/diff.c
index f7e1c8e..c9d1d87 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -77,11 +77,24 @@ static int diff_insert_delta(
static int diff_delta__from_one(
git_diff *diff,
git_delta_t status,
- const git_index_entry *entry)
+ const git_index_entry *oitem,
+ const git_index_entry *nitem)
{
+ const git_index_entry *entry = nitem;
+ bool has_old = false;
git_diff_delta *delta;
const char *matched_pathspec;
+ assert((oitem != NULL) ^ (nitem != NULL));
+
+ if (oitem) {
+ entry = oitem;
+ has_old = true;
+ }
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
+ has_old = !has_old;
+
if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
return 0;
@@ -111,20 +124,21 @@ static int diff_delta__from_one(
assert(status != GIT_DELTA_MODIFIED);
delta->nfiles = 1;
- if (delta->status == GIT_DELTA_DELETED) {
+ if (has_old) {
delta->old_file.mode = entry->mode;
delta->old_file.size = entry->file_size;
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
git_oid_cpy(&delta->old_file.id, &entry->id);
} else /* ADDED, IGNORED, UNTRACKED */ {
delta->new_file.mode = entry->mode;
delta->new_file.size = entry->file_size;
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
git_oid_cpy(&delta->new_file.id, &entry->id);
}
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
- if (delta->status == GIT_DELTA_DELETED ||
- !git_oid_iszero(&delta->new_file.id))
+ if (has_old || !git_oid_iszero(&delta->new_file.id))
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
return diff_insert_delta(diff, delta, matched_pathspec);
@@ -137,9 +151,10 @@ static int diff_delta__from_two(
uint32_t old_mode,
const git_index_entry *new_entry,
uint32_t new_mode,
- git_oid *new_oid,
+ const git_oid *new_id,
const char *matched_pathspec)
{
+ const git_oid *old_id = &old_entry->id;
git_diff_delta *delta;
const char *canonical_path = old_entry->path;
@@ -147,38 +162,45 @@ static int diff_delta__from_two(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
return 0;
+ if (!new_id)
+ new_id = &new_entry->id;
+
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
uint32_t temp_mode = old_mode;
const git_index_entry *temp_entry = old_entry;
+ const git_oid *temp_id = old_id;
+
old_entry = new_entry;
new_entry = temp_entry;
old_mode = new_mode;
new_mode = temp_mode;
+ old_id = new_id;
+ new_id = temp_id;
}
delta = diff_delta__alloc(diff, status, canonical_path);
GITERR_CHECK_ALLOC(delta);
delta->nfiles = 2;
- git_oid_cpy(&delta->old_file.id, &old_entry->id);
- delta->old_file.size = old_entry->file_size;
- delta->old_file.mode = old_mode;
- delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
+ if (!git_index_entry_is_conflict(old_entry)) {
+ delta->old_file.size = old_entry->file_size;
+ delta->old_file.mode = old_mode;
+ git_oid_cpy(&delta->old_file.id, old_id);
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
+ GIT_DIFF_FLAG_EXISTS;
+ }
- git_oid_cpy(&delta->new_file.id, &new_entry->id);
- delta->new_file.size = new_entry->file_size;
- delta->new_file.mode = new_mode;
+ if (!git_index_entry_is_conflict(new_entry)) {
+ git_oid_cpy(&delta->new_file.id, new_id);
+ delta->new_file.size = new_entry->file_size;
+ delta->new_file.mode = new_mode;
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
- if (new_oid) {
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
- git_oid_cpy(&delta->old_file.id, new_oid);
- else
- git_oid_cpy(&delta->new_file.id, new_oid);
+ if (!git_oid_iszero(&new_entry->id))
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
}
- if (new_oid || !git_oid_iszero(&new_entry->id))
- delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
-
return diff_insert_delta(diff, delta, matched_pathspec);
}
@@ -327,6 +349,22 @@ static const char *diff_mnemonic_prefix(
return pfx;
}
+static int diff_entry_cmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcmp(entry_a->path, entry_b->path);
+}
+
+static int diff_entry_icmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcasecmp(entry_a->path, entry_b->path);
+}
+
static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
{
if (!ignore_case) {
@@ -335,7 +373,7 @@ static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
diff->strcomp = git__strcmp;
diff->strncomp = git__strncmp;
diff->pfxcomp = git__prefixcmp;
- diff->entrycomp = git_index_entry_cmp;
+ diff->entrycomp = diff_entry_cmp;
git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
} else {
@@ -344,7 +382,7 @@ static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
diff->strcomp = git__strcasecmp;
diff->strncomp = git__strncasecmp;
diff->pfxcomp = git__prefixcmp_icase;
- diff->entrycomp = git_index_entry_icmp;
+ diff->entrycomp = diff_entry_icmp;
git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
}
@@ -731,40 +769,47 @@ static int maybe_modified(
new_is_workdir)
nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
+ /* if one side is a conflict, mark the whole delta as conflicted */
+ if (git_index_entry_is_conflict(oitem) ||
+ git_index_entry_is_conflict(nitem)) {
+ status = GIT_DELTA_CONFLICTED;
+
/* support "assume unchanged" (poorly, b/c we still stat everything) */
- if ((oitem->flags & GIT_IDXENTRY_VALID) != 0)
+ } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
status = GIT_DELTA_UNMODIFIED;
/* support "skip worktree" index bit */
- else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0)
+ } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
status = GIT_DELTA_UNMODIFIED;
/* if basic type of file changed, then split into delete and add */
- else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE))
+ } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
status = GIT_DELTA_TYPECHANGE;
+ }
+
else if (nmode == GIT_FILEMODE_UNREADABLE) {
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem)))
- error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, nitem);
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
return error;
}
+
else {
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem)))
- error = diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem);
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
return error;
}
- }
/* if oids and modes match (and are valid), then file is unmodified */
- else if (git_oid_equal(&oitem->id, &nitem->id) &&
+ } else if (git_oid_equal(&oitem->id, &nitem->id) &&
omode == nmode &&
- !git_oid_iszero(&oitem->id))
+ !git_oid_iszero(&oitem->id)) {
status = GIT_DELTA_UNMODIFIED;
/* if we have an unknown OID and a workdir iterator, then check some
* circumstances that can accelerate things or need special handling
*/
- else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
+ } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
bool use_nanos = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_NANOSECS) != 0);
@@ -795,12 +840,12 @@ static int maybe_modified(
status = GIT_DELTA_MODIFIED;
modified_uncertain = true;
}
- }
/* if mode is GITLINK and submodules are ignored, then skip */
- else if (S_ISGITLINK(nmode) &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
+ } else if (S_ISGITLINK(nmode) &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
status = GIT_DELTA_UNMODIFIED;
+ }
/* if we got here and decided that the files are modified, but we
* haven't calculated the OID of the new item, then calculate it now
@@ -809,6 +854,7 @@ static int maybe_modified(
const git_oid *update_check =
DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
&oitem->id : NULL;
+
if ((error = git_diff__oid_for_entry(
&noid, diff, nitem, update_check)) < 0)
return error;
@@ -830,8 +876,9 @@ static int maybe_modified(
DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
strcmp(oitem->path, nitem->path) != 0) {
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem)))
- error = diff_delta__from_one(diff, GIT_DELTA_ADDED, nitem);
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
+
return error;
}
@@ -857,6 +904,84 @@ static bool entry_is_prefixed(
item->path[pathlen] == '/');
}
+static int iterator_current(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ int error;
+
+ if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ const git_index_entry *prev_entry = *entry;
+ int cmp, error;
+
+ /* if we're looking for conflicts, we only want to report
+ * one conflict for each file, instead of all three sides.
+ * so if this entry is a conflict for this file, and the
+ * previous one was a conflict for the same file, skip it.
+ */
+ while ((error = git_iterator_advance(entry, iterator)) == 0) {
+ if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
+ !git_index_entry_is_conflict(prev_entry) ||
+ !git_index_entry_is_conflict(*entry))
+ break;
+
+ cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
+ strcasecmp(prev_entry->path, (*entry)->path) :
+ strcmp(prev_entry->path, (*entry)->path);
+
+ if (cmp)
+ break;
+ }
+
+ if (error == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance_into(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ int error;
+
+ if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance_over_with_status(
+ const git_index_entry **entry,
+ git_iterator_status_t *status,
+ git_iterator *iterator)
+{
+ int error;
+
+ if ((error = git_iterator_advance_over_with_status(
+ entry, status, iterator)) == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
static int handle_unmatched_new_item(
git_diff *diff, diff_in_progress *info)
{
@@ -868,8 +993,12 @@ static int handle_unmatched_new_item(
/* check if this is a prefix of the other side */
contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_is_conflict(nitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
/* update delta_type if this item is ignored */
- if (git_iterator_current_is_ignored(info->new_iter))
+ else if (git_iterator_current_is_ignored(info->new_iter))
delta_type = GIT_DELTA_IGNORED;
if (nitem->mode == GIT_FILEMODE_TREE) {
@@ -904,18 +1033,17 @@ static int handle_unmatched_new_item(
git_iterator_status_t untracked_state;
/* attempt to insert record for this directory */
- if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
return error;
/* if delta wasn't created (because of rules), just skip ahead */
last = diff_delta__last_for_item(diff, nitem);
if (!last)
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
/* iterate into dir looking for an actual untracked file */
- if ((error = git_iterator_advance_over_with_status(
- &info->nitem, &untracked_state, info->new_iter)) < 0 &&
- error != GIT_ITEROVER)
+ if ((error = iterator_advance_over_with_status(
+ &info->nitem, &untracked_state, info->new_iter)) < 0)
return error;
/* if we found nothing or just ignored items, update the record */
@@ -935,7 +1063,7 @@ static int handle_unmatched_new_item(
/* try to advance into directory if necessary */
if (recurse_into_dir) {
- error = git_iterator_advance_into(&info->nitem, info->new_iter);
+ error = iterator_advance_into(&info->nitem, info->new_iter);
/* if real error or no error, proceed with iteration */
if (error != GIT_ENOTFOUND)
@@ -946,7 +1074,7 @@ static int handle_unmatched_new_item(
* it or ignore it
*/
if (contains_oitem)
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
delta_type = GIT_DELTA_IGNORED;
}
}
@@ -955,7 +1083,7 @@ static int handle_unmatched_new_item(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
git_iterator_current_tree_is_ignored(info->new_iter))
/* item contained in ignored directory, so skip over it */
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR)
delta_type = GIT_DELTA_ADDED;
@@ -968,12 +1096,12 @@ static int handle_unmatched_new_item(
/* if this contains a tracked item, treat as normal TREE */
if (contains_oitem) {
- error = git_iterator_advance_into(&info->nitem, info->new_iter);
+ error = iterator_advance_into(&info->nitem, info->new_iter);
if (error != GIT_ENOTFOUND)
return error;
giterr_clear();
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
}
}
}
@@ -986,7 +1114,7 @@ static int handle_unmatched_new_item(
}
/* Actually create the record for this item if necessary */
- if ((error = diff_delta__from_one(diff, delta_type, nitem)) != 0)
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
return error;
/* If user requested TYPECHANGE records, then check for that instead of
@@ -1004,14 +1132,20 @@ static int handle_unmatched_new_item(
}
}
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
}
static int handle_unmatched_old_item(
git_diff *diff, diff_in_progress *info)
{
- int error = diff_delta__from_one(diff, GIT_DELTA_DELETED, info->oitem);
- if (error != 0)
+ git_delta_t delta_type = GIT_DELTA_DELETED;
+ int error;
+
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_is_conflict(info->oitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
+ if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
return error;
/* if we are generating TYPECHANGE records then check for that
@@ -1033,10 +1167,10 @@ static int handle_unmatched_old_item(
*/
if (S_ISDIR(info->nitem->mode) &&
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
- return git_iterator_advance(&info->nitem, info->new_iter);
+ return iterator_advance(&info->nitem, info->new_iter);
}
- return git_iterator_advance(&info->oitem, info->old_iter);
+ return iterator_advance(&info->oitem, info->old_iter);
}
static int handle_matched_item(
@@ -1047,9 +1181,8 @@ static int handle_matched_item(
if ((error = maybe_modified(diff, info)) < 0)
return error;
- if (!(error = git_iterator_advance(&info->oitem, info->old_iter)) ||
- error == GIT_ITEROVER)
- error = git_iterator_advance(&info->nitem, info->new_iter);
+ if (!(error = iterator_advance(&info->oitem, info->old_iter)))
+ error = iterator_advance(&info->nitem, info->new_iter);
return error;
}
@@ -1085,13 +1218,9 @@ int git_diff__from_iterators(
if ((error = diff_list_apply_options(diff, opts)) < 0)
goto cleanup;
- if ((error = git_iterator_current(&info.oitem, old_iter)) < 0 &&
- error != GIT_ITEROVER)
+ if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
+ (error = iterator_current(&info.nitem, new_iter)) < 0)
goto cleanup;
- if ((error = git_iterator_current(&info.nitem, new_iter)) < 0 &&
- error != GIT_ITEROVER)
- goto cleanup;
- error = 0;
/* run iterators building diffs */
while (!error && (info.oitem || info.nitem)) {
@@ -1113,10 +1242,6 @@ int git_diff__from_iterators(
*/
else
error = handle_matched_item(diff, &info);
-
- /* because we are iterating over two lists, ignore ITEROVER */
- if (error == GIT_ITEROVER)
- error = 0;
}
diff->perf.stat_calls += old_iter->stat_calls + new_iter->stat_calls;
@@ -1186,6 +1311,8 @@ int git_diff_tree_to_index(
{
int error = 0;
bool index_ignore_case = false;
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
assert(diff && repo);
@@ -1195,10 +1322,8 @@ int git_diff_tree_to_index(
index_ignore_case = index->ignore_case;
DIFF_FROM_ITERATORS(
- git_iterator_for_tree(
- &a, old_tree, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx),
- git_iterator_for_index(
- &b, index, GIT_ITERATOR_DONT_IGNORE_CASE, pfx, pfx)
+ git_iterator_for_tree(&a, old_tree, iflag, pfx, pfx),
+ git_iterator_for_index(&b, index, iflag, pfx, pfx)
);
/* if index is in case-insensitive order, re-sort deltas to match */
@@ -1222,7 +1347,8 @@ int git_diff_index_to_workdir(
return error;
DIFF_FROM_ITERATORS(
- git_iterator_for_index(&a, index, 0, pfx, pfx),
+ git_iterator_for_index(
+ &a, index, GIT_ITERATOR_INCLUDE_CONFLICTS, pfx, pfx),
git_iterator_for_workdir(
&b, repo, index, NULL, GIT_ITERATOR_DONT_AUTOEXPAND, pfx, pfx)
);
diff --git a/src/index.c b/src/index.c
index 561fe73..0422ff1 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1173,6 +1173,9 @@ int git_index_remove_bypath(git_index *index, const char *path)
ret != GIT_ENOTFOUND))
return ret;
+ if (ret == GIT_ENOTFOUND)
+ giterr_clear();
+
return 0;
}
@@ -1314,6 +1317,30 @@ int git_index_conflict_add(git_index *index,
(ret = index_entry_dup(&entries[2], INDEX_OWNER(index), their_entry)) < 0)
goto on_error;
+ /* Validate entries */
+ for (i = 0; i < 3; i++) {
+ if (entries[i] && !valid_filemode(entries[i]->mode)) {
+ giterr_set(GITERR_INDEX, "invalid filemode for stage %d entry",
+ i);
+ return -1;
+ }
+ }
+
+ /* Remove existing index entries for each path */
+ for (i = 0; i < 3; i++) {
+ if (entries[i] == NULL)
+ continue;
+
+ if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) {
+ if (ret != GIT_ENOTFOUND)
+ goto on_error;
+
+ giterr_clear();
+ ret = 0;
+ }
+ }
+
+ /* Add the conflict entries */
for (i = 0; i < 3; i++) {
if (entries[i] == NULL)
continue;
@@ -1321,7 +1348,7 @@ int git_index_conflict_add(git_index *index,
/* Make sure stage is correct */
GIT_IDXENTRY_STAGE_SET(entries[i], i + 1);
- if ((ret = index_insert(index, &entries[i], 1, true)) < 0)
+ if ((ret = index_insert(index, &entries[i], 0, true)) < 0)
goto on_error;
entries[i] = NULL; /* don't free if later entry fails */
@@ -1510,7 +1537,7 @@ int git_index_conflict_next(
while (iterator->cur < iterator->index->entries.length) {
entry = git_index_get_byindex(iterator->index, iterator->cur);
- if (git_index_entry_stage(entry) > 0) {
+ if (git_index_entry_is_conflict(entry)) {
if ((len = index_conflict__get_byindex(
ancestor_out,
our_out,
@@ -2356,6 +2383,11 @@ int git_index_entry_stage(const git_index_entry *entry)
return GIT_IDXENTRY_STAGE(entry);
}
+int git_index_entry_is_conflict(const git_index_entry *entry)
+{
+ return (GIT_IDXENTRY_STAGE(entry) > 0);
+}
+
typedef struct read_tree_data {
git_index *index;
git_vector *old_entries;
@@ -2638,7 +2670,8 @@ static int apply_each_file(const git_diff_delta *delta, float progress, void *pa
if (error < 0) /* actual error */
return error;
- if (delta->status == GIT_DELTA_DELETED)
+ /* If the workdir item does not exist, remove it from the index. */
+ if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0)
error = git_index_remove_bypath(data->index, path);
else
error = git_index_add_bypath(data->index, delta->new_file.path);
diff --git a/src/iterator.c b/src/iterator.c
index c5c5fd7..93303a8 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -46,6 +46,7 @@
#define iterator__include_trees(I) iterator__flag(I,INCLUDE_TREES)
#define iterator__dont_autoexpand(I) iterator__flag(I,DONT_AUTOEXPAND)
#define iterator__do_autoexpand(I) !iterator__flag(I,DONT_AUTOEXPAND)
+#define iterator__include_conflicts(I) iterator__flag(I, INCLUDE_CONFLICTS)
#define GIT_ITERATOR_FIRST_ACCESS (1 << 15)
#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
@@ -668,13 +669,16 @@ static const git_index_entry *index_iterator__index_entry(index_iterator *ii)
return ie;
}
-static const git_index_entry *index_iterator__skip_conflicts(index_iterator *ii)
+static const git_index_entry *index_iterator__advance_over_conflicts(index_iterator *ii)
{
- const git_index_entry *ie;
+ const git_index_entry *ie = index_iterator__index_entry(ii);
- while ((ie = index_iterator__index_entry(ii)) != NULL &&
- git_index_entry_stage(ie) != 0)
- ii->current++;
+ if (!iterator__include_conflicts(ii)) {
+ while (ie && git_index_entry_is_conflict(ie)) {
+ ii->current++;
+ ie = index_iterator__index_entry(ii);
+ }
+ }
return ie;
}
@@ -702,7 +706,7 @@ static void index_iterator__next_prefix_tree(index_iterator *ii)
static int index_iterator__first_prefix_tree(index_iterator *ii)
{
- const git_index_entry *ie = index_iterator__skip_conflicts(ii);
+ const git_index_entry *ie = index_iterator__advance_over_conflicts(ii);
const char *scan, *prior, *slash;
if (!ie || !iterator__include_trees(ii))
@@ -825,7 +829,7 @@ static int index_iterator__reset(
git_index_snapshot_find(
&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
- if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
+ if ((ie = index_iterator__advance_over_conflicts(ii)) == NULL)
return 0;
if (git_buf_sets(&ii->partial, ie->path) < 0)
diff --git a/src/iterator.h b/src/iterator.h
index 1520bff..db1f325 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -33,6 +33,8 @@ typedef enum {
GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3),
/** convert precomposed unicode to decomposed unicode */
GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
+ /** include conflicts */
+ GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 5),
} git_iterator_flag_t;
typedef struct {
diff --git a/src/merge.c b/src/merge.c
index 5e77274..eaf7eee 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2492,7 +2492,7 @@ int git_merge__check_result(git_repository *repo, git_index *index_new)
for (i = 0; i < git_index_entrycount(index_new); i++) {
e = git_index_get_byindex(index_new, i);
- if (git_index_entry_stage(e) != 0 &&
+ if (git_index_entry_is_conflict(e) &&
(git_vector_last(&paths) == NULL ||
strcmp(git_vector_last(&paths), e->path) != 0)) {
@@ -2544,7 +2544,7 @@ int git_merge__append_conflicts_to_merge_msg(
for (i = 0; i < git_index_entrycount(index); i++) {
const git_index_entry *e = git_index_get_byindex(index, i);
- if (git_index_entry_stage(e) == 0)
+ if (!git_index_entry_is_conflict(e))
continue;
if (last == NULL || strcmp(e->path, last) != 0)
diff --git a/src/reset.c b/src/reset.c
index d08e48c..0ffa51b 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -63,6 +63,7 @@ int git_reset_default(
assert(delta->status == GIT_DELTA_ADDED ||
delta->status == GIT_DELTA_MODIFIED ||
+ delta->status == GIT_DELTA_CONFLICTED ||
delta->status == GIT_DELTA_DELETED);
error = git_index_conflict_remove(index, delta->old_file.path);
diff --git a/src/status.c b/src/status.c
index 720ccb7..b206b0e 100644
--- a/src/status.c
+++ b/src/status.c
@@ -44,6 +44,9 @@ static unsigned int index_delta2status(const git_diff_delta *head2idx)
case GIT_DELTA_TYPECHANGE:
st = GIT_STATUS_INDEX_TYPECHANGE;
break;
+ case GIT_DELTA_CONFLICTED:
+ st = GIT_STATUS_CONFLICTED;
+ break;
default:
break;
}
@@ -102,6 +105,9 @@ static unsigned int workdir_delta2status(
case GIT_DELTA_TYPECHANGE:
st = GIT_STATUS_WT_TYPECHANGE;
break;
+ case GIT_DELTA_CONFLICTED:
+ st = GIT_STATUS_CONFLICTED;
+ break;
default:
break;
}
diff --git a/tests/checkout/conflict.c b/tests/checkout/conflict.c
index 3d763af..006dc65 100644
--- a/tests/checkout/conflict.c
+++ b/tests/checkout/conflict.c
@@ -102,7 +102,7 @@ static void create_index(struct checkout_index_entry *entries, size_t entries_le
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = entries[i].mode;
- entry.flags = entries[i].stage << GIT_IDXENTRY_STAGESHIFT;
+ GIT_IDXENTRY_STAGE_SET(&entry, entries[i].stage);
git_oid_fromstr(&entry.id, entries[i].oid_str);
entry.path = entries[i].path;
diff --git a/tests/checkout/index.c b/tests/checkout/index.c
index de0770d..89b0145 100644
--- a/tests/checkout/index.c
+++ b/tests/checkout/index.c
@@ -685,15 +685,15 @@ static void add_conflict(git_index *index, const char *path)
entry.path = path;
git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46");
- entry.flags = (1 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(index, &entry));
git_oid_fromstr(&entry.id, "4e886e602529caa9ab11d71f86634bd1b6e0de10");
- entry.flags = (2 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(index, &entry));
git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
- entry.flags = (3 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 3);
cl_git_pass(git_index_add(index, &entry));
}
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index 50541a7..4623231 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -893,16 +893,16 @@ static void create_conflict(const char *path)
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = 0100644;
- entry.flags = 1 << GIT_IDXENTRY_STAGESHIFT;
+ GIT_IDXENTRY_STAGE_SET(&entry, 1);
git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46");
entry.path = path;
cl_git_pass(git_index_add(index, &entry));
- entry.flags = 2 << GIT_IDXENTRY_STAGESHIFT;
+ GIT_IDXENTRY_STAGE_SET(&entry, 2);
git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
cl_git_pass(git_index_add(index, &entry));
- entry.flags = 3 << GIT_IDXENTRY_STAGESHIFT;
+ GIT_IDXENTRY_STAGE_SET(&entry, 3);
git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
cl_git_pass(git_index_add(index, &entry));
diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c
index 6087c2a..dabc47a 100644
--- a/tests/clar_libgit2.c
+++ b/tests/clar_libgit2.c
@@ -197,6 +197,14 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
return _cl_repo;
}
+git_repository *cl_git_sandbox_init_new(const char *sandbox)
+{
+ cl_git_pass(git_repository_init(&_cl_repo, sandbox, false));
+ _cl_sandbox = sandbox;
+
+ return _cl_repo;
+}
+
git_repository *cl_git_sandbox_reopen(void)
{
if (_cl_repo) {
diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h
index 86c90b0..9ab0da4 100644
--- a/tests/clar_libgit2.h
+++ b/tests/clar_libgit2.h
@@ -127,6 +127,7 @@ int cl_rename(const char *source, const char *dest);
/* Git sandbox setup helpers */
git_repository *cl_git_sandbox_init(const char *sandbox);
+git_repository *cl_git_sandbox_init_new(const char *name);
void cl_git_sandbox_cleanup(void);
git_repository *cl_git_sandbox_reopen(void);
diff --git a/tests/diff/diff_helpers.c b/tests/diff/diff_helpers.c
index 47e06f0..03862d6 100644
--- a/tests/diff/diff_helpers.c
+++ b/tests/diff/diff_helpers.c
@@ -68,7 +68,7 @@ int diff_file_cb(
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
e->files_binary++;
- cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);
+ cl_assert(delta->status <= GIT_DELTA_CONFLICTED);
e->file_status[delta->status] += 1;
diff --git a/tests/diff/diff_helpers.h b/tests/diff/diff_helpers.h
index bf21f4b..6bcd1e6 100644
--- a/tests/diff/diff_helpers.h
+++ b/tests/diff/diff_helpers.h
@@ -8,7 +8,7 @@ typedef struct {
int files;
int files_binary;
- int file_status[10]; /* indexed by git_delta_t value */
+ int file_status[11]; /* indexed by git_delta_t value */
int hunks;
int hunk_new_lines;
diff --git a/tests/diff/index.c b/tests/diff/index.c
index 21afe8d..a544b83 100644
--- a/tests/diff/index.c
+++ b/tests/diff/index.c
@@ -163,3 +163,79 @@ void test_diff_index__checks_options_version(void)
git_tree_free(a);
}
+static void do_conflicted_diff(diff_expects *exp, unsigned long flags)
+{
+ const char *a_commit = "26a125ee1bf"; /* the current HEAD */
+ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+ git_index_entry ancestor = {0}, ours = {0}, theirs = {0};
+ git_diff *diff = NULL;
+ git_index *index;
+
+ cl_assert(a);
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 1;
+ opts.flags |= flags;
+
+ memset(exp, 0, sizeof(diff_expects));
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ ancestor.path = ours.path = theirs.path = "staged_changes";
+ ancestor.mode = ours.mode = theirs.mode = 0100644;
+
+ git_oid_fromstr(&ancestor.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+ git_oid_fromstr(&ours.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+ git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+
+ cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
+ cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, &opts));
+
+ cl_git_pass(git_diff_foreach(
+ diff, diff_file_cb, diff_hunk_cb, diff_line_cb, exp));
+
+ git_diff_free(diff);
+ git_tree_free(a);
+ git_index_free(index);
+}
+
+void test_diff_index__reports_conflicts(void)
+{
+ diff_expects exp;
+
+ do_conflicted_diff(&exp, 0);
+
+ cl_assert_equal_i(8, exp.files);
+ cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);
+
+ cl_assert_equal_i(7, exp.hunks);
+
+ cl_assert_equal_i(9, exp.lines);
+ cl_assert_equal_i(2, exp.line_ctxt);
+ cl_assert_equal_i(5, exp.line_adds);
+ cl_assert_equal_i(2, exp.line_dels);
+}
+
+void test_diff_index__reports_conflicts_when_reversed(void)
+{
+ diff_expects exp;
+
+ do_conflicted_diff(&exp, GIT_DIFF_REVERSE);
+
+ cl_assert_equal_i(8, exp.files);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
+ cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);
+
+ cl_assert_equal_i(7, exp.hunks);
+
+ cl_assert_equal_i(9, exp.lines);
+ cl_assert_equal_i(2, exp.line_ctxt);
+ cl_assert_equal_i(2, exp.line_adds);
+ cl_assert_equal_i(5, exp.line_dels);
+}
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c
index 963be94..460f1b5 100644
--- a/tests/diff/workdir.c
+++ b/tests/diff/workdir.c
@@ -68,6 +68,51 @@ void test_diff_workdir__to_index(void)
git_diff_free(diff);
}
+void test_diff_workdir__to_index_with_conflicts(void)
+{
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+ git_diff *diff = NULL;
+ git_index *index;
+ git_index_entry our_entry = {0}, their_entry = {0};
+ diff_expects exp = {0};
+
+ g_repo = cl_git_sandbox_init("status");
+
+ opts.context_lines = 3;
+ opts.interhunk_lines = 1;
+
+ /* Adding an entry that represents a rename gets two files in conflict */
+ our_entry.path = "subdir/modified_file";
+ our_entry.mode = 0100644;
+
+ their_entry.path = "subdir/rename_conflict";
+ their_entry.mode = 0100644;
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_conflict_add(index, NULL, &our_entry, &their_entry));
+
+ cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &opts));
+
+ cl_git_pass(diff_foreach_via_iterator(
+ diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+ cl_assert_equal_i(9, exp.files);
+ cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
+ cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
+ cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(2, exp.file_status[GIT_DELTA_CONFLICTED]);
+
+ cl_assert_equal_i(7, exp.hunks);
+
+ cl_assert_equal_i(12, exp.lines);
+ cl_assert_equal_i(4, exp.line_ctxt);
+ cl_assert_equal_i(3, exp.line_adds);
+ cl_assert_equal_i(5, exp.line_dels);
+
+ git_diff_free(diff);
+ git_index_free(index);
+}
+
void test_diff_workdir__to_index_with_assume_unchanged(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
diff --git a/tests/index/addall.c b/tests/index/addall.c
index 377d207..211e762 100644
--- a/tests/index/addall.c
+++ b/tests/index/addall.c
@@ -12,10 +12,7 @@ void test_index_addall__initialize(void)
void test_index_addall__cleanup(void)
{
- git_repository_free(g_repo);
- g_repo = NULL;
-
- cl_fixture_cleanup(TEST_DIR);
+ cl_git_sandbox_cleanup();
}
#define STATUS_INDEX_FLAGS \
@@ -36,6 +33,7 @@ typedef struct {
size_t wt_dels;
size_t wt_mods;
size_t ignores;
+ size_t conflicts;
} index_status_counts;
static int index_status_cb(
@@ -67,6 +65,8 @@ static int index_status_cb(
if (status_flags & GIT_STATUS_IGNORED)
vals->ignores++;
+ if (status_flags & GIT_STATUS_CONFLICTED)
+ vals->conflicts++;
return 0;
}
@@ -75,7 +75,7 @@ static void check_status_at_line(
git_repository *repo,
size_t index_adds, size_t index_dels, size_t index_mods,
size_t wt_adds, size_t wt_dels, size_t wt_mods, size_t ignores,
- const char *file, int line)
+ size_t conflicts, const char *file, int line)
{
index_status_counts vals;
@@ -97,10 +97,12 @@ static void check_status_at_line(
file,line,"wrong workdir mods", 1, "%"PRIuZ, wt_mods, vals.wt_mods);
clar__assert_equal(
file,line,"wrong ignores", 1, "%"PRIuZ, ignores, vals.ignores);
+ clar__assert_equal(
+ file,line,"wrong conflicts", 1, "%"PRIuZ, conflicts, vals.conflicts);
}
-#define check_status(R,IA,ID,IM,WA,WD,WM,IG) \
- check_status_at_line(R,IA,ID,IM,WA,WD,WM,IG,__FILE__,__LINE__)
+#define check_status(R,IA,ID,IM,WA,WD,WM,IG,C) \
+ check_status_at_line(R,IA,ID,IM,WA,WD,WM,IG,C,__FILE__,__LINE__)
static void check_stat_data(git_index *index, const char *path, bool match)
{
@@ -137,21 +139,22 @@ static void check_stat_data(git_index *index, const char *path, bool match)
static void addall_create_test_repo(bool check_every_step)
{
- cl_git_pass(git_repository_init(&g_repo, TEST_DIR, false));
+ g_repo = cl_git_sandbox_init_new(TEST_DIR);
+
if (check_every_step)
- check_status(g_repo, 0, 0, 0, 0, 0, 0, 0);
+ check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
cl_git_mkfile(TEST_DIR "/file.foo", "a file");
if (check_every_step)
- check_status(g_repo, 0, 0, 0, 1, 0, 0, 0);
+ check_status(g_repo, 0, 0, 0, 1, 0, 0, 0, 0);
cl_git_mkfile(TEST_DIR "/.gitignore", "*.foo\n");
if (check_every_step)
- check_status(g_repo, 0, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 1, 0, 0, 1, 0);
cl_git_mkfile(TEST_DIR "/file.bar", "another file");
if (check_every_step)
- check_status(g_repo, 0, 0, 0, 2, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 2, 0, 0, 1, 0);
}
void test_index_addall__repo_lifecycle(void)
@@ -171,107 +174,107 @@ void test_index_addall__repo_lifecycle(void)
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
- check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
cl_git_rewritefile(TEST_DIR "/file.bar", "new content for file");
check_stat_data(index, TEST_DIR "/file.bar", false);
- check_status(g_repo, 1, 0, 0, 1, 0, 1, 1);
+ check_status(g_repo, 1, 0, 0, 1, 0, 1, 1, 0);
cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
- check_status(g_repo, 1, 0, 0, 4, 0, 1, 1);
+ check_status(g_repo, 1, 0, 0, 4, 0, 1, 1, 0);
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
- check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
+ check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.zzz", true);
- check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
- check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
if (cl_repo_get_bool(g_repo, "core.filemode")) {
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0777));
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
- check_status(g_repo, 0, 0, 1, 3, 0, 0, 1);
+ check_status(g_repo, 0, 0, 1, 3, 0, 0, 1, 0);
/* go back to what we had before */
cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0666));
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
- check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
}
/* attempt to add an ignored file - does nothing */
strs[0] = "file.foo";
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
- check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
/* add with check - should generate error */
error = git_index_add_all(
index, &paths, GIT_INDEX_ADD_CHECK_PATHSPEC, NULL, NULL);
cl_assert_equal_i(GIT_EINVALIDSPEC, error);
- check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
/* add with force - should allow */
cl_git_pass(git_index_add_all(
index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.foo", true);
- check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
+ check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
/* now it's in the index, so regular add should work */
cl_git_rewritefile(TEST_DIR "/file.foo", "new content for file");
check_stat_data(index, TEST_DIR "/file.foo", false);
- check_status(g_repo, 1, 0, 0, 3, 0, 1, 0);
+ check_status(g_repo, 1, 0, 0, 3, 0, 1, 0, 0);
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.foo", true);
- check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
+ check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
cl_git_pass(git_index_add_bypath(index, "more.zzz"));
check_stat_data(index, TEST_DIR "/more.zzz", true);
- check_status(g_repo, 2, 0, 0, 2, 0, 0, 0);
+ check_status(g_repo, 2, 0, 0, 2, 0, 0, 0, 0);
cl_git_rewritefile(TEST_DIR "/file.zzz", "new content for file");
- check_status(g_repo, 2, 0, 0, 2, 0, 1, 0);
+ check_status(g_repo, 2, 0, 0, 2, 0, 1, 0, 0);
cl_git_pass(git_index_add_bypath(index, "file.zzz"));
check_stat_data(index, TEST_DIR "/file.zzz", true);
- check_status(g_repo, 2, 0, 1, 2, 0, 0, 0);
+ check_status(g_repo, 2, 0, 1, 2, 0, 0, 0, 0);
strs[0] = "*.zzz";
cl_git_pass(git_index_remove_all(index, &paths, NULL, NULL));
- check_status(g_repo, 1, 1, 0, 4, 0, 0, 0);
+ check_status(g_repo, 1, 1, 0, 4, 0, 0, 0, 0);
cl_git_pass(git_index_add_bypath(index, "file.zzz"));
- check_status(g_repo, 1, 0, 1, 3, 0, 0, 0);
+ check_status(g_repo, 1, 0, 1, 3, 0, 0, 0, 0);
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
- check_status(g_repo, 0, 0, 0, 3, 0, 0, 0);
+ check_status(g_repo, 0, 0, 0, 3, 0, 0, 0, 0);
cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
- check_status(g_repo, 0, 0, 0, 3, 1, 0, 0);
+ check_status(g_repo, 0, 0, 0, 3, 1, 0, 0, 0);
/* update_all should be able to remove entries */
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
- check_status(g_repo, 0, 1, 0, 3, 0, 0, 0);
+ check_status(g_repo, 0, 1, 0, 3, 0, 0, 0, 0);
strs[0] = "*";
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
- check_status(g_repo, 3, 1, 0, 0, 0, 0, 0);
+ check_status(g_repo, 3, 1, 0, 0, 0, 0, 0, 0);
/* must be able to remove at any position while still updating other files */
cl_must_pass(p_unlink(TEST_DIR "/.gitignore"));
cl_git_rewritefile(TEST_DIR "/file.zzz", "reconstructed file");
cl_git_rewritefile(TEST_DIR "/more.zzz", "altered file reality");
- check_status(g_repo, 3, 1, 0, 1, 1, 1, 0);
+ check_status(g_repo, 3, 1, 0, 1, 1, 1, 0, 0);
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
- check_status(g_repo, 2, 1, 0, 1, 0, 0, 0);
+ check_status(g_repo, 2, 1, 0, 1, 0, 0, 0, 0);
/* this behavior actually matches 'git add -u' where "file.zzz" has
* been removed from the index, so when you go to update, even though
* it exists in the HEAD, it is not re-added to the index, leaving it
@@ -293,14 +296,14 @@ void test_index_addall__files_in_folders(void)
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
- check_status(g_repo, 2, 0, 0, 0, 0, 0, 1);
+ check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0);
cl_must_pass(p_mkdir(TEST_DIR "/subdir", 0777));
cl_git_mkfile(TEST_DIR "/subdir/file", "hello!\n");
- check_status(g_repo, 2, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 2, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
- check_status(g_repo, 3, 0, 0, 0, 0, 0, 1);
+ check_status(g_repo, 3, 0, 0, 0, 0, 0, 1, 0);
git_index_free(index);
}
@@ -337,7 +340,7 @@ void test_index_addall__callback_filtering(void)
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_prefix, "file."));
check_stat_data(index, TEST_DIR "/file.bar", true);
- check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
@@ -345,32 +348,32 @@ void test_index_addall__callback_filtering(void)
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
- check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
+ check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_prefix, "other"));
check_stat_data(index, TEST_DIR "/other.zzz", true);
- check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
+ check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
- check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(
git_index_remove_all(index, NULL, addall_match_suffix, ".zzz"));
- check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
+ check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_fail_with(
git_index_add_all(index, NULL, 0, addall_cancel_at, "more.zzz"), -123);
- check_status(g_repo, 3, 0, 0, 2, 0, 0, 1);
+ check_status(g_repo, 3, 0, 0, 2, 0, 0, 1, 0);
cl_git_fail_with(
git_index_add_all(index, NULL, 0, addall_cancel_at, "other.zzz"), -123);
- check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
+ check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
- check_status(g_repo, 5, 0, 0, 0, 0, 0, 1);
+ check_status(g_repo, 5, 0, 0, 0, 0, 0, 1, 0);
cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
cl_must_pass(p_unlink(TEST_DIR "/more.zzz"));
@@ -380,13 +383,65 @@ void test_index_addall__callback_filtering(void)
git_index_update_all(index, NULL, addall_cancel_at, "more.zzz"), -123);
/* file.zzz removed from index (so Index Adds 5 -> 4) and
* more.zzz + other.zzz removed (so Worktree Dels 0 -> 2) */
- check_status(g_repo, 4, 0, 0, 0, 2, 0, 1);
+ check_status(g_repo, 4, 0, 0, 0, 2, 0, 1, 0);
cl_git_fail_with(
git_index_update_all(index, NULL, addall_cancel_at, "other.zzz"), -123);
/* more.zzz removed from index (so Index Adds 4 -> 3) and
* Just other.zzz removed (so Worktree Dels 2 -> 1) */
- check_status(g_repo, 3, 0, 0, 0, 1, 0, 1);
+ check_status(g_repo, 3, 0, 0, 0, 1, 0, 1, 0);
git_index_free(index);
}
+
+void test_index_addall__adds_conflicts(void)
+{
+ git_index *index;
+ git_reference *ref;
+ git_annotated_commit *annotated;
+
+ g_repo = cl_git_sandbox_init("merge-resolve");
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
+ cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
+
+ cl_git_pass(git_merge(g_repo, &annotated, 1, NULL, NULL));
+ check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
+
+ cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
+ check_status(g_repo, 0, 1, 3, 0, 0, 0, 0, 0);
+
+ git_annotated_commit_free(annotated);
+ git_reference_free(ref);
+ git_index_free(index);
+}
+
+void test_index_addall__removes_deleted_conflicted_files(void)
+{
+ git_index *index;
+ git_reference *ref;
+ git_annotated_commit *annotated;
+
+ g_repo = cl_git_sandbox_init("merge-resolve");
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
+ cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
+
+ cl_git_pass(git_merge(g_repo, &annotated, 1, NULL, NULL));
+ check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
+
+ cl_git_rmfile("merge-resolve/conflicting.txt");
+
+ cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
+ check_status(g_repo, 0, 2, 2, 0, 0, 0, 0, 0);
+
+ git_annotated_commit_free(annotated);
+ git_reference_free(ref);
+ git_index_free(index);
+}
\ No newline at end of file
diff --git a/tests/index/collision.c b/tests/index/collision.c
index 1f002e8..19c1548 100644
--- a/tests/index/collision.c
+++ b/tests/index/collision.c
@@ -57,7 +57,7 @@ void test_index_collision__add_with_highstage_1(void)
git_oid_fromstr(&entry.id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
entry.path = "a/b";
- entry.flags = (2 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(index, &entry));
/* create a blob beneath the previous tree entry */
@@ -67,7 +67,7 @@ void test_index_collision__add_with_highstage_1(void)
/* create another tree entry above the blob */
entry.path = "a/b";
- entry.flags = (1 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(index, &entry));
git_index_free(index);
@@ -89,17 +89,17 @@ void test_index_collision__add_with_highstage_2(void)
git_oid_fromstr(&entry.id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
entry.path = "a/b/c";
- entry.flags = (1 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(index, &entry));
/* create a blob beneath the previous tree entry */
entry.path = "a/b/c";
- entry.flags = (2 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(index, &entry));
/* create another tree entry above the blob */
entry.path = "a/b";
- entry.flags = (3 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, 3);
cl_git_pass(git_index_add(index, &entry));
git_index_free(index);
diff --git a/tests/index/conflicts.c b/tests/index/conflicts.c
index 4273516..b7a2456 100644
--- a/tests/index/conflicts.c
+++ b/tests/index/conflicts.c
@@ -16,6 +16,7 @@ static git_index *repo_index;
#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"
+#define TEST_STAGED_OID "beefdadafeedabedcafedeedbabedeadbeaddeaf"
#define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f"
#define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b"
#define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567"
@@ -46,15 +47,18 @@ void test_index_conflicts__add(void)
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
- ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
+ ancestor_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
our_entry.path = "test-one.txt";
- ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
+ our_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&our_entry, 2);
git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
their_entry.path = "test-one.txt";
- ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
+ their_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 2);
git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
@@ -74,15 +78,18 @@ void test_index_conflicts__add_fixes_incorrect_stage(void)
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
- ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
+ ancestor_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 3);
git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
our_entry.path = "test-one.txt";
- ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
+ our_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&our_entry, 1);
git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
their_entry.path = "test-one.txt";
- ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
+ their_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&their_entry, 2);
git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
@@ -96,6 +103,55 @@ void test_index_conflicts__add_fixes_incorrect_stage(void)
cl_assert(git_index_entry_stage(conflict_entry[2]) == 3);
}
+void test_index_conflicts__add_removes_stage_zero(void)
+{
+ git_index_entry staged, ancestor_entry, our_entry, their_entry;
+ const git_index_entry *conflict_entry[3];
+
+ cl_assert(git_index_entrycount(repo_index) == 8);
+
+ memset(&staged, 0x0, sizeof(git_index_entry));
+ memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
+ memset(&our_entry, 0x0, sizeof(git_index_entry));
+ memset(&their_entry, 0x0, sizeof(git_index_entry));
+
+ staged.path = "test-one.txt";
+ staged.mode = 0100644;
+ git_oid_fromstr(&staged.id, TEST_STAGED_OID);
+ cl_git_pass(git_index_add(repo_index, &staged));
+ cl_assert(git_index_entrycount(repo_index) == 9);
+
+ ancestor_entry.path = "test-one.txt";
+ ancestor_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 3);
+ git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
+
+ our_entry.path = "test-one.txt";
+ our_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&our_entry, 1);
+ git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
+
+ their_entry.path = "test-one.txt";
+ their_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&their_entry, 2);
+ git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
+
+ cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
+
+ cl_assert(git_index_entrycount(repo_index) == 11);
+
+ cl_assert_equal_p(NULL, git_index_get_bypath(repo_index, "test-one.txt", 0));
+
+ cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt"));
+
+ cl_assert_equal_oid(&ancestor_entry.id, &conflict_entry[0]->id);
+ cl_assert_equal_i(1, git_index_entry_stage(conflict_entry[0]));
+ cl_assert_equal_oid(&our_entry.id, &conflict_entry[1]->id);
+ cl_assert_equal_i(2, git_index_entry_stage(conflict_entry[1]));
+ cl_assert_equal_oid(&their_entry.id, &conflict_entry[2]->id);
+ cl_assert_equal_i(3, git_index_entry_stage(conflict_entry[2]));
+}
+
void test_index_conflicts__get(void)
{
const git_index_entry *conflict_entry[3];
@@ -216,7 +272,7 @@ void test_index_conflicts__moved_to_reuc_on_add(void)
cl_assert(entry = git_index_get_byindex(repo_index, i));
if (strcmp(entry->path, "conflicts-one.txt") == 0)
- cl_assert(git_index_entry_stage(entry) == 0);
+ cl_assert(!git_index_entry_is_conflict(entry));
}
}
@@ -256,7 +312,7 @@ void test_index_conflicts__remove_all_conflicts(void)
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
- cl_assert(git_index_entry_stage(entry) == 0);
+ cl_assert(!git_index_entry_is_conflict(entry));
}
}
@@ -272,7 +328,8 @@ void test_index_conflicts__partial(void)
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
- ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
+ ancestor_entry.mode = 0100644;
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL));
diff --git a/tests/merge/trees/trivial.c b/tests/merge/trees/trivial.c
index 55f3824..2262edd 100644
--- a/tests/merge/trees/trivial.c
+++ b/tests/merge/trees/trivial.c
@@ -71,7 +71,7 @@ static int merge_trivial_conflict_entrycount(git_index *index)
for (i = 0; i < git_index_entrycount(index); i++) {
cl_assert(entry = git_index_get_byindex(index, i));
- if (git_index_entry_stage(entry) > 0)
+ if (git_index_entry_is_conflict(entry))
count++;
}
diff --git a/tests/merge/workdir/trivial.c b/tests/merge/workdir/trivial.c
index 5cc20f7..4ddaf23 100644
--- a/tests/merge/workdir/trivial.c
+++ b/tests/merge/workdir/trivial.c
@@ -66,7 +66,7 @@ static size_t merge_trivial_conflict_entrycount(void)
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
- if (git_index_entry_stage(entry) > 0)
+ if (git_index_entry_is_conflict(entry))
count++;
}
diff --git a/tests/object/tree/duplicateentries.c b/tests/object/tree/duplicateentries.c
index 65be12e..35dd383 100644
--- a/tests/object/tree/duplicateentries.c
+++ b/tests/object/tree/duplicateentries.c
@@ -126,17 +126,17 @@ static void add_fake_conflicts(git_index *index)
ancestor_entry.path = "duplicate";
ancestor_entry.mode = GIT_FILEMODE_BLOB;
- ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid_fromstr(&ancestor_entry.id, "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
our_entry.path = "duplicate";
our_entry.mode = GIT_FILEMODE_BLOB;
- ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&our_entry, 2);
git_oid_fromstr(&our_entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
their_entry.path = "duplicate";
their_entry.mode = GIT_FILEMODE_BLOB;
- ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&their_entry, 3);
git_oid_fromstr(&their_entry.id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
cl_git_pass(git_index_conflict_add(index, &ancestor_entry, &our_entry, &their_entry));
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 86d4be2..88055ad 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -108,7 +108,7 @@ static void index_entry_init(git_index *index, int side, git_oid *oid)
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = "conflicting_file";
- entry.flags = (side << GIT_IDXENTRY_STAGESHIFT);
+ GIT_IDXENTRY_STAGE_SET(&entry, side);
entry.mode = 0100644;
git_oid_cpy(&entry.id, oid);
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index 8897bf9..e272c0a 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -461,14 +461,17 @@ void test_status_worktree__conflict_with_diff3(void)
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "modified_file";
+ ancestor_entry.mode = 0100644;
git_oid_fromstr(&ancestor_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
our_entry.path = "modified_file";
+ our_entry.mode = 0100644;
git_oid_fromstr(&our_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
their_entry.path = "modified_file";
+ their_entry.mode = 0100644;
git_oid_fromstr(&their_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
@@ -484,7 +487,7 @@ void test_status_worktree__conflict_with_diff3(void)
cl_git_pass(git_status_file(&status, repo, "modified_file"));
- cl_assert_equal_i(GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW, status);
+ cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
}
static const char *filemode_paths[] = {
@@ -614,14 +617,17 @@ void test_status_worktree__conflicted_item(void)
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
+ ancestor_entry.mode = 0100644;
ancestor_entry.path = "modified_file";
git_oid_fromstr(&ancestor_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
+ our_entry.mode = 0100644;
our_entry.path = "modified_file";
git_oid_fromstr(&our_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
+ their_entry.mode = 0100644;
their_entry.path = "modified_file";
git_oid_fromstr(&their_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
@@ -634,9 +640,55 @@ void test_status_worktree__conflicted_item(void)
&our_entry, &their_entry));
cl_git_pass(git_status_file(&status, repo, "modified_file"));
- cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status);
+ cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
+
+ git_index_free(index);
+}
+
+void test_status_worktree__conflict_has_no_oid(void)
+{
+ git_repository *repo = cl_git_sandbox_init("status");
+ git_index *index;
+ git_index_entry entry = {0};
+ git_status_list *statuslist;
+ const git_status_entry *status;
+ git_oid zero_id = {0};
+
+ entry.mode = 0100644;
+ entry.path = "modified_file";
+ git_oid_fromstr(&entry.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_conflict_add(index, &entry, &entry, &entry));
+
+ git_status_list_new(&statuslist, repo, NULL);
+
+ cl_assert_equal_i(16, git_status_list_entrycount(statuslist));
+
+ status = git_status_byindex(statuslist, 2);
+
+ cl_assert_equal_i(GIT_STATUS_CONFLICTED, status->status);
+ cl_assert_equal_s("modified_file", status->head_to_index->old_file.path);
+ cl_assert(!git_oid_equal(&zero_id, &status->head_to_index->old_file.id));
+ cl_assert(0 != status->head_to_index->old_file.mode);
+ cl_assert_equal_s("modified_file", status->head_to_index->new_file.path);
+ cl_assert_equal_oid(&zero_id, &status->head_to_index->new_file.id);
+ cl_assert_equal_i(0, status->head_to_index->new_file.mode);
+ cl_assert_equal_i(0, status->head_to_index->new_file.size);
+
+ cl_assert_equal_s("modified_file", status->index_to_workdir->old_file.path);
+ cl_assert_equal_oid(&zero_id, &status->index_to_workdir->old_file.id);
+ cl_assert_equal_i(0, status->index_to_workdir->old_file.mode);
+ cl_assert_equal_i(0, status->index_to_workdir->old_file.size);
+ cl_assert_equal_s("modified_file", status->index_to_workdir->new_file.path);
+ cl_assert(
+ !git_oid_equal(&zero_id, &status->index_to_workdir->new_file.id) ||
+ !(status->index_to_workdir->new_file.flags & GIT_DIFF_FLAG_VALID_ID));
+ cl_assert(0 != status->index_to_workdir->new_file.mode);
+ cl_assert(0 != status->index_to_workdir->new_file.size);
git_index_free(index);
+ git_status_list_free(statuslist);
}
static void stage_and_commit(git_repository *repo, const char *path)