Merge pull request #1118 from arrbee/fix-reset-hard Fix checkout corner cases, index handling, and reset hard behavior
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 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673
diff --git a/docs/checkout-internals.md b/docs/checkout-internals.md
new file mode 100644
index 0000000..cb646da
--- /dev/null
+++ b/docs/checkout-internals.md
@@ -0,0 +1,203 @@
+Checkout Internals
+==================
+
+Checkout has to handle a lot of different cases. It examines the
+differences between the target tree, the baseline tree and the working
+directory, plus the contents of the index, and groups files into five
+categories:
+
+1. UNMODIFIED - Files that match in all places.
+2. SAFE - Files where the working directory and the baseline content
+ match that can be safely updated to the target.
+3. DIRTY/MISSING - Files where the working directory differs from the
+ baseline but there is no conflicting change with the target. One
+ example is a file that doesn't exist in the working directory - no
+ data would be lost as a result of writing this file. Which action
+ will be taken with these files depends on the options you use.
+4. CONFLICTS - Files where changes in the working directory conflict
+ with changes to be applied by the target. If conflicts are found,
+ they prevent any other modifications from being made (although there
+ are options to override that and force the update, of course).
+5. UNTRACKED/IGNORED - Files in the working directory that are untracked
+ or ignored (i.e. only in the working directory, not the other places).
+
+Right now, this classification is done via 3 iterators (for the three
+trees), with a final lookup in the index. At some point, this may move to
+a 4 iterator version to incorporate the index better.
+
+The actual checkout is done in five phases (at least right now).
+
+1. The diff between the baseline and the target tree is used as a base
+ list of possible updates to be applied.
+2. Iterate through the diff and the working directory, building a list of
+ actions to be taken (and sending notifications about conflicts and
+ dirty files).
+3. Remove any files / directories as needed (because alphabetical
+ iteration means that an untracked directory will end up sorted *after*
+ a blob that should be checked out with the same name).
+4. Update all blobs.
+5. Update all submodules (after 4 in case a new .gitmodules blob was
+ checked out)
+
+Checkout could be driven either off a target-to-workdir diff or a
+baseline-to-target diff. There are pros and cons of each.
+
+Target-to-workdir means the diff includes every file that could be
+modified, which simplifies bookkeeping, but the code to constantly refer
+back to the baseline gets complicated.
+
+Baseline-to-target has simpler code because the diff defines the action to
+take, but needs special handling for untracked and ignored files, if they
+need to be removed.
+
+The current checkout implementation is based on a baseline-to-target diff.
+
+
+Picking Actions
+===============
+
+The most interesting aspect of this is phase 2, picking the actions that
+should be taken. There are a lot of corner cases, so it may be easier to
+start by looking at the rules for a simple 2-iterator diff:
+
+Key
+---
+- B1,B2,B3 - blobs with different SHAs,
+- Bi - ignored blob (WD only)
+- T1,T2,T3 - trees with different SHAs,
+- Ti - ignored tree (WD only)
+- x - nothing
+
+Diff with 2 non-workdir iterators
+---------------------------------
+
+ Old New
+ --- ---
+ 0 x x - nothing
+ 1 x B1 - added blob
+ 2 x T1 - added tree
+ 3 B1 x - removed blob
+ 4 B1 B1 - unmodified blob
+ 5 B1 B2 - modified blob
+ 6 B1 T1 - typechange blob -> tree
+ 7 T1 x - removed tree
+ 8 T1 B1 - typechange tree -> blob
+ 9 T1 T1 - unmodified tree
+ 10 T1 T2 - modified tree (implies modified/added/removed blob inside)
+
+
+Now, let's make the "New" iterator into a working directory iterator, so
+we replace "added" items with either untracked or ignored, like this:
+
+Diff with non-work & workdir iterators
+--------------------------------------
+
+ Old New-WD
+ --- ------
+ 0 x x - nothing
+ 1 x B1 - untracked blob
+ 2 x Bi - ignored file
+ 3 x T1 - untracked tree
+ 4 x Ti - ignored tree
+ 5 B1 x - removed blob
+ 6 B1 B1 - unmodified blob
+ 7 B1 B2 - modified blob
+ 8 B1 T1 - typechange blob -> tree
+ 9 B1 Ti - removed blob AND ignored tree as separate items
+ 10 T1 x - removed tree
+ 11 T1 B1 - typechange tree -> blob
+ 12 T1 Bi - removed tree AND ignored blob as separate items
+ 13 T1 T1 - unmodified tree
+ 14 T1 T2 - modified tree (implies modified/added/removed blob inside)
+
+Note: if there is a corresponding entry in the old tree, then a working
+directory item won't be ignored (i.e. no Bi or Ti for tracked items).
+
+
+Now, expand this to three iterators: a baseline tree, a target tree, and
+an actual working directory tree:
+
+Checkout From 3 Iterators (2 not workdir, 1 workdir)
+----------------------------------------------------
+
+(base == old HEAD; target == what to checkout; actual == working dir)
+
+ base target actual/workdir
+ ---- ------ ------
+ 0 x x x - nothing
+ 1 x x B1/Bi/T1/Ti - untracked/ignored blob/tree (SAFE)
+ 2+ x B1 x - add blob (SAFE)
+ 3 x B1 B1 - independently added blob (FORCEABLE-2)
+ 4* x B1 B2/Bi/T1/Ti - add blob with content conflict (FORCEABLE-2)
+ 5+ x T1 x - add tree (SAFE)
+ 6* x T1 B1/Bi - add tree with blob conflict (FORCEABLE-2)
+ 7 x T1 T1/i - independently added tree (SAFE+MISSING)
+ 8 B1 x x - independently deleted blob (SAFE+MISSING)
+ 9- B1 x B1 - delete blob (SAFE)
+ 10- B1 x B2 - delete of modified blob (FORCEABLE-1)
+ 11 B1 x T1/Ti - independently deleted blob AND untrack/ign tree (SAFE+MISSING !!!)
+ 12 B1 B1 x - locally deleted blob (DIRTY || SAFE+CREATE)
+ 13+ B1 B2 x - update to deleted blob (SAFE+MISSING)
+ 14 B1 B1 B1 - unmodified file (SAFE)
+ 15 B1 B1 B2 - locally modified file (DIRTY)
+ 16+ B1 B2 B1 - update unmodified blob (SAFE)
+ 17 B1 B2 B2 - independently updated blob (FORCEABLE-1)
+ 18+ B1 B2 B3 - update to modified blob (FORCEABLE-1)
+ 19 B1 B1 T1/Ti - locally deleted blob AND untrack/ign tree (DIRTY)
+ 20* B1 B2 T1/Ti - update to deleted blob AND untrack/ign tree (F-1)
+ 21+ B1 T1 x - add tree with locally deleted blob (SAFE+MISSING)
+ 22* B1 T1 B1 - add tree AND deleted blob (SAFE)
+ 23* B1 T1 B2 - add tree with delete of modified blob (F-1)
+ 24 B1 T1 T1 - add tree with deleted blob (F-1)
+ 25 T1 x x - independently deleted tree (SAFE+MISSING)
+ 26 T1 x B1/Bi - independently deleted tree AND untrack/ign blob (F-1)
+ 27- T1 x T1 - deleted tree (MAYBE SAFE)
+ 28+ T1 B1 x - deleted tree AND added blob (SAFE+MISSING)
+ 29 T1 B1 B1 - independently typechanged tree -> blob (F-1)
+ 30+ T1 B1 B2 - typechange tree->blob with conflicting blob (F-1)
+ 31* T1 B1 T1/T2 - typechange tree->blob (MAYBE SAFE)
+ 32+ T1 T1 x - restore locally deleted tree (SAFE+MISSING)
+ 33 T1 T1 B1/Bi - locally typechange tree->untrack/ign blob (DIRTY)
+ 34 T1 T1 T1/T2 - unmodified tree (MAYBE SAFE)
+ 35+ T1 T2 x - update locally deleted tree (SAFE+MISSING)
+ 36* T1 T2 B1/Bi - update to tree with typechanged tree->blob conflict (F-1)
+ 37 T1 T2 T1/T2/T3 - update to existing tree (MAYBE SAFE)
+
+The number is followed by ' ' if no change is needed or '+' if the case
+needs to write to disk or '-' if something must be deleted and '*' if
+there should be a delete followed by an write.
+
+There are four tiers of safe cases:
+
+- SAFE == completely safe to update
+- SAFE+MISSING == safe except the workdir is missing the expect content
+- MAYBE SAFE == safe if workdir tree matches (or is missing) baseline
+ content, which is unknown at this point
+- FORCEABLE == conflict unless FORCE is given
+- DIRTY == no conflict but change is not applied unless FORCE
+
+Some slightly unusual circumstances:
+
+ 8 - parent dir is only deleted when file is, so parent will be left if
+ empty even though it would be deleted if the file were present
+ 11 - core git does not consider this a conflict but attempts to delete T1
+ and gives "unable to unlink file" error yet does not skip the rest
+ of the operation
+ 12 - without FORCE file is left deleted (i.e. not restored) so new wd is
+ dirty (and warning message "D file" is printed), with FORCE, file is
+ restored.
+ 24 - This should be considered MAYBE SAFE since effectively it is 7 and 8
+ combined, but core git considers this a conflict unless forced.
+ 26 - This combines two cases (1 & 25) (and also implied 8 for tree content)
+ which are ok on their own, but core git treat this as a conflict.
+ If not forced, this is a conflict. If forced, this actually doesn't
+ have to write anything and leaves the new blob as an untracked file.
+ 32 - This is the only case where the baseline and target values match
+ and yet we will still write to the working directory. In all other
+ cases, if baseline == target, we don't touch the workdir (it is
+ either already right or is "dirty"). However, since this case also
+ implies that a ?/B1/x case will exist as well, it can be skipped.
+
+Cases 3, 17, 24, 26, and 29 are all considered conflicts even though
+none of them will require making any updates to the working directory.
+
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index c36e2a4..12fffeb 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -9,8 +9,7 @@
#include "common.h"
#include "types.h"
-#include "indexer.h"
-#include "strarray.h"
+#include "diff.h"
/**
* @file git2/checkout.h
@@ -24,105 +23,113 @@ GIT_BEGIN_DECL
/**
* Checkout behavior flags
*
- * These flags control what checkout does with files. Pass in a
- * combination of these values OR'ed together. If you just pass zero
- * (i.e. no flags), then you are effectively doing a "dry run" where no
- * files will be modified.
- *
- * Checkout groups the working directory content into 3 classes of files:
- * (1) files that don't need a change, and files that do need a change
- * that either (2) we are allowed to modifed or (3) we are not. The flags
- * you pass in will decide which files we are allowed to modify.
- *
- * By default, checkout is not allowed to modify any files. Anything
- * needing a change would be considered a conflict.
- *
- * GIT_CHECKOUT_UPDATE_UNMODIFIED means that checkout is allowed to update
- * any file where the working directory content matches the HEAD
- * (e.g. either the files match or the file is absent in both places).
- *
- * GIT_CHECKOUT_UPDATE_MISSING means checkout can create a missing file
- * that exists in the index and does not exist in the working directory.
- * This is usually desirable for initial checkout, etc. Technically, the
- * missing file differs from the HEAD, which is why this is separate.
- *
- * GIT_CHECKOUT_UPDATE_MODIFIED means checkout is allowed to update files
- * where the working directory does not match the HEAD so long as the file
- * actually exists in the HEAD. This option implies UPDATE_UNMODIFIED.
- *
- * GIT_CHECKOUT_UPDATE_UNTRACKED means checkout is allowed to update files
- * even if there is a working directory version that does not exist in the
- * HEAD (i.e. the file was independently created in the workdir). This
- * implies UPDATE_UNMODIFIED | UPDATE_MISSING (but *not* UPDATE_MODIFIED).
- *
- *
- * On top of these three basic strategies, there are some modifiers
- * options that can be applied:
- *
- * If any files need update but are disallowed by the strategy, normally
- * checkout calls the conflict callback (if given) and then aborts.
- * GIT_CHECKOUT_ALLOW_CONFLICTS means it is okay to update the files that
- * are allowed by the strategy even if there are conflicts. The conflict
- * callbacks are still made, but non-conflicting files will be updated.
- *
- * Any unmerged entries in the index are automatically considered conflicts.
- * If you want to proceed anyhow and just skip unmerged entries, you can use
- * GIT_CHECKOUT_SKIP_UNMERGED which is less dangerous than just allowing all
- * conflicts. Alternatively, use GIT_CHECKOUT_USE_OURS to proceed and
- * checkout the stage 2 ("ours") version. GIT_CHECKOUT_USE_THEIRS means to
- * proceed and use the stage 3 ("theirs") version.
- *
- * GIT_CHECKOUT_UPDATE_ONLY means that update is not allowed to create new
- * files or delete old ones, only update existing content. With this
- * flag, files that needs to be created or deleted are not conflicts -
- * they are just skipped. This also skips typechanges to existing files
- * (because the old would have to be removed).
- *
- * GIT_CHECKOUT_REMOVE_UNTRACKED means that files in the working directory
- * that are untracked (and not ignored) will be removed altogether. These
- * untracked files (that do not shadow index entries) are not considered
- * conflicts and would normally be ignored.
- *
- *
- * Checkout is "semi-atomic" as in it will go through the work to be done
- * before making any changes and if may decide to abort if there are
- * conflicts, or you can use the conflict callback to explicitly abort the
- * action before any updates are made. Despite this, if a second process
- * is modifying the filesystem while checkout is running, it can't
- * guarantee that the choices is makes while initially examining the
- * filesystem are still going to be correct as it applies them.
+ * In libgit2, checkout is used to update the working directory and index
+ * to match a target tree. Unlike git checkout, it does not move the HEAD
+ * commit for you - use `git_repository_set_head` or the like to do that.
+ *
+ * Checkout looks at (up to) four things: the "target" tree you want to
+ * check out, the "baseline" tree of what was checked out previously, the
+ * working directory for actual files, and the index for staged changes.
+ *
+ * You give checkout one of four strategies for update:
+ *
+ * - `GIT_CHECKOUT_NONE` is a dry-run strategy that checks for conflicts,
+ * etc., but doesn't make any actual changes.
+ *
+ * - `GIT_CHECKOUT_FORCE` is at the opposite extreme, taking any action to
+ * make the working directory match the target (including potentially
+ * discarding modified files).
+ *
+ * In between those are `GIT_CHECKOUT_SAFE` and `GIT_CHECKOUT_SAFE_CREATE`
+ * both of which only make modifications that will not lose changes.
+ *
+ * | target == baseline | target != baseline |
+ * ---------------------|-----------------------|----------------------|
+ * workdir == baseline | no action | create, update, or |
+ * | | delete file |
+ * ---------------------|-----------------------|----------------------|
+ * workdir exists and | no action | conflict (notify |
+ * is != baseline | notify dirty MODIFIED | and cancel checkout) |
+ * ---------------------|-----------------------|----------------------|
+ * workdir missing, | create if SAFE_CREATE | create file |
+ * baseline present | notify dirty DELETED | |
+ * ---------------------|-----------------------|----------------------|
+ *
+ * The only difference between SAFE and SAFE_CREATE is that SAFE_CREATE
+ * will cause a file to be checked out if it is missing from the working
+ * directory even if it is not modified between the target and baseline.
+ *
+ *
+ * To emulate `git checkout`, use `GIT_CHECKOUT_SAFE` with a checkout
+ * notification callback (see below) that displays information about dirty
+ * files. The default behavior will cancel checkout on conflicts.
+ *
+ * To emulate `git checkout-index`, use `GIT_CHECKOUT_SAFE_CREATE` with a
+ * notification callback that cancels the operation if a dirty-but-existing
+ * file is found in the working directory. This core git command isn't
+ * quite "force" but is sensitive about some types of changes.
+ *
+ * To emulate `git checkout -f`, use `GIT_CHECKOUT_FORCE`.
+ *
+ * To emulate `git clone` use `GIT_CHECKOUT_SAFE_CREATE` in the options.
+ *
+ *
+ * There are some additional flags to modified the behavior of checkout:
+ *
+ * - GIT_CHECKOUT_ALLOW_CONFLICTS makes SAFE mode apply safe file updates
+ * even if there are conflicts (instead of cancelling the checkout).
+ *
+ * - GIT_CHECKOUT_REMOVE_UNTRACKED means remove untracked files (i.e. not
+ * in target, baseline, or index, and not ignored) from the working dir.
+ *
+ * - GIT_CHECKOUT_REMOVE_IGNORED means remove ignored files (that are also
+ * unrtacked) from the working directory as well.
+ *
+ * - GIT_CHECKOUT_UPDATE_ONLY means to only update the content of files that
+ * already exist. Files will not be created nor deleted. This just skips
+ * applying adds, deletes, and typechanges.
+ *
+ * - GIT_CHECKOUT_DONT_UPDATE_INDEX prevents checkout from writing the
+ * updated files' information to the index.
+ *
+ * - Normally, checkout will reload the index and git attributes from disk
+ * before any operations. GIT_CHECKOUT_NO_REFRESH prevents this reload.
+ *
+ * - Unmerged index entries are conflicts. GIT_CHECKOUT_SKIP_UNMERGED skips
+ * files with unmerged index entries instead. GIT_CHECKOUT_USE_OURS and
+ * GIT_CHECKOUT_USE_THEIRS to proceeed with the checkout using either the
+ * stage 2 ("ours") or stage 3 ("theirs") version of files in the index.
*/
typedef enum {
- GIT_CHECKOUT_DEFAULT = 0, /** default is a dry run, no actual updates */
-
- /** Allow update of entries where working dir matches HEAD. */
- GIT_CHECKOUT_UPDATE_UNMODIFIED = (1u << 0),
-
- /** Allow update of entries where working dir does not have file. */
- GIT_CHECKOUT_UPDATE_MISSING = (1u << 1),
+ GIT_CHECKOUT_NONE = 0, /** default is a dry run, no actual updates */
/** Allow safe updates that cannot overwrite uncommited data */
- GIT_CHECKOUT_SAFE =
- (GIT_CHECKOUT_UPDATE_UNMODIFIED | GIT_CHECKOUT_UPDATE_MISSING),
-
- /** Allow update of entries in working dir that are modified from HEAD. */
- GIT_CHECKOUT_UPDATE_MODIFIED = (1u << 2),
+ GIT_CHECKOUT_SAFE = (1u << 0),
- /** Update existing untracked files that are now present in the index. */
- GIT_CHECKOUT_UPDATE_UNTRACKED = (1u << 3),
+ /** Allow safe updates plus creation of missing files */
+ GIT_CHECKOUT_SAFE_CREATE = (1u << 1),
/** Allow all updates to force working directory to look like index */
- GIT_CHECKOUT_FORCE =
- (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_MODIFIED | GIT_CHECKOUT_UPDATE_UNTRACKED),
+ GIT_CHECKOUT_FORCE = (1u << 2),
+
- /** Allow checkout to make updates even if conflicts are found */
+ /** Allow checkout to make safe updates even if conflicts are found */
GIT_CHECKOUT_ALLOW_CONFLICTS = (1u << 4),
/** Remove untracked files not in index (that are not ignored) */
GIT_CHECKOUT_REMOVE_UNTRACKED = (1u << 5),
+ /** Remove ignored files not in index */
+ GIT_CHECKOUT_REMOVE_IGNORED = (1u << 6),
+
/** Only update existing files, don't create new ones */
- GIT_CHECKOUT_UPDATE_ONLY = (1u << 6),
+ GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
+
+ /** Normally checkout updates index entries as it goes; this stops that */
+ GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8),
+
+ /** Don't refresh index/config/etc before doing checkout */
+ GIT_CHECKOUT_NO_REFRESH = (1u << 9),
/**
* THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
@@ -143,55 +150,91 @@ typedef enum {
} git_checkout_strategy_t;
/**
+ * Checkout notification flags
+ *
+ * Checkout will invoke an options notification callback (`notify_cb`) for
+ * certain cases - you pick which ones via `notify_flags`:
+ *
+ * - GIT_CHECKOUT_NOTIFY_CONFLICT invokes checkout on conflicting paths.
+ *
+ * - GIT_CHECKOUT_NOTIFY_DIRTY notifies about "dirty" files, i.e. those that
+ * do not need an update but no longer match the baseline. Core git
+ * displays these files when checkout runs, but won't stop the checkout.
+ *
+ * - GIT_CHECKOUT_NOTIFY_UPDATED sends notification for any file changed.
+ *
+ * - GIT_CHECKOUT_NOTIFY_UNTRACKED notifies about untracked files.
+ *
+ * - GIT_CHECKOUT_NOTIFY_IGNORED notifies about ignored files.
+ *
+ * Returning a non-zero value from this callback will cancel the checkout.
+ * Notification callbacks are made prior to modifying any files on disk.
+ */
+typedef enum {
+ GIT_CHECKOUT_NOTIFY_NONE = 0,
+ GIT_CHECKOUT_NOTIFY_CONFLICT = (1u << 0),
+ GIT_CHECKOUT_NOTIFY_DIRTY = (1u << 1),
+ GIT_CHECKOUT_NOTIFY_UPDATED = (1u << 2),
+ GIT_CHECKOUT_NOTIFY_UNTRACKED = (1u << 3),
+ GIT_CHECKOUT_NOTIFY_IGNORED = (1u << 4),
+} git_checkout_notify_t;
+
+/** Checkout notification callback function */
+typedef int (*git_checkout_notify_cb)(
+ git_checkout_notify_t why,
+ const char *path,
+ const git_diff_file *baseline,
+ const git_diff_file *target,
+ const git_diff_file *workdir,
+ void *payload);
+
+/** Checkout progress notification function */
+typedef void (*git_checkout_progress_cb)(
+ const char *path,
+ size_t completed_steps,
+ size_t total_steps,
+ void *payload);
+
+/**
* Checkout options structure
*
- * Use zeros to indicate default settings.
- * This needs to be initialized with the `GIT_CHECKOUT_OPTS_INIT` macro:
+ * Zero out for defaults. Initialize with `GIT_CHECKOUT_OPTS_INIT` macro to
+ * correctly set the `version` field. E.g.
*
* git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
*/
typedef struct git_checkout_opts {
unsigned int version;
+
unsigned int checkout_strategy; /** default will be a dry run */
- int disable_filters; /** don't apply filters like CRLF conversion */
- int dir_mode; /** default is 0755 */
- int file_mode; /** default is 0644 or 0755 as dictated by blob */
- int file_open_flags; /** default is O_CREAT | O_TRUNC | O_WRONLY */
+ int disable_filters; /** don't apply filters like CRLF conversion */
+ unsigned int dir_mode; /** default is 0755 */
+ unsigned int file_mode; /** default is 0644 or 0755 as dictated by blob */
+ int file_open_flags; /** default is O_CREAT | O_TRUNC | O_WRONLY */
- /** Optional callback made on files where the index differs from the
- * working directory but the rules do not allow update. Return a
- * non-zero value to abort the checkout. All such callbacks will be
- * made before any changes are made to the working directory.
- */
- int (*conflict_cb)(
- const char *conflicting_path,
- const git_oid *index_oid,
- unsigned int index_mode,
- unsigned int wd_mode,
- void *payload);
- void *conflict_payload;
+ unsigned int notify_flags; /** see `git_checkout_notify_t` above */
+ git_checkout_notify_cb notify_cb;
+ void *notify_payload;
/* Optional callback to notify the consumer of checkout progress. */
- void (*progress_cb)(
- const char *path,
- size_t completed_steps,
- size_t total_steps,
- void *payload);
+ git_checkout_progress_cb progress_cb;
void *progress_payload;
/** When not zeroed out, array of fnmatch patterns specifying which
* paths should be taken into account, otherwise all files.
*/
git_strarray paths;
+
+ git_tree *baseline; /** expected content of workdir, defaults to HEAD */
} git_checkout_opts;
#define GIT_CHECKOUT_OPTS_VERSION 1
#define GIT_CHECKOUT_OPTS_INIT {GIT_CHECKOUT_OPTS_VERSION}
/**
- * Updates files in the index and the working tree to match the content of the
- * commit pointed at by HEAD.
+ * Updates files in the index and the working tree to match the content of
+ * the commit pointed at by HEAD.
*
* @param repo repository to check out (must be non-bare)
* @param opts specifies checkout options (may be NULL)
diff --git a/include/git2/index.h b/include/git2/index.h
index fa9a197..1d21403 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -313,6 +313,17 @@ GIT_EXTERN(const git_index_entry *) git_index_get_bypath(
GIT_EXTERN(int) git_index_remove(git_index *index, const char *path, int stage);
/**
+ * Remove all entries from the index under a given directory
+ *
+ * @param index an existing index object
+ * @param dir container directory path
+ * @param stage stage to search
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_index_remove_directory(
+ git_index *index, const char *dir, int stage);
+
+/**
* Add or update an index entry from an in-memory struct
*
* If a previous index entry exists that has the same path and stage
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 90e45ce..444b3a9 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -504,6 +504,24 @@ GIT_EXTERN(int) git_submodule_status(
unsigned int *status,
git_submodule *submodule);
+/**
+ * Get the locations of submodule information.
+ *
+ * This is a bit like a very lightweight version of `git_submodule_status`.
+ * It just returns a made of the first four submodule status values (i.e.
+ * the ones like GIT_SUBMODULE_STATUS_IN_HEAD, etc) that tell you where the
+ * submodule data comes from (i.e. the HEAD commit, gitmodules file, etc.).
+ * This can be useful if you want to know if the submodule is present in the
+ * working directory at this point in time, etc.
+ *
+ * @param status Combination of first four `GIT_SUBMODULE_STATUS` flags
+ * @param submodule Submodule for which to get status
+ * @return 0 on success, <0 on error
+ */
+GIT_EXTERN(int) git_submodule_location(
+ unsigned int *location_status,
+ git_submodule *submodule);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/attr.c b/src/attr.c
index 95d63be..1b41441 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -572,8 +572,10 @@ static int collect_attr_files(
error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
if (!error)
error = push_attr_file(repo, files, NULL, dir.ptr);
- else if (error == GIT_ENOTFOUND)
+ else if (error == GIT_ENOTFOUND) {
+ giterr_clear();
error = 0;
+ }
}
cleanup:
diff --git a/src/checkout.c b/src/checkout.c
index 66eb698..cf0a8b8 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -7,36 +7,636 @@
#include <assert.h>
-#include "git2/checkout.h"
+#include "checkout.h"
+
#include "git2/repository.h"
#include "git2/refs.h"
#include "git2/tree.h"
#include "git2/blob.h"
#include "git2/config.h"
#include "git2/diff.h"
+#include "git2/submodule.h"
-#include "common.h"
#include "refs.h"
-#include "buffer.h"
#include "repository.h"
#include "filter.h"
#include "blob.h"
#include "diff.h"
#include "pathspec.h"
+/* See docs/checkout-internals.md for more information */
+
+enum {
+ CHECKOUT_ACTION__NONE = 0,
+ CHECKOUT_ACTION__REMOVE = 1,
+ CHECKOUT_ACTION__UPDATE_BLOB = 2,
+ CHECKOUT_ACTION__UPDATE_SUBMODULE = 4,
+ CHECKOUT_ACTION__CONFLICT = 8,
+ CHECKOUT_ACTION__MAX = 8,
+ CHECKOUT_ACTION__DEFER_REMOVE = 16,
+ CHECKOUT_ACTION__REMOVE_AND_UPDATE =
+ (CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE),
+};
+
typedef struct {
git_repository *repo;
git_diff_list *diff;
- git_checkout_opts *opts;
- git_buf *path;
+ git_checkout_opts opts;
+ bool opts_free_baseline;
+ char *pfx;
+ git_index *index;
+ git_pool pool;
+ git_vector removes;
+ git_buf path;
size_t workdir_len;
- bool can_symlink;
- int error;
+ unsigned int strategy;
+ int can_symlink;
+ bool reload_submodules;
size_t total_steps;
size_t completed_steps;
-} checkout_diff_data;
+} checkout_data;
+
+static int checkout_notify(
+ checkout_data *data,
+ git_checkout_notify_t why,
+ const git_diff_delta *delta,
+ const git_index_entry *wditem)
+{
+ git_diff_file wdfile;
+ const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL;
+
+ if (!data->opts.notify_cb)
+ return 0;
+
+ if ((why & data->opts.notify_flags) == 0)
+ return 0;
+
+ if (wditem) {
+ memset(&wdfile, 0, sizeof(wdfile));
+
+ git_oid_cpy(&wdfile.oid, &wditem->oid);
+ wdfile.path = wditem->path;
+ wdfile.size = wditem->file_size;
+ wdfile.flags = GIT_DIFF_FILE_VALID_OID;
+ wdfile.mode = wditem->mode;
+
+ workdir = &wdfile;
+ }
+
+ if (delta) {
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED:
+ case GIT_DELTA_MODIFIED:
+ case GIT_DELTA_TYPECHANGE:
+ default:
+ baseline = &delta->old_file;
+ target = &delta->new_file;
+ break;
+ case GIT_DELTA_ADDED:
+ case GIT_DELTA_IGNORED:
+ case GIT_DELTA_UNTRACKED:
+ target = &delta->new_file;
+ break;
+ case GIT_DELTA_DELETED:
+ baseline = &delta->old_file;
+ break;
+ }
+ }
+
+ return data->opts.notify_cb(
+ why, delta ? delta->old_file.path : wditem->path,
+ baseline, target, workdir, data->opts.notify_payload);
+}
+
+static bool checkout_is_workdir_modified(
+ checkout_data *data,
+ const git_diff_file *baseitem,
+ const git_index_entry *wditem)
+{
+ git_oid oid;
+
+ /* handle "modified" submodule */
+ if (wditem->mode == GIT_FILEMODE_COMMIT) {
+ git_submodule *sm;
+ unsigned int sm_status = 0;
+ const git_oid *sm_oid = NULL;
+
+ if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0 ||
+ git_submodule_status(&sm_status, sm) < 0)
+ return true;
+
+ if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
+ return true;
+
+ sm_oid = git_submodule_wd_id(sm);
+ if (!sm_oid)
+ return false;
+
+ return (git_oid_cmp(&baseitem->oid, sm_oid) != 0);
+ }
+
+ /* depending on where base is coming from, we may or may not know
+ * the actual size of the data, so we can't rely on this shortcut.
+ */
+ if (baseitem->size && wditem->file_size != baseitem->size)
+ return true;
+
+ if (git_diff__oid_for_file(
+ data->repo, wditem->path, wditem->mode,
+ wditem->file_size, &oid) < 0)
+ return false;
+
+ return (git_oid_cmp(&baseitem->oid, &oid) != 0);
+}
+
+#define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
+ ((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
+
+static int checkout_action_common(
+ checkout_data *data,
+ int action,
+ const git_diff_delta *delta,
+ const git_index_entry *wd)
+{
+ git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
+
+ if (action <= 0)
+ return action;
+
+ if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
+ action = (action & ~CHECKOUT_ACTION__REMOVE);
+
+ if ((action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
+ if (S_ISGITLINK(delta->new_file.mode))
+ action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
+ CHECKOUT_ACTION__UPDATE_SUBMODULE;
+
+ notify = GIT_CHECKOUT_NOTIFY_UPDATED;
+ }
+
+ if ((action & CHECKOUT_ACTION__CONFLICT) != 0)
+ notify = GIT_CHECKOUT_NOTIFY_CONFLICT;
+
+ if (notify != GIT_CHECKOUT_NOTIFY_NONE &&
+ checkout_notify(data, notify, delta, wd) != 0)
+ return GIT_EUSER;
+
+ return action;
+}
+
+static int checkout_action_no_wd(
+ checkout_data *data,
+ const git_diff_delta *delta)
+{
+ int action = CHECKOUT_ACTION__NONE;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED: /* case 12 */
+ if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL))
+ return GIT_EUSER;
+ action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
+ break;
+ case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */
+ case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ break;
+ case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
+ if (delta->new_file.mode == GIT_FILEMODE_TREE)
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ break;
+ case GIT_DELTA_DELETED: /* case 8 or 25 */
+ default: /* impossible */
+ break;
+ }
+
+ return checkout_action_common(data, action, delta, NULL);
+}
+
+static int checkout_action_wd_only(
+ checkout_data *data,
+ git_iterator *workdir,
+ const git_index_entry *wd,
+ git_vector *pathspec)
+{
+ bool remove = false;
+ git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
+
+ if (!git_pathspec_match_path(
+ pathspec, wd->path, false, workdir->ignore_case))
+ return 0;
+
+ /* check if item is tracked in the index but not in the checkout diff */
+ if (data->index != NULL) {
+ if (wd->mode != GIT_FILEMODE_TREE) {
+ if (git_index_get_bypath(data->index, wd->path, 0) != NULL) {
+ notify = GIT_CHECKOUT_NOTIFY_DIRTY;
+ remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
+ }
+ } else {
+ /* for tree entries, we have to see if there are any index
+ * entries that are contained inside that tree
+ */
+ size_t pos = git_index__prefix_position(data->index, wd->path);
+ const git_index_entry *e = git_index_get_byindex(data->index, pos);
+
+ if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
+ notify = GIT_CHECKOUT_NOTIFY_DIRTY;
+ remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
+ }
+ }
+ }
+
+ if (notify != GIT_CHECKOUT_NOTIFY_NONE)
+ /* found in index */;
+ else if (git_iterator_current_is_ignored(workdir)) {
+ notify = GIT_CHECKOUT_NOTIFY_IGNORED;
+ remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0);
+ }
+ else {
+ notify = GIT_CHECKOUT_NOTIFY_UNTRACKED;
+ remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
+ }
+
+ if (checkout_notify(data, notify, NULL, wd))
+ return GIT_EUSER;
+
+ if (remove) {
+ char *path = git_pool_strdup(&data->pool, wd->path);
+ GITERR_CHECK_ALLOC(path);
+
+ if (git_vector_insert(&data->removes, path) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+static bool submodule_is_config_only(
+ checkout_data *data,
+ const char *path)
+{
+ git_submodule *sm = NULL;
+ unsigned int sm_loc = 0;
+
+ if (git_submodule_lookup(&sm, data->repo, path) < 0 ||
+ git_submodule_location(&sm_loc, sm) < 0 ||
+ sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG)
+ return true;
+
+ return false;
+}
+
+static int checkout_action_with_wd(
+ checkout_data *data,
+ const git_diff_delta *delta,
+ const git_index_entry *wd)
+{
+ int action = CHECKOUT_ACTION__NONE;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */
+ if (checkout_is_workdir_modified(data, &delta->old_file, wd)) {
+ if (checkout_notify(
+ data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
+ return GIT_EUSER;
+ action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
+ }
+ break;
+ case GIT_DELTA_ADDED: /* case 3, 4 or 6 */
+ action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+ break;
+ case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */
+ if (checkout_is_workdir_modified(data, &delta->old_file, wd))
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ else
+ action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
+ break;
+ case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */
+ if (checkout_is_workdir_modified(data, &delta->old_file, wd))
+ action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
+ else
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ break;
+ case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */
+ if (delta->old_file.mode == GIT_FILEMODE_TREE) {
+ if (wd->mode == GIT_FILEMODE_TREE)
+ /* either deleting items in old tree will delete the wd dir,
+ * or we'll get a conflict when we attempt blob update...
+ */
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ else if (wd->mode == GIT_FILEMODE_COMMIT) {
+ /* workdir is possibly a "phantom" submodule - treat as a
+ * tree if the only submodule info came from the config
+ */
+ if (submodule_is_config_only(data, wd->path))
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ else
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ } else
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ }
+ else if (checkout_is_workdir_modified(data, &delta->old_file, wd))
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ else
+ action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
+
+ /* don't update if the typechange is to a tree */
+ if (delta->new_file.mode == GIT_FILEMODE_TREE)
+ action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB);
+ break;
+ default: /* impossible */
+ break;
+ }
+
+ return checkout_action_common(data, action, delta, wd);
+}
+
+static int checkout_action_with_wd_blocker(
+ checkout_data *data,
+ const git_diff_delta *delta,
+ const git_index_entry *wd)
+{
+ int action = CHECKOUT_ACTION__NONE;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED:
+ /* should show delta as dirty / deleted */
+ if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
+ return GIT_EUSER;
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
+ break;
+ case GIT_DELTA_ADDED:
+ case GIT_DELTA_MODIFIED:
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ break;
+ case GIT_DELTA_DELETED:
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
+ break;
+ case GIT_DELTA_TYPECHANGE:
+ /* not 100% certain about this... */
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ break;
+ default: /* impossible */
+ break;
+ }
+
+ return checkout_action_common(data, action, delta, wd);
+}
+
+static int checkout_action_with_wd_dir(
+ checkout_data *data,
+ const git_diff_delta *delta,
+ const git_index_entry *wd)
+{
+ int action = CHECKOUT_ACTION__NONE;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */
+ if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL) ||
+ checkout_notify(
+ data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
+ return GIT_EUSER;
+ break;
+ case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
+ case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
+ if (delta->old_file.mode == GIT_FILEMODE_COMMIT)
+ /* expected submodule (and maybe found one) */;
+ else if (delta->new_file.mode != GIT_FILEMODE_TREE)
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ break;
+ case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */
+ if (delta->old_file.mode != GIT_FILEMODE_TREE &&
+ checkout_notify(
+ data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
+ return GIT_EUSER;
+ break;
+ case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
+ if (delta->old_file.mode == GIT_FILEMODE_TREE) {
+ /* For typechange from dir, remove dir and add blob, but it is
+ * not safe to remove dir if it contains modified files.
+ * However, safely removing child files will remove the parent
+ * directory if is it left empty, so we can defer removing the
+ * dir and it will succeed if no children are left.
+ */
+ action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
+ if (action != CHECKOUT_ACTION__NONE)
+ action |= CHECKOUT_ACTION__DEFER_REMOVE;
+ }
+ else if (delta->new_file.mode != GIT_FILEMODE_TREE)
+ /* For typechange to dir, dir is already created so no action */
+ action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
+ break;
+ default: /* impossible */
+ break;
+ }
+
+ return checkout_action_common(data, action, delta, wd);
+}
+
+static int checkout_action(
+ checkout_data *data,
+ git_diff_delta *delta,
+ git_iterator *workdir,
+ const git_index_entry **wditem_ptr,
+ git_vector *pathspec)
+{
+ const git_index_entry *wd = *wditem_ptr;
+ int cmp = -1, act;
+ int (*strcomp)(const char *, const char *) = data->diff->strcomp;
+ int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
+
+ /* move workdir iterator to follow along with deltas */
+
+ while (1) {
+ if (!wd)
+ return checkout_action_no_wd(data, delta);
+
+ cmp = strcomp(wd->path, delta->old_file.path);
+
+ /* 1. wd before delta ("a/a" before "a/b")
+ * 2. wd prefixes delta & should expand ("a/" before "a/b")
+ * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c")
+ * 4. wd equals delta ("a/b" and "a/b")
+ * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b")
+ * 6. wd after delta ("a/c" after "a/b")
+ */
+
+ if (cmp < 0) {
+ cmp = pfxcomp(delta->old_file.path, wd->path);
+
+ if (cmp == 0) {
+ if (wd->mode == GIT_FILEMODE_TREE) {
+ /* case 2 - entry prefixed by workdir tree */
+ if (git_iterator_advance_into_directory(workdir, &wd) < 0)
+ goto fail;
+ continue;
+ }
+
+ /* case 3 maybe - wd contains non-dir where dir expected */
+ if (delta->old_file.path[strlen(wd->path)] == '/') {
+ act = checkout_action_with_wd_blocker(data, delta, wd);
+ *wditem_ptr =
+ git_iterator_advance(workdir, &wd) ? NULL : wd;
+ return act;
+ }
+ }
+
+ /* case 1 - handle wd item (if it matches pathspec) */
+ if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 ||
+ git_iterator_advance(workdir, &wd) < 0)
+ goto fail;
+
+ *wditem_ptr = wd;
+ continue;
+ }
+
+ if (cmp == 0) {
+ /* case 4 */
+ act = checkout_action_with_wd(data, delta, wd);
+ *wditem_ptr = git_iterator_advance(workdir, &wd) ? NULL : wd;
+ return act;
+ }
+
+ cmp = pfxcomp(wd->path, delta->old_file.path);
+
+ if (cmp == 0) { /* case 5 */
+ if (wd->path[strlen(delta->old_file.path)] != '/')
+ return checkout_action_no_wd(data, delta);
+
+ if (delta->status == GIT_DELTA_TYPECHANGE) {
+ if (delta->old_file.mode == GIT_FILEMODE_TREE) {
+ act = checkout_action_with_wd(data, delta, wd);
+ if (git_iterator_advance_into_directory(workdir, &wd) < 0)
+ wd = NULL;
+ *wditem_ptr = wd;
+ return act;
+ }
+
+ if (delta->new_file.mode == GIT_FILEMODE_TREE ||
+ delta->new_file.mode == GIT_FILEMODE_COMMIT ||
+ delta->old_file.mode == GIT_FILEMODE_COMMIT)
+ {
+ act = checkout_action_with_wd(data, delta, wd);
+ if (git_iterator_advance(workdir, &wd) < 0)
+ wd = NULL;
+ *wditem_ptr = wd;
+ return act;
+ }
+ }
+
+ return checkout_action_with_wd_dir(data, delta, wd);
+ }
+
+ /* case 6 - wd is after delta */
+ return checkout_action_no_wd(data, delta);
+ }
+
+fail:
+ *wditem_ptr = NULL;
+ return -1;
+}
+
+static int checkout_remaining_wd_items(
+ checkout_data *data,
+ git_iterator *workdir,
+ const git_index_entry *wd,
+ git_vector *spec)
+{
+ int error = 0;
+
+ while (wd && !error) {
+ if (!(error = checkout_action_wd_only(data, workdir, wd, spec)))
+ error = git_iterator_advance(workdir, &wd);
+ }
+
+ return error;
+}
+
+static int checkout_get_actions(
+ uint32_t **actions_ptr,
+ size_t **counts_ptr,
+ checkout_data *data,
+ git_iterator *workdir)
+{
+ int error = 0;
+ const git_index_entry *wditem;
+ git_vector pathspec = GIT_VECTOR_INIT, *deltas;
+ git_pool pathpool = GIT_POOL_INIT_STRINGPOOL;
+ git_diff_delta *delta;
+ size_t i, *counts = NULL;
+ uint32_t *actions = NULL;
+
+ if (data->opts.paths.count > 0 &&
+ git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0)
+ return -1;
+
+ if ((error = git_iterator_current(workdir, &wditem)) < 0)
+ goto fail;
+
+ deltas = &data->diff->deltas;
+
+ *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t));
+ *actions_ptr = actions = git__calloc(
+ deltas->length ? deltas->length : 1, sizeof(uint32_t));
+ if (!counts || !actions) {
+ error = -1;
+ goto fail;
+ }
+
+ git_vector_foreach(deltas, i, delta) {
+ int act = checkout_action(data, delta, workdir, &wditem, &pathspec);
+
+ if (act < 0) {
+ error = act;
+ goto fail;
+ }
+
+ actions[i] = act;
+
+ if (act & CHECKOUT_ACTION__REMOVE)
+ counts[CHECKOUT_ACTION__REMOVE]++;
+ if (act & CHECKOUT_ACTION__UPDATE_BLOB)
+ counts[CHECKOUT_ACTION__UPDATE_BLOB]++;
+ if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE)
+ counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++;
+ if (act & CHECKOUT_ACTION__CONFLICT)
+ counts[CHECKOUT_ACTION__CONFLICT]++;
+ }
+
+ error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
+ if (error < 0)
+ goto fail;
+
+ counts[CHECKOUT_ACTION__REMOVE] += data->removes.length;
+
+ if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
+ (data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
+ {
+ giterr_set(GITERR_CHECKOUT, "%d conflicts prevent checkout",
+ (int)counts[CHECKOUT_ACTION__CONFLICT]);
+ error = -1;
+ goto fail;
+ }
+
+ git_pathspec_free(&pathspec);
+ git_pool_clear(&pathpool);
+
+ return 0;
+
+fail:
+ *counts_ptr = NULL;
+ git__free(counts);
+ *actions_ptr = NULL;
+ git__free(actions);
+
+ git_pathspec_free(&pathspec);
+ git_pool_clear(&pathpool);
+
+ return error;
+}
static int buffer_to_file(
+ struct stat *st,
git_buf *buffer,
const char *path,
mode_t dir_mode,
@@ -57,6 +657,9 @@ static int buffer_to_file(
giterr_set(GITERR_OS, "Could not write to '%s'", path);
(void)p_close(fd);
} else {
+ if ((error = p_fstat(fd, st)) < 0)
+ giterr_set(GITERR_OS, "Error while statting '%s'", path);
+
if ((error = p_close(fd)) < 0)
giterr_set(GITERR_OS, "Error while closing '%s'", path);
}
@@ -70,6 +673,7 @@ static int buffer_to_file(
}
static int blob_content_to_file(
+ struct stat *st,
git_blob *blob,
const char *path,
mode_t entry_filemode,
@@ -112,7 +716,12 @@ static int blob_content_to_file(
file_mode = entry_filemode;
error = buffer_to_file(
- &filtered, path, opts->dir_mode, opts->file_open_flags, file_mode);
+ st, &filtered, path, opts->dir_mode, opts->file_open_flags, file_mode);
+
+ if (!error) {
+ st->st_size = blob->odb_object->raw.len;
+ st->st_mode = entry_filemode;
+ }
cleanup:
git_filters_free(&filters);
@@ -124,7 +733,7 @@ cleanup:
}
static int blob_content_to_link(
- git_blob *blob, const char *path, bool can_symlink)
+ struct stat *st, git_blob *blob, const char *path, int can_symlink)
{
git_buf linktarget = GIT_BUF_INIT;
int error;
@@ -132,448 +741,275 @@ static int blob_content_to_link(
if ((error = git_blob__getbuf(&linktarget, blob)) < 0)
return error;
- if (can_symlink)
- error = p_symlink(git_buf_cstr(&linktarget), path);
- else
+ if (can_symlink) {
+ if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0)
+ giterr_set(GITERR_CHECKOUT, "Could not create symlink %s\n", path);
+ } else {
error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
-
- git_buf_free(&linktarget);
-
- return error;
-}
-
-static int checkout_submodule(
- checkout_diff_data *data,
- const git_diff_file *file)
-{
- /* Until submodules are supported, UPDATE_ONLY means do nothing here */
- if ((data->opts->checkout_strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
- return 0;
-
- if (git_futils_mkdir(
- file->path, git_repository_workdir(data->repo),
- data->opts->dir_mode, GIT_MKDIR_PATH) < 0)
- return -1;
-
- /* TODO: Support checkout_strategy options. Two circumstances:
- * 1 - submodule already checked out, but we need to move the HEAD
- * to the new OID, or
- * 2 - submodule not checked out and we should recursively check it out
- *
- * Checkout will not execute a pull on the submodule, but a clone
- * command should probably be able to. Do we need a submodule callback?
- */
-
- return 0;
-}
-
-static void report_progress(
- checkout_diff_data *data,
- const char *path)
-{
- if (data->opts->progress_cb)
- data->opts->progress_cb(
- path, data->completed_steps, data->total_steps,
- data->opts->progress_payload);
-}
-
-static int checkout_blob(
- checkout_diff_data *data,
- const git_diff_file *file)
-{
- int error = 0;
- git_blob *blob;
-
- git_buf_truncate(data->path, data->workdir_len);
- if (git_buf_puts(data->path, file->path) < 0)
- return -1;
-
- if ((error = git_blob_lookup(&blob, data->repo, &file->oid)) < 0)
- return error;
-
- if (S_ISLNK(file->mode))
- error = blob_content_to_link(
- blob, git_buf_cstr(data->path), data->can_symlink);
- else
- error = blob_content_to_file(
- blob, git_buf_cstr(data->path), file->mode, data->opts);
-
- git_blob_free(blob);
-
- return error;
-}
-
-static int retrieve_symlink_caps(git_repository *repo, bool *out)
-{
- git_config *cfg;
- int can_symlink = 0;
- int error;
-
- if (git_repository_config__weakptr(&cfg, repo) < 0)
- return -1;
-
- error = git_config_get_bool(&can_symlink, cfg, "core.symlinks");
-
- /* If "core.symlinks" is not found anywhere, default to true. */
- if (error == GIT_ENOTFOUND) {
- can_symlink = true;
- error = 0;
- }
-
- if (error >= 0)
- *out = can_symlink;
-
- return error;
-}
-
-static void normalize_options(
- git_checkout_opts *normalized, git_checkout_opts *proposed)
-{
- assert(normalized);
-
- if (!proposed)
- GIT_INIT_STRUCTURE(normalized, GIT_CHECKOUT_OPTS_VERSION);
- else
- memmove(normalized, proposed, sizeof(git_checkout_opts));
-
- /* implied checkout strategies */
- if ((normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_MODIFIED) != 0 ||
- (normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0)
- normalized->checkout_strategy |= GIT_CHECKOUT_UPDATE_UNMODIFIED;
-
- if ((normalized->checkout_strategy & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0)
- normalized->checkout_strategy |= GIT_CHECKOUT_UPDATE_MISSING;
-
- /* opts->disable_filters is false by default */
-
- if (!normalized->dir_mode)
- normalized->dir_mode = GIT_DIR_MODE;
-
- if (!normalized->file_open_flags)
- normalized->file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
-}
-
-enum {
- CHECKOUT_ACTION__NONE = 0,
- CHECKOUT_ACTION__REMOVE = 1,
- CHECKOUT_ACTION__UPDATE_BLOB = 2,
- CHECKOUT_ACTION__UPDATE_SUBMODULE = 4,
- CHECKOUT_ACTION__CONFLICT = 8,
- CHECKOUT_ACTION__MAX = 8
-};
-
-static int checkout_confirm_update_blob(
- checkout_diff_data *data,
- const git_diff_delta *delta,
- int action)
-{
- int error;
- unsigned int strat = data->opts->checkout_strategy;
- struct stat st;
- bool update_only = ((strat & GIT_CHECKOUT_UPDATE_ONLY) != 0);
-
- /* for typechange, remove the old item first */
- if (delta->status == GIT_DELTA_TYPECHANGE) {
- if (update_only)
- action = CHECKOUT_ACTION__NONE;
- else
- action |= CHECKOUT_ACTION__REMOVE;
-
- return action;
- }
-
- git_buf_truncate(data->path, data->workdir_len);
- if (git_buf_puts(data->path, delta->new_file.path) < 0)
- return -1;
-
- if ((error = p_lstat_posixly(git_buf_cstr(data->path), &st)) < 0) {
- if (errno == ENOENT) {
- if (update_only)
- action = CHECKOUT_ACTION__NONE;
- } else if (errno == ENOTDIR) {
- /* File exists where a parent dir needs to go - i.e. untracked
- * typechange. Ignore if UPDATE_ONLY, remove if allowed.
- */
- if (update_only)
- action = CHECKOUT_ACTION__NONE;
- else if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0)
- action |= CHECKOUT_ACTION__REMOVE;
- else
- action = CHECKOUT_ACTION__CONFLICT;
- }
- /* otherwise let error happen when we attempt blob checkout later */
- }
- else if (S_ISDIR(st.st_mode)) {
- /* Directory exists where a blob needs to go - i.e. untracked
- * typechange. Ignore if UPDATE_ONLY, remove if allowed.
- */
- if (update_only)
- action = CHECKOUT_ACTION__NONE;
- else if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) != 0)
- action |= CHECKOUT_ACTION__REMOVE;
- else
- action = CHECKOUT_ACTION__CONFLICT;
}
- return action;
-}
-
-static int checkout_action_for_delta(
- checkout_diff_data *data,
- const git_diff_delta *delta,
- const git_index_entry *head_entry)
-{
- int action = CHECKOUT_ACTION__NONE;
- unsigned int strat = data->opts->checkout_strategy;
+ if (!error) {
+ if ((error = p_lstat(path, st)) < 0)
+ giterr_set(GITERR_CHECKOUT, "Could not stat symlink %s", path);
- switch (delta->status) {
- case GIT_DELTA_UNMODIFIED:
- if (!head_entry) {
- /* file independently created in wd, even though not in HEAD */
- if ((strat & GIT_CHECKOUT_UPDATE_MISSING) == 0)
- action = CHECKOUT_ACTION__CONFLICT;
- }
- else if (!git_oid_equal(&head_entry->oid, &delta->old_file.oid)) {
- /* working directory was independently updated to match index */
- if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) == 0)
- action = CHECKOUT_ACTION__CONFLICT;
- }
- break;
-
- case GIT_DELTA_ADDED:
- /* Impossible. New files should be UNTRACKED or TYPECHANGE */
- action = CHECKOUT_ACTION__CONFLICT;
- break;
-
- case GIT_DELTA_DELETED:
- if (head_entry && /* working dir missing, but exists in HEAD */
- (strat & GIT_CHECKOUT_UPDATE_MISSING) == 0)
- action = CHECKOUT_ACTION__CONFLICT;
- else
- action = CHECKOUT_ACTION__UPDATE_BLOB;
- break;
-
- case GIT_DELTA_MODIFIED:
- case GIT_DELTA_TYPECHANGE:
- if (!head_entry) {
- /* working dir was independently updated & does not match index */
- if ((strat & GIT_CHECKOUT_UPDATE_UNTRACKED) == 0)
- action = CHECKOUT_ACTION__CONFLICT;
- else
- action = CHECKOUT_ACTION__UPDATE_BLOB;
- }
- else if (git_oid_equal(&head_entry->oid, &delta->new_file.oid))
- action = CHECKOUT_ACTION__UPDATE_BLOB;
- else if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) == 0)
- action = CHECKOUT_ACTION__CONFLICT;
- else
- action = CHECKOUT_ACTION__UPDATE_BLOB;
- break;
-
- case GIT_DELTA_UNTRACKED:
- if (!head_entry) {
- if ((strat & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0)
- action = CHECKOUT_ACTION__REMOVE;
- }
- else if ((strat & GIT_CHECKOUT_UPDATE_MODIFIED) != 0) {
- action = CHECKOUT_ACTION__REMOVE;
- } else if ((strat & GIT_CHECKOUT_UPDATE_UNMODIFIED) != 0) {
- git_oid wd_oid;
-
- /* if HEAD matches workdir, then remove, else conflict */
-
- if (git_oid_iszero(&delta->new_file.oid) &&
- git_diff__oid_for_file(
- data->repo, delta->new_file.path, delta->new_file.mode,
- delta->new_file.size, &wd_oid) < 0)
- action = -1;
- else if (git_oid_equal(&head_entry->oid, &wd_oid))
- action = CHECKOUT_ACTION__REMOVE;
- else
- action = CHECKOUT_ACTION__CONFLICT;
- } else {
- /* present in HEAD and workdir, but absent in index */
- action = CHECKOUT_ACTION__CONFLICT;
- }
- break;
-
- case GIT_DELTA_IGNORED:
- default:
- /* just skip these files */
- break;
- }
-
- if (action > 0 && (action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
- if (S_ISGITLINK(delta->old_file.mode))
- action = (action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
- CHECKOUT_ACTION__UPDATE_SUBMODULE;
-
- action = checkout_confirm_update_blob(data, delta, action);
- }
-
- if (action == CHECKOUT_ACTION__CONFLICT &&
- data->opts->conflict_cb != NULL &&
- data->opts->conflict_cb(
- delta->old_file.path, &delta->old_file.oid,
- delta->old_file.mode, delta->new_file.mode,
- data->opts->conflict_payload) != 0)
- {
- giterr_clear();
- action = GIT_EUSER;
+ st->st_mode = GIT_FILEMODE_LINK;
}
- if (action > 0 && (strat & GIT_CHECKOUT_UPDATE_ONLY) != 0)
- action = (action & ~CHECKOUT_ACTION__REMOVE);
+ git_buf_free(&linktarget);
- return action;
+ return error;
}
-static int checkout_get_actions(
- uint32_t **actions_ptr,
- size_t **counts_ptr,
- checkout_diff_data *data)
+static int checkout_update_index(
+ checkout_data *data,
+ const git_diff_file *file,
+ struct stat *st)
{
- int error;
- git_diff_list *diff = data->diff;
- git_diff_delta *delta;
- size_t i, *counts = NULL;
- uint32_t *actions = NULL;
- git_tree *head = NULL;
- git_iterator *hiter = NULL;
- char *pfx = git_pathspec_prefix(&data->opts->paths);
- const git_index_entry *he;
+ git_index_entry entry;
- /* if there is no HEAD, that's okay - we'll make an empty iterator */
- if (((error = git_repository_head_tree(&head, data->repo)) < 0) &&
- !(error == GIT_ENOTFOUND || error == GIT_EORPHANEDHEAD))
- return -1;
+ if (!data->index)
+ return 0;
- if ((error = git_iterator_for_tree_range(&hiter, head, pfx, pfx)) < 0)
- goto fail;
+ memset(&entry, 0, sizeof(entry));
+ entry.path = (char *)file->path; /* cast to prevent warning */
+ git_index_entry__init_from_stat(&entry, st);
+ git_oid_cpy(&entry.oid, &file->oid);
- if ((diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 &&
- (error = git_iterator_spoolandsort_push(hiter, true)) < 0)
- goto fail;
+ return git_index_add(data->index, &entry);
+}
- if ((error = git_iterator_current(hiter, &he)) < 0)
- goto fail;
+static int checkout_submodule(
+ checkout_data *data,
+ const git_diff_file *file)
+{
+ int error = 0;
+ git_submodule *sm;
+
+ /* Until submodules are supported, UPDATE_ONLY means do nothing here */
+ if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
+ return 0;
- git__free(pfx);
- pfx = NULL;
+ if ((error = git_futils_mkdir(
+ file->path, git_repository_workdir(data->repo),
+ data->opts.dir_mode, GIT_MKDIR_PATH)) < 0)
+ return error;
- *counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t));
- *actions_ptr = actions = git__calloc(diff->deltas.length, sizeof(uint32_t));
- if (!counts || !actions) {
- error = -1;
- goto fail;
- }
+ if ((error = git_submodule_lookup(&sm, data->repo, file->path)) < 0)
+ return error;
- git_vector_foreach(&diff->deltas, i, delta) {
- int cmp = -1, act;
+ /* TODO: Support checkout_strategy options. Two circumstances:
+ * 1 - submodule already checked out, but we need to move the HEAD
+ * to the new OID, or
+ * 2 - submodule not checked out and we should recursively check it out
+ *
+ * Checkout will not execute a pull on the submodule, but a clone
+ * command should probably be able to. Do we need a submodule callback?
+ */
- /* try to track HEAD entries parallel to deltas */
- while (he) {
- cmp = S_ISDIR(delta->new_file.mode) ?
- diff->pfxcomp(he->path, delta->new_file.path) :
- diff->strcomp(he->path, delta->old_file.path);
- if (cmp >= 0)
- break;
- if (git_iterator_advance(hiter, &he) < 0)
- he = NULL;
- }
+ /* update the index unless prevented */
+ if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) {
+ struct stat st;
- act = checkout_action_for_delta(data, delta, !cmp ? he : NULL);
+ git_buf_truncate(&data->path, data->workdir_len);
+ if (git_buf_puts(&data->path, file->path) < 0)
+ return -1;
- if (act < 0) {
- error = act;
- goto fail;
+ if ((error = p_stat(git_buf_cstr(&data->path), &st)) < 0) {
+ giterr_set(
+ GITERR_CHECKOUT, "Could not stat submodule %s\n", file->path);
+ return error;
}
- if (!cmp && git_iterator_advance(hiter, &he) < 0)
- he = NULL;
-
- actions[i] = act;
+ st.st_mode = GIT_FILEMODE_COMMIT;
- if (act & CHECKOUT_ACTION__REMOVE)
- counts[CHECKOUT_ACTION__REMOVE]++;
- if (act & CHECKOUT_ACTION__UPDATE_BLOB)
- counts[CHECKOUT_ACTION__UPDATE_BLOB]++;
- if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE)
- counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++;
- if (act & CHECKOUT_ACTION__CONFLICT)
- counts[CHECKOUT_ACTION__CONFLICT]++;
+ error = checkout_update_index(data, file, &st);
}
- if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
- (data->opts->checkout_strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
- {
- giterr_set(GITERR_CHECKOUT, "%d conflicts prevent checkout",
- (int)counts[CHECKOUT_ACTION__CONFLICT]);
- goto fail;
+ return error;
+}
+
+static void report_progress(
+ checkout_data *data,
+ const char *path)
+{
+ if (data->opts.progress_cb)
+ data->opts.progress_cb(
+ path, data->completed_steps, data->total_steps,
+ data->opts.progress_payload);
+}
+
+static int checkout_safe_for_update_only(const char *path, mode_t expected_mode)
+{
+ struct stat st;
+
+ if (p_lstat(path, &st) < 0) {
+ /* if doesn't exist, then no error and no update */
+ if (errno == ENOENT || errno == ENOTDIR)
+ return 0;
+
+ /* otherwise, stat error and no update */
+ giterr_set(GITERR_OS, "Failed to stat file '%s'", path);
+ return -1;
}
- git_iterator_free(hiter);
- git_tree_free(head);
+ /* only safe for update if this is the same type of file */
+ if ((st.st_mode & ~0777) == (expected_mode & ~0777))
+ return 1;
return 0;
+}
-fail:
- *counts_ptr = NULL;
- git__free(counts);
- *actions_ptr = NULL;
- git__free(actions);
+static int checkout_blob(
+ checkout_data *data,
+ const git_diff_file *file)
+{
+ int error = 0;
+ git_blob *blob;
+ struct stat st;
- git_iterator_free(hiter);
- git_tree_free(head);
- git__free(pfx);
+ git_buf_truncate(&data->path, data->workdir_len);
+ if (git_buf_puts(&data->path, file->path) < 0)
+ return -1;
- return -1;
+ if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) {
+ int rval = checkout_safe_for_update_only(
+ git_buf_cstr(&data->path), file->mode);
+ if (rval <= 0)
+ return rval;
+ }
+
+ if ((error = git_blob_lookup(&blob, data->repo, &file->oid)) < 0)
+ return error;
+
+ if (S_ISLNK(file->mode))
+ error = blob_content_to_link(
+ &st, blob, git_buf_cstr(&data->path), data->can_symlink);
+ else
+ error = blob_content_to_file(
+ &st, blob, git_buf_cstr(&data->path), file->mode, &data->opts);
+
+ git_blob_free(blob);
+
+ /* if we try to create the blob and an existing directory blocks it from
+ * being written, then there must have been a typechange conflict in a
+ * parent directory - suppress the error and try to continue.
+ */
+ if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 &&
+ (error == GIT_ENOTFOUND || error == GIT_EEXISTS))
+ {
+ giterr_clear();
+ error = 0;
+ }
+
+ /* update the index unless prevented */
+ if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
+ error = checkout_update_index(data, file, &st);
+
+ /* update the submodule data if this was a new .gitmodules file */
+ if (!error && strcmp(file->path, ".gitmodules") == 0)
+ data->reload_submodules = true;
+
+ return error;
}
static int checkout_remove_the_old(
- git_diff_list *diff,
unsigned int *actions,
- checkout_diff_data *data)
+ checkout_data *data)
{
+ int error = 0;
git_diff_delta *delta;
+ const char *str;
size_t i;
+ const char *workdir = git_buf_cstr(&data->path);
+ uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
+ GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
- git_buf_truncate(data->path, data->workdir_len);
+ git_buf_truncate(&data->path, data->workdir_len);
- git_vector_foreach(&diff->deltas, i, delta) {
+ git_vector_foreach(&data->diff->deltas, i, delta) {
if (actions[i] & CHECKOUT_ACTION__REMOVE) {
- int error = git_futils_rmdir_r(
- delta->new_file.path,
- git_buf_cstr(data->path), /* here set to work dir root */
- GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS |
- GIT_RMDIR_REMOVE_BLOCKERS);
+ error = git_futils_rmdir_r(delta->old_file.path, workdir, flg);
if (error < 0)
return error;
data->completed_steps++;
- report_progress(data, delta->new_file.path);
+ report_progress(data, delta->old_file.path);
+
+ if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 &&
+ (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
+ data->index != NULL)
+ {
+ (void)git_index_remove(data->index, delta->old_file.path, 0);
+ }
+ }
+ }
+
+ git_vector_foreach(&data->removes, i, str) {
+ error = git_futils_rmdir_r(str, workdir, flg);
+ if (error < 0)
+ return error;
+
+ data->completed_steps++;
+ report_progress(data, str);
+
+ if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
+ data->index != NULL)
+ {
+ if (str[strlen(str) - 1] == '/')
+ (void)git_index_remove_directory(data->index, str, 0);
+ else
+ (void)git_index_remove(data->index, str, 0);
}
}
return 0;
}
+static int checkout_deferred_remove(git_repository *repo, const char *path)
+{
+#if 0
+ int error = git_futils_rmdir_r(
+ path, git_repository_workdir(repo), GIT_RMDIR_EMPTY_PARENTS);
+
+ if (error == GIT_ENOTFOUND) {
+ error = 0;
+ giterr_clear();
+ }
+
+ return error;
+#else
+ GIT_UNUSED(repo);
+ GIT_UNUSED(path);
+ assert(false);
+ return 0;
+#endif
+}
+
static int checkout_create_the_new(
- git_diff_list *diff,
unsigned int *actions,
- checkout_diff_data *data)
+ checkout_data *data)
{
+ int error = 0;
git_diff_delta *delta;
size_t i;
- git_vector_foreach(&diff->deltas, i, delta) {
+ git_vector_foreach(&data->diff->deltas, i, delta) {
+ if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
+ /* this had a blocker directory that should only be removed iff
+ * all of the contents of the directory were safely removed
+ */
+ if ((error = checkout_deferred_remove(
+ data->repo, delta->old_file.path)) < 0)
+ return error;
+ }
+
if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) {
- int error = checkout_blob(data, &delta->old_file);
+ error = checkout_blob(data, &delta->new_file);
if (error < 0)
return error;
data->completed_steps++;
- report_progress(data, delta->old_file.path);
+ report_progress(data, delta->new_file.path);
}
}
@@ -581,103 +1017,247 @@ static int checkout_create_the_new(
}
static int checkout_create_submodules(
- git_diff_list *diff,
unsigned int *actions,
- checkout_diff_data *data)
+ checkout_data *data)
{
+ int error = 0;
git_diff_delta *delta;
size_t i;
- git_vector_foreach(&diff->deltas, i, delta) {
+ /* initial reload of submodules if .gitmodules was changed */
+ if (data->reload_submodules &&
+ (error = git_submodule_reload_all(data->repo)) < 0)
+ return error;
+
+ git_vector_foreach(&data->diff->deltas, i, delta) {
+ if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
+ /* this has a blocker directory that should only be removed iff
+ * all of the contents of the directory were safely removed
+ */
+ if ((error = checkout_deferred_remove(
+ data->repo, delta->old_file.path)) < 0)
+ return error;
+ }
+
if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) {
- int error = checkout_submodule(data, &delta->old_file);
+ int error = checkout_submodule(data, &delta->new_file);
if (error < 0)
return error;
data->completed_steps++;
- report_progress(data, delta->old_file.path);
+ report_progress(data, delta->new_file.path);
}
}
- return 0;
+ /* final reload once submodules have been updated */
+ return git_submodule_reload_all(data->repo);
}
-int git_checkout_index(
- git_repository *repo,
- git_index *index,
- git_checkout_opts *opts)
+static int checkout_lookup_head_tree(git_tree **out, git_repository *repo)
{
- git_diff_list *diff = NULL;
- git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
- git_checkout_opts checkout_opts;
- checkout_diff_data data;
- git_buf workdir = GIT_BUF_INIT;
- uint32_t *actions = NULL;
- size_t *counts = NULL;
- int error;
+ int error = 0;
+ git_reference *ref = NULL;
+ git_object *head;
+
+ if (!(error = git_repository_head(&ref, repo)) &&
+ !(error = git_reference_peel(&head, ref, GIT_OBJ_TREE)))
+ *out = (git_tree *)head;
+
+ git_reference_free(ref);
- assert(repo);
+ return error;
+}
+
+static void checkout_data_clear(checkout_data *data)
+{
+ if (data->opts_free_baseline) {
+ git_tree_free(data->opts.baseline);
+ data->opts.baseline = NULL;
+ }
+
+ git_vector_free(&data->removes);
+ git_pool_clear(&data->pool);
+
+ git__free(data->pfx);
+ data->pfx = NULL;
+
+ git_buf_free(&data->path);
- GITERR_CHECK_VERSION(opts, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts");
+ git_index_free(data->index);
+ data->index = NULL;
+}
+
+static int checkout_data_init(
+ checkout_data *data,
+ git_iterator *target,
+ git_checkout_opts *proposed)
+{
+ int error = 0;
+ git_config *cfg;
+ git_repository *repo = git_iterator_owner(target);
+
+ memset(data, 0, sizeof(*data));
+
+ if (!repo) {
+ giterr_set(GITERR_CHECKOUT, "Cannot checkout nothing");
+ return -1;
+ }
if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
return error;
- diff_opts.flags =
- GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED |
- GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_SKIP_BINARY_CHECK;
+ if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
+ return error;
+
+ data->repo = repo;
+
+ GITERR_CHECK_VERSION(
+ proposed, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts");
+
+ if (!proposed)
+ GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTS_VERSION);
+ else
+ memmove(&data->opts, proposed, sizeof(git_checkout_opts));
+
+ /* refresh config and index content unless NO_REFRESH is given */
+ if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
+ if ((error = git_config_refresh(cfg)) < 0)
+ goto cleanup;
+
+ if (git_iterator_inner_type(target) == GIT_ITERATOR_INDEX) {
+ /* if we are iterating over the index, don't reload */
+ data->index = git_iterator_index_get_index(target);
+ GIT_REFCOUNT_INC(data->index);
+ } else {
+ /* otherwise, grab and reload the index */
+ if ((error = git_repository_index(&data->index, data->repo)) < 0 ||
+ (error = git_index_read(data->index)) < 0)
+ goto cleanup;
+ }
+ }
+
+ /* if you are forcing, definitely allow safe updates */
+ if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0)
+ data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE_CREATE;
+ if ((data->opts.checkout_strategy & GIT_CHECKOUT_SAFE_CREATE) != 0)
+ data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
+
+ data->strategy = data->opts.checkout_strategy;
- if (opts && opts->paths.count > 0)
- diff_opts.pathspec = opts->paths;
+ /* opts->disable_filters is false by default */
+
+ if (!data->opts.dir_mode)
+ data->opts.dir_mode = GIT_DIR_MODE;
+
+ if (!data->opts.file_open_flags)
+ data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
+
+ data->pfx = git_pathspec_prefix(&data->opts.paths);
+
+ error = git_config_get_bool(&data->can_symlink, cfg, "core.symlinks");
+ if (error < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto cleanup;
+
+ /* If "core.symlinks" is not found anywhere, default to true. */
+ data->can_symlink = true;
+ giterr_clear();
+ error = 0;
+ }
+
+ if (!data->opts.baseline) {
+ data->opts_free_baseline = true;
+ if ((error = checkout_lookup_head_tree(&data->opts.baseline, repo)) < 0)
+ goto cleanup;
+ }
- if ((error = git_diff_index_to_workdir(&diff, repo, index, &diff_opts)) < 0)
+ if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
+ (error = git_pool_init(&data->pool, 1, 0)) < 0 ||
+ (error = git_buf_puts(&data->path, git_repository_workdir(repo))) < 0)
goto cleanup;
- if ((error = git_buf_puts(&workdir, git_repository_workdir(repo))) < 0)
+ data->workdir_len = git_buf_len(&data->path);
+
+cleanup:
+ if (error < 0)
+ checkout_data_clear(data);
+
+ return error;
+}
+
+int git_checkout_iterator(
+ git_iterator *target,
+ git_checkout_opts *opts)
+{
+ int error = 0;
+ git_iterator *baseline = NULL, *workdir = NULL;
+ checkout_data data = {0};
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ uint32_t *actions = NULL;
+ size_t *counts = NULL;
+
+ /* initialize structures and options */
+ error = checkout_data_init(&data, target, opts);
+ if (error < 0)
+ return error;
+
+ diff_opts.flags =
+ GIT_DIFF_INCLUDE_UNMODIFIED |
+ GIT_DIFF_INCLUDE_UNTRACKED |
+ GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */
+ GIT_DIFF_INCLUDE_IGNORED |
+ GIT_DIFF_INCLUDE_TYPECHANGE |
+ GIT_DIFF_INCLUDE_TYPECHANGE_TREES |
+ GIT_DIFF_SKIP_BINARY_CHECK;
+ if (data.opts.paths.count > 0)
+ diff_opts.pathspec = data.opts.paths;
+
+ /* set up iterators */
+ if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 ||
+ (error = git_iterator_for_workdir_range(
+ &workdir, data.repo, data.pfx, data.pfx)) < 0 ||
+ (error = git_iterator_for_tree_range(
+ &baseline, data.opts.baseline, data.pfx, data.pfx)) < 0)
goto cleanup;
- normalize_options(&checkout_opts, opts);
+ /* Handle case insensitivity for baseline if necessary */
+ if (workdir->ignore_case && !baseline->ignore_case) {
+ if ((error = git_iterator_spoolandsort_push(baseline, true)) < 0)
+ goto cleanup;
+ }
- /* Checkout is best performed with up to four passes through the diff.
- *
- * 0. Figure out what actions should be taken and record for later.
- * 1. Next do removes, because we iterate in alphabetical order, thus
- * a new untracked directory will end up sorted *after* a blob that
- * should be checked out with the same name.
- * 2. Then checkout all blobs.
- * 3. Then checkout all submodules in case a new .gitmodules blob was
- * checked out during pass #2.
+ /* Generate baseline-to-target diff which will include an entry for
+ * every possible update that might need to be made.
*/
+ if ((error = git_diff__from_iterators(
+ &data.diff, data.repo, baseline, target, &diff_opts)) < 0)
+ goto cleanup;
- memset(&data, 0, sizeof(data));
- data.path = &workdir;
- data.workdir_len = git_buf_len(&workdir);
- data.repo = repo;
- data.diff = diff;
- data.opts = &checkout_opts;
-
- if ((error = checkout_get_actions(&actions, &counts, &data)) < 0)
+ /* Loop through diff (and working directory iterator) building a list of
+ * actions to be taken, plus look for conflicts and send notifications.
+ */
+ if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) < 0)
goto cleanup;
data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
counts[CHECKOUT_ACTION__UPDATE_BLOB] +
counts[CHECKOUT_ACTION__UPDATE_SUBMODULE];
- if ((error = retrieve_symlink_caps(repo, &data.can_symlink)) < 0)
- goto cleanup;
-
report_progress(&data, NULL); /* establish 0 baseline */
+ /* To deal with some order dependencies, perform remaining checkout
+ * in three passes: removes, then update blobs, then update submodules.
+ */
if (counts[CHECKOUT_ACTION__REMOVE] > 0 &&
- (error = checkout_remove_the_old(diff, actions, &data)) < 0)
+ (error = checkout_remove_the_old(actions, &data)) < 0)
goto cleanup;
if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 &&
- (error = checkout_create_the_new(diff, actions, &data)) < 0)
+ (error = checkout_create_the_new(actions, &data)) < 0)
goto cleanup;
if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 &&
- (error = checkout_create_submodules(diff, actions, &data)) < 0)
+ (error = checkout_create_submodules(actions, &data)) < 0)
goto cleanup;
assert(data.completed_steps == data.total_steps);
@@ -686,10 +1266,40 @@ cleanup:
if (error == GIT_EUSER)
giterr_clear();
+ if (!error && data.index != NULL &&
+ (data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
+ error = git_index_write(data.index);
+
+ git_diff_list_free(data.diff);
+ git_iterator_free(workdir);
+ git_iterator_free(baseline);
git__free(actions);
git__free(counts);
- git_diff_list_free(diff);
- git_buf_free(&workdir);
+ checkout_data_clear(&data);
+
+ return error;
+}
+
+int git_checkout_index(
+ git_repository *repo,
+ git_index *index,
+ git_checkout_opts *opts)
+{
+ int error;
+ git_iterator *index_i;
+
+ if ((error = git_repository__ensure_not_bare(repo, "checkout index")) < 0)
+ return error;
+
+ if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0)
+ return error;
+ GIT_REFCOUNT_INC(index);
+
+ if (!(error = git_iterator_for_index(&index_i, index)))
+ error = git_checkout_iterator(index_i, opts);
+
+ git_iterator_free(index_i);
+ git_index_free(index);
return error;
}
@@ -699,11 +1309,12 @@ int git_checkout_tree(
const git_object *treeish,
git_checkout_opts *opts)
{
- int error = 0;
- git_index *index = NULL;
+ int error;
git_tree *tree = NULL;
+ git_iterator *tree_i = NULL;
- assert(repo && treeish);
+ if ((error = git_repository__ensure_not_bare(repo, "checkout tree")) < 0)
+ return error;
if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
giterr_set(
@@ -711,16 +1322,10 @@ int git_checkout_tree(
return -1;
}
- /* TODO: create a temp index, load tree there and check it out */
-
- /* load paths in tree that match pathspec into index */
- if (!(error = git_repository_index(&index, repo)) &&
- !(error = git_index_read_tree_match(
- index, tree, opts ? &opts->paths : NULL)) &&
- !(error = git_index_write(index)))
- error = git_checkout_index(repo, NULL, opts);
+ if (!(error = git_iterator_for_tree(&tree_i, tree)))
+ error = git_checkout_iterator(tree_i, opts);
- git_index_free(index);
+ git_iterator_free(tree_i);
git_tree_free(tree);
return error;
@@ -731,17 +1336,18 @@ int git_checkout_head(
git_checkout_opts *opts)
{
int error;
- git_reference *head = NULL;
- git_object *tree = NULL;
+ git_tree *head = NULL;
+ git_iterator *head_i = NULL;
- assert(repo);
+ if ((error = git_repository__ensure_not_bare(repo, "checkout head")) < 0)
+ return error;
- if (!(error = git_repository_head(&head, repo)) &&
- !(error = git_reference_peel(&tree, head, GIT_OBJ_TREE)))
- error = git_checkout_tree(repo, tree, opts);
+ if (!(error = checkout_lookup_head_tree(&head, repo)) &&
+ !(error = git_iterator_for_tree(&head_i, head)))
+ error = git_checkout_iterator(head_i, opts);
- git_reference_free(head);
- git_object_free(tree);
+ git_iterator_free(head_i);
+ git_tree_free(head);
return error;
}
diff --git a/src/checkout.h b/src/checkout.h
new file mode 100644
index 0000000..815abdf
--- /dev/null
+++ b/src/checkout.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * 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_checkout_h__
+#define INCLUDE_checkout_h__
+
+#include "git2/checkout.h"
+#include "iterator.h"
+
+#define GIT_CHECKOUT__NOTIFY_CONFLICT_TREE (1u << 12)
+
+/**
+ * Update the working directory to match the target iterator. The
+ * expected baseline value can be passed in via the checkout options
+ * or else will default to the HEAD commit.
+ */
+extern int git_checkout_iterator(
+ git_iterator *target,
+ git_checkout_opts *opts);
+
+#endif
diff --git a/src/clone.c b/src/clone.c
index 39c0ba2..ff586a6 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -362,7 +362,7 @@ static bool should_checkout(
if (!opts)
return false;
- if (opts->checkout_strategy == GIT_CHECKOUT_DEFAULT)
+ if (opts->checkout_strategy == GIT_CHECKOUT_NONE)
return false;
return !git_repository_head_orphan(repo);
diff --git a/src/diff.c b/src/diff.c
index 83e73cd..82a8164 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -164,6 +164,11 @@ static git_diff_delta *diff_delta__last_for_item(
if (git_oid_cmp(&delta->new_file.oid, &item->oid) == 0)
return delta;
break;
+ case GIT_DELTA_UNTRACKED:
+ if (diff->strcomp(delta->new_file.path, item->path) == 0 &&
+ git_oid_cmp(&delta->new_file.oid, &item->oid) == 0)
+ return delta;
+ break;
case GIT_DELTA_MODIFIED:
if (git_oid_cmp(&delta->old_file.oid, &item->oid) == 0 ||
git_oid_cmp(&delta->new_file.oid, &item->oid) == 0)
@@ -531,14 +536,14 @@ static bool entry_is_prefixed(
{
size_t pathlen;
- if (!prefix_item || diff->pfxcomp(prefix_item->path, item->path))
+ if (!item || diff->pfxcomp(item->path, prefix_item->path) != 0)
return false;
- pathlen = strlen(item->path);
+ pathlen = strlen(prefix_item->path);
- return (item->path[pathlen - 1] == '/' ||
- prefix_item->path[pathlen] == '\0' ||
- prefix_item->path[pathlen] == '/');
+ return (prefix_item->path[pathlen - 1] == '/' ||
+ item->path[pathlen] == '\0' ||
+ item->path[pathlen] == '/');
}
static int diff_list_init_from_iterators(
@@ -584,8 +589,7 @@ int git_diff__from_iterators(
*diff_ptr = NULL;
- if (!diff ||
- diff_list_init_from_iterators(diff, old_iter, new_iter) < 0)
+ if (!diff || diff_list_init_from_iterators(diff, old_iter, new_iter) < 0)
goto fail;
if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) {
@@ -616,7 +620,7 @@ int git_diff__from_iterators(
* instead of just generating a DELETE record
*/
if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 &&
- entry_is_prefixed(diff, oitem, nitem))
+ entry_is_prefixed(diff, nitem, oitem))
{
/* this entry has become a tree! convert to TYPECHANGE */
git_diff_delta *last = diff_delta__last_for_item(diff, oitem);
@@ -624,6 +628,17 @@ int git_diff__from_iterators(
last->status = GIT_DELTA_TYPECHANGE;
last->new_file.mode = GIT_FILEMODE_TREE;
}
+
+ /* If new_iter is a workdir iterator, then this situation
+ * will certainly be followed by a series of untracked items.
+ * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
+ */
+ if (S_ISDIR(nitem->mode) &&
+ !(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS))
+ {
+ if (git_iterator_advance(new_iter, &nitem) < 0)
+ goto fail;
+ }
}
if (git_iterator_advance(old_iter, &oitem) < 0)
@@ -635,6 +650,7 @@ int git_diff__from_iterators(
*/
else if (cmp > 0) {
git_delta_t delta_type = GIT_DELTA_UNTRACKED;
+ bool contains_oitem = entry_is_prefixed(diff, oitem, nitem);
/* check if contained in ignored parent directory */
if (git_buf_len(&ignore_prefix) &&
@@ -646,14 +662,12 @@ int git_diff__from_iterators(
* it or if the user requested the contents of untracked
* directories and it is not under an ignored directory.
*/
- bool contains_tracked =
- entry_is_prefixed(diff, nitem, oitem);
bool recurse_untracked =
(delta_type == GIT_DELTA_UNTRACKED &&
(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) != 0);
/* do not advance into directories that contain a .git file */
- if (!contains_tracked && recurse_untracked) {
+ if (!contains_oitem && recurse_untracked) {
git_buf *full = NULL;
if (git_iterator_current_workdir_path(new_iter, &full) < 0)
goto fail;
@@ -661,7 +675,7 @@ int git_diff__from_iterators(
recurse_untracked = false;
}
- if (contains_tracked || recurse_untracked) {
+ if (contains_oitem || recurse_untracked) {
/* if this directory is ignored, remember it as the
* "ignore_prefix" for processing contained items
*/
@@ -707,14 +721,14 @@ int git_diff__from_iterators(
goto fail;
/* if we are generating TYPECHANGE records then check for that
- * instead of just generating an ADD/UNTRACKED record
+ * instead of just generating an ADDED/UNTRACKED record
*/
if (delta_type != GIT_DELTA_IGNORED &&
(diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 &&
- entry_is_prefixed(diff, nitem, oitem))
+ contains_oitem)
{
- /* this entry was a tree! convert to TYPECHANGE */
- git_diff_delta *last = diff_delta__last_for_item(diff, oitem);
+ /* this entry was prefixed with a tree - make TYPECHANGE */
+ git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
if (last) {
last->status = GIT_DELTA_TYPECHANGE;
last->old_file.mode = GIT_FILEMODE_TREE;
diff --git a/src/fileops.c b/src/fileops.c
index 7f023bf..47b47d6 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -352,6 +352,7 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
typedef struct {
const char *base;
+ size_t baselen;
uint32_t flags;
int error;
} futils__rmdir_data;
@@ -443,9 +444,13 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
{
- int error = p_rmdir(path->ptr);
+ futils__rmdir_data *data = opaque;
+ int error;
+
+ if (git_buf_len(path) <= data->baselen)
+ return GIT_ITEROVER;
- GIT_UNUSED(opaque);
+ error = p_rmdir(git_buf_cstr(path));
if (error) {
int en = errno;
@@ -457,7 +462,7 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
giterr_clear();
error = GIT_ITEROVER;
} else {
- futils__error_cannot_rmdir(path->ptr, NULL);
+ futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
}
}
@@ -475,9 +480,10 @@ int git_futils_rmdir_r(
if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
return -1;
- data.base = base ? base : "";
- data.flags = flags;
- data.error = 0;
+ data.base = base ? base : "";
+ data.baselen = base ? strlen(base) : 0;
+ data.flags = flags;
+ data.error = 0;
error = futils__rmdir_recurs_foreach(&data, &fullpath);
diff --git a/src/index.c b/src/index.c
index c047968..ce902c5 100644
--- a/src/index.c
+++ b/src/index.c
@@ -794,6 +794,44 @@ int git_index_remove(git_index *index, const char *path, int stage)
return error;
}
+int git_index_remove_directory(git_index *index, const char *dir, int stage)
+{
+ git_buf pfx = GIT_BUF_INIT;
+ int error = 0;
+ size_t pos;
+ git_index_entry *entry;
+
+ if (git_buf_sets(&pfx, dir) < 0 || git_path_to_dir(&pfx) < 0)
+ return -1;
+
+ git_vector_sort(&index->entries);
+
+ pos = git_index__prefix_position(index, pfx.ptr);
+
+ while (1) {
+ entry = git_vector_get(&index->entries, pos);
+ if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
+ break;
+
+ if (index_entry_stage(entry) != stage) {
+ ++pos;
+ continue;
+ }
+
+ git_tree_cache_invalidate_path(index->tree, entry->path);
+
+ if ((error = git_vector_remove(&index->entries, pos)) < 0)
+ break;
+ index_entry_free(entry);
+
+ /* removed entry at 'pos' so we don't need to increment it */
+ }
+
+ git_buf_free(&pfx);
+
+ return error;
+}
+
static int index_find(git_index *index, const char *path, int stage)
{
struct entry_srch_key srch_key;
@@ -814,7 +852,10 @@ int git_index_find(git_index *index, const char *path)
if ((pos = git_vector_bsearch2(
&index->entries, index->entries_search_path, path)) < 0)
+ {
+ giterr_set(GITERR_INDEX, "Index does not contain %s", path);
return pos;
+ }
/* Since our binary search only looked at path, we may be in the
* middle of a list of stages.
diff --git a/src/iterator.c b/src/iterator.c
index 28fccce..cf88eff 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -30,8 +30,8 @@
(P)->base.start = start ? git__strdup(start) : NULL; \
(P)->base.end = end ? git__strdup(end) : NULL; \
(P)->base.ignore_case = false; \
- if ((start && !(P)->base.start) || (end && !(P)->base.end)) \
- return -1; \
+ if ((start && !(P)->base.start) || (end && !(P)->base.end)) { \
+ git__free(P); return -1; } \
} while (0)
static int iterator__reset_range(
@@ -988,6 +988,26 @@ fail:
return -1;
}
+git_index *git_iterator_index_get_index(git_iterator *iter)
+{
+ if (iter->type == GIT_ITERATOR_INDEX)
+ return ((index_iterator *)iter)->index;
+
+ if (iter->type == GIT_ITERATOR_SPOOLANDSORT &&
+ ((spoolandsort_callbacks *)iter->cb)->orig_type == GIT_ITERATOR_INDEX)
+ return ((index_iterator *)iter)->index;
+
+ return NULL;
+}
+
+git_iterator_type_t git_iterator_inner_type(git_iterator *iter)
+{
+ if (iter->type == GIT_ITERATOR_SPOOLANDSORT)
+ return ((spoolandsort_callbacks *)iter->cb)->orig_type;
+
+ return iter->type;
+}
+
int git_iterator_current_tree_entry(
git_iterator *iter, const git_tree_entry **tree_entry)
{
@@ -1058,8 +1078,8 @@ int git_iterator_advance_into_directory(
if (iter->type == GIT_ITERATOR_WORKDIR &&
wi->entry.path &&
- S_ISDIR(wi->entry.mode) &&
- !S_ISGITLINK(wi->entry.mode))
+ (wi->entry.mode == GIT_FILEMODE_TREE ||
+ wi->entry.mode == GIT_FILEMODE_COMMIT))
{
if (workdir_iterator__expand_dir(wi) < 0)
/* if error loading or if empty, skip the directory. */
diff --git a/src/iterator.h b/src/iterator.h
index 8bcb6fb..c0e3560 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -193,4 +193,9 @@ extern int git_iterator_cmp(
extern int git_iterator_current_workdir_path(
git_iterator *iter, git_buf **path);
+
+extern git_index *git_iterator_index_get_index(git_iterator *iter);
+
+extern git_iterator_type_t git_iterator_inner_type(git_iterator *iter);
+
#endif
diff --git a/src/oid.c b/src/oid.c
index 1bf74b9..474129b 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -95,11 +95,14 @@ char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
{
char str[GIT_OID_HEXSZ];
- if (!out || n == 0 || !oid)
+ if (!out || n == 0)
return "";
n--; /* allow room for terminating NUL */
+ if (oid == NULL)
+ n = 0;
+
if (n > 0) {
git_oid_fmt(str, oid);
if (n > GIT_OID_HEXSZ)
diff --git a/src/reset.c b/src/reset.c
index 04b0863..f5daa8f 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -62,13 +62,10 @@ int git_reset(
git_object *commit = NULL;
git_index *index = NULL;
git_tree *tree = NULL;
- int error;
+ int error = 0;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
assert(repo && target);
- assert(reset_type == GIT_RESET_SOFT
- || reset_type == GIT_RESET_MIXED
- || reset_type == GIT_RESET_HARD);
if (git_object_owner(target) != repo) {
giterr_set(GITERR_OBJECT,
@@ -76,56 +73,50 @@ int git_reset(
return -1;
}
- if (reset_type != GIT_RESET_SOFT
- && (error = git_repository__ensure_not_bare(
- repo,
+ if (reset_type != GIT_RESET_SOFT &&
+ (error = git_repository__ensure_not_bare(repo,
reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0)
- return error;
-
- if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0)
- goto cleanup;
+ return error;
- if ((error = git_repository_index(&index, repo)) < 0)
+ if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
+ (error = git_repository_index(&index, repo)) < 0 ||
+ (error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
goto cleanup;
- if (reset_type == GIT_RESET_SOFT &&
+ if (reset_type == GIT_RESET_SOFT &&
(git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE ||
- git_index_has_conflicts(index))) {
- giterr_set(GITERR_OBJECT, "%s (soft) while in the middle of a merge.", ERROR_MSG);
- error = GIT_EUNMERGED;
- goto cleanup;
- }
-
- if ((error = update_head(repo, commit)) < 0)
- goto cleanup;
-
- if (reset_type == GIT_RESET_SOFT) {
- error = 0;
+ git_index_has_conflicts(index)))
+ {
+ giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG);
+ error = GIT_EUNMERGED;
goto cleanup;
}
- if ((error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
- goto cleanup;
-
- if ((error = git_index_read_tree(index, tree)) < 0)
+ /* move HEAD to the new target */
+ if ((error = update_head(repo, commit)) < 0)
goto cleanup;
- if ((error = git_index_write(index)) < 0)
- goto cleanup;
+ if (reset_type == GIT_RESET_HARD) {
+ /* overwrite working directory with HEAD */
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
- if ((error = git_repository_merge_cleanup(repo)) < 0) {
- giterr_set(GITERR_REPOSITORY, "%s - Failed to clean up merge data.", ERROR_MSG);
- goto cleanup;
+ if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0)
+ goto cleanup;
}
- if (reset_type == GIT_RESET_MIXED) {
- error = 0;
- goto cleanup;
- }
+ if (reset_type > GIT_RESET_SOFT) {
+ /* reset index to the target content */
- opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ if ((error = git_repository_index(&index, repo)) < 0 ||
+ (error = git_index_read_tree(index, tree)) < 0 ||
+ (error = git_index_write(index)) < 0)
+ goto cleanup;
- error = git_checkout_index(repo, NULL, &opts);
+ if ((error = git_repository_merge_cleanup(repo)) < 0) {
+ giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG);
+ goto cleanup;
+ }
+ }
cleanup:
git_object_free(commit);
diff --git a/src/stash.c b/src/stash.c
index 705fc75..dbd626a 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -22,15 +22,6 @@ static int create_error(int error, const char *msg)
return error;
}
-static int ensure_non_bare_repository(git_repository *repo)
-{
- if (!git_repository_is_bare(repo))
- return 0;
-
- return create_error(GIT_EBAREREPO,
- "Stash related operations require a working directory.");
-}
-
static int retrieve_head(git_reference **out, git_repository *repo)
{
int error = git_repository_head(out, repo);
@@ -89,23 +80,22 @@ static int retrieve_base_commit_and_message(
if ((error = retrieve_head(&head, repo)) < 0)
return error;
- error = -1;
-
if (strcmp("HEAD", git_reference_name(head)) == 0)
- git_buf_puts(stash_message, "(no branch): ");
+ error = git_buf_puts(stash_message, "(no branch): ");
else
- git_buf_printf(
+ error = git_buf_printf(
stash_message,
"%s: ",
git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
-
- if (git_commit_lookup(b_commit, repo, git_reference_target(head)) < 0)
+ if (error < 0)
goto cleanup;
- if (append_commit_description(stash_message, *b_commit) < 0)
+ if ((error = git_commit_lookup(
+ b_commit, repo, git_reference_target(head))) < 0)
goto cleanup;
- error = 0;
+ if ((error = append_commit_description(stash_message, *b_commit)) < 0)
+ goto cleanup;
cleanup:
git_reference_free(head);
@@ -114,9 +104,10 @@ cleanup:
static int build_tree_from_index(git_tree **out, git_index *index)
{
+ int error;
git_oid i_tree_oid;
- if (git_index_write_tree(&i_tree_oid, index) < 0)
+ if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
return -1;
return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
@@ -132,15 +123,15 @@ static int commit_index(
git_tree *i_tree = NULL;
git_oid i_commit_oid;
git_buf msg = GIT_BUF_INIT;
- int error = -1;
+ int error;
- if (build_tree_from_index(&i_tree, index) < 0)
+ if ((error = build_tree_from_index(&i_tree, index)) < 0)
goto cleanup;
- if (git_buf_printf(&msg, "index on %s\n", message) < 0)
+ if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0)
goto cleanup;
- if (git_commit_create(
+ if ((error = git_commit_create(
&i_commit_oid,
git_index_owner(index),
NULL,
@@ -150,8 +141,8 @@ static int commit_index(
git_buf_cstr(&msg),
i_tree,
1,
- &parent) < 0)
- goto cleanup;
+ &parent)) < 0)
+ goto cleanup;
error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
@@ -164,6 +155,8 @@ cleanup:
struct cb_data {
git_index *index;
+ int error;
+
bool include_changed;
bool include_untracked;
bool include_ignored;
@@ -174,52 +167,50 @@ static int update_index_cb(
float progress,
void *payload)
{
- int pos;
struct cb_data *data = (struct cb_data *)payload;
+ const char *add_path = NULL;
GIT_UNUSED(progress);
switch (delta->status) {
case GIT_DELTA_IGNORED:
- if (!data->include_ignored)
- break;
-
- return git_index_add_from_workdir(data->index, delta->new_file.path);
+ if (data->include_ignored)
+ add_path = delta->new_file.path;
+ break;
case GIT_DELTA_UNTRACKED:
- if (!data->include_untracked)
- break;
-
- return git_index_add_from_workdir(data->index, delta->new_file.path);
+ if (data->include_untracked)
+ add_path = delta->new_file.path;
+ break;
case GIT_DELTA_ADDED:
- /* Fall through */
case GIT_DELTA_MODIFIED:
- if (!data->include_changed)
- break;
-
- return git_index_add_from_workdir(data->index, delta->new_file.path);
+ if (data->include_changed)
+ add_path = delta->new_file.path;
+ break;
case GIT_DELTA_DELETED:
if (!data->include_changed)
break;
-
- if ((pos = git_index_find(data->index, delta->new_file.path)) < 0)
- return -1;
-
- if (git_index_remove(data->index, delta->new_file.path, 0) < 0)
- return -1;
+ if (git_index_find(data->index, delta->old_file.path) == 0)
+ data->error = git_index_remove(
+ data->index, delta->old_file.path, 0);
+ break;
default:
/* Unimplemented */
giterr_set(
GITERR_INVALID,
- "Cannot update index. Unimplemented status kind (%d)",
+ "Cannot update index. Unimplemented status (%d)",
delta->status);
- return -1;
+ data->error = -1;
+ break;
}
- return 0;
+ if (add_path != NULL)
+ data->error = git_index_add_from_workdir(data->index, add_path);
+
+ return data->error;
}
static int build_untracked_tree(
@@ -232,14 +223,15 @@ static int build_untracked_tree(
git_diff_list *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct cb_data data = {0};
- int error = -1;
+ int error;
git_index_clear(index);
data.index = index;
if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
- opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
+ opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
+ GIT_DIFF_RECURSE_UNTRACKED_DIRS;
data.include_untracked = true;
}
@@ -248,19 +240,22 @@ static int build_untracked_tree(
data.include_ignored = true;
}
- if (git_commit_tree(&i_tree, i_commit) < 0)
- goto cleanup;
-
- if (git_diff_tree_to_workdir(&diff, git_index_owner(index), i_tree, &opts) < 0)
+ if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
goto cleanup;
- if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0)
+ if ((error = git_diff_tree_to_workdir(
+ &diff, git_index_owner(index), i_tree, &opts)) < 0)
goto cleanup;
- if (build_tree_from_index(tree_out, index) < 0)
+ if ((error = git_diff_foreach(
+ diff, update_index_cb, NULL, NULL, &data)) < 0)
+ {
+ if (error == GIT_EUSER)
+ error = data.error;
goto cleanup;
+ }
- error = 0;
+ error = build_tree_from_index(tree_out, index);
cleanup:
git_diff_list_free(diff);
@@ -279,15 +274,15 @@ static int commit_untracked(
git_tree *u_tree = NULL;
git_oid u_commit_oid;
git_buf msg = GIT_BUF_INIT;
- int error = -1;
+ int error;
- if (build_untracked_tree(&u_tree, index, i_commit, flags) < 0)
+ if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0)
goto cleanup;
- if (git_buf_printf(&msg, "untracked files on %s\n", message) < 0)
+ if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0)
goto cleanup;
- if (git_commit_create(
+ if ((error = git_commit_create(
&u_commit_oid,
git_index_owner(index),
NULL,
@@ -297,8 +292,8 @@ static int commit_untracked(
git_buf_cstr(&msg),
u_tree,
0,
- NULL) < 0)
- goto cleanup;
+ NULL)) < 0)
+ goto cleanup;
error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
@@ -318,35 +313,40 @@ static int build_workdir_tree(
git_diff_list *diff = NULL, *diff2 = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct cb_data data = {0};
- int error = -1;
+ int error;
- if (git_commit_tree(&b_tree, b_commit) < 0)
+ if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
goto cleanup;
- if (git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts) < 0)
+ if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0)
goto cleanup;
- if (git_diff_index_to_workdir(&diff2, repo, NULL, &opts) < 0)
+ if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0)
goto cleanup;
- if (git_diff_merge(diff, diff2) < 0)
+ if ((error = git_diff_merge(diff, diff2)) < 0)
goto cleanup;
data.index = index;
data.include_changed = true;
- if (git_diff_foreach(diff, update_index_cb, NULL, NULL, &data) < 0)
+ if ((error = git_diff_foreach(
+ diff, update_index_cb, NULL, NULL, &data)) < 0)
+ {
+ if (error == GIT_EUSER)
+ error = data.error;
goto cleanup;
+ }
- if (build_tree_from_index(tree_out, index) < 0)
- goto cleanup;
- error = 0;
+ if ((error = build_tree_from_index(tree_out, index)) < 0)
+ goto cleanup;
cleanup:
git_diff_list_free(diff);
git_diff_list_free(diff2);
git_tree_free(b_tree);
+
return error;
}
@@ -359,25 +359,24 @@ static int commit_worktree(
git_commit *b_commit,
git_commit *u_commit)
{
+ int error = 0;
git_tree *w_tree = NULL, *i_tree = NULL;
- int error = -1;
-
const git_commit *parents[] = { NULL, NULL, NULL };
parents[0] = b_commit;
parents[1] = i_commit;
parents[2] = u_commit;
- if (git_commit_tree(&i_tree, i_commit) < 0)
- return -1;
+ if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
+ goto cleanup;
- if (git_index_read_tree(index, i_tree) < 0)
+ if ((error = git_index_read_tree(index, i_tree)) < 0)
goto cleanup;
- if (build_workdir_tree(&w_tree, index, b_commit) < 0)
+ if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0)
goto cleanup;
- if (git_commit_create(
+ error = git_commit_create(
w_commit_oid,
git_index_owner(index),
NULL,
@@ -386,10 +385,8 @@ static int commit_worktree(
NULL,
message,
w_tree,
- u_commit ? 3 : 2, parents) < 0)
- goto cleanup;
-
- error = 0;
+ u_commit ? 3 : 2,
+ parents);
cleanup:
git_tree_free(i_tree);
@@ -402,9 +399,11 @@ static int prepare_worktree_commit_message(
const char *user_message)
{
git_buf buf = GIT_BUF_INIT;
- int error = -1;
+ int error;
+
+ if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
+ return error;
- git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg));
git_buf_clear(msg);
if (!user_message)
@@ -420,10 +419,11 @@ static int prepare_worktree_commit_message(
git_buf_printf(msg, ": %s\n", user_message);
}
- error = git_buf_oom(msg) || git_buf_oom(&buf) ? -1 : 0;
+ error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
cleanup:
git_buf_free(&buf);
+
return error;
}
@@ -449,8 +449,6 @@ static int update_reflog(
if ((error = git_reflog_write(reflog)) < 0)
goto cleanup;
- error = 0;
-
cleanup:
git_reference_free(stash);
git_reflog_free(reflog);
@@ -500,8 +498,7 @@ static int reset_index_and_workdir(
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
- opts.checkout_strategy =
- GIT_CHECKOUT_UPDATE_MODIFIED | GIT_CHECKOUT_UPDATE_UNTRACKED;
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
if (remove_untracked)
opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
@@ -523,7 +520,7 @@ int git_stash_save(
assert(out && repo && stasher);
- if ((error = ensure_non_bare_repository(repo)) < 0)
+ if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
return error;
if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
@@ -531,47 +528,50 @@ int git_stash_save(
if ((error = ensure_there_are_changes_to_stash(
repo,
- (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED,
- (flags & GIT_STASH_INCLUDE_IGNORED) == GIT_STASH_INCLUDE_IGNORED)) < 0)
+ (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
+ (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
goto cleanup;
- error = -1;
-
- if (git_repository_index(&index, repo) < 0)
+ if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup;
- if (commit_index(&i_commit, index, stasher, git_buf_cstr(&msg), b_commit) < 0)
+ if ((error = commit_index(
+ &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
goto cleanup;
- if ((flags & GIT_STASH_INCLUDE_UNTRACKED || flags & GIT_STASH_INCLUDE_IGNORED)
- && commit_untracked(&u_commit, index, stasher, git_buf_cstr(&msg), i_commit, flags) < 0)
+ if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
+ (error = commit_untracked(
+ &u_commit, index, stasher, git_buf_cstr(&msg),
+ i_commit, flags)) < 0)
goto cleanup;
- if (prepare_worktree_commit_message(&msg, message) < 0)
+ if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
goto cleanup;
- if (commit_worktree(out, index, stasher, git_buf_cstr(&msg), i_commit, b_commit, u_commit) < 0)
+ if ((error = commit_worktree(
+ out, index, stasher, git_buf_cstr(&msg),
+ i_commit, b_commit, u_commit)) < 0)
goto cleanup;
git_buf_rtrim(&msg);
- if (update_reflog(out, repo, stasher, git_buf_cstr(&msg)) < 0)
+
+ if ((error = update_reflog(out, repo, stasher, git_buf_cstr(&msg))) < 0)
goto cleanup;
- if (reset_index_and_workdir(
+ if ((error = reset_index_and_workdir(
repo,
- ((flags & GIT_STASH_KEEP_INDEX) == GIT_STASH_KEEP_INDEX) ?
- i_commit : b_commit,
- (flags & GIT_STASH_INCLUDE_UNTRACKED) == GIT_STASH_INCLUDE_UNTRACKED) < 0)
+ ((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit,
+ (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0)) < 0)
goto cleanup;
- error = 0;
-
cleanup:
+
git_buf_free(&msg);
git_commit_free(i_commit);
git_commit_free(b_commit);
git_commit_free(u_commit);
git_index_free(index);
+
return error;
}
@@ -589,7 +589,6 @@ int git_stash_foreach(
error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
if (error == GIT_ENOTFOUND)
return 0;
-
if (error < 0)
goto cleanup;
@@ -599,18 +598,16 @@ int git_stash_foreach(
max = git_reflog_entrycount(reflog);
for (i = 0; i < max; i++) {
entry = git_reflog_entry_byindex(reflog, i);
-
+
if (callback(i,
git_reflog_entry_message(entry),
git_reflog_entry_id_new(entry),
payload)) {
error = GIT_EUSER;
- goto cleanup;
+ break;
}
}
- error = 0;
-
cleanup:
git_reference_free(stash);
git_reflog_free(reflog);
diff --git a/src/submodule.c b/src/submodule.c
index 94b4f72..9ed6707 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -66,7 +66,7 @@ __KHASH_IMPL(
str, static kh_inline, const char *, void *, 1,
str_hash_no_trailing_slash, str_equal_no_trailing_slash);
-static int load_submodule_config(git_repository *repo, bool force);
+static int load_submodule_config(git_repository *repo);
static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *);
static int lookup_head_remote(git_buf *url, git_repository *repo);
static int submodule_get(git_submodule **, git_repository *, const char *, const char *);
@@ -106,7 +106,7 @@ int git_submodule_lookup(
assert(repo && name);
- if ((error = load_submodule_config(repo, false)) < 0)
+ if ((error = load_submodule_config(repo)) < 0)
return error;
pos = git_strmap_lookup_index(repo->submodules, name);
@@ -148,7 +148,7 @@ int git_submodule_foreach(
assert(repo && callback);
- if ((error = load_submodule_config(repo, false)) < 0)
+ if ((error = load_submodule_config(repo)) < 0)
return error;
git_strmap_foreach_value(repo->submodules, sm, {
@@ -708,7 +708,8 @@ int git_submodule_open(
int git_submodule_reload_all(git_repository *repo)
{
assert(repo);
- return load_submodule_config(repo, true);
+ git_submodule_config_free(repo);
+ return load_submodule_config(repo);
}
int git_submodule_reload(git_submodule *submodule)
@@ -829,6 +830,20 @@ int git_submodule_status(
return error;
}
+int git_submodule_location(
+ unsigned int *location_status,
+ git_submodule *submodule)
+{
+ assert(location_status && submodule);
+
+ *location_status = submodule->flags &
+ (GIT_SUBMODULE_STATUS_IN_HEAD | GIT_SUBMODULE_STATUS_IN_INDEX |
+ GIT_SUBMODULE_STATUS_IN_CONFIG | GIT_SUBMODULE_STATUS_IN_WD);
+
+ return 0;
+}
+
+
/*
* INTERNAL FUNCTIONS
*/
@@ -1225,14 +1240,14 @@ static git_config_backend *open_gitmodules(
return mods;
}
-static int load_submodule_config(git_repository *repo, bool force)
+static int load_submodule_config(git_repository *repo)
{
int error;
git_oid gitmodules_oid;
git_buf path = GIT_BUF_INIT;
git_config_backend *mods = NULL;
- if (repo->submodules && !force)
+ if (repo->submodules)
return 0;
memset(&gitmodules_oid, 0, sizeof(gitmodules_oid));
diff --git a/tests-clar/checkout/head.c b/tests-clar/checkout/head.c
index 103b999..8b30993 100644
--- a/tests-clar/checkout/head.c
+++ b/tests-clar/checkout/head.c
@@ -1,6 +1,8 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
+#include "path.h"
+#include "fileops.h"
static git_repository *g_repo;
@@ -14,9 +16,48 @@ void test_checkout_head__cleanup(void)
cl_git_sandbox_cleanup();
}
-void test_checkout_head__checking_out_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void)
+void test_checkout_head__orphaned_head_returns_GIT_EORPHANEDHEAD(void)
{
make_head_orphaned(g_repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EORPHANEDHEAD, git_checkout_head(g_repo, NULL));
}
+
+void test_checkout_head__with_index_only_tree(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_index *index;
+
+ /* let's start by getting things into a known state */
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ /* now let's stage some new stuff including a new directory */
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ p_mkdir("testrepo/newdir", 0777);
+ cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
+
+ cl_git_pass(git_index_add_from_workdir(index, "newdir/newfile.txt"));
+ cl_git_pass(git_index_write(index));
+
+ cl_assert(git_path_isfile("testrepo/newdir/newfile.txt"));
+ cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) != NULL);
+
+ git_index_free(index);
+
+ /* okay, so now we have staged this new file; let's see if we can remove */
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_read(index)); /* reload if needed */
+
+ cl_assert(!git_path_isfile("testrepo/newdir/newfile.txt"));
+ cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) == NULL);
+
+ git_index_free(index);
+}
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index a67765b..fe1f687 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -4,7 +4,6 @@
#include "repository.h"
static git_repository *g_repo;
-static git_checkout_opts g_opts;
static void reset_index_to_treeish(git_object *treeish)
{
@@ -25,9 +24,6 @@ void test_checkout_index__initialize(void)
{
git_tree *tree;
- GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTS_VERSION);
- g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
-
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_head_tree(&tree, g_repo));
@@ -66,7 +62,6 @@ void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
test_checkout_index__cleanup();
- GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTS_VERSION);
g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
@@ -74,11 +69,15 @@ void test_checkout_index__cannot_checkout_a_bare_repository(void)
void test_checkout_index__can_create_missing_files(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/README", "hey there\n");
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
@@ -87,30 +86,37 @@ void test_checkout_index__can_create_missing_files(void)
void test_checkout_index__can_remove_untracked_files(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
git_futils_mkdir("./testrepo/dir/subdir/subsubdir", NULL, 0755, GIT_MKDIR_PATH);
cl_git_mkfile("./testrepo/dir/one", "one\n");
cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");
cl_assert_equal_i(true, git_path_isdir("./testrepo/dir/subdir/subsubdir"));
- g_opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy =
+ GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_path_isdir("./testrepo/dir"));
}
void test_checkout_index__honor_the_specified_pathspecs(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
char *entries[] = { "*.txt" };
- g_opts.paths.strings = entries;
- g_opts.paths.count = 1;
+ opts.paths.strings = entries;
+ opts.paths.count = 1;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
@@ -134,6 +140,7 @@ static void set_core_autocrlf_to(bool value)
void test_checkout_index__honor_the_gitattributes_directives(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
const char *attributes =
"branch_file.txt text eol=crlf\n"
"new.txt text eol=lf\n";
@@ -141,7 +148,9 @@ void test_checkout_index__honor_the_gitattributes_directives(void)
cl_git_mkfile("./testrepo/.gitattributes", attributes);
set_core_autocrlf_to(false);
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/README", "hey there\n");
test_file_contents("./testrepo/new.txt", "my new file\n");
@@ -151,12 +160,15 @@ void test_checkout_index__honor_the_gitattributes_directives(void)
void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
{
#ifdef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
const char *expected_readme_text = "hey there\r\n";
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
set_core_autocrlf_to(true);
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/README", expected_readme_text);
#endif
@@ -169,9 +181,13 @@ static void set_repo_symlink_handling_cap_to(bool value)
void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
set_repo_symlink_handling_cap_to(true);
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
#ifdef GIT_WIN32
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
@@ -191,51 +207,63 @@ void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
set_repo_symlink_handling_cap_to(false);
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
}
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
/* set this up to not return an error code on conflicts, but it
* still will not have permission to overwrite anything...
*/
- g_opts.checkout_strategy = GIT_CHECKOUT_ALLOW_CONFLICTS;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
}
void test_checkout_index__can_overwrite_modified_file(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
- g_opts.checkout_strategy |= GIT_CHECKOUT_UPDATE_MODIFIED;
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__options_disable_filters(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
- g_opts.disable_filters = false;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ opts.disable_filters = false;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/new.txt", "my new file\r\n");
p_unlink("./testrepo/new.txt");
- g_opts.disable_filters = true;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.disable_filters = true;
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/new.txt", "my new file\n");
}
@@ -243,6 +271,7 @@ void test_checkout_index__options_disable_filters(void)
void test_checkout_index__options_dir_modes(void)
{
#ifndef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
struct stat st;
git_oid oid;
git_commit *commit;
@@ -252,8 +281,10 @@ void test_checkout_index__options_dir_modes(void)
reset_index_to_treeish((git_object *)commit);
- g_opts.dir_mode = 0701;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ opts.dir_mode = 0701;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(p_stat("./testrepo/a", &st));
cl_assert_equal_i(st.st_mode & 0777, 0701);
@@ -269,11 +300,13 @@ void test_checkout_index__options_dir_modes(void)
void test_checkout_index__options_override_file_modes(void)
{
#ifndef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
struct stat st;
- g_opts.file_mode = 0700;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ opts.file_mode = 0700;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(p_stat("./testrepo/new.txt", &st));
cl_assert_equal_i(st.st_mode & 0777, 0700);
@@ -282,42 +315,48 @@ void test_checkout_index__options_override_file_modes(void)
void test_checkout_index__options_open_flags(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_git_mkfile("./testrepo/new.txt", "hi\n");
- g_opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
- g_opts.checkout_strategy |= GIT_CHECKOUT_UPDATE_MODIFIED;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
}
-struct conflict_data {
+struct notify_data {
const char *file;
const char *sha;
};
-static int conflict_cb(
- const char *conflict_file,
- const git_oid *blob_oid,
- unsigned int index_mode,
- unsigned int wd_mode,
+static int test_checkout_notify_cb(
+ git_checkout_notify_t why,
+ const char *path,
+ const git_diff_file *baseline,
+ const git_diff_file *target,
+ const git_diff_file *workdir,
void *payload)
{
- struct conflict_data *expectations = (struct conflict_data *)payload;
+ struct notify_data *expectations = (struct notify_data *)payload;
- GIT_UNUSED(index_mode);
- GIT_UNUSED(wd_mode);
+ GIT_UNUSED(workdir);
- cl_assert_equal_s(expectations->file, conflict_file);
- cl_assert_equal_i(0, git_oid_streq(blob_oid, expectations->sha));
+ cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
+ cl_assert_equal_s(expectations->file, path);
+ cl_assert_equal_i(0, git_oid_streq(&baseline->oid, expectations->sha));
+ cl_assert_equal_i(0, git_oid_streq(&target->oid, expectations->sha));
return 0;
}
void test_checkout_index__can_notify_of_skipped_files(void)
{
- struct conflict_data data;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ struct notify_data data;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
@@ -330,24 +369,28 @@ void test_checkout_index__can_notify_of_skipped_files(void)
data.file = "new.txt";
data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd";
- g_opts.checkout_strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS;
- g_opts.conflict_cb = conflict_cb;
- g_opts.conflict_payload = &data;
+ opts.checkout_strategy =
+ GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
+ opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
+ opts.notify_cb = test_checkout_notify_cb;
+ opts.notify_payload = &data;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
-static int dont_conflict_cb(
- const char *conflict_file,
- const git_oid *blob_oid,
- unsigned int index_mode,
- unsigned int wd_mode,
+static int dont_notify_cb(
+ git_checkout_notify_t why,
+ const char *path,
+ const git_diff_file *baseline,
+ const git_diff_file *target,
+ const git_diff_file *workdir,
void *payload)
{
- GIT_UNUSED(conflict_file);
- GIT_UNUSED(blob_oid);
- GIT_UNUSED(index_mode);
- GIT_UNUSED(wd_mode);
+ GIT_UNUSED(why);
+ GIT_UNUSED(path);
+ GIT_UNUSED(baseline);
+ GIT_UNUSED(target);
+ GIT_UNUSED(workdir);
GIT_UNUSED(payload);
cl_assert(false);
@@ -357,37 +400,45 @@ static int dont_conflict_cb(
void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
set_core_autocrlf_to(true);
cl_git_mkfile("./testrepo/new.txt", "my new file\r\n");
- g_opts.checkout_strategy |= GIT_CHECKOUT_ALLOW_CONFLICTS;
- g_opts.conflict_cb = dont_conflict_cb;
- g_opts.conflict_payload = NULL;
+ opts.checkout_strategy =
+ GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
+ opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
+ opts.notify_cb = dont_notify_cb;
+ opts.notify_payload = NULL;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
-static void progress(const char *path, size_t cur, size_t tot, void *payload)
+static void checkout_progress_counter(
+ const char *path, size_t cur, size_t tot, void *payload)
{
- bool *was_called = (bool*)payload;
GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
- *was_called = true;
+ (*(int *)payload)++;
}
void test_checkout_index__calls_progress_callback(void)
{
- bool was_called = 0;
- g_opts.progress_cb = progress;
- g_opts.progress_payload = &was_called;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ int calls = 0;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ opts.progress_cb = checkout_progress_counter;
+ opts.progress_payload = &calls;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
- cl_assert_equal_i(was_called, true);
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
+ cl_assert(calls > 0);
}
void test_checkout_index__can_overcome_name_clashes(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
@@ -400,7 +451,6 @@ void test_checkout_index__can_overcome_name_clashes(void)
cl_git_pass(git_index_add_from_workdir(index, "path0"));
cl_git_pass(git_index_add_from_workdir(index, "path1/file1"));
-
cl_git_pass(p_unlink("./testrepo/path0"));
cl_git_pass(git_futils_rmdir_r(
"./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES));
@@ -412,14 +462,15 @@ void test_checkout_index__can_overcome_name_clashes(void)
cl_assert(git_path_isfile("./testrepo/path1"));
cl_assert(git_path_isfile("./testrepo/path0/file0"));
- g_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy =
+ GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
+ cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_path_isfile("./testrepo/path1"));
cl_assert(git_path_isfile("./testrepo/path0/file0"));
- g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
- cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_path_isfile("./testrepo/path0"));
cl_assert(git_path_isfile("./testrepo/path1/file1"));
@@ -429,18 +480,44 @@ void test_checkout_index__can_overcome_name_clashes(void)
void test_checkout_index__validates_struct_version(void)
{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
const git_error *err;
- g_opts.version = 1024;
- cl_git_fail(git_checkout_index(g_repo, NULL, &g_opts));
+ opts.version = 1024;
+ cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = giterr_last();
cl_assert_equal_i(err->klass, GITERR_INVALID);
- g_opts.version = 0;
+ opts.version = 0;
giterr_clear();
- cl_git_fail(git_checkout_index(g_repo, NULL, &g_opts));
+ cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = giterr_last();
cl_assert_equal_i(err->klass, GITERR_INVALID);
}
+
+void test_checkout_index__can_update_prefixed_files(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ cl_git_mkfile("./testrepo/READ", "content\n");
+ cl_git_mkfile("./testrepo/README.after", "content\n");
+ cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
+ cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777));
+ cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
+ cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
+
+ test_file_contents("./testrepo/README", "hey there\n");
+ test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
+ test_file_contents("./testrepo/new.txt", "my new file\n");
+
+ cl_assert(!git_path_exists("testrepo/READ"));
+ cl_assert(!git_path_exists("testrepo/README.after"));
+ cl_assert(!git_path_exists("testrepo/branch_file"));
+ cl_assert(!git_path_exists("testrepo/branch_file.txt.after"));
+}
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index 88dbe4f..ff5c43a 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -2,6 +2,8 @@
#include "git2/checkout.h"
#include "repository.h"
+#include "buffer.h"
+#include "fileops.h"
static git_repository *g_repo;
static git_checkout_opts g_opts;
@@ -12,7 +14,7 @@ void test_checkout_tree__initialize(void)
g_repo = cl_git_sandbox_init("testrepo");
GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTS_VERSION);
- g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+ g_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
}
void test_checkout_tree__cleanup(void)
@@ -48,6 +50,34 @@ void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));
}
+void test_checkout_tree__can_checkout_and_remove_directory(void)
+{
+ cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
+
+ /* Checkout brach "subtrees" and update HEAD, so that HEAD matches the
+ * current working tree
+ */
+ cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
+ cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
+
+ cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+
+ cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/"));
+ cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
+ cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));
+
+ /* Checkout brach "master" and update HEAD, so that HEAD matches the
+ * current working tree
+ */
+ cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
+ cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
+
+ cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
+
+ /* This directory should no longer exist */
+ cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
+}
+
void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
{
char *entries[] = { "de/" };
@@ -85,3 +115,193 @@ void test_checkout_tree__calls_progress_callback(void)
cl_assert_equal_i(was_called, true);
}
+
+void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
+{
+ git_oid master_oid;
+ git_oid chomped_oid;
+ git_commit* p_master_commit;
+ git_commit* p_chomped_commit;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ git_oid_fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+ git_oid_fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d");
+ cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid));
+ cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid));
+
+ /* GIT_CHECKOUT_NONE should not add any file to the working tree from the
+ * index as it is supposed to be a dry run.
+ */
+ opts.checkout_strategy = GIT_CHECKOUT_NONE;
+ git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts);
+ cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt"));
+}
+
+static void assert_on_branch(git_repository *repo, const char *branch)
+{
+ git_reference *head;
+ git_buf bname = GIT_BUF_INIT;
+
+ cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
+ cl_assert_(git_reference_type(head) == GIT_REF_SYMBOLIC, branch);
+
+ cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
+ cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
+
+ git_reference_free(head);
+ git_buf_free(&bname);
+}
+
+void test_checkout_tree__can_switch_branches(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_oid oid;
+ git_object *obj = NULL;
+
+ assert_on_branch(g_repo, "master");
+
+ /* do first checkout with FORCE because we don't know if testrepo
+ * base data is clean for a checkout or not
+ */
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+
+ cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
+ cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
+
+ cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
+ cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
+
+ cl_assert(git_path_isfile("testrepo/README"));
+ cl_assert(git_path_isfile("testrepo/branch_file.txt"));
+ cl_assert(git_path_isfile("testrepo/new.txt"));
+ cl_assert(git_path_isfile("testrepo/a/b.txt"));
+
+ cl_assert(!git_path_isdir("testrepo/ab"));
+
+ assert_on_branch(g_repo, "dir");
+
+ git_object_free(obj);
+
+ /* do second checkout safe because we should be clean after first */
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+
+ cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
+ cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
+
+ cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
+ cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
+
+ cl_assert(git_path_isfile("testrepo/README"));
+ cl_assert(git_path_isfile("testrepo/branch_file.txt"));
+ cl_assert(git_path_isfile("testrepo/new.txt"));
+ cl_assert(git_path_isfile("testrepo/ab/4.txt"));
+ cl_assert(git_path_isfile("testrepo/ab/c/3.txt"));
+ cl_assert(git_path_isfile("testrepo/ab/de/2.txt"));
+ cl_assert(git_path_isfile("testrepo/ab/de/fgh/1.txt"));
+
+ cl_assert(!git_path_isdir("testrepo/a"));
+
+ assert_on_branch(g_repo, "subtrees");
+
+ git_object_free(obj);
+}
+
+void test_checkout_tree__can_remove_untracked(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+
+ cl_git_mkfile("testrepo/untracked_file", "as you wish");
+ cl_assert(git_path_isfile("testrepo/untracked_file"));
+
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isfile("testrepo/untracked_file"));
+}
+
+void test_checkout_tree__can_remove_ignored(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ int ignored = 0;
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED;
+
+ cl_git_mkfile("testrepo/ignored_file", "as you wish");
+
+ cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n"));
+
+ cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file"));
+ cl_assert_equal_i(1, ignored);
+
+ cl_assert(git_path_isfile("testrepo/ignored_file"));
+
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isfile("testrepo/ignored_file"));
+}
+
+/* this is essentially the code from git__unescape modified slightly */
+static void strip_cr_from_buf(git_buf *buf)
+{
+ char *scan, *pos = buf->ptr;
+
+ for (scan = pos; *scan; pos++, scan++) {
+ if (*scan == '\r')
+ scan++; /* skip '\r' */
+ if (pos != scan)
+ *pos = *scan;
+ }
+
+ *pos = '\0';
+ buf->size = (pos - buf->ptr);
+}
+
+void test_checkout_tree__can_update_only(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_oid oid;
+ git_object *obj = NULL;
+ git_buf buf = GIT_BUF_INIT;
+
+ /* first let's get things into a known state - by checkout out the HEAD */
+
+ assert_on_branch(g_repo, "master");
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isdir("testrepo/a"));
+
+ cl_git_pass(git_futils_readbuffer(&buf, "testrepo/branch_file.txt"));
+ strip_cr_from_buf(&buf);
+ cl_assert_equal_s("hi\nbye!\n", buf.ptr);
+ git_buf_free(&buf);
+
+ /* now checkout branch but with update only */
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
+
+ cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
+ cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
+
+ cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
+ cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
+
+ assert_on_branch(g_repo, "dir");
+
+ /* this normally would have been created (which was tested separately in
+ * the test_checkout_tree__can_switch_branches test), but with
+ * UPDATE_ONLY it will not have been created.
+ */
+ cl_assert(!git_path_isdir("testrepo/a"));
+
+ /* but this file still should have been updated */
+ cl_git_pass(git_futils_readbuffer(&buf, "testrepo/branch_file.txt"));
+ strip_cr_from_buf(&buf);
+ cl_assert_equal_s("hi\n", buf.ptr);
+
+ git_buf_free(&buf);
+
+ git_object_free(obj);
+}
diff --git a/tests-clar/checkout/typechange.c b/tests-clar/checkout/typechange.c
index 98c15bc..b92cc23 100644
--- a/tests-clar/checkout/typechange.c
+++ b/tests-clar/checkout/typechange.c
@@ -2,6 +2,7 @@
#include "git2/checkout.h"
#include "path.h"
#include "posix.h"
+#include "fileops.h"
static git_repository *g_repo = NULL;
@@ -34,28 +35,97 @@ void test_checkout_typechange__cleanup(void)
cl_fixture_cleanup("submod2_target");
}
-void test_checkout_typechange__checkout_typechanges(void)
+static void assert_file_exists(const char *path)
+{
+ cl_assert_(git_path_isfile(path), path);
+}
+
+static void assert_dir_exists(const char *path)
+{
+ cl_assert_(git_path_isdir(path), path);
+}
+
+static void assert_workdir_matches_tree(
+ git_repository *repo, const git_oid *id, const char *root, bool recurse)
{
- int i;
git_object *obj;
- git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_tree *tree;
+ size_t i, max_i;
+ git_buf path = GIT_BUF_INIT;
+
+ if (!root)
+ root = git_repository_workdir(repo);
+ cl_assert(root);
+
+ cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJ_ANY));
+ cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJ_TREE));
+ git_object_free(obj);
+
+ max_i = git_tree_entrycount(tree);
+
+ for (i = 0; i < max_i; ++i) {
+ const git_tree_entry *te = git_tree_entry_byindex(tree, i);
+ cl_assert(te);
+
+ cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te)));
+
+ switch (git_tree_entry_type(te)) {
+ case GIT_OBJ_COMMIT:
+ assert_dir_exists(path.ptr);
+ break;
+ case GIT_OBJ_TREE:
+ assert_dir_exists(path.ptr);
+ if (recurse)
+ assert_workdir_matches_tree(
+ repo, git_tree_entry_id(te), path.ptr, true);
+ break;
+ case GIT_OBJ_BLOB:
+ switch (git_tree_entry_filemode(te)) {
+ case GIT_FILEMODE_BLOB:
+ case GIT_FILEMODE_BLOB_EXECUTABLE:
+ assert_file_exists(path.ptr);
+ /* because of cross-platform, don't confirm exec bit yet */
+ break;
+ case GIT_FILEMODE_LINK:
+ cl_assert_(git_path_exists(path.ptr), path.ptr);
+ /* because of cross-platform, don't confirm link yet */
+ break;
+ default:
+ cl_assert(false); /* really?! */
+ }
+ break;
+ default:
+ cl_assert(false); /* really?!! */
+ }
+ }
- opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ git_tree_free(tree);
+ git_buf_free(&path);
+}
- /* if you don't include GIT_CHECKOUT_REMOVE_UNTRACKED then on the final
- * checkout which is supposed to remove all the files, we will not
- * actually remove them!
- */
+void test_checkout_typechange__checkout_typechanges_safe(void)
+{
+ int i;
+ git_object *obj;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
- /* fprintf(stderr, "checking out '%s'\n", g_typechange_oids[i]); */
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+
+ /* There are bugs in some submodule->tree changes that prevent
+ * SAFE from passing here, even though the following should work:
+ */
+ /* !i ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE; */
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(obj)));
+ assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
+
git_object_free(obj);
if (!g_typechange_empty[i]) {
@@ -75,3 +145,96 @@ void test_checkout_typechange__checkout_typechanges(void)
}
}
}
+
+typedef struct {
+ int conflicts;
+ int dirty;
+ int updates;
+ int untracked;
+ int ignored;
+} notify_counts;
+
+static int notify_counter(
+ git_checkout_notify_t why,
+ const char *path,
+ const git_diff_file *baseline,
+ const git_diff_file *target,
+ const git_diff_file *workdir,
+ void *payload)
+{
+ notify_counts *cts = payload;
+
+ GIT_UNUSED(path);
+ GIT_UNUSED(baseline);
+ GIT_UNUSED(target);
+ GIT_UNUSED(workdir);
+
+ switch (why) {
+ case GIT_CHECKOUT_NOTIFY_CONFLICT: cts->conflicts++; break;
+ case GIT_CHECKOUT_NOTIFY_DIRTY: cts->dirty++; break;
+ case GIT_CHECKOUT_NOTIFY_UPDATED: cts->updates++; break;
+ case GIT_CHECKOUT_NOTIFY_UNTRACKED: cts->untracked++; break;
+ case GIT_CHECKOUT_NOTIFY_IGNORED: cts->ignored++; break;
+ default: break;
+ }
+
+ return 0;
+}
+
+static void force_create_file(const char *file)
+{
+ int error = git_futils_rmdir_r(file, NULL,
+ GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
+ cl_assert(!error || error == GIT_ENOTFOUND);
+ cl_git_pass(git_futils_mkpath2file(file, 0777));
+ cl_git_rewritefile(file, "yowza!");
+}
+
+void test_checkout_typechange__checkout_with_conflicts(void)
+{
+ int i;
+ git_object *obj;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ notify_counts cts = {0};
+
+ opts.notify_flags =
+ GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_UNTRACKED;
+ opts.notify_cb = notify_counter;
+ opts.notify_payload = &cts;
+
+ for (i = 0; g_typechange_oids[i] != NULL; ++i) {
+ cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
+
+ force_create_file("typechanges/a/blocker");
+ force_create_file("typechanges/b");
+ force_create_file("typechanges/c/sub/sub/file");
+ git_futils_rmdir_r("typechanges/d", NULL, GIT_RMDIR_REMOVE_FILES);
+ p_mkdir("typechanges/d", 0777); /* intentionally empty dir */
+ force_create_file("typechanges/untracked");
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ memset(&cts, 0, sizeof(cts));
+
+ cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
+ cl_assert(cts.conflicts > 0);
+ cl_assert(cts.untracked > 0);
+
+ opts.checkout_strategy =
+ GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+ memset(&cts, 0, sizeof(cts));
+
+ cl_assert(git_path_exists("typechanges/untracked"));
+
+ cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
+ cl_assert_equal_i(0, cts.conflicts);
+
+ cl_assert(!git_path_exists("typechanges/untracked"));
+
+ cl_git_pass(
+ git_repository_set_head_detached(g_repo, git_object_id(obj)));
+
+ assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
+
+ git_object_free(obj);
+ }
+}
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index ce3ec4a..88ffb2b 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -2,6 +2,16 @@
#include "posix.h"
#include "path.h"
+void cl_git_report_failure(
+ int error, const char *file, int line, const char *fncall)
+{
+ char msg[4096];
+ const git_error *last = giterr_last();
+ p_snprintf(msg, 4096, "error %d - %s",
+ error, last ? last->message : "<no message>");
+ clar__assert(0, file, line, fncall, msg, 1);
+}
+
void cl_git_mkfile(const char *filename, const char *content)
{
int fd;
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index 91a5426..321ec5f 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -6,17 +6,17 @@
#include "common.h"
/**
- * Special wrapper for `clar_must_pass` that passes
- * the last library error as the test failure message.
+ * Replace for `clar_must_pass` that passes the last library error as the
+ * test failure message.
*
- * Use this wrapper around all `git_` library calls that
- * return error codes!
+ * Use this wrapper around all `git_` library calls that return error codes!
*/
#define cl_git_pass(expr) do { \
+ int _lg2_error; \
giterr_clear(); \
- if ((expr) != 0) \
- clar__assert(0, __FILE__, __LINE__, "Function call failed: " #expr, giterr_last() ? giterr_last()->message : NULL, 1); \
- } while(0)
+ if ((_lg2_error = (expr)) != 0) \
+ cl_git_report_failure(_lg2_error, __FILE__, __LINE__, "Function call failed: " #expr); \
+ } while (0)
/**
* Wrapper for `clar_must_fail` -- this one is
@@ -25,6 +25,10 @@
*/
#define cl_git_fail(expr) cl_must_fail(expr)
+#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
+
+void cl_git_report_failure(int, const char *, int, const char *);
+
#define cl_assert_equal_sz(sz1,sz2) cl_assert((sz1) == (sz2))
/*
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c
index 989734c..5c3d4cf 100644
--- a/tests-clar/index/tests.c
+++ b/tests-clar/index/tests.c
@@ -290,3 +290,86 @@ void test_index_tests__write_invalid_filename(void)
cl_fixture_cleanup("read_tree");
}
+
+void test_index_tests__remove_entry(void)
+{
+ git_repository *repo;
+ git_index *index;
+
+ p_mkdir("index_test", 0770);
+
+ cl_git_pass(git_repository_init(&repo, "index_test", 0));
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_assert(git_index_entrycount(index) == 0);
+
+ cl_git_mkfile("index_test/hello", NULL);
+ cl_git_pass(git_index_add_from_workdir(index, "hello"));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_index_read(index)); /* reload */
+ cl_assert(git_index_entrycount(index) == 1);
+ cl_assert(git_index_get_bypath(index, "hello", 0) != NULL);
+
+ cl_git_pass(git_index_remove(index, "hello", 0));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_index_read(index)); /* reload */
+ cl_assert(git_index_entrycount(index) == 0);
+ cl_assert(git_index_get_bypath(index, "hello", 0) == NULL);
+
+ git_index_free(index);
+ git_repository_free(repo);
+ cl_fixture_cleanup("index_test");
+}
+
+void test_index_tests__remove_directory(void)
+{
+ git_repository *repo;
+ git_index *index;
+
+ p_mkdir("index_test", 0770);
+
+ cl_git_pass(git_repository_init(&repo, "index_test", 0));
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_assert_equal_i(0, (int)git_index_entrycount(index));
+
+ p_mkdir("index_test/a", 0770);
+ cl_git_mkfile("index_test/a/1.txt", NULL);
+ cl_git_mkfile("index_test/a/2.txt", NULL);
+ cl_git_mkfile("index_test/a/3.txt", NULL);
+ cl_git_mkfile("index_test/b.txt", NULL);
+
+ cl_git_pass(git_index_add_from_workdir(index, "a/1.txt"));
+ cl_git_pass(git_index_add_from_workdir(index, "a/2.txt"));
+ cl_git_pass(git_index_add_from_workdir(index, "a/3.txt"));
+ cl_git_pass(git_index_add_from_workdir(index, "b.txt"));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_index_read(index)); /* reload */
+ cl_assert_equal_i(4, (int)git_index_entrycount(index));
+ cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL);
+ cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
+ cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
+
+ cl_git_pass(git_index_remove(index, "a/1.txt", 0));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_index_read(index)); /* reload */
+ cl_assert_equal_i(3, (int)git_index_entrycount(index));
+ cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
+ cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
+ cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
+
+ cl_git_pass(git_index_remove_directory(index, "a", 0));
+ cl_git_pass(git_index_write(index));
+
+ cl_git_pass(git_index_read(index)); /* reload */
+ cl_assert_equal_i(1, (int)git_index_entrycount(index));
+ cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
+ cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL);
+ cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
+
+ git_index_free(index);
+ git_repository_free(repo);
+ cl_fixture_cleanup("index_test");
+}
diff --git a/tests-clar/object/raw/convert.c b/tests-clar/object/raw/convert.c
index 7f310dd..74442c1 100644
--- a/tests-clar/object/raw/convert.c
+++ b/tests-clar/object/raw/convert.c
@@ -21,9 +21,9 @@ void test_object_raw_convert__succeed_on_oid_to_string_conversion(void)
str = git_oid_tostr(out, 0, &in);
cl_assert(str && *str == '\0' && str != out);
- /* NULL oid pointer, returns static empty string */
+ /* NULL oid pointer, sets existing buffer to empty string */
str = git_oid_tostr(out, sizeof(out), NULL);
- cl_assert(str && *str == '\0' && str != out);
+ cl_assert(str && *str == '\0' && str == out);
/* n == 1, returns out as an empty string */
str = git_oid_tostr(out, 1, &in);
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index c216a1e..082ed52 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -91,7 +91,7 @@ void test_online_clone__can_checkout_a_cloned_repo(void)
bool checkout_progress_cb_was_called = false,
fetch_progress_cb_was_called = false;
- g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+ g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
g_options.checkout_opts.progress_cb = &checkout_progress;
g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
g_options.fetch_progress_cb = &fetch_progress;
diff --git a/tests-clar/reset/hard.c b/tests-clar/reset/hard.c
index 9381007..6d2123e 100644
--- a/tests-clar/reset/hard.c
+++ b/tests-clar/reset/hard.c
@@ -54,9 +54,7 @@ void test_reset_hard__resetting_reverts_modified_files(void)
static const char *after[4] = {
"current_file\n",
"modified_file\n",
- /* wrong value because reset is still slightly incorrect */
- "staged_new_file\n",
- /* right value: NULL, */
+ NULL,
"staged_changes_modified_file\n"
};
const char *wd = git_repository_workdir(repo);
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
index c146e90..2af95c7 100644
--- a/tests-clar/stash/drop.c
+++ b/tests-clar/stash/drop.c
@@ -36,15 +36,22 @@ static void push_three_states(void)
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_from_workdir(index, "zero.txt"));
commit_staged_files(&oid, index, signature);
+ cl_assert(git_path_exists("stash/zero.txt"));
cl_git_mkfile("stash/one.txt", "content\n");
cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
+ cl_assert(!git_path_exists("stash/one.txt"));
+ cl_assert(git_path_exists("stash/zero.txt"));
cl_git_mkfile("stash/two.txt", "content\n");
cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
+ cl_assert(!git_path_exists("stash/two.txt"));
+ cl_assert(git_path_exists("stash/zero.txt"));
cl_git_mkfile("stash/three.txt", "content\n");
cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
+ cl_assert(!git_path_exists("stash/three.txt"));
+ cl_assert(git_path_exists("stash/zero.txt"));
git_index_free(index);
}
diff --git a/tests-clar/stash/foreach.c b/tests-clar/stash/foreach.c
index c7d59a3..f198362 100644
--- a/tests-clar/stash/foreach.c
+++ b/tests-clar/stash/foreach.c
@@ -38,10 +38,10 @@ void test_stash_foreach__cleanup(void)
}
static int callback_cb(
- size_t index,
- const char* message,
- const git_oid *stash_oid,
- void *payload)
+ size_t index,
+ const char* message,
+ const git_oid *stash_oid,
+ void *payload)
{
struct callback_data *data = (struct callback_data *)payload;