Merge pull request #4705 from libgit2/ethomson/apply Patch (diff) application
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 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039
diff --git a/include/git2.h b/include/git2.h
index e182ce9..9239b48 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -9,6 +9,7 @@
#define INCLUDE_git_git_h__
#include "git2/annotated_commit.h"
+#include "git2/apply.h"
#include "git2/attr.h"
#include "git2/blob.h"
#include "git2/blame.h"
diff --git a/include/git2/apply.h b/include/git2/apply.h
new file mode 100644
index 0000000..7cc1c22
--- /dev/null
+++ b/include/git2/apply.h
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_apply_h__
+#define INCLUDE_git_apply_h__
+
+#include "common.h"
+#include "types.h"
+#include "oid.h"
+#include "diff.h"
+
+/**
+ * @file git2/apply.h
+ * @brief Git patch application routines
+ * @defgroup git_apply Git patch application routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * When applying a patch, callback that will be made per delta (file).
+ *
+ * When the callback:
+ * - returns < 0, the apply process will be aborted.
+ * - returns > 0, the delta will not be applied, but the apply process
+ * continues
+ * - returns 0, the delta is applied, and the apply process continues.
+ *
+ * @param delta The delta to be applied
+ * @param payload User-specified payload
+ */
+typedef int (*git_apply_delta_cb)(
+ const git_diff_delta *delta,
+ void *payload);
+
+/**
+ * When applying a patch, callback that will be made per hunk.
+ *
+ * When the callback:
+ * - returns < 0, the apply process will be aborted.
+ * - returns > 0, the hunk will not be applied, but the apply process
+ * continues
+ * - returns 0, the hunk is applied, and the apply process continues.
+ *
+ * @param hunk The hunk to be applied
+ * @param payload User-specified payload
+ */
+typedef int (*git_apply_hunk_cb)(
+ const git_diff_hunk *hunk,
+ void *payload);
+
+/**
+ * Apply options structure
+ *
+ * Initialize with `GIT_APPLY_OPTIONS_INIT`. Alternatively, you can
+ * use `git_apply_init_options`.
+ *
+ * @see git_apply_to_tree, git_apply
+ */
+typedef struct {
+ unsigned int version;
+
+ git_apply_delta_cb delta_cb;
+ git_apply_hunk_cb hunk_cb;
+ void *payload;
+} git_apply_options;
+
+#define GIT_APPLY_OPTIONS_VERSION 1
+#define GIT_APPLY_OPTIONS_INIT {GIT_APPLY_OPTIONS_VERSION}
+
+/**
+ * Apply a `git_diff` to a `git_tree`, and return the resulting image
+ * as an index.
+ *
+ * @param out the postimage of the application
+ * @param repo the repository to apply
+ * @param preimage the tree to apply the diff to
+ * @param diff the diff to apply
+ * @param options the options for the apply (or null for defaults)
+ */
+GIT_EXTERN(int) git_apply_to_tree(
+ git_index **out,
+ git_repository *repo,
+ git_tree *preimage,
+ git_diff *diff,
+ const git_apply_options *options);
+
+typedef enum {
+ /**
+ * Apply the patch to the workdir, leaving the index untouched.
+ * This is the equivalent of `git apply` with no location argument.
+ */
+ GIT_APPLY_LOCATION_WORKDIR = 0,
+
+ /**
+ * Apply the patch to the index, leaving the working directory
+ * untouched. This is the equivalent of `git apply --cached`.
+ */
+ GIT_APPLY_LOCATION_INDEX = 1,
+
+ /**
+ * Apply the patch to both the working directory and the index.
+ * This is the equivalent of `git apply --index`.
+ */
+ GIT_APPLY_LOCATION_BOTH = 2,
+} git_apply_location_t;
+
+/**
+ * Apply a `git_diff` to the given repository, making changes directly
+ * in the working directory, the index, or both.
+ *
+ * @param repo the repository to apply to
+ * @param diff the diff to apply
+ * @param location the location to apply (workdir, index or both)
+ * @param options the options for the apply (or null for defaults)
+ */
+GIT_EXTERN(int) git_apply(
+ git_repository *repo,
+ git_diff *diff,
+ git_apply_location_t location,
+ const git_apply_options *options);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/include/git2/errors.h b/include/git2/errors.h
index c2a01de..b0ce45f 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -57,6 +57,7 @@ typedef enum {
GIT_RETRY = -32, /**< Internal only */
GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */
GIT_EINDEXDIRTY = -34, /**< Unsaved changes in the index would be overwritten */
+ GIT_EAPPLYFAIL = -35, /**< Patch application failed */
} git_error_code;
/**
diff --git a/src/apply.c b/src/apply.c
index 8c7bb6b..614baf7 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -9,16 +9,23 @@
#include <assert.h>
+#include "git2/apply.h"
#include "git2/patch.h"
#include "git2/filter.h"
+#include "git2/blob.h"
+#include "git2/index.h"
+#include "git2/checkout.h"
+#include "git2/repository.h"
#include "array.h"
#include "patch.h"
#include "fileops.h"
#include "delta.h"
#include "zstream.h"
+#include "reader.h"
+#include "index.h"
#define apply_err(...) \
- ( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
+ ( giterr_set(GITERR_PATCH, __VA_ARGS__), GIT_EAPPLYFAIL )
typedef struct {
/* The lines that we allocate ourself are allocated out of the pool.
@@ -160,15 +167,36 @@ static int update_hunk(
return 0;
}
+typedef struct {
+ git_apply_options opts;
+ size_t skipped_new_lines;
+ size_t skipped_old_lines;
+} apply_hunks_ctx;
+
static int apply_hunk(
patch_image *image,
git_patch *patch,
- git_patch_hunk *hunk)
+ git_patch_hunk *hunk,
+ apply_hunks_ctx *ctx)
{
patch_image preimage = PATCH_IMAGE_INIT, postimage = PATCH_IMAGE_INIT;
size_t line_num, i;
int error = 0;
+ if (ctx->opts.hunk_cb) {
+ error = ctx->opts.hunk_cb(&hunk->hunk, ctx->opts.payload);
+
+ if (error) {
+ if (error > 0) {
+ ctx->skipped_new_lines += hunk->hunk.new_lines;
+ ctx->skipped_old_lines += hunk->hunk.old_lines;
+ error = 0;
+ }
+
+ goto done;
+ }
+ }
+
for (i = 0; i < hunk->line_count; i++) {
size_t linenum = hunk->line_start + i;
git_diff_line *line = git_array_get(patch->lines, linenum);
@@ -191,7 +219,14 @@ static int apply_hunk(
}
}
- line_num = hunk->hunk.new_start ? hunk->hunk.new_start - 1 : 0;
+ if (hunk->hunk.new_start) {
+ line_num = hunk->hunk.new_start -
+ ctx->skipped_new_lines +
+ ctx->skipped_old_lines -
+ 1;
+ } else {
+ line_num = 0;
+ }
if (!find_hunk_linenum(&line_num, image, &preimage, line_num)) {
error = apply_err("hunk at line %d did not apply",
@@ -212,7 +247,8 @@ static int apply_hunks(
git_buf *out,
const char *source,
size_t source_len,
- git_patch *patch)
+ git_patch *patch,
+ apply_hunks_ctx *ctx)
{
git_patch_hunk *hunk;
git_diff_line *line;
@@ -224,7 +260,7 @@ static int apply_hunks(
goto done;
git_array_foreach(patch->hunks, i, hunk) {
- if ((error = apply_hunk(&image, patch, hunk)) < 0)
+ if ((error = apply_hunk(&image, patch, hunk, ctx)) < 0)
goto done;
}
@@ -332,14 +368,19 @@ int git_apply__patch(
unsigned int *mode_out,
const char *source,
size_t source_len,
- git_patch *patch)
+ git_patch *patch,
+ const git_apply_options *given_opts)
{
+ apply_hunks_ctx ctx = { GIT_APPLY_OPTIONS_INIT };
char *filename = NULL;
unsigned int mode = 0;
int error = 0;
assert(contents_out && filename_out && mode_out && (source || !source_len) && patch);
+ if (given_opts)
+ memcpy(&ctx.opts, given_opts, sizeof(git_apply_options));
+
*filename_out = NULL;
*mode_out = 0;
@@ -354,7 +395,7 @@ int git_apply__patch(
if (patch->delta->flags & GIT_DIFF_FLAG_BINARY)
error = apply_binary(contents_out, source, source_len, patch);
else if (patch->hunks.size)
- error = apply_hunks(contents_out, source, source_len, patch);
+ error = apply_hunks(contents_out, source, source_len, patch, &ctx);
else
error = git_buf_put(contents_out, source, source_len);
@@ -376,3 +417,438 @@ done:
return error;
}
+
+static int apply_one(
+ git_repository *repo,
+ git_reader *preimage_reader,
+ git_index *preimage,
+ git_reader *postimage_reader,
+ git_index *postimage,
+ git_diff *diff,
+ git_strmap *removed_paths,
+ size_t i,
+ const git_apply_options *opts)
+{
+ git_patch *patch = NULL;
+ git_buf pre_contents = GIT_BUF_INIT, post_contents = GIT_BUF_INIT;
+ const git_diff_delta *delta;
+ char *filename = NULL;
+ unsigned int mode;
+ git_oid pre_id, post_id;
+ git_filemode_t pre_filemode;
+ git_index_entry pre_entry, post_entry;
+ bool skip_preimage = false;
+ size_t pos;
+ int error;
+
+ if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
+ goto done;
+
+ delta = git_patch_get_delta(patch);
+
+ if (opts->delta_cb) {
+ error = opts->delta_cb(delta, opts->payload);
+
+ if (error) {
+ if (error > 0)
+ error = 0;
+
+ goto done;
+ }
+ }
+
+ /*
+ * Ensure that the file has not been deleted or renamed if we're
+ * applying a modification delta.
+ */
+ if (delta->status != GIT_DELTA_RENAMED &&
+ delta->status != GIT_DELTA_ADDED) {
+ pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
+ if (git_strmap_valid_index(removed_paths, pos)) {
+ error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
+ goto done;
+ }
+ }
+
+ /*
+ * We may be applying a second delta to an already seen file. If so,
+ * use the already modified data in the postimage instead of the
+ * content from the index or working directory. (Don't do this in
+ * the case of a rename, which must be specified before additional
+ * deltas since we apply deltas to the target filename.)
+ */
+ if (delta->status != GIT_DELTA_RENAMED) {
+ if ((error = git_reader_read(&pre_contents, &pre_id, &pre_filemode,
+ postimage_reader, delta->old_file.path)) == 0) {
+ skip_preimage = true;
+ } else if (error == GIT_ENOTFOUND) {
+ giterr_clear();
+ error = 0;
+ } else {
+ goto done;
+ }
+ }
+
+ if (!skip_preimage && delta->status != GIT_DELTA_ADDED) {
+ error = git_reader_read(&pre_contents, &pre_id, &pre_filemode,
+ preimage_reader, delta->old_file.path);
+
+ /* ENOTFOUND means the preimage was not found; apply failed. */
+ if (error == GIT_ENOTFOUND)
+ error = GIT_EAPPLYFAIL;
+
+ /* When applying to BOTH, the index did not match the workdir. */
+ if (error == GIT_READER_MISMATCH)
+ error = apply_err("%s: does not match index", delta->old_file.path);
+
+ if (error < 0)
+ goto done;
+
+ /*
+ * We need to populate the preimage data structure with the
+ * contents that we are using as the preimage for this file.
+ * This allows us to apply patches to files that have been
+ * modified in the working directory. During checkout,
+ * we will use this expected preimage as the baseline, and
+ * limit checkout to only the paths affected by patch
+ * application. (Without this, we would fail to write the
+ * postimage contents to any file that had been modified
+ * from HEAD on-disk, even if the patch application succeeded.)
+ * Use the contents from the delta where available - some
+ * fields may not be available, like the old file mode (eg in
+ * an exact rename situation) so trust the patch parsing to
+ * validate and use the preimage data in that case.
+ */
+ if (preimage) {
+ memset(&pre_entry, 0, sizeof(git_index_entry));
+ pre_entry.path = delta->old_file.path;
+ pre_entry.mode = delta->old_file.mode ? delta->old_file.mode : pre_filemode;
+ git_oid_cpy(&pre_entry.id, &pre_id);
+
+ if ((error = git_index_add(preimage, &pre_entry)) < 0)
+ goto done;
+ }
+ }
+
+ if (delta->status != GIT_DELTA_DELETED) {
+ if ((error = git_apply__patch(&post_contents, &filename, &mode,
+ pre_contents.ptr, pre_contents.size, patch, opts)) < 0 ||
+ (error = git_blob_create_frombuffer(&post_id, repo,
+ post_contents.ptr, post_contents.size)) < 0)
+ goto done;
+
+ memset(&post_entry, 0, sizeof(git_index_entry));
+ post_entry.path = filename;
+ post_entry.mode = mode;
+ git_oid_cpy(&post_entry.id, &post_id);
+
+ if ((error = git_index_add(postimage, &post_entry)) < 0)
+ goto done;
+ }
+
+ if (delta->status == GIT_DELTA_RENAMED ||
+ delta->status == GIT_DELTA_DELETED)
+ git_strmap_insert(removed_paths, delta->old_file.path, (char *)delta->old_file.path, &error);
+
+ if (delta->status == GIT_DELTA_RENAMED ||
+ delta->status == GIT_DELTA_ADDED)
+ git_strmap_delete(removed_paths, delta->new_file.path);
+
+done:
+ git_buf_dispose(&pre_contents);
+ git_buf_dispose(&post_contents);
+ git__free(filename);
+ git_patch_free(patch);
+
+ return error;
+}
+
+static int apply_deltas(
+ git_repository *repo,
+ git_reader *pre_reader,
+ git_index *preimage,
+ git_reader *post_reader,
+ git_index *postimage,
+ git_diff *diff,
+ const git_apply_options *opts)
+{
+ git_strmap *removed_paths;
+ size_t i;
+ int error;
+
+ if (git_strmap_alloc(&removed_paths) < 0)
+ return -1;
+
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ if ((error = apply_one(repo, pre_reader, preimage, post_reader, postimage, diff, removed_paths, i, opts)) < 0)
+ goto done;
+ }
+
+done:
+ git_strmap_free(removed_paths);
+ return error;
+}
+
+int git_apply_to_tree(
+ git_index **out,
+ git_repository *repo,
+ git_tree *preimage,
+ git_diff *diff,
+ const git_apply_options *given_opts)
+{
+ git_index *postimage = NULL;
+ git_reader *pre_reader = NULL, *post_reader = NULL;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ const git_diff_delta *delta;
+ size_t i;
+ int error = 0;
+
+ assert(out && repo && preimage && diff);
+
+ *out = NULL;
+
+ if (given_opts)
+ memcpy(&opts, given_opts, sizeof(git_apply_options));
+
+ if ((error = git_reader_for_tree(&pre_reader, preimage)) < 0)
+ goto done;
+
+ /*
+ * put the current tree into the postimage as-is - the diff will
+ * replace any entries contained therein
+ */
+ if ((error = git_index_new(&postimage)) < 0 ||
+ (error = git_index_read_tree(postimage, preimage)) < 0 ||
+ (error = git_reader_for_index(&post_reader, repo, postimage)) < 0)
+ goto done;
+
+ /*
+ * Remove the old paths from the index before applying diffs -
+ * we need to do a full pass to remove them before adding deltas,
+ * in order to handle rename situations.
+ */
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ delta = git_diff_get_delta(diff, i);
+
+ if ((error = git_index_remove(postimage,
+ delta->old_file.path, 0)) < 0)
+ goto done;
+ }
+
+ if ((error = apply_deltas(repo, pre_reader, NULL, post_reader, postimage, diff, &opts)) < 0)
+ goto done;
+
+ *out = postimage;
+
+done:
+ if (error < 0)
+ git_index_free(postimage);
+
+ git_reader_free(pre_reader);
+ git_reader_free(post_reader);
+
+ return error;
+}
+
+static int git_apply__to_workdir(
+ git_repository *repo,
+ git_diff *diff,
+ git_index *preimage,
+ git_index *postimage,
+ git_apply_location_t location,
+ git_apply_options *opts)
+{
+ git_vector paths = GIT_VECTOR_INIT;
+ git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
+ const git_diff_delta *delta;
+ size_t i;
+ int error;
+
+ GIT_UNUSED(opts);
+
+ /*
+ * Limit checkout to the paths affected by the diff; this ensures
+ * that other modifications in the working directory are unaffected.
+ */
+ if ((error = git_vector_init(&paths, git_diff_num_deltas(diff), NULL)) < 0)
+ goto done;
+
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ delta = git_diff_get_delta(diff, i);
+
+ if ((error = git_vector_insert(&paths, (void *)delta->old_file.path)) < 0)
+ goto done;
+
+ if (strcmp(delta->old_file.path, delta->new_file.path) &&
+ (error = git_vector_insert(&paths, (void *)delta->new_file.path)) < 0)
+ goto done;
+ }
+
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+ if (location == GIT_APPLY_LOCATION_WORKDIR)
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
+
+ checkout_opts.paths.strings = (char **)paths.contents;
+ checkout_opts.paths.count = paths.length;
+
+ checkout_opts.baseline_index = preimage;
+
+ error = git_checkout_index(repo, postimage, &checkout_opts);
+
+done:
+ git_vector_free(&paths);
+ return error;
+}
+
+static int git_apply__to_index(
+ git_repository *repo,
+ git_diff *diff,
+ git_index *preimage,
+ git_index *postimage,
+ git_apply_options *opts)
+{
+ git_index *index = NULL;
+ const git_diff_delta *delta;
+ const git_index_entry *entry;
+ size_t i;
+ int error;
+
+ GIT_UNUSED(preimage);
+ GIT_UNUSED(opts);
+
+ if ((error = git_repository_index(&index, repo)) < 0)
+ goto done;
+
+ /* Remove deleted (or renamed) paths from the index. */
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ delta = git_diff_get_delta(diff, i);
+
+ if (delta->status == GIT_DELTA_DELETED ||
+ delta->status == GIT_DELTA_RENAMED) {
+ if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0)
+ goto done;
+ }
+ }
+
+ /* Then add the changes back to the index. */
+ for (i = 0; i < git_index_entrycount(postimage); i++) {
+ entry = git_index_get_byindex(postimage, i);
+
+ if ((error = git_index_add(index, entry)) < 0)
+ goto done;
+ }
+
+done:
+ git_index_free(index);
+ return error;
+}
+
+/*
+ * Handle the three application options ("locations"):
+ *
+ * GIT_APPLY_LOCATION_WORKDIR: the default, emulates `git apply`.
+ * Applies the diff only to the workdir items and ignores the index
+ * entirely.
+ *
+ * GIT_APPLY_LOCATION_INDEX: emulates `git apply --cached`.
+ * Applies the diff only to the index items and ignores the workdir
+ * completely.
+ *
+ * GIT_APPLY_LOCATION_BOTH: emulates `git apply --index`.
+ * Applies the diff to both the index items and the working directory
+ * items.
+ */
+
+int git_apply(
+ git_repository *repo,
+ git_diff *diff,
+ git_apply_location_t location,
+ const git_apply_options *given_opts)
+{
+ git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
+ git_index *index = NULL, *preimage = NULL, *postimage = NULL;
+ git_reader *pre_reader = NULL, *post_reader = NULL;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ int error = GIT_EINVALID;
+
+ assert(repo && diff);
+
+ GITERR_CHECK_VERSION(
+ given_opts, GIT_APPLY_OPTIONS_VERSION, "git_apply_options");
+
+ if (given_opts)
+ memcpy(&opts, given_opts, sizeof(git_apply_options));
+
+ /*
+ * by default, we apply a patch directly to the working directory;
+ * in `--cached` or `--index` mode, we apply to the contents already
+ * in the index.
+ */
+ switch (location) {
+ case GIT_APPLY_LOCATION_BOTH:
+ error = git_reader_for_workdir(&pre_reader, repo, true);
+ break;
+ case GIT_APPLY_LOCATION_INDEX:
+ error = git_reader_for_index(&pre_reader, repo, NULL);
+ break;
+ case GIT_APPLY_LOCATION_WORKDIR:
+ error = git_reader_for_workdir(&pre_reader, repo, false);
+ break;
+ default:
+ assert(false);
+ }
+
+ if (error < 0)
+ goto done;
+
+ /*
+ * Build the preimage and postimage (differences). Note that
+ * this is not the complete preimage or postimage, it only
+ * contains the files affected by the patch. We want to avoid
+ * having the full repo index, so we will limit our checkout
+ * to only write these files that were affected by the diff.
+ */
+ if ((error = git_index_new(&preimage)) < 0 ||
+ (error = git_index_new(&postimage)) < 0 ||
+ (error = git_reader_for_index(&post_reader, repo, postimage)) < 0)
+ goto done;
+
+ if ((error = git_repository_index(&index, repo)) < 0 ||
+ (error = git_indexwriter_init(&indexwriter, index)) < 0)
+ goto done;
+
+ if ((error = apply_deltas(repo, pre_reader, preimage, post_reader, postimage, diff, &opts)) < 0)
+ goto done;
+
+ switch (location) {
+ case GIT_APPLY_LOCATION_BOTH:
+ error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts);
+ break;
+ case GIT_APPLY_LOCATION_INDEX:
+ error = git_apply__to_index(repo, diff, preimage, postimage, &opts);
+ break;
+ case GIT_APPLY_LOCATION_WORKDIR:
+ error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts);
+ break;
+ default:
+ assert(false);
+ }
+
+ if (error < 0)
+ goto done;
+
+ error = git_indexwriter_commit(&indexwriter);
+
+done:
+ git_indexwriter_cleanup(&indexwriter);
+ git_index_free(postimage);
+ git_index_free(preimage);
+ git_index_free(index);
+ git_reader_free(pre_reader);
+ git_reader_free(post_reader);
+
+ return error;
+}
diff --git a/src/apply.h b/src/apply.h
index b29460c..11ec756 100644
--- a/src/apply.h
+++ b/src/apply.h
@@ -10,6 +10,7 @@
#include "common.h"
#include "git2/patch.h"
+#include "git2/apply.h"
#include "buffer.h"
extern int git_apply__patch(
@@ -18,6 +19,7 @@ extern int git_apply__patch(
unsigned int *mode,
const char *source,
size_t source_len,
- git_patch *patch);
+ git_patch *patch,
+ const git_apply_options *opts);
#endif
diff --git a/src/iterator.c b/src/iterator.c
index 6e7300a..40f6759 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1015,6 +1015,7 @@ typedef struct {
struct stat st;
size_t path_len;
iterator_pathlist_search_t match;
+ git_oid id;
char path[GIT_FLEX_ARRAY];
} filesystem_iterator_entry;
@@ -1265,7 +1266,32 @@ GIT_INLINE(bool) filesystem_iterator_is_dot_git(
return (len == 4 || path[len - 5] == '/');
}
-static filesystem_iterator_entry *filesystem_iterator_entry_init(
+static int filesystem_iterator_entry_hash(
+ filesystem_iterator *iter,
+ filesystem_iterator_entry *entry)
+{
+ git_buf fullpath = GIT_BUF_INIT;
+ int error;
+
+ if (S_ISDIR(entry->st.st_mode)) {
+ memset(&entry->id, 0, GIT_OID_RAWSZ);
+ return 0;
+ }
+
+ if (iter->base.type == GIT_ITERATOR_TYPE_WORKDIR)
+ return git_repository_hashfile(&entry->id,
+ iter->base.repo, entry->path, GIT_OBJ_BLOB, NULL);
+
+ if (!(error = git_buf_joinpath(&fullpath, iter->root, entry->path)))
+ error = git_odb_hashfile(&entry->id, fullpath.ptr, GIT_OBJ_BLOB);
+
+ git_buf_dispose(&fullpath);
+ return error;
+}
+
+static int filesystem_iterator_entry_init(
+ filesystem_iterator_entry **out,
+ filesystem_iterator *iter,
filesystem_iterator_frame *frame,
const char *path,
size_t path_len,
@@ -1274,15 +1300,19 @@ static filesystem_iterator_entry *filesystem_iterator_entry_init(
{
filesystem_iterator_entry *entry;
size_t entry_size;
+ int error = 0;
+
+ *out = NULL;
/* Make sure to append two bytes, one for the path's null
* termination, one for a possible trailing '/' for folders.
*/
- if (GIT_ADD_SIZET_OVERFLOW(&entry_size,
- sizeof(filesystem_iterator_entry), path_len) ||
- GIT_ADD_SIZET_OVERFLOW(&entry_size, entry_size, 2) ||
- (entry = git_pool_malloc(&frame->entry_pool, entry_size)) == NULL)
- return NULL;
+ GITERR_CHECK_ALLOC_ADD(&entry_size,
+ sizeof(filesystem_iterator_entry), path_len);
+ GITERR_CHECK_ALLOC_ADD(&entry_size, entry_size, 2);
+
+ entry = git_pool_malloc(&frame->entry_pool, entry_size);
+ GITERR_CHECK_ALLOC(entry);
entry->path_len = path_len;
entry->match = pathlist_match;
@@ -1295,7 +1325,13 @@ static filesystem_iterator_entry *filesystem_iterator_entry_init(
entry->path[entry->path_len] = '\0';
- return entry;
+ if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH)
+ error = filesystem_iterator_entry_hash(iter, entry);
+
+ if (!error)
+ *out = entry;
+
+ return error;
}
static int filesystem_iterator_frame_push(
@@ -1418,9 +1454,9 @@ static int filesystem_iterator_frame_push(
else if (dir_expected)
continue;
- entry = filesystem_iterator_entry_init(new_frame,
- path, path_len, &statbuf, pathlist_match);
- GITERR_CHECK_ALLOC(entry);
+ if ((error = filesystem_iterator_entry_init(&entry,
+ iter, new_frame, path, path_len, &statbuf, pathlist_match)) < 0)
+ goto done;
git_vector_insert(&new_frame->entries, entry);
}
@@ -1460,7 +1496,7 @@ static void filesystem_iterator_set_current(
iter->entry.ctime.seconds = entry->st.st_ctime;
iter->entry.mtime.seconds = entry->st.st_mtime;
-#if defined(GIT_USE_NSEC)
+#if defined(GIT_USE_NSEC)
iter->entry.ctime.nanoseconds = entry->st.st_ctime_nsec;
iter->entry.mtime.nanoseconds = entry->st.st_mtime_nsec;
#else
@@ -1475,6 +1511,9 @@ static void filesystem_iterator_set_current(
iter->entry.gid = entry->st.st_gid;
iter->entry.file_size = entry->st.st_size;
+ if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH)
+ git_oid_cpy(&iter->entry.id, &entry->id);
+
iter->entry.path = entry->path;
iter->current_is_ignored = GIT_IGNORE_UNCHECKED;
@@ -2259,6 +2298,35 @@ void git_iterator_free(git_iterator *iter)
git__free(iter);
}
+int git_iterator_foreach(
+ git_iterator *iterator,
+ git_iterator_foreach_cb cb,
+ void *data)
+{
+ const git_index_entry *iterator_item;
+ int error = 0;
+
+ if ((error = git_iterator_current(&iterator_item, iterator)) < 0)
+ goto done;
+
+ if ((error = cb(iterator_item, data)) != 0)
+ goto done;
+
+ while (true) {
+ if ((error = git_iterator_advance(&iterator_item, iterator)) < 0)
+ goto done;
+
+ if ((error = cb(iterator_item, data)) != 0)
+ goto done;
+ }
+
+done:
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+ return error;
+}
+
int git_iterator_walk(
git_iterator **iterators,
size_t cnt,
diff --git a/src/iterator.h b/src/iterator.h
index a6497d8..bbe357f 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -41,6 +41,8 @@ typedef enum {
GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
/** descend into symlinked directories */
GIT_ITERATOR_DESCEND_SYMLINKS = (1u << 7),
+ /** hash files in workdir or filesystem iterators */
+ GIT_ITERATOR_INCLUDE_HASH = (1u << 8),
} git_iterator_flag_t;
typedef enum {
@@ -289,6 +291,19 @@ extern int git_iterator_current_workdir_path(
*/
extern git_index *git_iterator_index(git_iterator *iter);
+typedef int (*git_iterator_foreach_cb)(
+ const git_index_entry *entry,
+ void *data);
+
+/**
+ * Walk the given iterator and invoke the callback for each path
+ * contained in the iterator.
+ */
+extern int git_iterator_foreach(
+ git_iterator *iterator,
+ git_iterator_foreach_cb cb,
+ void *data);
+
typedef int (*git_iterator_walk_cb)(
const git_index_entry **entries,
void *data);
diff --git a/src/reader.c b/src/reader.c
new file mode 100644
index 0000000..9375ff3
--- /dev/null
+++ b/src/reader.c
@@ -0,0 +1,259 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "reader.h"
+
+#include "fileops.h"
+#include "blob.h"
+
+#include "git2/tree.h"
+#include "git2/blob.h"
+#include "git2/index.h"
+#include "git2/repository.h"
+
+/* tree reader */
+
+typedef struct {
+ git_reader reader;
+ git_tree *tree;
+} tree_reader;
+
+static int tree_reader_read(
+ git_buf *out,
+ git_oid *out_id,
+ git_filemode_t *out_filemode,
+ git_reader *_reader,
+ const char *filename)
+{
+ tree_reader *reader = (tree_reader *)_reader;
+ git_tree_entry *tree_entry = NULL;
+ git_blob *blob = NULL;
+ int error;
+
+ if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 ||
+ (error = git_blob_lookup(&blob, git_tree_owner(reader->tree), git_tree_entry_id(tree_entry))) < 0 ||
+ (error = git_buf_set(out, git_blob_rawcontent(blob), git_blob_rawsize(blob))) < 0)
+ goto done;
+
+ if (out_id)
+ git_oid_cpy(out_id, git_tree_entry_id(tree_entry));
+
+ if (out_filemode)
+ *out_filemode = git_tree_entry_filemode(tree_entry);
+
+done:
+ git_blob_free(blob);
+ git_tree_entry_free(tree_entry);
+ return error;
+}
+
+int git_reader_for_tree(git_reader **out, git_tree *tree)
+{
+ tree_reader *reader;
+
+ assert(out && tree);
+
+ reader = git__calloc(1, sizeof(tree_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = tree_reader_read;
+ reader->tree = tree;
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* workdir reader */
+
+typedef struct {
+ git_reader reader;
+ git_repository *repo;
+ git_index *index;
+} workdir_reader;
+
+static int workdir_reader_read(
+ git_buf *out,
+ git_oid *out_id,
+ git_filemode_t *out_filemode,
+ git_reader *_reader,
+ const char *filename)
+{
+ workdir_reader *reader = (workdir_reader *)_reader;
+ git_buf path = GIT_BUF_INIT;
+ struct stat st;
+ git_filemode_t filemode;
+ git_filter_list *filters = NULL;
+ const git_index_entry *idx_entry;
+ git_oid id;
+ int error;
+
+ if ((error = git_buf_joinpath(&path,
+ git_repository_workdir(reader->repo), filename)) < 0)
+ goto done;
+
+ if ((error = p_lstat(path.ptr, &st)) < 0) {
+ if (error == -1 && errno == ENOENT)
+ error = GIT_ENOTFOUND;
+
+ giterr_set(GITERR_OS, "could not stat '%s'", path.ptr);
+ goto done;
+ }
+
+ filemode = git_futils_canonical_mode(st.st_mode);
+
+ /*
+ * Patch application - for example - uses the filtered version of
+ * the working directory data to match git. So we will run the
+ * workdir -> ODB filter on the contents in this workdir reader.
+ */
+ if ((error = git_filter_list_load(&filters, reader->repo, NULL, filename,
+ GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT)) < 0)
+ goto done;
+
+ if ((error = git_filter_list_apply_to_file(out,
+ filters, reader->repo, path.ptr)) < 0)
+ goto done;
+
+ if (out_id || reader->index) {
+ if ((error = git_odb_hash(&id, out->ptr, out->size, GIT_OBJ_BLOB)) < 0)
+ goto done;
+ }
+
+ if (reader->index) {
+ if (!(idx_entry = git_index_get_bypath(reader->index, filename, 0)) ||
+ filemode != idx_entry->mode ||
+ !git_oid_equal(&id, &idx_entry->id)) {
+ error = GIT_READER_MISMATCH;
+ goto done;
+ }
+ }
+
+ if (out_id)
+ git_oid_cpy(out_id, &id);
+
+ if (out_filemode)
+ *out_filemode = filemode;
+
+done:
+ git_filter_list_free(filters);
+ git_buf_dispose(&path);
+ return error;
+}
+
+int git_reader_for_workdir(
+ git_reader **out,
+ git_repository *repo,
+ bool validate_index)
+{
+ workdir_reader *reader;
+ int error;
+
+ assert(out && repo);
+
+ reader = git__calloc(1, sizeof(workdir_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = workdir_reader_read;
+ reader->repo = repo;
+
+ if (validate_index &&
+ (error = git_repository_index__weakptr(&reader->index, repo)) < 0) {
+ git__free(reader);
+ return error;
+ }
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* index reader */
+
+typedef struct {
+ git_reader reader;
+ git_repository *repo;
+ git_index *index;
+} index_reader;
+
+static int index_reader_read(
+ git_buf *out,
+ git_oid *out_id,
+ git_filemode_t *out_filemode,
+ git_reader *_reader,
+ const char *filename)
+{
+ index_reader *reader = (index_reader *)_reader;
+ const git_index_entry *entry;
+ git_blob *blob;
+ int error;
+
+ if ((entry = git_index_get_bypath(reader->index, filename, 0)) == NULL)
+ return GIT_ENOTFOUND;
+
+ if ((error = git_blob_lookup(&blob, reader->repo, &entry->id)) < 0)
+ goto done;
+
+ if (out_id)
+ git_oid_cpy(out_id, &entry->id);
+
+ if (out_filemode)
+ *out_filemode = entry->mode;
+
+ error = git_blob__getbuf(out, blob);
+
+done:
+ git_blob_free(blob);
+ return error;
+}
+
+int git_reader_for_index(
+ git_reader **out,
+ git_repository *repo,
+ git_index *index)
+{
+ index_reader *reader;
+ int error;
+
+ assert(out && repo);
+
+ reader = git__calloc(1, sizeof(index_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = index_reader_read;
+ reader->repo = repo;
+
+ if (index) {
+ reader->index = index;
+ } else if ((error = git_repository_index__weakptr(&reader->index, repo)) < 0) {
+ git__free(reader);
+ return error;
+ }
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* generic */
+
+int git_reader_read(
+ git_buf *out,
+ git_oid *out_id,
+ git_filemode_t *out_filemode,
+ git_reader *reader,
+ const char *filename)
+{
+ assert(out && reader && filename);
+
+ return reader->read(out, out_id, out_filemode, reader, filename);
+}
+
+void git_reader_free(git_reader *reader)
+{
+ if (!reader)
+ return;
+
+ git__free(reader);
+}
diff --git a/src/reader.h b/src/reader.h
new file mode 100644
index 0000000..18a6a11
--- /dev/null
+++ b/src/reader.h
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_reader_h__
+#define INCLUDE_reader_h__
+
+#include "common.h"
+
+/* Returned when the workdir does not match the index */
+#define GIT_READER_MISMATCH 1
+
+typedef struct git_reader git_reader;
+
+/*
+ * The `git_reader` structure is a generic interface for reading the
+ * contents of a file by its name, and implementations are provided
+ * for reading out of a tree, the index, and the working directory.
+ *
+ * Note that the reader implementation is meant to have a short
+ * lifecycle and does not increase the refcount of the object that
+ * it's reading. Callers should ensure that they do not use a
+ * reader after disposing the underlying object that it reads.
+ */
+struct git_reader {
+ int (*read)(git_buf *out, git_oid *out_oid, git_filemode_t *mode, git_reader *reader, const char *filename);
+};
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * tree. Paths requested via `git_reader_read` will be rooted at this
+ * tree, callers are not expected to recurse through tree lookups. Thus,
+ * you can request to read `/src/foo.c` and the tree provided to this
+ * function will be searched to find another tree named `src`, which
+ * will then be opened to find `foo.c`.
+ *
+ * @param out The reader for the given tree
+ * @param tree The tree object to read
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_tree(
+ git_reader **out,
+ git_tree *tree);
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * index, or the repository's index.
+ *
+ * @param out The reader for the given index
+ * @param repo The repository containing the index
+ * @param index The index to read, or NULL to use the repository's index
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_index(
+ git_reader **out,
+ git_repository *repo,
+ git_index *index);
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * repository's working directory. Note that the contents are read
+ * in repository format, meaning any workdir -> odb filters are
+ * applied.
+ *
+ * If `validate_index` is set to true, reads of files will hash the
+ * on-disk contents and ensure that the resulting object ID matches
+ * the repository's index. This ensures that the working directory
+ * is unmodified from the index contents.
+ *
+ * @param out The reader for the given working directory
+ * @param repo The repository containing the working directory
+ * @param validate_index If true, the working directory contents will
+ * be compared to the index contents during read to ensure that
+ * the working directory is unmodified.
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_workdir(
+ git_reader **out,
+ git_repository *repo,
+ bool validate_index);
+
+/**
+ * Read the given filename from the reader and populate the given buffer
+ * with the contents and the given oid with the object ID.
+ *
+ * @param out The buffer to populate with the file contents
+ * @param out_id The oid to populate with the object ID
+ * @param reader The reader to read
+ * @param filename The filename to read from the reader
+ */
+extern int git_reader_read(
+ git_buf *out,
+ git_oid *out_id,
+ git_filemode_t *out_filemode,
+ git_reader *reader,
+ const char *filename);
+
+/**
+ * Free the given reader and any associated objects.
+ *
+ * @param reader The reader to free
+ */
+extern void git_reader_free(git_reader *reader);
+
+#endif
diff --git a/tests/apply/apply_helpers.h b/tests/apply/apply_helpers.h
new file mode 100644
index 0000000..8156335
--- /dev/null
+++ b/tests/apply/apply_helpers.h
@@ -0,0 +1,587 @@
+#include "../merge/merge_helpers.h"
+
+#define TEST_REPO_PATH "merge-recursive"
+
+#define DIFF_MODIFY_TWO_FILES \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "index f516580..ffb36e5 100644\n" \
+ "--- a/asparagus.txt\n" \
+ "+++ b/asparagus.txt\n" \
+ "@@ -1 +1 @@\n" \
+ "-ASPARAGUS SOUP!\n" \
+ "+ASPARAGUS SOUP.\n" \
+ "diff --git a/veal.txt b/veal.txt\n" \
+ "index 94d2c01..a7b0665 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/veal.txt\n" \
+ "@@ -1 +1 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP.\n" \
+ "@@ -7 +7 @@ occasionally, then put into it a shin of veal, let it boil two hours\n" \
+ "-longer. take out the slices of ham, and skim off the grease if any\n" \
+ "+longer; take out the slices of ham, and skim off the grease if any\n"
+
+#define DIFF_DELETE_FILE \
+ "diff --git a/gravy.txt b/gravy.txt\n" \
+ "deleted file mode 100644\n" \
+ "index c4e6cca..0000000\n" \
+ "--- a/gravy.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1,8 +0,0 @@\n" \
+ "-GRAVY SOUP.\n" \
+ "-\n" \
+ "-Get eight pounds of coarse lean beef--wash it clean and lay it in your\n" \
+ "-pot, put in the same ingredients as for the shin soup, with the same\n" \
+ "-quantity of water, and follow the process directed for that. Strain the\n" \
+ "-soup through a sieve, and serve it up clear, with nothing more than\n" \
+ "-toasted bread in it; two table-spoonsful of mushroom catsup will add a\n" \
+ "-fine flavour to the soup.\n"
+
+#define DIFF_ADD_FILE \
+ "diff --git a/newfile.txt b/newfile.txt\n" \
+ "new file mode 100644\n" \
+ "index 0000000..6370543\n" \
+ "--- /dev/null\n" \
+ "+++ b/newfile.txt\n" \
+ "@@ -0,0 +1,2 @@\n" \
+ "+This is a new file!\n" \
+ "+Added by a patch.\n"
+
+#define DIFF_EXECUTABLE_FILE \
+ "diff --git a/beef.txt b/beef.txt\n" \
+ "old mode 100644\n" \
+ "new mode 100755\n"
+
+#define DIFF_MANY_CHANGES_ONE \
+ "diff --git a/veal.txt b/veal.txt\n" \
+ "index 94d2c01..c9d7d5d 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/veal.txt\n" \
+ "@@ -1,2 +1,2 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP\n" \
+ " \n" \
+ "@@ -4,3 +4,2 @@\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n" \
+ "-slices of lean ham; let it boil steadily two hours; skim it\n" \
+ " occasionally, then put into it a shin of veal, let it boil two hours\n" \
+ "@@ -8,3 +7,3 @@\n" \
+ " should rise, take a gill of good cream, mix with it two table-spoonsful\n" \
+ "-of flour very nicely, and the yelks of two eggs beaten well, strain this\n" \
+ "+OF FLOUR very nicely, and the yelks of two eggs beaten well, strain this\n" \
+ " mixture, and add some chopped parsley; pour some soup on by degrees,\n" \
+ "@@ -12,2 +11,3 @@\n" \
+ " boiled two or three minutes to take off the raw taste of the eggs. If\n" \
+ "+Inserted line.\n" \
+ " the cream be not perfectly sweet, and the eggs quite new, the thickening\n" \
+ "@@ -15,3 +15,3 @@\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ "-hot water, when they may be easily peeled. When made in this way you\n" \
+ "+Changed line.\n" \
+ " must thicken it with the flour only. Any part of the veal may be used,\n"
+
+#define DIFF_MANY_CHANGES_TWO \
+ "diff --git a/veal.txt b/veal.txt\n" \
+ "index 94d2c01..6b943d6 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/veal.txt\n" \
+ "@@ -1,2 +1,2 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP!!!\n" \
+ " \n" \
+ "@@ -4,3 +4,2 @@\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n" \
+ "-slices of lean ham; let it boil steadily two hours; skim it\n" \
+ " occasionally, then put into it a shin of veal, let it boil two hours\n" \
+ "@@ -8,3 +7,3 @@\n" \
+ " should rise, take a gill of good cream, mix with it two table-spoonsful\n" \
+ "-of flour very nicely, and the yelks of two eggs beaten well, strain this\n" \
+ "+of flour very nicely, AND the yelks of two eggs beaten well, strain this\n" \
+ " mixture, and add some chopped parsley; pour some soup on by degrees,\n" \
+ "@@ -12,2 +11,3 @@\n" \
+ " boiled two or three minutes to take off the raw taste of the eggs. If\n" \
+ "+New line.\n" \
+ " the cream be not perfectly sweet, and the eggs quite new, the thickening\n" \
+ "@@ -15,4 +15,5 @@\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ "-hot water, when they may be easily peeled. When made in this way you\n" \
+ "-must thicken it with the flour only. Any part of the veal may be used,\n" \
+ "-but the shin or knuckle is the nicest.\n" \
+ "+HOT water, when they may be easily peeled. When made in this way you\n" \
+ "+must THICKEN it with the flour only. Any part of the veal may be used,\n" \
+ "+but the shin OR knuckle is the nicest.\n" \
+ "+Another new line.\n" \
+
+#define DIFF_RENAME_FILE \
+ "diff --git a/beef.txt b/notbeef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from beef.txt\n" \
+ "rename to notbeef.txt\n"
+
+#define DIFF_RENAME_AND_MODIFY_FILE \
+ "diff --git a/beef.txt b/notbeef.txt\n" \
+ "similarity index 97%\n" \
+ "rename from beef.txt\n" \
+ "rename to notbeef.txt\n" \
+ "index 68f6182..6fa1014 100644\n" \
+ "--- a/beef.txt\n" \
+ "+++ b/notbeef.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-BEEF SOUP.\n" \
+ "+THIS IS NOT BEEF SOUP, IT HAS A NEW NAME.\n" \
+ "\n" \
+ " Take the hind shin of beef, cut off all the flesh off the leg-bone,\n" \
+ " which must be taken away entirely, or the soup will be greasy. Wash the\n"
+
+#define DIFF_RENAME_A_TO_B_TO_C \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "deleted file mode 100644\n" \
+ "index f516580..0000000\n" \
+ "--- a/asparagus.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1,10 +0,0 @@\n" \
+ "-ASPARAGUS SOUP!\n" \
+ "-\n" \
+ "-Take four large bunches of asparagus, scrape it nicely, cut off one inch\n" \
+ "-of the tops, and lay them in water, chop the stalks and put them on the\n" \
+ "-fire with a piece of bacon, a large onion cut up, and pepper and salt;\n" \
+ "-add two quarts of water, boil them till the stalks are quite soft, then\n" \
+ "-pulp them through a sieve, and strain the water to it, which must be put\n" \
+ "-back in the pot; put into it a chicken cut up, with the tops of\n" \
+ "-asparagus which had been laid by, boil it until these last articles are\n" \
+ "-sufficiently done, thicken with flour, butter and milk, and serve it up.\n" \
+ "diff --git a/beef.txt b/beef.txt\n" \
+ "index 68f6182..f516580 100644\n" \
+ "--- a/beef.txt\n" \
+ "+++ b/beef.txt\n" \
+ "@@ -1,22 +1,10 @@\n" \
+ "-BEEF SOUP.\n" \
+ "+ASPARAGUS SOUP!\n" \
+ "\n" \
+ "-Take the hind shin of beef, cut off all the flesh off the leg-bone,\n" \
+ "-which must be taken away entirely, or the soup will be greasy. Wash the\n" \
+ "-meat clean and lay it in a pot, sprinkle over it one small\n" \
+ "-table-spoonful of pounded black pepper, and two of salt; three onions\n" \
+ "-the size of a hen's egg, cut small, six small carrots scraped and cut\n" \
+ "-up, two small turnips pared and cut into dice; pour on three quarts of\n" \
+ "-water, cover the pot close, and keep it gently and steadily boiling five\n" \
+ "-hours, which will leave about three pints of clear soup; do not let the\n" \
+ "-pot boil over, but take off the scum carefully, as it rises. When it has\n" \
+ "-boiled four hours, put in a small bundle of thyme and parsley, and a\n" \
+ "-pint of celery cut small, or a tea-spoonful of celery seed pounded.\n" \
+ "-These latter ingredients would lose their delicate flavour if boiled too\n" \
+ "-much. Just before you take it up, brown it in the following manner: put\n" \
+ "-a small table-spoonful of nice brown sugar into an iron skillet, set it\n" \
+ "-on the fire and stir it till it melts and looks very dark, pour into it\n" \
+ "-a ladle full of the soup, a little at a time; stirring it all the while.\n" \
+ "-Strain this browning and mix it well with the soup; take out the bundle\n" \
+ "-of thyme and parsley, put the nicest pieces of meat in your tureen, and\n" \
+ "-pour on the soup and vegetables; put in some toasted bread cut in dice,\n" \
+ "-and serve it up.\n" \
+ "+Take four large bunches of asparagus, scrape it nicely, cut off one inch\n" \
+ "+of the tops, and lay them in water, chop the stalks and put them on the\n" \
+ "+fire with a piece of bacon, a large onion cut up, and pepper and salt;\n" \
+ "+add two quarts of water, boil them till the stalks are quite soft, then\n" \
+ "+pulp them through a sieve, and strain the water to it, which must be put\n" \
+ "+back in the pot; put into it a chicken cut up, with the tops of\n" \
+ "+asparagus which had been laid by, boil it until these last articles are\n" \
+ "+sufficiently done, thicken with flour, butter and milk, and serve it up.\n" \
+ "diff --git a/notbeef.txt b/notbeef.txt\n" \
+ "new file mode 100644\n" \
+ "index 0000000..68f6182\n" \
+ "--- /dev/null\n" \
+ "+++ b/notbeef.txt\n" \
+ "@@ -0,0 +1,22 @@\n" \
+ "+BEEF SOUP.\n" \
+ "+\n" \
+ "+Take the hind shin of beef, cut off all the flesh off the leg-bone,\n" \
+ "+which must be taken away entirely, or the soup will be greasy. Wash the\n" \
+ "+meat clean and lay it in a pot, sprinkle over it one small\n" \
+ "+table-spoonful of pounded black pepper, and two of salt; three onions\n" \
+ "+the size of a hen's egg, cut small, six small carrots scraped and cut\n" \
+ "+up, two small turnips pared and cut into dice; pour on three quarts of\n" \
+ "+water, cover the pot close, and keep it gently and steadily boiling five\n" \
+ "+hours, which will leave about three pints of clear soup; do not let the\n" \
+ "+pot boil over, but take off the scum carefully, as it rises. When it has\n" \
+ "+boiled four hours, put in a small bundle of thyme and parsley, and a\n" \
+ "+pint of celery cut small, or a tea-spoonful of celery seed pounded.\n" \
+ "+These latter ingredients would lose their delicate flavour if boiled too\n" \
+ "+much. Just before you take it up, brown it in the following manner: put\n" \
+ "+a small table-spoonful of nice brown sugar into an iron skillet, set it\n" \
+ "+on the fire and stir it till it melts and looks very dark, pour into it\n" \
+ "+a ladle full of the soup, a little at a time; stirring it all the while.\n" \
+ "+Strain this browning and mix it well with the soup; take out the bundle\n" \
+ "+of thyme and parsley, put the nicest pieces of meat in your tureen, and\n" \
+ "+pour on the soup and vegetables; put in some toasted bread cut in dice,\n" \
+ "+and serve it up.\n"
+
+#define DIFF_RENAME_A_TO_B_TO_C_EXACT \
+ "diff --git a/asparagus.txt b/beef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from asparagus.txt\n" \
+ "rename to beef.txt\n" \
+ "diff --git a/beef.txt b/notbeef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from beef.txt\n" \
+ "rename to notbeef.txt\n"
+
+#define DIFF_RENAME_CIRCULAR \
+ "diff --git a/asparagus.txt b/beef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from asparagus.txt\n" \
+ "rename to beef.txt\n" \
+ "diff --git a/beef.txt b/notbeef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from beef.txt\n" \
+ "rename to asparagus.txt\n"
+
+#define DIFF_RENAME_2_TO_1 \
+ "diff --git a/asparagus.txt b/2.txt\n" \
+ "similarity index 100%\n" \
+ "rename from asparagus.txt\n" \
+ "rename to 2.txt\n" \
+ "diff --git a/beef.txt b/2.txt\n" \
+ "similarity index 100%\n" \
+ "rename from beef.txt\n" \
+ "rename to 2.txt\n"
+
+#define DIFF_RENAME_1_TO_2 \
+ "diff --git a/asparagus.txt b/2.txt\n" \
+ "similarity index 100%\n" \
+ "rename from asparagus.txt\n" \
+ "rename to 1.txt\n" \
+ "diff --git a/asparagus.txt b/2.txt\n" \
+ "similarity index 100%\n" \
+ "rename from asparagus.txt\n" \
+ "rename to 2.txt\n"
+
+#define DIFF_TWO_DELTAS_ONE_FILE \
+ "diff --git a/beef.txt b/beef.txt\n" \
+ "index 68f6182..235069d 100644\n" \
+ "--- a/beef.txt\n" \
+ "+++ b/beef.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-BEEF SOUP.\n" \
+ "+BEEF SOUP!\n" \
+ "\n" \
+ " Take the hind shin of beef, cut off all the flesh off the leg-bone,\n" \
+ " which must be taken away entirely, or the soup will be greasy. Wash the\n" \
+ "diff --git a/beef.txt b/beef.txt\n" \
+ "index 68f6182..e059eb5 100644\n" \
+ "--- a/beef.txt\n" \
+ "+++ b/beef.txt\n" \
+ "@@ -19,4 +19,4 @@ a ladle full of the soup, a little at a time; stirring it all the while.\n" \
+ " Strain this browning and mix it well with the soup; take out the bundle\n" \
+ " of thyme and parsley, put the nicest pieces of meat in your tureen, and\n" \
+ " pour on the soup and vegetables; put in some toasted bread cut in dice,\n" \
+ "-and serve it up.\n" \
+ "+and serve it up!\n"
+
+#define DIFF_TWO_DELTAS_ONE_NEW_FILE \
+ "diff --git a/newfile.txt b/newfile.txt\n" \
+ "new file mode 100644\n" \
+ "index 0000000..6434b13\n" \
+ "--- /dev/null\n" \
+ "+++ b/newfile.txt\n" \
+ "@@ -0,0 +1 @@\n" \
+ "+This is a new file.\n" \
+ "diff --git a/newfile.txt b/newfile.txt\n" \
+ "index 6434b13..08d4c44 100644\n" \
+ "--- a/newfile.txt\n" \
+ "+++ b/newfile.txt\n" \
+ "@@ -1 +1,3 @@\n" \
+ " This is a new file.\n" \
+ "+\n" \
+ "+This is another change to a new file.\n"
+
+#define DIFF_RENAME_AND_MODIFY_DELTAS \
+ "diff --git a/veal.txt b/asdf.txt\n" \
+ "similarity index 96%\n" \
+ "rename from veal.txt\n" \
+ "rename to asdf.txt\n" \
+ "index 94d2c01..292cb60 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/asdf.txt\n" \
+ "@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ " hot water, when they may be easily peeled. When made in this way you\n" \
+ " must thicken it with the flour only. Any part of the veal may be used,\n" \
+ "-but the shin or knuckle is the nicest.\n" \
+ "+but the shin or knuckle is the nicest!\n" \
+ "diff --git a/asdf.txt b/asdf.txt\n" \
+ "index 292cb60..61c686b 100644\n" \
+ "--- a/asdf.txt\n" \
+ "+++ b/asdf.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP\n" \
+ "\n" \
+ " Put into a pot three quarts of water, three onions cut small, one\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n"
+
+#define DIFF_RENAME_AFTER_MODIFY \
+ "diff --git a/veal.txt b/veal.txt\n" \
+ "index 292cb60..61c686b 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/veal.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP\n" \
+ "\n" \
+ " Put into a pot three quarts of water, three onions cut small, one\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n" \
+ "diff --git a/veal.txt b/other.txt\n" \
+ "similarity index 96%\n" \
+ "rename from veal.txt\n" \
+ "rename to other.txt\n" \
+ "index 94d2c01..292cb60 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/other.txt\n" \
+ "@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ " hot water, when they may be easily peeled. When made in this way you\n" \
+ " must thicken it with the flour only. Any part of the veal may be used,\n" \
+ "-but the shin or knuckle is the nicest.\n" \
+ "+but the shin or knuckle is the nicest!\n"
+
+#define DIFF_RENAME_AFTER_MODIFY_TARGET_PATH \
+ "diff --git a/beef.txt b/beef.txt\n" \
+ "index 292cb60..61c686b 100644\n" \
+ "--- a/beef.txt\n" \
+ "+++ b/beef.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP\n" \
+ "\n" \
+ " Put into a pot three quarts of water, three onions cut small, one\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n" \
+ "diff --git a/veal.txt b/beef.txt\n" \
+ "similarity index 96%\n" \
+ "rename from veal.txt\n" \
+ "rename to beef.txt\n" \
+ "index 94d2c01..292cb60 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/beef.txt\n" \
+ "@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ " hot water, when they may be easily peeled. When made in this way you\n" \
+ " must thicken it with the flour only. Any part of the veal may be used,\n" \
+ "-but the shin or knuckle is the nicest.\n" \
+ "+but the shin or knuckle is the nicest!\n"
+
+#define DIFF_RENAME_AND_MODIFY_SOURCE_PATH \
+ "diff --git a/veal.txt b/asdf.txt\n" \
+ "similarity index 96%\n" \
+ "rename from veal.txt\n" \
+ "rename to asdf.txt\n" \
+ "index 94d2c01..292cb60 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/asdf.txt\n" \
+ "@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
+ " in, first taking off their skins, by letting them stand a few minutes in\n" \
+ " hot water, when they may be easily peeled. When made in this way you\n" \
+ " must thicken it with the flour only. Any part of the veal may be used,\n" \
+ "-but the shin or knuckle is the nicest.\n" \
+ "+but the shin or knuckle is the nicest!\n" \
+ "diff --git a/veal.txt b/veal.txt\n" \
+ "index 292cb60..61c686b 100644\n" \
+ "--- a/veal.txt\n" \
+ "+++ b/veal.txt\n" \
+ "@@ -1,4 +1,4 @@\n" \
+ "-VEAL SOUP!\n" \
+ "+VEAL SOUP\n" \
+ "\n" \
+ " Put into a pot three quarts of water, three onions cut small, one\n" \
+ " spoonful of black pepper pounded, and two of salt, with two or three\n"
+
+#define DIFF_DELETE_AND_READD_FILE \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "deleted file mode 100644\n" \
+ "index f516580..0000000\n" \
+ "--- a/asparagus.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1,10 +0,0 @@\n" \
+ "-ASPARAGUS SOUP!\n" \
+ "-\n" \
+ "-Take four large bunches of asparagus, scrape it nicely, cut off one inch\n" \
+ "-of the tops, and lay them in water, chop the stalks and put them on the\n" \
+ "-fire with a piece of bacon, a large onion cut up, and pepper and salt;\n" \
+ "-add two quarts of water, boil them till the stalks are quite soft, then\n" \
+ "-pulp them through a sieve, and strain the water to it, which must be put\n" \
+ "-back in the pot; put into it a chicken cut up, with the tops of\n" \
+ "-asparagus which had been laid by, boil it until these last articles are\n" \
+ "-sufficiently done, thicken with flour, butter and milk, and serve it up.\n" \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "new file mode 100644\n" \
+ "index 0000000..2dc7f8b\n" \
+ "--- /dev/null\n" \
+ "+++ b/asparagus.txt\n" \
+ "@@ -0,0 +1 @@\n" \
+ "+New file.\n" \
+
+#define DIFF_REMOVE_FILE_TWICE \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "deleted file mode 100644\n" \
+ "index f516580..0000000\n" \
+ "--- a/asparagus.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1,10 +0,0 @@\n" \
+ "-ASPARAGUS SOUP!\n" \
+ "-\n" \
+ "-Take four large bunches of asparagus, scrape it nicely, cut off one inch\n" \
+ "-of the tops, and lay them in water, chop the stalks and put them on the\n" \
+ "-fire with a piece of bacon, a large onion cut up, and pepper and salt;\n" \
+ "-add two quarts of water, boil them till the stalks are quite soft, then\n" \
+ "-pulp them through a sieve, and strain the water to it, which must be put\n" \
+ "-back in the pot; put into it a chicken cut up, with the tops of\n" \
+ "-asparagus which had been laid by, boil it until these last articles are\n" \
+ "-sufficiently done, thicken with flour, butter and milk, and serve it up.\n" \
+ "diff --git a/asparagus.txt b/asparagus.txt\n" \
+ "deleted file mode 100644\n" \
+ "index f516580..0000000\n" \
+ "--- a/asparagus.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1,10 +0,0 @@\n" \
+ "-ASPARAGUS SOUP!\n" \
+ "-\n" \
+ "-Take four large bunches of asparagus, scrape it nicely, cut off one inch\n" \
+ "-of the tops, and lay them in water, chop the stalks and put them on the\n" \
+ "-fire with a piece of bacon, a large onion cut up, and pepper and salt;\n" \
+ "-add two quarts of water, boil them till the stalks are quite soft, then\n" \
+ "-pulp them through a sieve, and strain the water to it, which must be put\n" \
+ "-back in the pot; put into it a chicken cut up, with the tops of\n" \
+ "-asparagus which had been laid by, boil it until these last articles are\n" \
+ "-sufficiently done, thicken with flour, butter and milk, and serve it up.\n"
+
+struct iterator_compare_data {
+ struct merge_index_entry *expected;
+ size_t cnt;
+ size_t idx;
+};
+
+static int iterator_compare(const git_index_entry *entry, void *_data)
+{
+ git_oid expected_id;
+
+ struct iterator_compare_data *data = (struct iterator_compare_data *)_data;
+
+ cl_assert_equal_i(GIT_IDXENTRY_STAGE(entry), data->expected[data->idx].stage);
+ cl_git_pass(git_oid_fromstr(&expected_id, data->expected[data->idx].oid_str));
+ cl_assert_equal_oid(&entry->id, &expected_id);
+ cl_assert_equal_i(entry->mode, data->expected[data->idx].mode);
+ cl_assert_equal_s(entry->path, data->expected[data->idx].path);
+
+ if (data->idx >= data->cnt)
+ return -1;
+
+ data->idx++;
+
+ return 0;
+}
+
+static void validate_apply_workdir(
+ git_repository *repo,
+ struct merge_index_entry *workdir_entries,
+ size_t workdir_cnt)
+{
+ git_index *index;
+ git_iterator *iterator;
+ git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
+ struct iterator_compare_data data = { workdir_entries, workdir_cnt };
+
+ opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
+
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_iterator_for_workdir(&iterator, repo, index, NULL, &opts));
+
+ cl_git_pass(git_iterator_foreach(iterator, iterator_compare, &data));
+ cl_assert_equal_i(data.idx, data.cnt);
+
+ git_iterator_free(iterator);
+ git_index_free(index);
+}
+
+static void validate_apply_index(
+ git_repository *repo,
+ struct merge_index_entry *index_entries,
+ size_t index_cnt)
+{
+ git_index *index;
+ git_iterator *iterator;
+ struct iterator_compare_data data = { index_entries, index_cnt };
+
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_iterator_for_index(&iterator, repo, index, NULL));
+
+ cl_git_pass(git_iterator_foreach(iterator, iterator_compare, &data));
+ cl_assert_equal_i(data.idx, data.cnt);
+
+ git_iterator_free(iterator);
+ git_index_free(index);
+}
+
+static int iterator_eq(const git_index_entry **entry, void *_data)
+{
+ GIT_UNUSED(_data);
+
+ if (!entry[0] || !entry[1])
+ return -1;
+
+ cl_assert_equal_i(GIT_IDXENTRY_STAGE(entry[0]), GIT_IDXENTRY_STAGE(entry[1]));
+ cl_assert_equal_oid(&entry[0]->id, &entry[1]->id);
+ cl_assert_equal_i(entry[0]->mode, entry[1]->mode);
+ cl_assert_equal_s(entry[0]->path, entry[1]->path);
+
+ return 0;
+}
+
+static void validate_index_unchanged(git_repository *repo)
+{
+ git_tree *head;
+ git_index *index;
+ git_iterator *head_iterator, *index_iterator, *iterators[2];
+
+ cl_git_pass(git_repository_head_tree(&head, repo));
+ cl_git_pass(git_repository_index(&index, repo));
+
+ cl_git_pass(git_iterator_for_tree(&head_iterator, head, NULL));
+ cl_git_pass(git_iterator_for_index(&index_iterator, repo, index, NULL));
+
+ iterators[0] = head_iterator;
+ iterators[1] = index_iterator;
+
+ cl_git_pass(git_iterator_walk(iterators, 2, iterator_eq, NULL));
+
+ git_iterator_free(head_iterator);
+ git_iterator_free(index_iterator);
+
+ git_tree_free(head);
+ git_index_free(index);
+}
+
+static void validate_workdir_unchanged(git_repository *repo)
+{
+ git_tree *head;
+ git_index *index;
+ git_iterator *head_iterator, *workdir_iterator, *iterators[2];
+ git_iterator_options workdir_opts = GIT_ITERATOR_OPTIONS_INIT;
+
+ cl_git_pass(git_repository_head_tree(&head, repo));
+ cl_git_pass(git_repository_index(&index, repo));
+
+ workdir_opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
+
+ cl_git_pass(git_iterator_for_tree(&head_iterator, head, NULL));
+ cl_git_pass(git_iterator_for_workdir(&workdir_iterator, repo, index, NULL, &workdir_opts));
+
+ iterators[0] = head_iterator;
+ iterators[1] = workdir_iterator;
+
+ cl_git_pass(git_iterator_walk(iterators, 2, iterator_eq, NULL));
+
+ git_iterator_free(head_iterator);
+ git_iterator_free(workdir_iterator);
+
+ git_tree_free(head);
+ git_index_free(index);
+}
diff --git a/tests/apply/both.c b/tests/apply/both.c
new file mode 100644
index 0000000..5091b8c
--- /dev/null
+++ b/tests/apply/both.c
@@ -0,0 +1,736 @@
+#include "clar_libgit2.h"
+#include "apply_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+void test_apply_both__initialize(void)
+{
+ git_oid oid;
+ git_commit *commit;
+
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
+ git_commit_free(commit);
+}
+
+void test_apply_both__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_apply_both__generated_diff(void)
+{
+ git_oid a_oid, b_oid;
+ git_commit *a_commit, *b_commit;
+ git_tree *a_tree, *b_tree;
+ git_diff *diff;
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f");
+ cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+ cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
+
+ cl_git_pass(git_commit_tree(&a_tree, a_commit));
+ cl_git_pass(git_commit_tree(&b_tree, b_commit));
+
+ cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &diff_opts));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+ git_tree_free(a_tree);
+ git_tree_free(b_tree);
+ git_commit_free(a_commit);
+ git_commit_free(b_commit);
+}
+
+void test_apply_both__parsed_diff(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__removes_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_DELETE_FILE,
+ strlen(DIFF_DELETE_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__adds_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "6370543fcfedb3e6516ec53b06158f3687dc1447", 0, "newfile.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_ADD_FILE, strlen(DIFF_ADD_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__application_failure_leaves_index_unmodified(void)
+{
+ git_diff *diff;
+ git_index *index;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the index */
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_remove(index, "veal.txt", 0));
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__index_must_match_workdir(void)
+{
+ git_diff *diff;
+ git_index *index;
+ git_index_entry idx_entry;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ /*
+ * Append a line to the end of the file in both the index and the
+ * working directory. Although the appended line would allow for
+ * patch application in each, the line appended is different in
+ * each, so the application should not be allowed.
+ */
+ cl_git_append2file("merge-recursive/asparagus.txt",
+ "This is a modification.\n");
+
+ cl_git_pass(git_repository_index(&index, repo));
+
+ memset(&idx_entry, 0, sizeof(git_index_entry));
+ idx_entry.mode = 0100644;
+ idx_entry.path = "asparagus.txt";
+ cl_git_pass(git_oid_fromstr(&idx_entry.id, "06d3fefb8726ab1099acc76e02dfb85e034b2538"));
+ cl_git_pass(git_index_add(index, &idx_entry));
+
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__index_mode_must_match_workdir(void)
+{
+ git_diff *diff;
+
+ if (!cl_is_chmod_supported())
+ clar__skip();
+
+ /* Set a file in the working directory executable. */
+ cl_must_pass(p_chmod("merge-recursive/asparagus.txt", 0755));
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_MODIFY_TWO_FILES,
+ strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__application_failure_leaves_workdir_unmodified(void)
+{
+ git_diff *diff;
+ git_index *index;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "8684724651336001c5dbce74bed6736d2443958d", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the workdir */
+ cl_git_rewritefile("merge-recursive/veal.txt",
+ "This is a modification.\n");
+
+ cl_git_pass(git_repository_index(&index, repo));
+ git_index_add_bypath(index, "veal.txt");
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__keeps_nonconflicting_changes(void)
+{
+ git_diff *diff;
+ git_index *index;
+ git_index_entry idx_entry;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 0, "beef.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "f75ba05f340c51065cbea2e1fdbfe5fe13144c97", 0, "gravy.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the index */
+ cl_git_pass(git_repository_index(&index, repo));
+
+ memset(&idx_entry, 0, sizeof(git_index_entry));
+ idx_entry.mode = 0100644;
+ idx_entry.path = "beef.txt";
+ cl_git_pass(git_oid_fromstr(&idx_entry.id, "898d12687fb35be271c27c795a6b32c8b51da79e"));
+ git_index_add(index, &idx_entry);
+
+ cl_git_pass(git_index_remove(index, "bouilli.txt", 0));
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ /* and mutate the working directory */
+ cl_git_rmfile("merge-recursive/oyster.txt");
+ cl_git_rewritefile("merge-recursive/gravy.txt", "Hello, world.\n");
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__can_apply_nonconflicting_file_changes(void)
+{
+ git_diff *diff;
+ git_index *index;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f8a701c8a1a22c1729ee50faff1111f2d64f96fc", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ /*
+ * Replace the workdir file with a version that is different than
+ * HEAD but such that the patch still applies cleanly. This item
+ * has a new line appended.
+ */
+ cl_git_append2file("merge-recursive/asparagus.txt",
+ "This line is added in the index and the workdir.\n");
+
+ cl_git_pass(git_repository_index(&index, repo));
+ git_index_add_bypath(index, "asparagus.txt");
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__honors_crlf_attributes(void)
+{
+ git_diff *diff;
+ git_oid oid;
+ git_commit *commit;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "176a458f94e0ea5272ce67c36bf30b6be9caf623", 0, ".gitattributes" },
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_mkfile("merge-recursive/.gitattributes", "* text=auto\n");
+
+ cl_git_rmfile("merge-recursive/asparagus.txt");
+ cl_git_rmfile("merge-recursive/veal.txt");
+
+ git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
+ git_commit_free(commit);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "notbeef.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_FILE,
+ strlen(DIFF_RENAME_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_and_modify(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "6fa10147f00fe1fab1d5e835529a9dad53db8552", 0, "notbeef.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AND_MODIFY_FILE,
+ strlen(DIFF_RENAME_AND_MODIFY_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_a_to_b_to_c(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "notbeef.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_A_TO_B_TO_C,
+ strlen(DIFF_RENAME_A_TO_B_TO_C)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_a_to_b_to_c_exact(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "notbeef.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_A_TO_B_TO_C_EXACT,
+ strlen(DIFF_RENAME_A_TO_B_TO_C_EXACT)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_circular(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "asparagus.txt" },
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_CIRCULAR,
+ strlen(DIFF_RENAME_CIRCULAR)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_2_to_1(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "2.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_2_TO_1,
+ strlen(DIFF_RENAME_2_TO_1)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_1_to_2(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "1.txt" },
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "2.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_1_TO_2,
+ strlen(DIFF_RENAME_1_TO_2)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__two_deltas_one_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "0a9fd4415635e72573f0f6b5e68084cfe18f5075", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_TWO_DELTAS_ONE_FILE,
+ strlen(DIFF_TWO_DELTAS_ONE_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__two_deltas_one_new_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "08d4c445cf0078f3d9b604b82f32f4d87e083325", 0, "newfile.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_TWO_DELTAS_ONE_NEW_FILE,
+ strlen(DIFF_TWO_DELTAS_ONE_NEW_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_and_modify_deltas(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "61c686bed39684eee8a2757ceb1291004a21333f", 0, "asdf.txt" },
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AND_MODIFY_DELTAS,
+ strlen(DIFF_RENAME_AND_MODIFY_DELTAS)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__rename_delta_after_modify_delta(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "292cb60ce5e25c337c5b6e12957bbbfe1be4bf49", 0, "other.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "c8c120f466591bbe3b8867361d5ec3cdd9fda756", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AFTER_MODIFY,
+ strlen(DIFF_RENAME_AFTER_MODIFY)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__cant_rename_after_modify_nonexistent_target_path(void)
+{
+ git_diff *diff;
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AFTER_MODIFY_TARGET_PATH,
+ strlen(DIFF_RENAME_AFTER_MODIFY_TARGET_PATH)));
+ cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__cant_modify_source_path_after_rename(void)
+{
+ git_diff *diff;
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AND_MODIFY_SOURCE_PATH,
+ strlen(DIFF_RENAME_AND_MODIFY_SOURCE_PATH)));
+ cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__readd_deleted_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "2dc7f8b24ba27f3888368bd180df03ff4c6c6fab", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_DELETE_AND_READD_FILE,
+ strlen(DIFF_DELETE_AND_READD_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_both__cant_remove_file_twice(void)
+{
+ git_diff *diff;
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_REMOVE_FILE_TWICE,
+ strlen(DIFF_REMOVE_FILE_TWICE)));
+ cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ git_diff_free(diff);
+}
diff --git a/tests/apply/callbacks.c b/tests/apply/callbacks.c
new file mode 100644
index 0000000..1b759dc
--- /dev/null
+++ b/tests/apply/callbacks.c
@@ -0,0 +1,128 @@
+#include "clar_libgit2.h"
+#include "apply_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+void test_apply_callbacks__initialize(void)
+{
+ git_oid oid;
+ git_commit *commit;
+
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
+ git_commit_free(commit);
+}
+
+void test_apply_callbacks__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static int delta_abort_cb(const git_diff_delta *delta, void *payload)
+{
+ GIT_UNUSED(payload);
+
+ if (!strcmp(delta->old_file.path, "veal.txt"))
+ return -99;
+
+ return 0;
+}
+
+void test_apply_callbacks__delta_aborts(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+
+ opts.delta_cb = delta_abort_cb;
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_fail_with(-99,
+ git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts));
+
+ validate_index_unchanged(repo);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+static int delta_skip_cb(const git_diff_delta *delta, void *payload)
+{
+ GIT_UNUSED(payload);
+
+ if (!strcmp(delta->old_file.path, "asparagus.txt"))
+ return 1;
+
+ return 0;
+}
+
+void test_apply_callbacks__delta_can_skip(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ opts.delta_cb = delta_skip_cb;
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+static int hunk_skip_odds_cb(const git_diff_hunk *hunk, void *payload)
+{
+ int *count = (int *)payload;
+ GIT_UNUSED(hunk);
+
+ return ((*count)++ % 2 == 1);
+}
+
+void test_apply_callbacks__hunk_can_skip(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ int count = 0;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "06f751b6ba4f017ddbf4248015768300268e092a", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ opts.hunk_cb = hunk_skip_odds_cb;
+ opts.payload = &count;
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MANY_CHANGES_ONE, strlen(DIFF_MANY_CHANGES_ONE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
diff --git a/tests/apply/fromdiff.c b/tests/apply/fromdiff.c
index 6110609..8a6d8fa 100644
--- a/tests/apply/fromdiff.c
+++ b/tests/apply/fromdiff.c
@@ -49,7 +49,7 @@ static int apply_gitbuf(
cl_assert_equal_s(patch_expected, patchbuf.ptr);
}
- error = git_apply__patch(&result, &filename, &mode, old ? old->ptr : NULL, old ? old->size : 0, patch);
+ error = git_apply__patch(&result, &filename, &mode, old ? old->ptr : NULL, old ? old->size : 0, patch, NULL);
if (error == 0 && new == NULL) {
cl_assert_equal_i(0, result.size);
@@ -150,6 +150,52 @@ void test_apply_fromdiff__prepend_nocontext(void)
PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT, &diff_opts));
}
+void test_apply_fromdiff__prepend_and_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE, NULL));
+}
+
+void test_apply_fromdiff__prepend_and_change_nocontext(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE_NOCONTEXT, &diff_opts));
+}
+
+void test_apply_fromdiff__delete_and_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_AND_CHANGE, NULL));
+}
+
+void test_apply_fromdiff__delete_and_change_nocontext(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_AND_CHANGE_NOCONTEXT, &diff_opts));
+}
+
+void test_apply_fromdiff__delete_firstline(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_FIRSTLINE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_FIRSTLINE, NULL));
+}
+
void test_apply_fromdiff__append(void)
{
cl_git_pass(apply_buf(
diff --git a/tests/apply/fromfile.c b/tests/apply/fromfile.c
index 0ace639..6d4379b 100644
--- a/tests/apply/fromfile.c
+++ b/tests/apply/fromfile.c
@@ -39,7 +39,7 @@ static int apply_patchfile(
cl_git_pass(git_patch_from_buffer(&patch, patchfile, strlen(patchfile), NULL));
- error = git_apply__patch(&result, &filename, &mode, old, old_len, patch);
+ error = git_apply__patch(&result, &filename, &mode, old, old_len, patch, NULL);
if (error == 0) {
cl_assert_equal_i(new_len, result.size);
diff --git a/tests/apply/index.c b/tests/apply/index.c
new file mode 100644
index 0000000..9c9094c
--- /dev/null
+++ b/tests/apply/index.c
@@ -0,0 +1,321 @@
+#include "clar_libgit2.h"
+#include "apply_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+void test_apply_index__initialize(void)
+{
+ git_oid oid;
+ git_commit *commit;
+
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
+ git_commit_free(commit);
+}
+
+void test_apply_index__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_apply_index__generate_diff(void)
+{
+ git_oid a_oid, b_oid;
+ git_commit *a_commit, *b_commit;
+ git_tree *a_tree, *b_tree;
+ git_diff *diff;
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f");
+ cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+ cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
+
+ cl_git_pass(git_commit_tree(&a_tree, a_commit));
+ cl_git_pass(git_commit_tree(&b_tree, b_commit));
+
+ cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &diff_opts));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+ git_tree_free(a_tree);
+ git_tree_free(b_tree);
+ git_commit_free(a_commit);
+ git_commit_free(b_commit);
+}
+
+void test_apply_index__parsed_diff(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__removes_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_DELETE_FILE,
+ strlen(DIFF_DELETE_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__adds_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "6370543fcfedb3e6516ec53b06158f3687dc1447", 0, "newfile.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_ADD_FILE, strlen(DIFF_ADD_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__modified_workdir_with_unmodified_index_is_ok(void)
+{
+ git_diff *diff;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "f75ba05f340c51065cbea2e1fdbfe5fe13144c97", 0, "veal.txt" }
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the workdir and leave the index matching HEAD */
+ cl_git_rmfile("merge-recursive/asparagus.txt");
+ cl_git_rewritefile("merge-recursive/veal.txt", "Hello, world.\n");
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__application_failure_leaves_index_unmodified(void)
+{
+ git_diff *diff;
+ git_index *index;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the index */
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_remove(index, "veal.txt", 0));
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__keeps_nonconflicting_changes(void)
+{
+ git_diff *diff;
+ git_index *index;
+ git_index_entry idx_entry;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 0, "beef.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the index */
+ cl_git_pass(git_repository_index(&index, repo));
+
+ memset(&idx_entry, 0, sizeof(git_index_entry));
+ idx_entry.mode = 0100644;
+ idx_entry.path = "beef.txt";
+ cl_git_pass(git_oid_fromstr(&idx_entry.id, "898d12687fb35be271c27c795a6b32c8b51da79e"));
+ cl_git_pass(git_index_add(index, &idx_entry));
+
+ cl_git_pass(git_index_remove(index, "bouilli.txt", 0));
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__can_apply_nonconflicting_file_changes(void)
+{
+ git_diff *diff;
+ git_index *index;
+ git_index_entry idx_entry;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "4f2d1645dee99ced096877911de540c65ade2ef8", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ /*
+ * Replace the index entry with a version that is different than
+ * HEAD but such that the patch still applies cleanly. This item
+ * has a new line appended.
+ */
+
+ cl_git_pass(git_repository_index(&index, repo));
+
+ memset(&idx_entry, 0, sizeof(git_index_entry));
+ idx_entry.mode = 0100644;
+ idx_entry.path = "asparagus.txt";
+ cl_git_pass(git_oid_fromstr(&idx_entry.id, "06d3fefb8726ab1099acc76e02dfb85e034b2538"));
+ cl_git_pass(git_index_add(index, &idx_entry));
+
+ cl_git_pass(git_index_write(index));
+ git_index_free(index);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
+
+void test_apply_index__change_mode(void)
+{
+ git_diff *diff;
+
+ const char *diff_file = DIFF_EXECUTABLE_FILE;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100755, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_workdir_unchanged(repo);
+
+ git_diff_free(diff);
+}
diff --git a/tests/apply/partial.c b/tests/apply/partial.c
new file mode 100644
index 0000000..243dccf
--- /dev/null
+++ b/tests/apply/partial.c
@@ -0,0 +1,232 @@
+#include "clar_libgit2.h"
+#include "git2/sys/repository.h"
+
+#include "apply.h"
+#include "repository.h"
+#include "buf_text.h"
+
+#include "../patch/patch_common.h"
+
+static git_repository *repo = NULL;
+
+void test_apply_partial__initialize(void)
+{
+ repo = cl_git_sandbox_init("renames");
+}
+
+void test_apply_partial__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static int skip_addition(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines > hunk->old_lines) ? 1 : 0;
+}
+
+static int skip_deletion(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines < hunk->old_lines) ? 1 : 0;
+}
+
+static int skip_change(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines == hunk->old_lines) ? 1 : 0;
+}
+
+static int abort_addition(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines > hunk->old_lines) ? GIT_EUSER : 0;
+}
+
+static int abort_deletion(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines < hunk->old_lines) ? GIT_EUSER : 0;
+}
+
+static int abort_change(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines == hunk->old_lines) ? GIT_EUSER : 0;
+}
+
+static int apply_buf(
+ const char *old,
+ const char *oldname,
+ const char *new,
+ const char *newname,
+ const char *expected,
+ const git_diff_options *diff_opts,
+ git_apply_hunk_cb hunk_cb,
+ void *payload)
+{
+ git_patch *patch;
+ git_buf result = GIT_BUF_INIT;
+ git_buf patchbuf = GIT_BUF_INIT;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ char *filename;
+ unsigned int mode;
+ int error;
+ size_t oldsize = strlen(old);
+ size_t newsize = strlen(new);
+
+ opts.hunk_cb = hunk_cb;
+ opts.payload = payload;
+
+ cl_git_pass(git_patch_from_buffers(&patch, old, oldsize, oldname, new, newsize, newname, diff_opts));
+ if ((error = git_apply__patch(&result, &filename, &mode, old, oldsize, patch, &opts)) == 0) {
+ cl_assert_equal_s(expected, result.ptr);
+ cl_assert_equal_s(newname, filename);
+ cl_assert_equal_i(0100644, mode);
+ }
+
+ git__free(filename);
+ git_buf_free(&result);
+ git_buf_free(&patchbuf);
+ git_patch_free(patch);
+
+ return error;
+}
+
+void test_apply_partial__prepend_and_change_skip_addition(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, NULL, skip_addition, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_skip_addition(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_CHANGE_MIDDLE, &diff_opts, skip_addition, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_abort_addition(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_fail(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, &diff_opts, abort_addition, NULL));
+}
+
+void test_apply_partial__prepend_and_change_skip_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_PREPEND_AND_CHANGE, NULL, skip_change, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_skip_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_PREPEND, &diff_opts, skip_change, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_abort_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_fail(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_PREPEND, &diff_opts, abort_change, NULL));
+}
+
+void test_apply_partial__delete_and_change_skip_deletion(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, NULL, skip_deletion, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_skip_deletion(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_CHANGE_MIDDLE, &diff_opts, skip_deletion, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_abort_deletion(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_fail(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, &diff_opts, abort_deletion, NULL));
+}
+
+void test_apply_partial__delete_and_change_skip_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_DELETE_AND_CHANGE, NULL, skip_change, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_skip_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_DELETE_FIRSTLINE, &diff_opts, skip_change, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_abort_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_fail(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_DELETE_FIRSTLINE, &diff_opts, abort_change, NULL));
+}
diff --git a/tests/apply/tree.c b/tests/apply/tree.c
new file mode 100644
index 0000000..f35b13c
--- /dev/null
+++ b/tests/apply/tree.c
@@ -0,0 +1,58 @@
+#include "clar_libgit2.h"
+#include "../merge/merge_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+
+void test_apply_tree__initialize(void)
+{
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+}
+
+void test_apply_tree__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_apply_tree__one(void)
+{
+ git_oid a_oid, b_oid;
+ git_commit *a_commit, *b_commit;
+ git_tree *a_tree, *b_tree;
+ git_diff *diff;
+ git_index *index = NULL;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+
+ struct merge_index_entry expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+
+ git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f");
+
+ cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+ cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
+
+ cl_git_pass(git_commit_tree(&a_tree, a_commit));
+ cl_git_pass(git_commit_tree(&b_tree, b_commit));
+
+ cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &opts));
+
+ cl_git_pass(git_apply_to_tree(&index, repo, a_tree, diff, NULL));
+ merge_test_index(index, expected, 6);
+
+ git_index_free(index);
+ git_diff_free(diff);
+ git_tree_free(a_tree);
+ git_tree_free(b_tree);
+ git_commit_free(a_commit);
+ git_commit_free(b_commit);
+}
+
diff --git a/tests/apply/workdir.c b/tests/apply/workdir.c
new file mode 100644
index 0000000..1a82925
--- /dev/null
+++ b/tests/apply/workdir.c
@@ -0,0 +1,358 @@
+#include "clar_libgit2.h"
+#include "apply_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+void test_apply_workdir__initialize(void)
+{
+ git_oid oid;
+ git_commit *commit;
+
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
+ git_commit_free(commit);
+}
+
+void test_apply_workdir__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_apply_workdir__generated_diff(void)
+{
+ git_oid a_oid, b_oid;
+ git_commit *a_commit, *b_commit;
+ git_tree *a_tree, *b_tree;
+ git_diff *diff;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f"); cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+ cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
+
+ cl_git_pass(git_commit_tree(&a_tree, a_commit));
+ cl_git_pass(git_commit_tree(&b_tree, b_commit));
+
+ cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &opts));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+ git_tree_free(a_tree);
+ git_tree_free(b_tree);
+ git_commit_free(a_commit);
+ git_commit_free(b_commit);
+}
+
+void test_apply_workdir__parsed_diff(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__removes_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_DELETE_FILE,
+ strlen(DIFF_DELETE_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__adds_file(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "6370543fcfedb3e6516ec53b06158f3687dc1447", 0, "newfile.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_ADD_FILE, strlen(DIFF_ADD_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__modified_index_with_unmodified_workdir_is_ok(void)
+{
+ git_index *index;
+ git_index_entry idx_entry = {{0}};
+ git_diff *diff;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry index_expected[] = {
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "veal.txt" }
+ };
+ size_t index_expected_cnt = sizeof(index_expected) /
+ sizeof(struct merge_index_entry);
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the index and leave the workdir matching HEAD */
+ git_repository_index(&index, repo);
+
+ idx_entry.mode = 0100644;
+ idx_entry.path = "veal.txt";
+ cl_git_pass(git_oid_fromstr(&idx_entry.id, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d"));
+
+ cl_git_pass(git_index_add(index, &idx_entry));
+ cl_git_pass(git_index_remove(index, "asparagus.txt", 0));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_apply_index(repo, index_expected, index_expected_cnt);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_index_free(index);
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__application_failure_leaves_workdir_unmodified(void)
+{
+ git_diff *diff;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "8684724651336001c5dbce74bed6736d2443958d", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /* mutate the workdir */
+ cl_git_rewritefile("merge-recursive/veal.txt",
+ "This is a modification.\n");
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__keeps_nonconflicting_changes(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "f75ba05f340c51065cbea2e1fdbfe5fe13144c97", 0, "gravy.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_rmfile("merge-recursive/oyster.txt");
+ cl_git_rewritefile("merge-recursive/gravy.txt", "Hello, world.\n");
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__can_apply_nonconflicting_file_changes(void)
+{
+ git_diff *diff;
+
+ const char *diff_file = DIFF_MODIFY_TWO_FILES;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "5db1a0fef164cb66cc0c00d35cc5af979ddc1a64", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ /*
+ * Replace the workdir file with a version that is different than
+ * HEAD but such that the patch still applies cleanly. This item
+ * has a new line appended.
+ */
+ cl_git_append2file("merge-recursive/asparagus.txt",
+ "This line is added in the workdir.\n");
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__change_mode(void)
+{
+#ifndef GIT_WIN32
+ git_diff *diff;
+
+ const char *diff_file = DIFF_EXECUTABLE_FILE;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100755, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+#endif
+}
+
+void test_apply_workdir__apply_many_changes_one(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "c9d7d5d58088bc91f6e06f17ca3a205091568d3a", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MANY_CHANGES_ONE, strlen(DIFF_MANY_CHANGES_ONE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
+
+void test_apply_workdir__apply_many_changes_two(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "6b943d65af6d8db74d747284fa4ca7d716ad5bbb", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MANY_CHANGES_TWO, strlen(DIFF_MANY_CHANGES_TWO)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c
index a16acd7..889fcd6 100644
--- a/tests/iterator/workdir.c
+++ b/tests/iterator/workdir.c
@@ -3,6 +3,7 @@
#include "repository.h"
#include "fileops.h"
#include "../submodule/submodule_helpers.h"
+#include "../merge/merge_helpers.h"
#include "iterator_helpers.h"
#include <stdarg.h>
@@ -1474,3 +1475,48 @@ void test_iterator_workdir__pathlist_with_directory_include_trees(void)
git_vector_free(&filelist);
}
+void test_iterator_workdir__hash_when_requested(void)
+{
+ git_iterator *iter;
+ const git_index_entry *entry;
+ git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
+ git_oid expected_id = {{0}};
+ size_t i;
+
+ struct merge_index_entry expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "7c7e08f9559d9e1551b91e1cf68f1d0066109add", 0, "oyster.txt" },
+ { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 0, "veal.txt" },
+ };
+
+ g_repo = cl_git_sandbox_init("merge-recursive");
+
+ /* do the iteration normally, ensure there are no hashes */
+ cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
+
+ for (i = 0; i < sizeof(expected) / sizeof(struct merge_index_entry); i++) {
+ cl_git_pass(git_iterator_advance(&entry, iter));
+
+ cl_assert_equal_oid(&expected_id, &entry->id);
+ cl_assert_equal_s(expected[i].path, entry->path);
+ }
+ cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&entry, iter));
+ git_iterator_free(iter);
+
+ /* do the iteration requesting hashes */
+ iter_opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
+ cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
+
+ for (i = 0; i < sizeof(expected) / sizeof(struct merge_index_entry); i++) {
+ cl_git_pass(git_iterator_advance(&entry, iter));
+
+ cl_git_pass(git_oid_fromstr(&expected_id, expected[i].oid_str));
+ cl_assert_equal_oid(&expected_id, &entry->id);
+ cl_assert_equal_s(expected[i].path, entry->path);
+ }
+ cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&entry, iter));
+ git_iterator_free(iter);
+}
diff --git a/tests/patch/patch_common.h b/tests/patch/patch_common.h
index e838e60..3f2668d 100644
--- a/tests/patch/patch_common.h
+++ b/tests/patch/patch_common.h
@@ -220,6 +220,112 @@
"@@ -0,0 +1 @@\n" \
"+insert at front\n"
+/* An insertion at the beginning of the file and change in the middle */
+
+#define FILE_PREPEND_AND_CHANGE \
+ "insert at front\n" \
+ "hey!\n" \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(THIS line is changed!)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f73c8bb 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,9 +1,10 @@\n" \
+ "+insert at front\n" \
+ " hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n" \
+ " yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n" \
+ " and this\n" \
+ " is additional context\n" \
+ " below it!\n"
+
+#define PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE_NOCONTEXT \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f73c8bb 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -0,0 +1 @@\n" \
+ "+insert at front\n" \
+ "@@ -6 +7 @@ yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n"
+
+/* A deletion at the beginning of the file and a change in the middle */
+
+#define FILE_DELETE_AND_CHANGE \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(THIS line is changed!)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_AND_CHANGE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..1e2dfa6 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,9 +1,8 @@\n" \
+ "-hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n" \
+ " yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n" \
+ " and this\n" \
+ " is additional context\n" \
+ " below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_AND_CHANGE_NOCONTEXT \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..1e2dfa6 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1 +0,0 @@\n" \
+ "-hey!\n" \
+ "@@ -6 +5 @@ yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n"
+
+/* A deletion at the beginning of the file */
+
+#define FILE_DELETE_FIRSTLINE \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(this line is changed)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_FIRSTLINE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f31fa13 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,4 +1,3 @@\n" \
+ "-hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n"
+
/* An insertion at the end of the file (and the resultant patch) */
#define FILE_APPEND \
diff --git a/tests/resources/merge-recursive/.gitted/objects/06/d3fefb8726ab1099acc76e02dfb85e034b2538 b/tests/resources/merge-recursive/.gitted/objects/06/d3fefb8726ab1099acc76e02dfb85e034b2538
new file mode 100644
index 0000000..b3919aa
Binary files /dev/null and b/tests/resources/merge-recursive/.gitted/objects/06/d3fefb8726ab1099acc76e02dfb85e034b2538 differ