Merge pull request #3436 from pks-t/libgit2-worktree Worktree implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 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
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 34354f4..88fe723 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -246,6 +246,18 @@ GIT_EXTERN(int) git_branch_is_head(
const git_reference *branch);
/**
+ * Determine if the current branch is checked out in any linked
+ * repository.
+ *
+ * @param branch Reference to the branch.
+ *
+ * @return 1 if branch is checked out, 0 if it isn't,
+ * error code otherwise.
+ */
+GIT_EXTERN(int) git_branch_is_checked_out(
+ const git_reference *branch);
+
+/**
* Return the name of remote that the remote tracking branch belongs to.
*
* @param out Pointer to the user-allocated git_buf which will be filled with the name of the remote.
diff --git a/include/git2/errors.h b/include/git2/errors.h
index e959ffd..1d27136 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -100,6 +100,7 @@ typedef enum {
GITERR_REBASE,
GITERR_FILESYSTEM,
GITERR_PATCH,
+ GITERR_WORKTREE
} git_error_t;
/**
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 3d70d1b..a396a54 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -35,6 +35,17 @@ GIT_BEGIN_DECL
* @return 0 or an error code
*/
GIT_EXTERN(int) git_repository_open(git_repository **out, const char *path);
+/**
+ * Open working tree as a repository
+ *
+ * Open the working directory of the working tree as a normal
+ * repository that can then be worked on.
+ *
+ * @param out Output pointer containing opened repository
+ * @param wt Working tree to open
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_repository_open_from_worktree(git_repository **out, git_worktree *wt);
/**
* Create a "fake" repository to wrap an object database
@@ -335,6 +346,17 @@ GIT_EXTERN(int) git_repository_init_ext(
GIT_EXTERN(int) git_repository_head(git_reference **out, git_repository *repo);
/**
+ * Retrieve the referenced HEAD for the worktree
+ *
+ * @param out pointer to the reference which will be retrieved
+ * @param repo a repository object
+ * @param name name of the worktree to retrieve HEAD for
+ * @return 0 when successful, error-code otherwise
+ */
+GIT_EXTERN(int) git_repository_head_for_worktree(git_reference **out, git_repository *repo,
+ const char *name);
+
+/**
* Check if a repository's HEAD is detached
*
* A repository's HEAD is detached when it points directly to a commit
@@ -346,6 +368,20 @@ GIT_EXTERN(int) git_repository_head(git_reference **out, git_repository *repo);
*/
GIT_EXTERN(int) git_repository_head_detached(git_repository *repo);
+/*
+ * Check if a worktree's HEAD is detached
+ *
+ * A worktree's HEAD is detached when it points directly to a
+ * commit instead of a branch.
+ *
+ * @param repo a repository object
+ * @param name name of the worktree to retrieve HEAD for
+ * @return 1 if HEAD is detached, 0 if its not; error code if
+ * there was an error
+ */
+GIT_EXTERN(int) git_repository_head_detached_for_worktree(git_repository *repo,
+ const char *name);
+
/**
* Check if the current branch is unborn
*
@@ -371,6 +407,42 @@ GIT_EXTERN(int) git_repository_head_unborn(git_repository *repo);
GIT_EXTERN(int) git_repository_is_empty(git_repository *repo);
/**
+ * List of items which belong to the git repository layout
+ */
+typedef enum {
+ GIT_REPOSITORY_ITEM_GITDIR,
+ GIT_REPOSITORY_ITEM_WORKDIR,
+ GIT_REPOSITORY_ITEM_COMMONDIR,
+ GIT_REPOSITORY_ITEM_INDEX,
+ GIT_REPOSITORY_ITEM_OBJECTS,
+ GIT_REPOSITORY_ITEM_REFS,
+ GIT_REPOSITORY_ITEM_PACKED_REFS,
+ GIT_REPOSITORY_ITEM_REMOTES,
+ GIT_REPOSITORY_ITEM_CONFIG,
+ GIT_REPOSITORY_ITEM_INFO,
+ GIT_REPOSITORY_ITEM_HOOKS,
+ GIT_REPOSITORY_ITEM_LOGS,
+ GIT_REPOSITORY_ITEM_MODULES,
+ GIT_REPOSITORY_ITEM_WORKTREES
+} git_repository_item_t;
+
+/**
+ * Get the location of a specific repository file or directory
+ *
+ * This function will retrieve the path of a specific repository
+ * item. It will thereby honor things like the repository's
+ * common directory, gitdir, etc. In case a file path cannot
+ * exist for a given item (e.g. the working directory of a bare
+ * repository), an error is returned.
+ *
+ * @param out Buffer to store the path at
+ * @param repo Repository to get path for
+ * @param item The repository item for which to retrieve the path
+ * @return 0 on success, otherwise a negative value
+ */
+GIT_EXTERN(int) git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item);
+
+/**
* Get the path of this repository
*
* This is the path of the `.git` folder for normal repositories,
@@ -393,6 +465,17 @@ GIT_EXTERN(const char *) git_repository_path(git_repository *repo);
GIT_EXTERN(const char *) git_repository_workdir(git_repository *repo);
/**
+ * Get the path of the shared common directory for this repository
+ *
+ * If the repository is bare is not a worktree, the git directory
+ * path is returned.
+ *
+ * @param repo A repository object
+ * @return the path to the common dir
+ */
+GIT_EXTERN(const char *) git_repository_commondir(git_repository *repo);
+
+/**
* Set the path to the working directory for this repository
*
* The working directory doesn't need to be the same one
@@ -421,6 +504,14 @@ GIT_EXTERN(int) git_repository_set_workdir(
GIT_EXTERN(int) git_repository_is_bare(git_repository *repo);
/**
+ * Check if a repository is a linked work tree
+ *
+ * @param repo Repo to test
+ * @return 1 if the repository is a linked work tree, 0 otherwise.
+ */
+GIT_EXTERN(int) git_repository_is_worktree(git_repository *repo);
+
+/**
* Get the configuration file for this repository.
*
* If a configuration file has not been set, the default
diff --git a/include/git2/types.h b/include/git2/types.h
index 6f41014..dfdaa29 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -104,6 +104,9 @@ typedef struct git_refdb_backend git_refdb_backend;
*/
typedef struct git_repository git_repository;
+/** Representation of a working tree */
+typedef struct git_worktree git_worktree;
+
/** Representation of a generic object in a repository */
typedef struct git_object git_object;
diff --git a/include/git2/worktree.h b/include/git2/worktree.h
new file mode 100644
index 0000000..cad1284
--- /dev/null
+++ b/include/git2/worktree.h
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_worktree_h__
+#define INCLUDE_git_worktree_h__
+
+#include "common.h"
+#include "buffer.h"
+#include "types.h"
+#include "strarray.h"
+
+/**
+ * @file git2/worktrees.h
+ * @brief Git worktree related functions
+ * @defgroup git_commit Git worktree related functions
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * List names of linked working trees
+ *
+ * The returned list should be released with `git_strarray_free`
+ * when no longer needed.
+ *
+ * @param out pointer to the array of working tree names
+ * @param repo the repo to use when listing working trees
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_worktree_list(git_strarray *out, git_repository *repo);
+
+/**
+ * Lookup a working tree by its name for a given repository
+ *
+ * @param out Output pointer to looked up worktree or `NULL`
+ * @param repo The repository containing worktrees
+ * @param name Name of the working tree to look up
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name);
+
+/**
+ * Free a previously allocated worktree
+ *
+ * @param wt worktree handle to close. If NULL nothing occurs.
+ */
+GIT_EXTERN(void) git_worktree_free(git_worktree *wt);
+
+/**
+ * Check if worktree is valid
+ *
+ * A valid worktree requires both the git data structures inside
+ * the linked parent repository and the linked working copy to be
+ * present.
+ *
+ * @param wt Worktree to check
+ * @return 0 when worktree is valid, error-code otherwise
+ */
+GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt);
+
+/**
+ * Add a new working tree
+ *
+ * Add a new working tree for the repository, that is create the
+ * required data structures inside the repository and check out
+ * the current HEAD at `path`
+ *
+ * @param out Output pointer containing new working tree
+ * @param repo Repository to create working tree for
+ * @param name Name of the working tree
+ * @param path Path to create working tree at
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *path);
+
+/**
+ * Lock worktree if not already locked
+ *
+ * Lock a worktree, optionally specifying a reason why the linked
+ * working tree is being locked.
+ *
+ * @param wt Worktree to lock
+ * @param reason Reason why the working tree is being locked
+ * @return 0 on success, non-zero otherwise
+ */
+GIT_EXTERN(int) git_worktree_lock(git_worktree *wt, char *reason);
+
+/**
+ * Unlock a locked worktree
+ *
+ * @param wt Worktree to unlock
+ * @return 0 on success, 1 if worktree was not locked, error-code
+ * otherwise
+ */
+GIT_EXTERN(int) git_worktree_unlock(git_worktree *wt);
+
+/**
+ * Check if worktree is locked
+ *
+ * A worktree may be locked if the linked working tree is stored
+ * on a portable device which is not available.
+ *
+ * @param reason Buffer to store reason in. If NULL no reason is stored.
+ * @param wt Worktree to check
+ * @return 0 when the working tree not locked, a value greater
+ * than zero if it is locked, less than zero if there was an
+ * error
+ */
+GIT_EXTERN(int) git_worktree_is_locked(git_buf *reason, const git_worktree *wt);
+
+/**
+ * Flags which can be passed to git_worktree_prune to alter its
+ * behavior.
+ */
+typedef enum {
+ /* Prune working tree even if working tree is valid */
+ GIT_WORKTREE_PRUNE_VALID = 1u << 0,
+ /* Prune working tree even if it is locked */
+ GIT_WORKTREE_PRUNE_LOCKED = 1u << 1,
+ /* Prune checked out working tree */
+ GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2,
+} git_worktree_prune_t;
+
+/**
+ * Is the worktree prunable with the given set of flags?
+ *
+ * A worktree is not prunable in the following scenarios:
+ *
+ * - the worktree is linking to a valid on-disk worktree. The
+ * GIT_WORKTREE_PRUNE_VALID flag will cause this check to be
+ * ignored.
+ * - the worktree is not valid but locked. The
+ * GIT_WORKRTEE_PRUNE_LOCKED flag will cause this check to be
+ * ignored.
+ *
+ * If the worktree is not valid and not locked or if the above
+ * flags have been passed in, this function will return a
+ * positive value.
+ */
+GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags);
+
+/**
+ * Prune working tree
+ *
+ * Prune the working tree, that is remove the git data
+ * structures on disk. The repository will only be pruned of
+ * `git_worktree_is_prunable` succeeds.
+ *
+ * @param wt Worktree to prune
+ * @param flags git_worktree_prune_t flags
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_worktree_prune(git_worktree *wt, unsigned flags);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/attr.c b/src/attr.c
index d43a15f..93dea12 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -292,7 +292,7 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
int error = 0;
const char *workdir = git_repository_workdir(repo);
git_index *idx = NULL;
- git_buf sys = GIT_BUF_INIT;
+ git_buf path = GIT_BUF_INIT;
if (attr_session && attr_session->init_setup)
return 0;
@@ -304,40 +304,45 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
* definitions will be available for later file parsing
*/
- error = system_attr_file(&sys, attr_session);
+ error = system_attr_file(&path, attr_session);
if (error == 0)
error = preload_attr_file(
- repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, sys.ptr);
+ repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, path.ptr);
if (error != GIT_ENOTFOUND)
- return error;
-
- git_buf_free(&sys);
+ goto out;
if ((error = preload_attr_file(
repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
- return error;
+ goto out;
+
+ if ((error = git_repository_item_path(&path,
+ repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
+ goto out;
if ((error = preload_attr_file(
repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
- git_repository_path(repo), GIT_ATTR_FILE_INREPO)) < 0)
- return error;
+ path.ptr, GIT_ATTR_FILE_INREPO)) < 0)
+ goto out;
if (workdir != NULL &&
(error = preload_attr_file(
repo, attr_session, GIT_ATTR_FILE__FROM_FILE, workdir, GIT_ATTR_FILE)) < 0)
- return error;
+ goto out;
if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
(error = preload_attr_file(
repo, attr_session, GIT_ATTR_FILE__FROM_INDEX, NULL, GIT_ATTR_FILE)) < 0)
- return error;
+ goto out;
if (attr_session)
attr_session->init_setup = 1;
+out:
+ git_buf_free(&path);
+
return error;
}
@@ -472,7 +477,7 @@ static int collect_attr_files(
git_vector *files)
{
int error = 0;
- git_buf dir = GIT_BUF_INIT;
+ git_buf dir = GIT_BUF_INIT, attrfile = GIT_BUF_INIT;
const char *workdir = git_repository_workdir(repo);
attr_walk_up_info info = { NULL };
@@ -494,9 +499,13 @@ static int collect_attr_files(
* - $GIT_PREFIX/etc/gitattributes
*/
+ error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO);
+ if (error < 0)
+ goto cleanup;
+
error = push_attr_file(
repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
- git_repository_path(repo), GIT_ATTR_FILE_INREPO);
+ attrfile.ptr, GIT_ATTR_FILE_INREPO);
if (error < 0)
goto cleanup;
@@ -538,6 +547,7 @@ static int collect_attr_files(
cleanup:
if (error < 0)
release_attr_files(files);
+ git_buf_free(&attrfile);
git_buf_free(&dir);
return error;
diff --git a/src/attr_file.h b/src/attr_file.h
index 388ecf4..a9af240 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -15,7 +15,7 @@
#include "fileops.h"
#define GIT_ATTR_FILE ".gitattributes"
-#define GIT_ATTR_FILE_INREPO "info/attributes"
+#define GIT_ATTR_FILE_INREPO "attributes"
#define GIT_ATTR_FILE_SYSTEM "gitattributes"
#define GIT_ATTR_FILE_XDG "attributes"
diff --git a/src/blob.c b/src/blob.c
index cd5df35..19d3039 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -326,8 +326,8 @@ int git_blob_create_fromstream(git_writestream **out, git_repository *repo, cons
stream->parent.close = blob_writestream_close;
stream->parent.free = blob_writestream_free;
- if ((error = git_buf_joinpath(&path,
- git_repository_path(repo), GIT_OBJECTS_DIR "streamed")) < 0)
+ if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
+ || (error = git_buf_joinpath(&path, path.ptr, "streamed")) < 0)
goto cleanup;
if ((error = git_filebuf_open_withsize(&stream->fbuf, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY,
diff --git a/src/branch.c b/src/branch.c
index 7ddcb3d..7d5e9cb 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -13,6 +13,7 @@
#include "refs.h"
#include "remote.h"
#include "annotated_commit.h"
+#include "worktree.h"
#include "git2/branch.h"
@@ -126,6 +127,62 @@ int git_branch_create_from_annotated(
repository, branch_name, commit->commit, commit->description, force);
}
+int git_branch_is_checked_out(
+ const git_reference *branch)
+{
+ git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+ git_strarray worktrees;
+ git_reference *ref = NULL;
+ git_repository *repo;
+ const char *worktree;
+ int found = false;
+ size_t i;
+
+ assert(branch && git_reference_is_branch(branch));
+
+ repo = git_reference_owner(branch);
+
+ if (git_worktree_list(&worktrees, repo) < 0)
+ return -1;
+
+ for (i = 0; i < worktrees.count; i++) {
+ worktree = worktrees.strings[i];
+
+ if (git_repository_head_for_worktree(&ref, repo, worktree) < 0)
+ continue;
+
+ if (git__strcmp(ref->name, branch->name) == 0) {
+ found = true;
+ git_reference_free(ref);
+ break;
+ }
+
+ git_reference_free(ref);
+ }
+ git_strarray_free(&worktrees);
+
+ if (found)
+ return found;
+
+ /* Check HEAD of parent */
+ if (git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE) < 0)
+ goto out;
+ if (git_futils_readbuffer(&buf, path.ptr) < 0)
+ goto out;
+ if (git__prefixcmp(buf.ptr, "ref: ") == 0)
+ git_buf_consume(&buf, buf.ptr + strlen("ref: "));
+ git_buf_rtrim(&buf);
+
+ found = git__strcmp(buf.ptr, branch->name) == 0;
+
+out:
+ git_buf_free(&buf);
+ git_buf_free(&path);
+
+ return found;
+}
+
+
int git_branch_delete(git_reference *branch)
{
int is_head;
@@ -149,6 +206,12 @@ int git_branch_delete(git_reference *branch)
return -1;
}
+ if (git_reference_is_branch(branch) && git_branch_is_checked_out(branch)) {
+ giterr_set(GITERR_REFERENCE, "Cannot delete branch '%s' as it is "
+ "the current HEAD of a linked repository.", git_reference_name(branch));
+ return -1;
+ }
+
if (git_buf_join(&config_section, '.', "branch",
git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
goto on_error;
diff --git a/src/cherrypick.c b/src/cherrypick.c
index ab06733..d8b6858 100644
--- a/src/cherrypick.c
+++ b/src/cherrypick.c
@@ -28,7 +28,7 @@ static int write_cherrypick_head(
git_buf file_path = GIT_BUF_INIT;
int error = 0;
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) >= 0 &&
(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
error = git_filebuf_commit(&file);
@@ -49,7 +49,7 @@ static int write_merge_msg(
git_buf file_path = GIT_BUF_INIT;
int error = 0;
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) < 0 ||
(error = git_filebuf_printf(&file, "%s", commit_msg)) < 0)
goto cleanup;
diff --git a/src/clone.c b/src/clone.c
index 0d4756e..16ddfac 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -513,9 +513,8 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_
return error;
}
- git_buf_joinpath(&src_odb, git_repository_path(src), GIT_OBJECTS_DIR);
- git_buf_joinpath(&dst_odb, git_repository_path(repo), GIT_OBJECTS_DIR);
- if (git_buf_oom(&src_odb) || git_buf_oom(&dst_odb)) {
+ if (git_repository_item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0
+ || git_repository_item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) {
error = -1;
goto cleanup;
}
diff --git a/src/fetchhead.c b/src/fetchhead.c
index 0d9ab2c..6e6f3eb 100644
--- a/src/fetchhead.c
+++ b/src/fetchhead.c
@@ -115,7 +115,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
assert(repo && fetchhead_refs);
- if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
@@ -249,7 +249,7 @@ int git_repository_fetchhead_foreach(git_repository *repo,
assert(repo && cb);
- if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0)
diff --git a/src/ignore.c b/src/ignore.c
index cc9e08e..c324d4d 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -277,6 +277,7 @@ int git_ignore__for_path(
{
int error = 0;
const char *workdir = git_repository_workdir(repo);
+ git_buf infopath = GIT_BUF_INIT;
assert(repo && ignores && path);
@@ -322,10 +323,14 @@ int git_ignore__for_path(
goto cleanup;
}
+ if ((error = git_repository_item_path(&infopath,
+ repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
+ goto cleanup;
+
/* load .git/info/exclude */
error = push_ignore_file(
ignores, &ignores->ign_global,
- git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
+ infopath.ptr, GIT_IGNORE_FILE_INREPO);
if (error < 0)
goto cleanup;
@@ -336,6 +341,7 @@ int git_ignore__for_path(
git_repository_attr_cache(repo)->cfg_excl_file);
cleanup:
+ git_buf_free(&infopath);
if (error < 0)
git_ignore__free(ignores);
diff --git a/src/ignore.h b/src/ignore.h
index d40bd60..876c8e0 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -12,7 +12,7 @@
#include "attr_file.h"
#define GIT_IGNORE_FILE ".gitignore"
-#define GIT_IGNORE_FILE_INREPO "info/exclude"
+#define GIT_IGNORE_FILE_INREPO "exclude"
#define GIT_IGNORE_FILE_XDG "ignore"
/* The git_ignores structure maintains three sets of ignores:
diff --git a/src/merge.c b/src/merge.c
index eceadf0..857d513 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -562,7 +562,7 @@ int git_repository_mergehead_foreach(
assert(repo && cb);
- if ((error = git_buf_joinpath(&merge_head_path, repo->path_repository,
+ if ((error = git_buf_joinpath(&merge_head_path, repo->gitdir,
GIT_MERGE_HEAD_FILE)) < 0)
return error;
@@ -2277,7 +2277,7 @@ static int write_merge_head(
assert(repo && heads);
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_HEAD_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_HEAD_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;
@@ -2305,7 +2305,7 @@ static int write_merge_mode(git_repository *repo)
assert(repo);
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MODE_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MODE_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;
@@ -2536,7 +2536,7 @@ static int write_merge_msg(
for (i = 0; i < heads_len; i++)
entries[i].merge_head = heads[i];
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0 ||
(error = git_filebuf_write(&file, "Merge ", 6)) < 0)
goto cleanup;
@@ -2914,7 +2914,7 @@ int git_merge__append_conflicts_to_merge_msg(
if (!git_index_has_conflicts(index))
return 0;
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_APPEND, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;
diff --git a/src/rebase.c b/src/rebase.c
index b2024a4..09941a2 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -92,7 +92,7 @@ static int rebase_state_type(
git_buf path = GIT_BUF_INIT;
git_rebase_type_t type = GIT_REBASE_TYPE_NONE;
- if (git_buf_joinpath(&path, repo->path_repository, REBASE_APPLY_DIR) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, REBASE_APPLY_DIR) < 0)
return -1;
if (git_path_isdir(git_buf_cstr(&path))) {
@@ -101,7 +101,7 @@ static int rebase_state_type(
}
git_buf_clear(&path);
- if (git_buf_joinpath(&path, repo->path_repository, REBASE_MERGE_DIR) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, REBASE_MERGE_DIR) < 0)
return -1;
if (git_path_isdir(git_buf_cstr(&path))) {
@@ -624,7 +624,7 @@ static int rebase_init_merge(
GIT_UNUSED(upstream);
- if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0)
+ if ((error = git_buf_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0)
goto done;
rebase->state_path = git_buf_detach(&state_path);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index e40f48b..3d69063 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -55,7 +55,10 @@ typedef struct refdb_fs_backend {
git_refdb_backend parent;
git_repository *repo;
- char *path;
+ /* path to git directory */
+ char *gitpath;
+ /* path to common objects' directory */
+ char *commonpath;
git_sortedcache *refcache;
int peeling_mode;
@@ -77,7 +80,7 @@ static int packed_reload(refdb_fs_backend *backend)
git_buf packedrefs = GIT_BUF_INIT;
char *scan, *eof, *eol;
- if (!backend->path)
+ if (!backend->gitpath)
return 0;
error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
@@ -238,7 +241,7 @@ static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
/* if we fail to load the loose reference, assume someone changed
* the filesystem under us and skip it...
*/
- if (loose_readbuffer(&ref_file, backend->path, name) < 0) {
+ if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) {
giterr_clear();
goto done;
}
@@ -287,7 +290,7 @@ static int _dirent_loose_load(void *payload, git_buf *full_path)
return error;
}
- file_path = full_path->ptr + strlen(backend->path);
+ file_path = full_path->ptr + strlen(backend->gitpath);
return loose_lookup_to_packfile(backend, file_path);
}
@@ -303,7 +306,7 @@ static int packed_loadloose(refdb_fs_backend *backend)
int error;
git_buf refs_path = GIT_BUF_INIT;
- if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
+ if (git_buf_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0)
return -1;
/*
@@ -331,7 +334,7 @@ static int refdb_fs_backend__exists(
assert(backend);
if ((error = packed_reload(backend)) < 0 ||
- (error = git_buf_joinpath(&ref_path, backend->path, ref_name)) < 0)
+ (error = git_buf_joinpath(&ref_path, backend->gitpath, ref_name)) < 0)
return error;
*exists = git_path_isfile(ref_path.ptr) ||
@@ -362,6 +365,14 @@ static const char *loose_parse_symbolic(git_buf *file_content)
return refname_start;
}
+static bool is_per_worktree_ref(const char *ref_name)
+{
+ return strcmp("HEAD", ref_name) == 0 ||
+ strcmp("FETCH_HEAD", ref_name) == 0 ||
+ strcmp("MERGE_HEAD", ref_name) == 0 ||
+ strcmp("ORIG_HEAD", ref_name) == 0;
+}
+
static int loose_lookup(
git_reference **out,
refdb_fs_backend *backend,
@@ -369,11 +380,17 @@ static int loose_lookup(
{
git_buf ref_file = GIT_BUF_INIT;
int error = 0;
+ const char *ref_dir;
if (out)
*out = NULL;
- if ((error = loose_readbuffer(&ref_file, backend->path, ref_name)) < 0)
+ if (is_per_worktree_ref(ref_name))
+ ref_dir = backend->gitpath;
+ else
+ ref_dir = backend->commonpath;
+
+ if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0)
/* cannot read loose ref file - gah */;
else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) {
const char *target;
@@ -484,12 +501,12 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry = NULL;
- if (!backend->path) /* do nothing if no path for loose refs */
+ if (!backend->commonpath) /* do nothing if no commonpath for loose refs */
return 0;
fsit_opts.flags = backend->iterator_flags;
- if ((error = git_buf_printf(&path, "%s/refs", backend->path)) < 0 ||
+ if ((error = git_buf_printf(&path, "%s/refs", backend->commonpath)) < 0 ||
(error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) {
git_buf_free(&path);
return error;
@@ -729,10 +746,10 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *
/* Remove a possibly existing empty directory hierarchy
* which name would collide with the reference name
*/
- if ((error = git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY)) < 0)
+ if ((error = git_futils_rmdir_r(name, backend->gitpath, GIT_RMDIR_SKIP_NONEMPTY)) < 0)
return error;
- if (git_buf_joinpath(&ref_path, backend->path, name) < 0)
+ if (git_buf_joinpath(&ref_path, backend->gitpath, name) < 0)
return -1;
error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE);
@@ -1283,7 +1300,7 @@ static int refdb_fs_backend__delete_tail(
}
/* If a loose reference exists, remove it from the filesystem */
- if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
+ if (git_buf_joinpath(&loose_path, backend->gitpath, ref_name) < 0)
return -1;
@@ -1408,20 +1425,23 @@ static void refdb_fs_backend__free(git_refdb_backend *_backend)
assert(backend);
git_sortedcache_free(backend->refcache);
- git__free(backend->path);
+ git__free(backend->gitpath);
+ git__free(backend->commonpath);
git__free(backend);
}
-static int setup_namespace(git_buf *path, git_repository *repo)
+static int setup_namespace(git_buf *gitpath, git_repository *repo)
{
char *parts, *start, *end;
- /* Not all repositories have a path */
- if (repo->path_repository == NULL)
+ /* Not all repositories have a gitpath */
+ if (repo->gitdir == NULL)
+ return 0;
+ if (repo->commondir == NULL)
return 0;
/* Load the path to the repo first */
- git_buf_puts(path, repo->path_repository);
+ git_buf_puts(gitpath, repo->gitdir);
/* if the repo is not namespaced, nothing else to do */
if (repo->namespace == NULL)
@@ -1438,19 +1458,19 @@ static int setup_namespace(git_buf *path, git_repository *repo)
* refs under refs/namespaces/foo/refs/namespaces/bar/
*/
while ((start = git__strsep(&end, "/")) != NULL) {
- git_buf_printf(path, "refs/namespaces/%s/", start);
+ git_buf_printf(gitpath, "refs/namespaces/%s/", start);
}
- git_buf_printf(path, "refs/namespaces/%s/refs", end);
+ git_buf_printf(gitpath, "refs/namespaces/%s/refs", end);
git__free(parts);
/* Make sure that the folder with the namespace exists */
- if (git_futils_mkdir_relative(git_buf_cstr(path), repo->path_repository,
+ if (git_futils_mkdir_relative(git_buf_cstr(gitpath), repo->commondir,
0777, GIT_MKDIR_PATH, NULL) < 0)
return -1;
- /* Return root of the namespaced path, i.e. without the trailing '/refs' */
- git_buf_rtruncate_at_char(path, '/');
+ /* Return root of the namespaced gitpath, i.e. without the trailing '/refs' */
+ git_buf_rtruncate_at_char(gitpath, '/');
return 0;
}
@@ -1562,7 +1582,7 @@ static int create_new_reflog_file(const char *filepath)
GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name)
{
- return git_buf_join3(path, '/', repo->path_repository, GIT_REFLOG_DIR, name);
+ return git_buf_join3(path, '/', repo->commondir, GIT_REFLOG_DIR, name);
}
static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
@@ -1857,7 +1877,7 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_
&normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
return error;
- if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0)
+ if (git_buf_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0)
return -1;
if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0)
@@ -1948,7 +1968,7 @@ int git_refdb_backend_fs(
git_repository *repository)
{
int t = 0;
- git_buf path = GIT_BUF_INIT;
+ git_buf gitpath = GIT_BUF_INIT;
refdb_fs_backend *backend;
backend = git__calloc(1, sizeof(refdb_fs_backend));
@@ -1956,18 +1976,20 @@ int git_refdb_backend_fs(
backend->repo = repository;
- if (setup_namespace(&path, repository) < 0)
+ if (setup_namespace(&gitpath, repository) < 0)
goto fail;
- backend->path = git_buf_detach(&path);
+ backend->gitpath = backend->commonpath = git_buf_detach(&gitpath);
+ if (repository->commondir)
+ backend->commonpath = git__strdup(repository->commondir);
- if (git_buf_joinpath(&path, backend->path, GIT_PACKEDREFS_FILE) < 0 ||
+ if (git_buf_joinpath(&gitpath, backend->commonpath, GIT_PACKEDREFS_FILE) < 0 ||
git_sortedcache_new(
&backend->refcache, offsetof(struct packref, name),
- NULL, NULL, packref_cmp, git_buf_cstr(&path)) < 0)
+ NULL, NULL, packref_cmp, git_buf_cstr(&gitpath)) < 0)
goto fail;
- git_buf_free(&path);
+ git_buf_free(&gitpath);
if (!git_repository__cvar(&t, backend->repo, GIT_CVAR_IGNORECASE) && t) {
backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
@@ -1999,8 +2021,9 @@ int git_refdb_backend_fs(
return 0;
fail:
- git_buf_free(&path);
- git__free(backend->path);
+ git_buf_free(&gitpath);
+ git__free(backend->gitpath);
+ git__free(backend->commonpath);
git__free(backend);
return -1;
}
diff --git a/src/repository.c b/src/repository.c
index 2185632..4b937be 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -28,6 +28,7 @@
#include "diff_driver.h"
#include "annotated_commit.h"
#include "submodule.h"
+#include "worktree.h"
GIT__USE_STRMAP
#include "strmap.h"
@@ -36,8 +37,32 @@ GIT__USE_STRMAP
# include "win32/w32_util.h"
#endif
+static const struct {
+ git_repository_item_t parent;
+ const char *name;
+ bool directory;
+} items[] = {
+ { GIT_REPOSITORY_ITEM_GITDIR, NULL, true },
+ { GIT_REPOSITORY_ITEM_WORKDIR, NULL, true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, NULL, true },
+ { GIT_REPOSITORY_ITEM_GITDIR, "index", false },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "objects", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "refs", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "packed-refs", false },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "remotes", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "config", false },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "info", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "hooks", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "logs", true },
+ { GIT_REPOSITORY_ITEM_GITDIR, "modules", true },
+ { GIT_REPOSITORY_ITEM_COMMONDIR, "worktrees", true }
+};
+
static int check_repositoryformatversion(git_config *config);
+#define GIT_COMMONDIR_FILE "commondir"
+#define GIT_GITDIR_FILE "gitdir"
+
#define GIT_FILE_CONTENT_PREFIX "gitdir:"
#define GIT_BRANCH_MASTER "master"
@@ -141,8 +166,9 @@ void git_repository_free(git_repository *repo)
git_buf_free(git_array_get(repo->reserved_names, i));
git_array_clear(repo->reserved_names);
- git__free(repo->path_gitlink);
- git__free(repo->path_repository);
+ git__free(repo->gitlink);
+ git__free(repo->gitdir);
+ git__free(repo->commondir);
git__free(repo->workdir);
git__free(repo->namespace);
git__free(repo->ident_name);
@@ -157,17 +183,41 @@ void git_repository_free(git_repository *repo)
*
* Open a repository object from its path
*/
-static bool valid_repository_path(git_buf *repository_path)
+static bool valid_repository_path(git_buf *repository_path, git_buf *common_path)
{
- /* Check OBJECTS_DIR first, since it will generate the longest path name */
- if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR) == false)
- return false;
+ /* Check if we have a separate commondir (e.g. we have a
+ * worktree) */
+ if (git_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) {
+ git_buf common_link = GIT_BUF_INIT;
+ git_buf_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE);
+
+ git_futils_readbuffer(&common_link, common_link.ptr);
+ git_buf_rtrim(&common_link);
+
+ if (git_path_is_relative(common_link.ptr)) {
+ git_buf_joinpath(common_path, repository_path->ptr, common_link.ptr);
+ } else {
+ git_buf_swap(common_path, &common_link);
+ }
+
+ git_buf_free(&common_link);
+ }
+ else {
+ git_buf_set(common_path, repository_path->ptr, repository_path->size);
+ }
+
+ /* Make sure the commondir path always has a trailing * slash */
+ if (git_buf_rfind(common_path, '/') != (ssize_t)common_path->size - 1)
+ git_buf_putc(common_path, '/');
/* Ensure HEAD file exists */
if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
return false;
- if (git_path_contains_dir(repository_path, GIT_REFS_DIR) == false)
+ /* Check files in common dir */
+ if (git_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false)
+ return false;
+ if (git_path_contains_dir(common_path, GIT_REFS_DIR) == false)
return false;
return true;
@@ -206,6 +256,7 @@ int git_repository_new(git_repository **out)
GITERR_CHECK_ALLOC(repo);
repo->is_bare = 1;
+ repo->is_worktree = 0;
return 0;
}
@@ -225,9 +276,10 @@ static int load_config_data(git_repository *repo, const git_config *config)
static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path)
{
- int error;
+ int error;
git_config_entry *ce;
- git_buf worktree = GIT_BUF_INIT;
+ git_buf worktree = GIT_BUF_INIT;
+ git_buf path = GIT_BUF_INIT;
if (repo->is_bare)
return 0;
@@ -236,9 +288,26 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren
&ce, config, "core.worktree", false)) < 0)
return error;
- if (ce && ce->value) {
+ if (repo->is_worktree) {
+ char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE);
+ if (!gitlink) {
+ error = -1;
+ goto cleanup;
+ }
+
+ git_buf_attach(&worktree, gitlink, 0);
+
+ if ((git_path_dirname_r(&worktree, worktree.ptr)) < 0 ||
+ git_path_to_dir(&worktree) < 0) {
+ error = -1;
+ goto cleanup;
+ }
+
+ repo->workdir = git_buf_detach(&worktree);
+ }
+ else if (ce && ce->value) {
if ((error = git_path_prettify_dir(
- &worktree, ce->value, repo->path_repository)) < 0)
+ &worktree, ce->value, repo->gitdir)) < 0)
goto cleanup;
repo->workdir = git_buf_detach(&worktree);
@@ -246,7 +315,7 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren
else if (parent_path && git_path_isdir(parent_path->ptr))
repo->workdir = git_buf_detach(parent_path);
else {
- if (git_path_dirname_r(&worktree, repo->path_repository) < 0 ||
+ if (git_path_dirname_r(&worktree, repo->gitdir) < 0 ||
git_path_to_dir(&worktree) < 0) {
error = -1;
goto cleanup;
@@ -257,6 +326,7 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren
GITERR_CHECK_ALLOC(repo->workdir);
cleanup:
+ git_buf_free(&path);
git_config_entry_free(ce);
return error;
}
@@ -356,6 +426,7 @@ static int find_repo(
git_buf *repo_path,
git_buf *parent_path,
git_buf *link_path,
+ git_buf *common_path,
const char *start_path,
uint32_t flags,
const char *ceiling_dirs)
@@ -363,6 +434,7 @@ static int find_repo(
int error;
git_buf path = GIT_BUF_INIT;
git_buf repo_link = GIT_BUF_INIT;
+ git_buf common_link = GIT_BUF_INIT;
struct stat st;
dev_t initial_device = 0;
int min_iterations;
@@ -409,9 +481,16 @@ static int find_repo(
break;
if (S_ISDIR(st.st_mode)) {
- if (valid_repository_path(&path)) {
+ if (valid_repository_path(&path, &common_link)) {
git_path_to_dir(&path);
git_buf_set(repo_path, path.ptr, path.size);
+
+ if (link_path)
+ git_buf_attach(link_path,
+ git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0);
+ if (common_path)
+ git_buf_swap(&common_link, common_path);
+
break;
}
}
@@ -419,11 +498,13 @@ static int find_repo(
error = read_gitfile(&repo_link, path.ptr);
if (error < 0)
break;
- if (valid_repository_path(&repo_link)) {
+ if (valid_repository_path(&repo_link, &common_link)) {
git_buf_swap(repo_path, &repo_link);
if (link_path)
error = git_buf_put(link_path, path.ptr, path.size);
+ if (common_path)
+ git_buf_swap(&common_link, common_path);
}
break;
}
@@ -470,6 +551,7 @@ static int find_repo(
git_buf_free(&path);
git_buf_free(&repo_link);
+ git_buf_free(&common_link);
return error;
}
@@ -478,14 +560,15 @@ int git_repository_open_bare(
const char *bare_path)
{
int error;
- git_buf path = GIT_BUF_INIT;
+ git_buf path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
git_repository *repo = NULL;
if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0)
return error;
- if (!valid_repository_path(&path)) {
+ if (!valid_repository_path(&path, &common_path)) {
git_buf_free(&path);
+ git_buf_free(&common_path);
giterr_set(GITERR_REPOSITORY, "path is not a repository: %s", bare_path);
return GIT_ENOTFOUND;
}
@@ -493,11 +576,14 @@ int git_repository_open_bare(
repo = repository_alloc();
GITERR_CHECK_ALLOC(repo);
- repo->path_repository = git_buf_detach(&path);
- GITERR_CHECK_ALLOC(repo->path_repository);
+ repo->gitdir = git_buf_detach(&path);
+ GITERR_CHECK_ALLOC(repo->gitdir);
+ repo->commondir = git_buf_detach(&common_path);
+ GITERR_CHECK_ALLOC(repo->commondir);
/* of course we're bare! */
repo->is_bare = 1;
+ repo->is_worktree = 0;
repo->workdir = NULL;
*repo_ptr = repo;
@@ -681,7 +767,7 @@ int git_repository_open_ext(
{
int error;
git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT,
- link_path = GIT_BUF_INIT;
+ link_path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
git_repository *repo;
git_config *config = NULL;
@@ -692,7 +778,7 @@ int git_repository_open_ext(
*repo_ptr = NULL;
error = find_repo(
- &path, &parent, &link_path, start_path, flags, ceiling_dirs);
+ &path, &parent, &link_path, &common_path, start_path, flags, ceiling_dirs);
if (error < 0 || !repo_ptr)
return error;
@@ -700,13 +786,24 @@ int git_repository_open_ext(
repo = repository_alloc();
GITERR_CHECK_ALLOC(repo);
- repo->path_repository = git_buf_detach(&path);
- GITERR_CHECK_ALLOC(repo->path_repository);
+ repo->gitdir = git_buf_detach(&path);
+ GITERR_CHECK_ALLOC(repo->gitdir);
if (link_path.size) {
- repo->path_gitlink = git_buf_detach(&link_path);
- GITERR_CHECK_ALLOC(repo->path_gitlink);
+ repo->gitlink = git_buf_detach(&link_path);
+ GITERR_CHECK_ALLOC(repo->gitlink);
}
+ if (common_path.size) {
+ repo->commondir = git_buf_detach(&common_path);
+ GITERR_CHECK_ALLOC(repo->commondir);
+ }
+
+ if ((error = git_buf_joinpath(&path, repo->gitdir, "gitdir")) < 0)
+ goto cleanup;
+ /* A 'gitdir' file inside a git directory is currently
+ * only used when the repository is a working tree. */
+ if (git_path_exists(path.ptr))
+ repo->is_worktree = 1;
/*
* We'd like to have the config, but git doesn't particularly
@@ -731,6 +828,7 @@ int git_repository_open_ext(
}
cleanup:
+ git_buf_free(&path);
git_buf_free(&parent);
git_config_free(config);
@@ -748,6 +846,36 @@ int git_repository_open(git_repository **repo_out, const char *path)
repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
}
+int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *wt)
+{
+ git_buf path = GIT_BUF_INIT;
+ git_repository *repo = NULL;
+ int len, err;
+
+ assert(repo_out && wt);
+
+ *repo_out = NULL;
+ len = strlen(wt->gitlink_path);
+
+ if (len <= 4 || strcasecmp(wt->gitlink_path + len - 4, ".git")) {
+ err = -1;
+ goto out;
+ }
+
+ if ((err = git_buf_set(&path, wt->gitlink_path, len - 4)) < 0)
+ goto out;
+
+ if ((err = git_repository_open(&repo, path.ptr)) < 0)
+ goto out;
+
+ *repo_out = repo;
+
+out:
+ git_buf_free(&path);
+
+ return err;
+}
+
int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
{
git_repository *repo;
@@ -773,7 +901,7 @@ int git_repository_discover(
git_buf_sanitize(out);
- return find_repo(out, NULL, NULL, start_path, flags, ceiling_dirs);
+ return find_repo(out, NULL, NULL, NULL, start_path, flags, ceiling_dirs);
}
static int load_config(
@@ -793,8 +921,7 @@ static int load_config(
if ((error = git_config_new(&cfg)) < 0)
return error;
- error = git_buf_joinpath(
- &config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO);
+ error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG);
if (error < 0)
goto on_error;
@@ -928,7 +1055,8 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
git_buf odb_path = GIT_BUF_INIT;
git_odb *odb;
- if ((error = git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR)) < 0)
+ if ((error = git_repository_item_path(&odb_path, repo,
+ GIT_REPOSITORY_ITEM_OBJECTS)) < 0)
return error;
error = git_odb_open(&odb, odb_path.ptr);
@@ -1014,7 +1142,7 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
git_buf index_path = GIT_BUF_INIT;
git_index *index;
- if ((error = git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE)) < 0)
+ if ((error = git_buf_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0)
return error;
error = git_index_open(&index, index_path.ptr);
@@ -1130,13 +1258,13 @@ bool git_repository__reserved_names(
prefixcmp = (error || ignorecase) ? git__prefixcmp_icase :
git__prefixcmp;
- if (repo->path_gitlink &&
- reserved_names_add8dot3(repo, repo->path_gitlink) < 0)
+ if (repo->gitlink &&
+ reserved_names_add8dot3(repo, repo->gitlink) < 0)
goto on_error;
- if (repo->path_repository &&
- prefixcmp(repo->path_repository, repo->workdir) == 0 &&
- reserved_names_add8dot3(repo, repo->path_repository) < 0)
+ if (repo->gitdir &&
+ prefixcmp(repo->gitdir, repo->workdir) == 0 &&
+ reserved_names_add8dot3(repo, repo->gitdir) < 0)
goto on_error;
}
}
@@ -1193,7 +1321,7 @@ static int check_repositoryformatversion(git_config *config)
return 0;
}
-static int repo_init_create_head(const char *git_dir, const char *ref_name)
+int git_repository_create_head(const char *git_dir, const char *ref_name)
{
git_buf ref_path = GIT_BUF_INIT;
git_filebuf ref = GIT_FILEBUF_INIT;
@@ -1856,7 +1984,8 @@ int git_repository_init_ext(
git_repository_init_options *opts)
{
int error;
- git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT;
+ git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
+ common_path = GIT_BUF_INIT;
const char *wd;
assert(out && given_repo && opts);
@@ -1868,7 +1997,7 @@ int git_repository_init_ext(
goto cleanup;
wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
- if (valid_repository_path(&repo_path)) {
+ if (valid_repository_path(&repo_path, &common_path)) {
if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
giterr_set(GITERR_REPOSITORY,
@@ -1889,7 +2018,7 @@ int git_repository_init_ext(
repo_path.ptr, wd, opts)) &&
!(error = repo_init_config(
repo_path.ptr, wd, opts->flags, opts->mode)))
- error = repo_init_create_head(
+ error = git_repository_create_head(
repo_path.ptr, opts->initial_head);
}
if (error < 0)
@@ -1901,6 +2030,7 @@ int git_repository_init_ext(
error = repo_init_create_origin(*out, opts->origin_url);
cleanup:
+ git_buf_free(&common_path);
git_buf_free(&repo_path);
git_buf_free(&wd_path);
@@ -1930,6 +2060,49 @@ int git_repository_head_detached(git_repository *repo)
return exists;
}
+static int read_worktree_head(git_buf *out, git_repository *repo, const char *name)
+{
+ git_buf path = GIT_BUF_INIT;
+ int err;
+
+ assert(out && repo && name);
+
+ git_buf_clear(out);
+
+ if ((err = git_buf_printf(&path, "%s/worktrees/%s/HEAD", repo->commondir, name)) < 0)
+ goto out;
+ if (!git_path_exists(path.ptr))
+ {
+ err = -1;
+ goto out;
+ }
+
+ if ((err = git_futils_readbuffer(out, path.ptr)) < 0)
+ goto out;
+ git_buf_rtrim(out);
+
+out:
+ git_buf_free(&path);
+
+ return err;
+}
+
+int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
+{
+ git_buf buf = GIT_BUF_INIT;
+ int ret;
+
+ assert(repo && name);
+
+ if (read_worktree_head(&buf, repo, name) < 0)
+ return -1;
+
+ ret = git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) != 0;
+ git_buf_free(&buf);
+
+ return ret;
+}
+
int git_repository_head(git_reference **head_out, git_repository *repo)
{
git_reference *head;
@@ -1949,6 +2122,48 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error;
}
+int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
+{
+ git_buf buf = GIT_BUF_INIT;
+ git_reference *head;
+ int err;
+
+ assert(out && repo && name);
+
+ *out = NULL;
+
+ if (git_repository_head_detached_for_worktree(repo, name))
+ return -1;
+ if ((err = read_worktree_head(&buf, repo, name)) < 0)
+ goto out;
+
+ /* We can only resolve symbolic references */
+ if (git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
+ {
+ err = -1;
+ goto out;
+ }
+ git_buf_consume(&buf, buf.ptr + strlen(GIT_SYMREF));
+
+ if ((err = git_reference_lookup(&head, repo, buf.ptr)) < 0)
+ goto out;
+ if (git_reference_type(head) == GIT_REF_OID)
+ {
+ *out = head;
+ err = 0;
+ goto out;
+ }
+
+ err = git_reference_lookup_resolved(
+ out, repo, git_reference_symbolic_target(head), -1);
+ git_reference_free(head);
+
+out:
+ git_buf_free(&buf);
+
+ return err;
+}
+
int git_repository_head_unborn(git_repository *repo)
{
git_reference *ref = NULL;
@@ -2007,10 +2222,50 @@ int git_repository_is_empty(git_repository *repo)
return is_empty;
}
+int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item)
+{
+ const char *parent;
+
+ switch (items[item].parent) {
+ case GIT_REPOSITORY_ITEM_GITDIR:
+ parent = git_repository_path(repo);
+ break;
+ case GIT_REPOSITORY_ITEM_WORKDIR:
+ parent = git_repository_workdir(repo);
+ break;
+ case GIT_REPOSITORY_ITEM_COMMONDIR:
+ parent = git_repository_commondir(repo);
+ break;
+ default:
+ giterr_set(GITERR_INVALID, "Invalid item directory");
+ return -1;
+ }
+
+ if (parent == NULL) {
+ giterr_set(GITERR_INVALID, "Path cannot exist in repository");
+ return -1;
+ }
+
+ if (git_buf_sets(out, parent) < 0)
+ return -1;
+
+ if (items[item].name) {
+ if (git_buf_joinpath(out, parent, items[item].name) < 0)
+ return -1;
+ }
+
+ if (items[item].directory) {
+ if (git_path_to_dir(out) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
const char *git_repository_path(git_repository *repo)
{
assert(repo);
- return repo->path_repository;
+ return repo->gitdir;
}
const char *git_repository_workdir(git_repository *repo)
@@ -2023,6 +2278,12 @@ const char *git_repository_workdir(git_repository *repo)
return repo->workdir;
}
+const char *git_repository_commondir(git_repository *repo)
+{
+ assert(repo);
+ return repo->commondir;
+}
+
int git_repository_set_workdir(
git_repository *repo, const char *workdir, int update_gitlink)
{
@@ -2073,6 +2334,12 @@ int git_repository_is_bare(git_repository *repo)
return repo->is_bare;
}
+int git_repository_is_worktree(git_repository *repo)
+{
+ assert(repo);
+ return repo->is_worktree;
+}
+
int git_repository_set_bare(git_repository *repo)
{
int error;
@@ -2127,7 +2394,7 @@ int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head
git_oid_fmt(orig_head_str, orig_head);
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_ORIG_HEAD_FILE)) == 0 &&
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) == 0 &&
(error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_HEXSZ, orig_head_str)) == 0)
error = git_filebuf_commit(&file);
@@ -2148,7 +2415,7 @@ int git_repository_message(git_buf *out, git_repository *repo)
git_buf_sanitize(out);
- if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
return -1;
if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) {
@@ -2169,7 +2436,7 @@ int git_repository_message_remove(git_repository *repo)
git_buf path = GIT_BUF_INIT;
int error;
- if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
+ if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
return -1;
error = p_unlink(git_buf_cstr(&path));
@@ -2290,6 +2557,12 @@ int git_repository_set_head(
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
+ if (ref && current->type == GIT_REF_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
+ git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
+ error = -1;
+ goto cleanup;
+ }
+
if (!error) {
if (git_reference_is_branch(ref)) {
error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
@@ -2405,7 +2678,7 @@ int git_repository_state(git_repository *repo)
assert(repo);
- if (git_buf_puts(&repo_path, repo->path_repository) < 0)
+ if (git_buf_puts(&repo_path, repo->gitdir) < 0)
return -1;
if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE))
@@ -2447,7 +2720,7 @@ int git_repository__cleanup_files(
for (error = 0, i = 0; !error && i < files_len; ++i) {
const char *path;
- if (git_buf_joinpath(&buf, repo->path_repository, files[i]) < 0)
+ if (git_buf_joinpath(&buf, repo->gitdir, files[i]) < 0)
return -1;
path = git_buf_cstr(&buf);
@@ -2491,7 +2764,7 @@ int git_repository_is_shallow(git_repository *repo)
struct stat st;
int error;
- if ((error = git_buf_joinpath(&path, repo->path_repository, "shallow")) < 0)
+ if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0)
return error;
error = git_path_lstat(path.ptr, &st);
diff --git a/src/repository.h b/src/repository.h
index 9d276f3..c328ecd 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -126,8 +126,9 @@ struct git_repository {
git_attr_cache *attrcache;
git_diff_driver_registry *diff_drivers;
- char *path_repository;
- char *path_gitlink;
+ char *gitlink;
+ char *gitdir;
+ char *commondir;
char *workdir;
char *namespace;
@@ -137,6 +138,7 @@ struct git_repository {
git_array_t(git_buf) reserved_names;
unsigned is_bare:1;
+ unsigned is_worktree:1;
unsigned int lru_counter;
@@ -152,6 +154,7 @@ GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
}
int git_repository_head_tree(git_tree **tree, git_repository *repo);
+int git_repository_create_head(const char *git_dir, const char *ref_name);
/*
* Weak pointers to repository internals.
diff --git a/src/revert.c b/src/revert.c
index b255245..747938f 100644
--- a/src/revert.c
+++ b/src/revert.c
@@ -27,7 +27,7 @@ static int write_revert_head(
git_buf file_path = GIT_BUF_INIT;
int error = 0;
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_REVERT_HEAD_FILE)) >= 0 &&
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) >= 0 &&
(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
error = git_filebuf_commit(&file);
@@ -49,7 +49,7 @@ static int write_merge_msg(
git_buf file_path = GIT_BUF_INIT;
int error = 0;
- if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
+ if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 ||
(error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
commit_msgline, commit_oidstr)) < 0)
diff --git a/src/submodule.c b/src/submodule.c
index 1c17075..3007d25 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -616,8 +616,10 @@ static int submodule_repo_init(
* Old style: sub-repo goes directly into repo/<name>/.git/
*/
if (use_gitlink) {
- error = git_buf_join3(
- &repodir, '/', git_repository_path(parent_repo), "modules", path);
+ error = git_repository_item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES);
+ if (error < 0)
+ goto cleanup;
+ error = git_buf_joinpath(&repodir, repodir.ptr, path);
if (error < 0)
goto cleanup;
@@ -1084,8 +1086,10 @@ static int submodule_repo_create(
* <repo-dir>/modules/<name>/ with a gitlink in the
* sub-repo workdir directory to that repository.
*/
- error = git_buf_join3(
- &repodir, '/', git_repository_path(parent_repo), "modules", path);
+ error = git_repository_item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES);
+ if (error < 0)
+ goto cleanup;
+ error = git_buf_joinpath(&repodir, repodir.ptr, path);
if (error < 0)
goto cleanup;
diff --git a/src/transports/local.c b/src/transports/local.c
index 87745ad..e24e998 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -375,7 +375,8 @@ static int local_push(
goto on_error;
}
- if ((error = git_buf_joinpath(&odb_path, git_repository_path(remote_repo), "objects/pack")) < 0)
+ if ((error = git_repository_item_path(&odb_path, remote_repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
+ || (error = git_buf_joinpath(&odb_path, odb_path.ptr, "pack")) < 0)
goto on_error;
error = git_packbuilder_write(push->pb, odb_path.ptr, 0, transfer_to_push_transfer, (void *) cbs);
diff --git a/src/worktree.c b/src/worktree.c
new file mode 100644
index 0000000..5abc989
--- /dev/null
+++ b/src/worktree.c
@@ -0,0 +1,432 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+
+#include "git2/branch.h"
+#include "git2/commit.h"
+#include "git2/worktree.h"
+
+#include "repository.h"
+#include "worktree.h"
+
+static bool is_worktree_dir(git_buf *dir)
+{
+ return git_path_contains_file(dir, "commondir")
+ && git_path_contains_file(dir, "gitdir")
+ && git_path_contains_file(dir, "HEAD");
+}
+
+int git_worktree_list(git_strarray *wts, git_repository *repo)
+{
+ git_vector worktrees = GIT_VECTOR_INIT;
+ git_buf path = GIT_BUF_INIT;
+ char *worktree;
+ unsigned i, len;
+ int error;
+
+ assert(wts && repo);
+
+ wts->count = 0;
+ wts->strings = NULL;
+
+ if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
+ goto exit;
+ if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
+ goto exit;
+ if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
+ goto exit;
+
+ len = path.size;
+
+ git_vector_foreach(&worktrees, i, worktree) {
+ git_buf_truncate(&path, len);
+ git_buf_puts(&path, worktree);
+
+ if (!is_worktree_dir(&path)) {
+ git_vector_remove(&worktrees, i);
+ git__free(worktree);
+ }
+ }
+
+ wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);
+
+exit:
+ git_buf_free(&path);
+
+ return error;
+}
+
+char *git_worktree__read_link(const char *base, const char *file)
+{
+ git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+
+ assert(base && file);
+
+ if (git_buf_joinpath(&path, base, file) < 0)
+ goto err;
+ if (git_futils_readbuffer(&buf, path.ptr) < 0)
+ goto err;
+ git_buf_free(&path);
+
+ git_buf_rtrim(&buf);
+
+ if (!git_path_is_relative(buf.ptr))
+ return git_buf_detach(&buf);
+
+ if (git_buf_sets(&path, base) < 0)
+ goto err;
+ if (git_path_apply_relative(&path, buf.ptr) < 0)
+ goto err;
+ git_buf_free(&buf);
+
+ return git_buf_detach(&path);
+
+err:
+ git_buf_free(&buf);
+ git_buf_free(&path);
+
+ return NULL;
+}
+
+static int write_wtfile(const char *base, const char *file, const git_buf *buf)
+{
+ git_buf path = GIT_BUF_INIT;
+ int err;
+
+ assert(base && file && buf);
+
+ if ((err = git_buf_joinpath(&path, base, file)) < 0)
+ goto out;
+
+ if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
+ goto out;
+
+out:
+ git_buf_free(&path);
+
+ return err;
+}
+
+int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
+{
+ git_buf path = GIT_BUF_INIT;
+ git_worktree *wt = NULL;
+ int error;
+
+ assert(repo && name);
+
+ *out = NULL;
+
+ if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
+ goto out;
+
+ if (!is_worktree_dir(&path)) {
+ error = -1;
+ goto out;
+ }
+
+ if ((wt = git__malloc(sizeof(struct git_repository))) == NULL) {
+ error = -1;
+ goto out;
+ }
+
+ if ((wt->name = git__strdup(name)) == NULL
+ || (wt->commondir_path = git_worktree__read_link(path.ptr, "commondir")) == NULL
+ || (wt->gitlink_path = git_worktree__read_link(path.ptr, "gitdir")) == NULL
+ || (wt->parent_path = git__strdup(git_repository_path(repo))) == NULL) {
+ error = -1;
+ goto out;
+ }
+ wt->gitdir_path = git_buf_detach(&path);
+ wt->locked = !!git_worktree_is_locked(NULL, wt);
+
+ (*out) = wt;
+
+out:
+ git_buf_free(&path);
+
+ if (error)
+ git_worktree_free(wt);
+
+ return error;
+}
+
+void git_worktree_free(git_worktree *wt)
+{
+ if (!wt)
+ return;
+
+ git__free(wt->commondir_path);
+ git__free(wt->gitlink_path);
+ git__free(wt->gitdir_path);
+ git__free(wt->parent_path);
+ git__free(wt->name);
+ git__free(wt);
+}
+
+int git_worktree_validate(const git_worktree *wt)
+{
+ git_buf buf = GIT_BUF_INIT;
+ int err = 0;
+
+ assert(wt);
+
+ git_buf_puts(&buf, wt->gitdir_path);
+ if (!is_worktree_dir(&buf)) {
+ giterr_set(GITERR_WORKTREE,
+ "Worktree gitdir ('%s') is not valid",
+ wt->gitlink_path);
+ err = -1;
+ goto out;
+ }
+
+ if (!git_path_exists(wt->parent_path)) {
+ giterr_set(GITERR_WORKTREE,
+ "Worktree parent directory ('%s') does not exist ",
+ wt->parent_path);
+ err = -2;
+ goto out;
+ }
+
+ if (!git_path_exists(wt->commondir_path)) {
+ giterr_set(GITERR_WORKTREE,
+ "Worktree common directory ('%s') does not exist ",
+ wt->commondir_path);
+ err = -3;
+ goto out;
+ }
+
+out:
+ git_buf_free(&buf);
+
+ return err;
+}
+
+int git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *worktree)
+{
+ git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+ git_reference *ref = NULL, *head = NULL;
+ git_commit *commit = NULL;
+ git_repository *wt = NULL;
+ git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
+ int err;
+
+ assert(out && repo && name && worktree);
+
+ *out = NULL;
+
+ /* Create worktree related files in commondir */
+ if ((err = git_buf_joinpath(&path, repo->commondir, "worktrees")) < 0)
+ goto out;
+ if (!git_path_exists(path.ptr))
+ if ((err = git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
+ goto out;
+ if ((err = git_buf_joinpath(&path, path.ptr, name)) < 0)
+ goto out;
+ if ((err = git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
+ goto out;
+
+ /* Create worktree work dir */
+ if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
+ goto out;
+
+ /* Create worktree .git file */
+ if ((err = git_buf_printf(&buf, "gitdir: %s\n", path.ptr)) < 0)
+ goto out;
+ if ((err = write_wtfile(worktree, ".git", &buf)) < 0)
+ goto out;
+
+ /* Create commondir files */
+ if ((err = git_buf_sets(&buf, repo->commondir)) < 0
+ || (err = git_buf_putc(&buf, '\n')) < 0
+ || (err = write_wtfile(path.ptr, "commondir", &buf)) < 0)
+ goto out;
+ if ((err = git_buf_joinpath(&buf, worktree, ".git")) < 0
+ || (err = git_buf_putc(&buf, '\n')) < 0
+ || (err = write_wtfile(path.ptr, "gitdir", &buf)) < 0)
+ goto out;
+
+ /* Create new branch */
+ if ((err = git_repository_head(&head, repo)) < 0)
+ goto out;
+ if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
+ goto out;
+ if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
+ goto out;
+
+ /* Set worktree's HEAD */
+ if ((err = git_repository_create_head(path.ptr, name)) < 0)
+ goto out;
+ if ((err = git_repository_open(&wt, worktree)) < 0)
+ goto out;
+
+ /* Checkout worktree's HEAD */
+ coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ if ((err = git_checkout_head(wt, &coopts)) < 0)
+ goto out;
+
+ /* Load result */
+ if ((err = git_worktree_lookup(out, repo, name)) < 0)
+ goto out;
+
+out:
+ git_buf_free(&path);
+ git_buf_free(&buf);
+ git_reference_free(ref);
+ git_reference_free(head);
+ git_commit_free(commit);
+ git_repository_free(wt);
+
+ return err;
+}
+
+int git_worktree_lock(git_worktree *wt, char *creason)
+{
+ git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
+ int err;
+
+ assert(wt);
+
+ if ((err = git_worktree_is_locked(NULL, wt)) < 0)
+ goto out;
+
+ if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
+ goto out;
+
+ if (creason)
+ git_buf_attach_notowned(&buf, creason, strlen(creason));
+
+ if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
+ goto out;
+
+ wt->locked = 1;
+
+out:
+ git_buf_free(&path);
+
+ return err;
+}
+
+int git_worktree_unlock(git_worktree *wt)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ assert(wt);
+
+ if (!git_worktree_is_locked(NULL, wt))
+ return 0;
+
+ if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
+ return -1;
+
+ if (p_unlink(path.ptr) != 0) {
+ git_buf_free(&path);
+ return -1;
+ }
+
+ wt->locked = 0;
+
+ git_buf_free(&path);
+
+ return 0;
+}
+
+int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
+{
+ git_buf path = GIT_BUF_INIT;
+ int ret;
+
+ assert(wt);
+
+ if (reason)
+ git_buf_clear(reason);
+
+ if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
+ goto out;
+ if ((ret = git_path_exists(path.ptr)) && reason)
+ git_futils_readbuffer(reason, path.ptr);
+
+out:
+ git_buf_free(&path);
+
+ return ret;
+}
+
+int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
+{
+ git_buf reason = GIT_BUF_INIT;
+
+ if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
+ git_worktree_is_locked(&reason, wt))
+ {
+ if (!reason.size)
+ git_buf_attach_notowned(&reason, "no reason given", 15);
+ giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr);
+ git_buf_free(&reason);
+
+ return 0;
+ }
+
+ if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
+ git_worktree_validate(wt) == 0)
+ {
+ giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
+ return 0;
+ }
+
+ return 1;
+}
+
+int git_worktree_prune(git_worktree *wt, unsigned flags)
+{
+ git_buf path = GIT_BUF_INIT;
+ char *wtpath;
+ int err;
+
+ if (!git_worktree_is_prunable(wt, flags)) {
+ err = -1;
+ goto out;
+ }
+
+ /* Delete gitdir in parent repository */
+ if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->parent_path, wt->name)) < 0)
+ goto out;
+ if (!git_path_exists(path.ptr))
+ {
+ giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr);
+ err = -1;
+ goto out;
+ }
+ if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
+ goto out;
+
+ /* Skip deletion of the actual working tree if it does
+ * not exist or deletion was not requested */
+ if ((flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
+ !git_path_exists(wt->gitlink_path))
+ {
+ goto out;
+ }
+
+ if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL)
+ goto out;
+ git_buf_attach(&path, wtpath, 0);
+ if (!git_path_exists(path.ptr))
+ {
+ giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr);
+ err = -1;
+ goto out;
+ }
+ if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
+ goto out;
+
+out:
+ git_buf_free(&path);
+
+ return err;
+}
diff --git a/src/worktree.h b/src/worktree.h
new file mode 100644
index 0000000..b8e5279
--- /dev/null
+++ b/src/worktree.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_worktree_h__
+#define INCLUDE_worktree_h__
+
+#include "git2/common.h"
+#include "git2/worktree.h"
+
+struct git_worktree {
+ /* Name of the working tree. This is the name of the
+ * containing directory in the `$PARENT/.git/worktrees/`
+ * directory. */
+ char *name;
+
+ /* Path to the .git file in the working tree's repository */
+ char *gitlink_path;
+ /* Path to the .git directory inside the parent's
+ * worktrees directory */
+ char *gitdir_path;
+ /* Path to the common directory contained in the parent
+ * repository */
+ char *commondir_path;
+ /* Path to the parent's .git directory */
+ char *parent_path;
+
+ int locked:1;
+};
+
+char *git_worktree__read_link(const char *base, const char *file);
+
+#endif
diff --git a/tests/iterator/workdir.c b/tests/iterator/workdir.c
index 28fcc0d..f33fd98 100644
--- a/tests/iterator/workdir.c
+++ b/tests/iterator/workdir.c
@@ -613,9 +613,11 @@ void test_iterator_workdir__filesystem2(void)
"heads/ident",
"heads/long-file-name",
"heads/master",
+ "heads/merge-conflict",
"heads/packed-test",
"heads/subtrees",
"heads/test",
+ "heads/testrepo-worktree",
"tags/e90810b",
"tags/foo/bar",
"tags/foo/foo/bar",
@@ -628,7 +630,7 @@ void test_iterator_workdir__filesystem2(void)
cl_git_pass(git_iterator_for_filesystem(
&i, "testrepo/.git/refs", NULL));
- expect_iterator_items(i, 13, expect_base, 13, expect_base);
+ expect_iterator_items(i, 15, expect_base, 15, expect_base);
git_iterator_free(i);
}
diff --git a/tests/refs/list.c b/tests/refs/list.c
index 374943b..f7ca3f7 100644
--- a/tests/refs/list.c
+++ b/tests/refs/list.c
@@ -36,7 +36,7 @@ void test_refs_list__all(void)
/* We have exactly 12 refs in total if we include the packed ones:
* there is a reference that exists both in the packfile and as
* loose, but we only list it once */
- cl_assert_equal_i((int)ref_list.count, 15);
+ cl_assert_equal_i((int)ref_list.count, 17);
git_strarray_free(&ref_list);
}
@@ -51,7 +51,7 @@ void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_exten
"144344043ba4d4a405da03de3844aa829ae8be0e\n");
cl_git_pass(git_reference_list(&ref_list, g_repo));
- cl_assert_equal_i((int)ref_list.count, 15);
+ cl_assert_equal_i((int)ref_list.count, 17);
git_strarray_free(&ref_list);
}
diff --git a/tests/resources/submodules-worktree-child/.gitted b/tests/resources/submodules-worktree-child/.gitted
new file mode 100644
index 0000000..03286f5
--- /dev/null
+++ b/tests/resources/submodules-worktree-child/.gitted
@@ -0,0 +1 @@
+gitdir: ../submodules/testrepo/.git/worktrees/submodules-worktree-child
diff --git a/tests/resources/submodules-worktree-child/README b/tests/resources/submodules-worktree-child/README
new file mode 100644
index 0000000..a823312
--- /dev/null
+++ b/tests/resources/submodules-worktree-child/README
@@ -0,0 +1 @@
+hey there
diff --git a/tests/resources/submodules-worktree-child/branch_file.txt b/tests/resources/submodules-worktree-child/branch_file.txt
new file mode 100644
index 0000000..3697d64
--- /dev/null
+++ b/tests/resources/submodules-worktree-child/branch_file.txt
@@ -0,0 +1,2 @@
+hi
+bye!
diff --git a/tests/resources/submodules-worktree-child/new.txt b/tests/resources/submodules-worktree-child/new.txt
new file mode 100644
index 0000000..a71586c
--- /dev/null
+++ b/tests/resources/submodules-worktree-child/new.txt
@@ -0,0 +1 @@
+my new file
diff --git a/tests/resources/submodules-worktree-parent/.gitmodules b/tests/resources/submodules-worktree-parent/.gitmodules
new file mode 100644
index 0000000..78308c9
--- /dev/null
+++ b/tests/resources/submodules-worktree-parent/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "testrepo"]
+ path = testrepo
+ url = /Users/rb/src/libgit2/tests/resources/testrepo.git
diff --git a/tests/resources/submodules-worktree-parent/.gitted b/tests/resources/submodules-worktree-parent/.gitted
new file mode 100644
index 0000000..87bd9ae
--- /dev/null
+++ b/tests/resources/submodules-worktree-parent/.gitted
@@ -0,0 +1 @@
+gitdir: ../submodules/.git/worktrees/submodules-worktree-parent
diff --git a/tests/resources/submodules-worktree-parent/deleted b/tests/resources/submodules-worktree-parent/deleted
new file mode 100644
index 0000000..092bfb9
--- /dev/null
+++ b/tests/resources/submodules-worktree-parent/deleted
@@ -0,0 +1 @@
+yo
diff --git a/tests/resources/submodules-worktree-parent/modified b/tests/resources/submodules-worktree-parent/modified
new file mode 100644
index 0000000..092bfb9
--- /dev/null
+++ b/tests/resources/submodules-worktree-parent/modified
@@ -0,0 +1 @@
+yo
diff --git a/tests/resources/submodules-worktree-parent/unmodified b/tests/resources/submodules-worktree-parent/unmodified
new file mode 100644
index 0000000..092bfb9
--- /dev/null
+++ b/tests/resources/submodules-worktree-parent/unmodified
@@ -0,0 +1 @@
+yo
diff --git a/tests/resources/submodules/.gitted/logs/refs/heads/submodules-worktree-parent b/tests/resources/submodules/.gitted/logs/refs/heads/submodules-worktree-parent
new file mode 100644
index 0000000..65e9885
--- /dev/null
+++ b/tests/resources/submodules/.gitted/logs/refs/heads/submodules-worktree-parent
@@ -0,0 +1 @@
+0000000000000000000000000000000000000000 97896810b3210244a62a82458b8e0819ecfc6850 Patrick Steinhardt <ps@pks.im> 1447084240 +0100 branch: Created from HEAD
diff --git a/tests/resources/submodules/.gitted/refs/heads/submodules-worktree-parent b/tests/resources/submodules/.gitted/refs/heads/submodules-worktree-parent
new file mode 100644
index 0000000..32b9358
--- /dev/null
+++ b/tests/resources/submodules/.gitted/refs/heads/submodules-worktree-parent
@@ -0,0 +1 @@
+97896810b3210244a62a82458b8e0819ecfc6850
diff --git a/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/HEAD b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/HEAD
new file mode 100644
index 0000000..a07134b
--- /dev/null
+++ b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/submodules-worktree-parent
diff --git a/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/ORIG_HEAD b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/ORIG_HEAD
new file mode 100644
index 0000000..32b9358
--- /dev/null
+++ b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/ORIG_HEAD
@@ -0,0 +1 @@
+97896810b3210244a62a82458b8e0819ecfc6850
diff --git a/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/commondir b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/commondir
new file mode 100644
index 0000000..aab0408
--- /dev/null
+++ b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/commondir
@@ -0,0 +1 @@
+../..
diff --git a/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/gitdir b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/gitdir
new file mode 100644
index 0000000..eaaf13b
--- /dev/null
+++ b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/gitdir
@@ -0,0 +1 @@
+../../../../submodules-worktree-parent/.git
diff --git a/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/index b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/index
new file mode 100644
index 0000000..5b68f18
Binary files /dev/null and b/tests/resources/submodules/.gitted/worktrees/submodules-worktree-parent/index differ
diff --git a/tests/resources/submodules/testrepo/.gitted/logs/refs/heads/submodules-worktree-child b/tests/resources/submodules/testrepo/.gitted/logs/refs/heads/submodules-worktree-child
new file mode 100644
index 0000000..dd4650f
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/logs/refs/heads/submodules-worktree-child
@@ -0,0 +1 @@
+0000000000000000000000000000000000000000 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Patrick Steinhardt <ps@pks.im> 1447084252 +0100 branch: Created from HEAD
diff --git a/tests/resources/submodules/testrepo/.gitted/refs/heads/submodules-worktree-child b/tests/resources/submodules/testrepo/.gitted/refs/heads/submodules-worktree-child
new file mode 100644
index 0000000..3d8f0a4
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/refs/heads/submodules-worktree-child
@@ -0,0 +1 @@
+a65fedf39aefe402d3bb6e24df4d4f5fe4547750
diff --git a/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/HEAD b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/HEAD
new file mode 100644
index 0000000..ef82bd4
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/submodules-worktree-child
diff --git a/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/ORIG_HEAD b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/ORIG_HEAD
new file mode 100644
index 0000000..3d8f0a4
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/ORIG_HEAD
@@ -0,0 +1 @@
+a65fedf39aefe402d3bb6e24df4d4f5fe4547750
diff --git a/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/commondir b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/commondir
new file mode 100644
index 0000000..aab0408
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/commondir
@@ -0,0 +1 @@
+../..
diff --git a/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/gitdir b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/gitdir
new file mode 100644
index 0000000..b0ef96e
--- /dev/null
+++ b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/gitdir
@@ -0,0 +1 @@
+../../../../../submodules-worktree-child/.git
diff --git a/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/index b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/index
new file mode 100644
index 0000000..52a42f9
Binary files /dev/null and b/tests/resources/submodules/testrepo/.gitted/worktrees/submodules-worktree-child/index differ
diff --git a/tests/resources/testrepo-worktree/.gitted b/tests/resources/testrepo-worktree/.gitted
new file mode 100644
index 0000000..fe4556a
--- /dev/null
+++ b/tests/resources/testrepo-worktree/.gitted
@@ -0,0 +1 @@
+gitdir: ../testrepo/.git/worktrees/testrepo-worktree
diff --git a/tests/resources/testrepo-worktree/README b/tests/resources/testrepo-worktree/README
new file mode 100644
index 0000000..a823312
--- /dev/null
+++ b/tests/resources/testrepo-worktree/README
@@ -0,0 +1 @@
+hey there
diff --git a/tests/resources/testrepo-worktree/branch_file.txt b/tests/resources/testrepo-worktree/branch_file.txt
new file mode 100644
index 0000000..3697d64
--- /dev/null
+++ b/tests/resources/testrepo-worktree/branch_file.txt
@@ -0,0 +1,2 @@
+hi
+bye!
diff --git a/tests/resources/testrepo-worktree/link_to_new.txt b/tests/resources/testrepo-worktree/link_to_new.txt
new file mode 120000
index 0000000..c0528fd
--- /dev/null
+++ b/tests/resources/testrepo-worktree/link_to_new.txt
@@ -0,0 +1 @@
+new.txt
\ No newline at end of file
diff --git a/tests/resources/testrepo-worktree/new.txt b/tests/resources/testrepo-worktree/new.txt
new file mode 100644
index 0000000..a71586c
--- /dev/null
+++ b/tests/resources/testrepo-worktree/new.txt
@@ -0,0 +1 @@
+my new file
diff --git a/tests/resources/testrepo/.gitted/logs/refs/heads/testrepo-worktree b/tests/resources/testrepo/.gitted/logs/refs/heads/testrepo-worktree
new file mode 100644
index 0000000..93ab5f0
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/logs/refs/heads/testrepo-worktree
@@ -0,0 +1 @@
+0000000000000000000000000000000000000000 099fabac3a9ea935598528c27f866e34089c2eff Patrick Steinhardt <ps@pks.im> 1442484463 +0200 branch: Created from HEAD
diff --git a/tests/resources/testrepo/.gitted/objects/9b/1719f5cf069568785080a0bbabbe7c377e22ae b/tests/resources/testrepo/.gitted/objects/9b/1719f5cf069568785080a0bbabbe7c377e22ae
new file mode 100644
index 0000000..13e3f58
Binary files /dev/null and b/tests/resources/testrepo/.gitted/objects/9b/1719f5cf069568785080a0bbabbe7c377e22ae differ
diff --git a/tests/resources/testrepo/.gitted/objects/a3/8d028f71eaa590febb7d716b1ca32350cf70da b/tests/resources/testrepo/.gitted/objects/a3/8d028f71eaa590febb7d716b1ca32350cf70da
new file mode 100644
index 0000000..4df22ec
Binary files /dev/null and b/tests/resources/testrepo/.gitted/objects/a3/8d028f71eaa590febb7d716b1ca32350cf70da differ
diff --git a/tests/resources/testrepo/.gitted/objects/ad/edac69457183c8265c8a9614c1c4fed31d1ff3 b/tests/resources/testrepo/.gitted/objects/ad/edac69457183c8265c8a9614c1c4fed31d1ff3
new file mode 100644
index 0000000..c054fc0
Binary files /dev/null and b/tests/resources/testrepo/.gitted/objects/ad/edac69457183c8265c8a9614c1c4fed31d1ff3 differ
diff --git a/tests/resources/testrepo/.gitted/refs/heads/merge-conflict b/tests/resources/testrepo/.gitted/refs/heads/merge-conflict
new file mode 100644
index 0000000..3e24a24
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/refs/heads/merge-conflict
@@ -0,0 +1 @@
+a38d028f71eaa590febb7d716b1ca32350cf70da
diff --git a/tests/resources/testrepo/.gitted/refs/heads/testrepo-worktree b/tests/resources/testrepo/.gitted/refs/heads/testrepo-worktree
new file mode 100644
index 0000000..f31fe78
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/refs/heads/testrepo-worktree
@@ -0,0 +1 @@
+099fabac3a9ea935598528c27f866e34089c2eff
diff --git a/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/HEAD b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/HEAD
new file mode 100644
index 0000000..1b8637e
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/testrepo-worktree
diff --git a/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/commondir b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/commondir
new file mode 100644
index 0000000..aab0408
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/commondir
@@ -0,0 +1 @@
+../..
diff --git a/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/gitdir b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/gitdir
new file mode 100644
index 0000000..0d37a57
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/gitdir
@@ -0,0 +1 @@
+../../../../testrepo-worktree/.git
diff --git a/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/index b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/index
new file mode 100644
index 0000000..4114190
Binary files /dev/null and b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/index differ
diff --git a/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/logs/HEAD b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/logs/HEAD
new file mode 100644
index 0000000..3bede50
--- /dev/null
+++ b/tests/resources/testrepo/.gitted/worktrees/testrepo-worktree/logs/HEAD
@@ -0,0 +1 @@
+099fabac3a9ea935598528c27f866e34089c2eff 099fabac3a9ea935598528c27f866e34089c2eff Patrick Steinhardt <ps@pks.im> 1442484463 +0200 checkout: moving from 099fabac3a9ea935598528c27f866e34089c2eff to testrepo-worktree
diff --git a/tests/revwalk/basic.c b/tests/revwalk/basic.c
index 572035c..a38d7f4 100644
--- a/tests/revwalk/basic.c
+++ b/tests/revwalk/basic.c
@@ -177,7 +177,7 @@ void test_revwalk_basic__glob_heads_with_invalid(void)
/* walking */;
/* git log --branches --oneline | wc -l => 16 */
- cl_assert_equal_i(18, i);
+ cl_assert_equal_i(19, i);
}
void test_revwalk_basic__push_head(void)
diff --git a/tests/worktree/config.c b/tests/worktree/config.c
new file mode 100644
index 0000000..3ab317b
--- /dev/null
+++ b/tests/worktree/config.c
@@ -0,0 +1,45 @@
+#include "clar_libgit2.h"
+#include "worktree_helpers.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+void test_worktree_config__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_config__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_config__open(void)
+{
+ git_config *cfg;
+
+ cl_git_pass(git_repository_config(&cfg, fixture.worktree));
+ cl_assert(cfg != NULL);
+
+ git_config_free(cfg);
+}
+
+void test_worktree_config__set(void)
+{
+ git_config *cfg;
+ int32_t val;
+
+ cl_git_pass(git_repository_config(&cfg, fixture.worktree));
+ cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5));
+ git_config_free(cfg);
+
+ // reopen to verify configuration has been set in the
+ // common dir
+ cl_git_pass(git_repository_config(&cfg, fixture.repo));
+ cl_git_pass(git_config_get_int32(&val, cfg, "core.dummy"));
+ cl_assert_equal_i(val, 5);
+ git_config_free(cfg);
+}
diff --git a/tests/worktree/merge.c b/tests/worktree/merge.c
new file mode 100644
index 0000000..36cc2a6
--- /dev/null
+++ b/tests/worktree/merge.c
@@ -0,0 +1,121 @@
+#include "clar_libgit2.h"
+
+#include "worktree_helpers.h"
+#include "merge/merge_helpers.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+#define MASTER_BRANCH "refs/heads/master"
+#define CONFLICT_BRANCH "refs/heads/merge-conflict"
+
+#define CONFLICT_BRANCH_FILE_TXT \
+ "<<<<<<< HEAD\n" \
+ "hi\n" \
+ "bye!\n" \
+ "=======\n" \
+ "conflict\n" \
+ ">>>>>>> merge-conflict\n" \
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+static const char *merge_files[] = {
+ GIT_MERGE_HEAD_FILE,
+ GIT_ORIG_HEAD_FILE,
+ GIT_MERGE_MODE_FILE,
+ GIT_MERGE_MSG_FILE,
+};
+
+void test_worktree_merge__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_merge__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_merge__merge_head(void)
+{
+ git_reference *theirs_ref, *ref;
+ git_annotated_commit *theirs;
+
+ cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH));
+ cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref));
+ cl_git_pass(git_merge(fixture.worktree, (const git_annotated_commit **)&theirs, 1, NULL, NULL));
+
+ cl_git_pass(git_reference_lookup(&ref, fixture.worktree, GIT_MERGE_HEAD_FILE));
+
+ git_reference_free(ref);
+ git_reference_free(theirs_ref);
+ git_annotated_commit_free(theirs);
+}
+
+void test_worktree_merge__merge_setup(void)
+{
+ git_reference *ours_ref, *theirs_ref;
+ git_annotated_commit *ours, *theirs;
+ git_buf path = GIT_BUF_INIT;
+ unsigned i;
+
+ cl_git_pass(git_reference_lookup(&ours_ref, fixture.worktree, MASTER_BRANCH));
+ cl_git_pass(git_annotated_commit_from_ref(&ours, fixture.worktree, ours_ref));
+
+ cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH));
+ cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref));
+
+ cl_git_pass(git_merge__setup(fixture.worktree,
+ ours, (const git_annotated_commit **)&theirs, 1));
+
+ for (i = 0; i < ARRAY_SIZE(merge_files); i++) {
+ git_buf_clear(&path);
+ cl_git_pass(git_buf_printf(&path, "%s/%s",
+ fixture.worktree->gitdir, merge_files[i]));
+ cl_assert(git_path_exists(path.ptr));
+ }
+
+ git_buf_free(&path);
+ git_reference_free(ours_ref);
+ git_reference_free(theirs_ref);
+ git_annotated_commit_free(ours);
+ git_annotated_commit_free(theirs);
+}
+
+void test_worktree_merge__merge_conflict(void)
+{
+ git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+ git_reference *theirs_ref;
+ git_annotated_commit *theirs;
+ git_index *index;
+ const git_index_entry *entry;
+ size_t i, conflicts = 0;
+
+ cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH));
+ cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref));
+
+ cl_git_pass(git_merge(fixture.worktree,
+ (const git_annotated_commit **)&theirs, 1, NULL, NULL));
+
+ cl_git_pass(git_repository_index(&index, fixture.worktree));
+ for (i = 0; i < git_index_entrycount(index); i++) {
+ cl_assert(entry = git_index_get_byindex(index, i));
+
+ if (git_index_entry_is_conflict(entry))
+ conflicts++;
+ }
+ cl_assert_equal_sz(conflicts, 3);
+
+ git_reference_free(theirs_ref);
+ git_annotated_commit_free(theirs);
+ git_index_free(index);
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.worktree->workdir, "branch_file.txt"));
+ cl_git_pass(git_futils_readbuffer(&buf, path.ptr));
+ cl_assert_equal_s(buf.ptr, CONFLICT_BRANCH_FILE_TXT);
+
+ git_buf_free(&path);
+ git_buf_free(&buf);
+}
+
diff --git a/tests/worktree/open.c b/tests/worktree/open.c
new file mode 100644
index 0000000..bdc8bcf
--- /dev/null
+++ b/tests/worktree/open.c
@@ -0,0 +1,194 @@
+#include "clar_libgit2.h"
+#include "repository.h"
+#include "worktree_helpers.h"
+
+#define WORKTREE_PARENT "submodules-worktree-parent"
+#define WORKTREE_CHILD "submodules-worktree-child"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ cl_assert(wt->is_worktree);
+
+ cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), wtdir));
+ cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
+ cl_git_pass(git_path_to_dir(&path));
+ cl_assert_equal_s(wt->workdir, path.ptr);
+
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
+ cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
+ cl_assert_equal_s(wt->gitlink, path.ptr);
+
+ cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), parentdir));
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, "worktrees"));
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, wtdir));
+ cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
+ cl_git_pass(git_path_to_dir(&path));
+ cl_assert_equal_s(wt->gitdir, path.ptr);
+
+ git_buf_free(&path);
+}
+
+void test_worktree_open__repository(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+ setup_fixture_worktree(&fixture);
+
+ assert_worktree_valid(fixture.worktree, COMMON_REPO, WORKTREE_REPO);
+
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__repository_through_workdir(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+ git_repository *wt;
+
+ setup_fixture_worktree(&fixture);
+
+ cl_git_pass(git_repository_open(&wt, WORKTREE_REPO));
+ assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
+
+ git_repository_free(wt);
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__repository_through_gitlink(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+ git_repository *wt;
+
+ setup_fixture_worktree(&fixture);
+
+ cl_git_pass(git_repository_open(&wt, WORKTREE_REPO "/.git"));
+ assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
+
+ git_repository_free(wt);
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__repository_through_gitdir(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+ git_buf gitdir_path = GIT_BUF_INIT;
+ git_repository *wt;
+
+ setup_fixture_worktree(&fixture);
+
+ cl_git_pass(git_buf_joinpath(&gitdir_path, COMMON_REPO, ".git"));
+ cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "worktrees"));
+ cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "testrepo-worktree"));
+
+ cl_git_pass(git_repository_open(&wt, gitdir_path.ptr));
+ assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
+
+ git_buf_free(&gitdir_path);
+ git_repository_free(wt);
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__open_discovered_worktree(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+ git_buf path = GIT_BUF_INIT;
+ git_repository *repo;
+
+ setup_fixture_worktree(&fixture);
+
+ cl_git_pass(git_repository_discover(&path,
+ git_repository_workdir(fixture.worktree), false, NULL));
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert_equal_s(git_repository_workdir(fixture.worktree),
+ git_repository_workdir(repo));
+
+ git_buf_free(&path);
+ git_repository_free(repo);
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__repository_with_nonexistent_parent(void)
+{
+ git_repository *repo;
+
+ cl_fixture_sandbox(WORKTREE_REPO);
+ cl_git_pass(p_chdir(WORKTREE_REPO));
+ cl_git_pass(cl_rename(".gitted", ".git"));
+ cl_git_pass(p_chdir(".."));
+
+ cl_git_fail(git_repository_open(&repo, WORKTREE_REPO));
+
+ cl_fixture_cleanup(WORKTREE_REPO);
+}
+
+void test_worktree_open__submodule_worktree_parent(void)
+{
+ worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT("submodules", WORKTREE_PARENT);
+ setup_fixture_worktree(&fixture);
+
+ cl_assert(git_repository_path(fixture.worktree) != NULL);
+ cl_assert(git_repository_workdir(fixture.worktree) != NULL);
+
+ cl_assert(!fixture.repo->is_worktree);
+ cl_assert(fixture.worktree->is_worktree);
+
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_open__submodule_worktree_child(void)
+{
+ worktree_fixture parent_fixture =
+ WORKTREE_FIXTURE_INIT("submodules", WORKTREE_PARENT);
+ worktree_fixture child_fixture =
+ WORKTREE_FIXTURE_INIT(NULL, WORKTREE_CHILD);
+
+ setup_fixture_worktree(&parent_fixture);
+ cl_git_pass(p_rename(
+ "submodules/testrepo/.gitted",
+ "submodules/testrepo/.git"));
+ setup_fixture_worktree(&child_fixture);
+
+ cl_assert(!parent_fixture.repo->is_worktree);
+ cl_assert(parent_fixture.worktree->is_worktree);
+ cl_assert(child_fixture.worktree->is_worktree);
+
+ cleanup_fixture_worktree(&child_fixture);
+ cleanup_fixture_worktree(&parent_fixture);
+}
+
+void test_worktree_open__open_discovered_submodule_worktree(void)
+{
+ worktree_fixture parent_fixture =
+ WORKTREE_FIXTURE_INIT("submodules", WORKTREE_PARENT);
+ worktree_fixture child_fixture =
+ WORKTREE_FIXTURE_INIT(NULL, WORKTREE_CHILD);
+ git_buf path = GIT_BUF_INIT;
+ git_repository *repo;
+
+ setup_fixture_worktree(&parent_fixture);
+ cl_git_pass(p_rename(
+ "submodules/testrepo/.gitted",
+ "submodules/testrepo/.git"));
+ setup_fixture_worktree(&child_fixture);
+
+ cl_git_pass(git_repository_discover(&path,
+ git_repository_workdir(child_fixture.worktree), false, NULL));
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert_equal_s(git_repository_workdir(child_fixture.worktree),
+ git_repository_workdir(repo));
+
+ git_buf_free(&path);
+ git_repository_free(repo);
+ cleanup_fixture_worktree(&child_fixture);
+ cleanup_fixture_worktree(&parent_fixture);
+}
diff --git a/tests/worktree/reflog.c b/tests/worktree/reflog.c
new file mode 100644
index 0000000..6152eb3
--- /dev/null
+++ b/tests/worktree/reflog.c
@@ -0,0 +1,65 @@
+#include "clar_libgit2.h"
+#include "worktree_helpers.h"
+
+#include "reflog.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+#define REFLOG "refs/heads/testrepo-worktree"
+#define REFLOG_MESSAGE "reflog message"
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+void test_worktree_reflog__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_reflog__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_reflog__read(void)
+{
+ git_reflog *reflog;
+ const git_reflog_entry *entry;
+
+ cl_git_pass(git_reflog_read(&reflog, fixture.worktree, REFLOG));
+ cl_assert_equal_i(git_reflog_entrycount(reflog), 1);
+
+ entry = git_reflog_entry_byindex(reflog, 0);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(git_reflog_entry_message(entry), "branch: Created from HEAD");
+
+ git_reflog_free(reflog);
+}
+
+void test_worktree_reflog__append_then_read(void)
+{
+ git_reflog *reflog, *parent_reflog;
+ const git_reflog_entry *entry;
+ git_reference *head;
+ git_signature *sig;
+ const git_oid *oid;
+
+ cl_git_pass(git_repository_head(&head, fixture.worktree));
+ cl_assert((oid = git_reference_target(head)) != NULL);
+ cl_git_pass(git_signature_now(&sig, "foo", "foo@bar"));
+
+ cl_git_pass(git_reflog_read(&reflog, fixture.worktree, REFLOG));
+ cl_git_pass(git_reflog_append(reflog, oid, sig, REFLOG_MESSAGE));
+ git_reflog_write(reflog);
+
+ cl_git_pass(git_reflog_read(&parent_reflog, fixture.repo, REFLOG));
+ entry = git_reflog_entry_byindex(parent_reflog, 0);
+ cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0);
+ cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
+
+ git_reference_free(head);
+ git_signature_free(sig);
+ git_reflog_free(reflog);
+ git_reflog_free(parent_reflog);
+}
diff --git a/tests/worktree/refs.c b/tests/worktree/refs.c
new file mode 100644
index 0000000..ccac8be
--- /dev/null
+++ b/tests/worktree/refs.c
@@ -0,0 +1,130 @@
+#include "clar_libgit2.h"
+#include "worktree.h"
+#include "worktree_helpers.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+void test_worktree_refs__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_refs__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_refs__list(void)
+{
+ git_strarray refs, wtrefs;
+ unsigned i, j;
+ int error = 0;
+
+ cl_git_pass(git_reference_list(&refs, fixture.repo));
+ cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));
+
+ if (refs.count != wtrefs.count)
+ {
+ error = GIT_ERROR;
+ goto exit;
+ }
+
+ for (i = 0; i < refs.count; i++)
+ {
+ int found = 0;
+
+ for (j = 0; j < wtrefs.count; j++)
+ {
+ if (!strcmp(refs.strings[i], wtrefs.strings[j]))
+ {
+ found = 1;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ error = GIT_ERROR;
+ goto exit;
+ }
+ }
+
+exit:
+ git_strarray_free(&refs);
+ git_strarray_free(&wtrefs);
+ cl_git_pass(error);
+}
+
+void test_worktree_refs__read_head(void)
+{
+ git_reference *head;
+
+ cl_git_pass(git_repository_head(&head, fixture.worktree));
+
+ git_reference_free(head);
+}
+
+void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void)
+{
+ git_reference *head;
+
+ cl_git_pass(git_repository_head(&head, fixture.repo));
+ cl_git_fail(git_repository_set_head(fixture.worktree, git_reference_name(head)));
+
+ git_reference_free(head);
+}
+
+void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void)
+{
+ git_reference *head;
+
+ cl_git_pass(git_repository_head(&head, fixture.worktree));
+ cl_git_fail(git_repository_set_head(fixture.repo, git_reference_name(head)));
+
+ git_reference_free(head);
+}
+
+void test_worktree_refs__set_head_works_for_current_HEAD(void)
+{
+ git_reference *head;
+
+ cl_git_pass(git_repository_head(&head, fixture.repo));
+ cl_git_pass(git_repository_set_head(fixture.repo, git_reference_name(head)));
+
+ git_reference_free(head);
+}
+
+void test_worktree_refs__set_head_fails_when_already_checked_out(void)
+{
+ cl_git_fail(git_repository_set_head(fixture.repo, "refs/heads/testrepo-worktree"));
+}
+
+void test_worktree_refs__delete_fails_for_checked_out_branch(void)
+{
+ git_reference *branch;
+
+ cl_git_pass(git_branch_lookup(&branch, fixture.repo,
+ "testrepo-worktree", GIT_BRANCH_LOCAL));
+ cl_git_fail(git_branch_delete(branch));
+
+ git_reference_free(branch);
+}
+
+void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
+{
+ git_reference *branch;
+ git_worktree *worktree;
+
+ cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename));
+ cl_git_pass(git_worktree_prune(worktree, GIT_WORKTREE_PRUNE_VALID));
+ git_worktree_free(worktree);
+
+ cl_git_pass(git_branch_lookup(&branch, fixture.repo,
+ "testrepo-worktree", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
+}
diff --git a/tests/worktree/repository.c b/tests/worktree/repository.c
new file mode 100644
index 0000000..5c7595c
--- /dev/null
+++ b/tests/worktree/repository.c
@@ -0,0 +1,63 @@
+#include "clar_libgit2.h"
+#include "worktree_helpers.h"
+#include "submodule/submodule_helpers.h"
+
+#include "repository.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+void test_worktree_repository__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_repository__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_repository__head(void)
+{
+ git_reference *ref, *head;
+
+ cl_git_pass(git_reference_lookup(&ref, fixture.repo, "refs/heads/testrepo-worktree"));
+ cl_git_pass(git_repository_head_for_worktree(&head, fixture.repo, "testrepo-worktree"));
+ cl_assert(git_reference_cmp(ref, head) == 0);
+
+ git_reference_free(ref);
+ git_reference_free(head);
+}
+
+void test_worktree_repository__head_fails_for_invalid_worktree(void)
+{
+ git_reference *head = NULL;
+
+ cl_git_fail(git_repository_head_for_worktree(&head, fixture.repo, "invalid"));
+ cl_assert(head == NULL);
+}
+
+void test_worktree_repository__head_detached(void)
+{
+ git_reference *ref, *head;
+
+ cl_git_pass(git_reference_lookup(&ref, fixture.repo, "refs/heads/testrepo-worktree"));
+ cl_git_pass(git_repository_set_head_detached(fixture.worktree, &ref->target.oid));
+
+ cl_assert(git_repository_head_detached(fixture.worktree));
+ cl_assert(git_repository_head_detached_for_worktree(fixture.repo, "testrepo-worktree"));
+ cl_git_fail(git_repository_head_for_worktree(&head, fixture.repo, "testrepo-worktree"));
+
+ git_reference_free(ref);
+}
+
+void test_worktree_repository__head_detached_fails_for_invalid_worktree(void)
+{
+ git_reference *head = NULL;
+
+ cl_git_fail(git_repository_head_detached_for_worktree(fixture.repo, "invalid"));
+ cl_assert(head == NULL);
+}
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c
new file mode 100644
index 0000000..f0c4235
--- /dev/null
+++ b/tests/worktree/worktree.c
@@ -0,0 +1,485 @@
+#include "clar_libgit2.h"
+#include "worktree_helpers.h"
+#include "submodule/submodule_helpers.h"
+
+#include "checkout.h"
+#include "repository.h"
+#include "worktree.h"
+
+#define COMMON_REPO "testrepo"
+#define WORKTREE_REPO "testrepo-worktree"
+
+static worktree_fixture fixture =
+ WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
+
+void test_worktree_worktree__initialize(void)
+{
+ setup_fixture_worktree(&fixture);
+}
+
+void test_worktree_worktree__cleanup(void)
+{
+ cleanup_fixture_worktree(&fixture);
+}
+
+void test_worktree_worktree__list(void)
+{
+ git_strarray wts;
+
+ cl_git_pass(git_worktree_list(&wts, fixture.repo));
+ cl_assert_equal_i(wts.count, 1);
+ cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
+
+ git_strarray_free(&wts);
+}
+
+void test_worktree_worktree__list_with_invalid_worktree_dirs(void)
+{
+ const char *filesets[3][2] = {
+ { "gitdir", "commondir" },
+ { "gitdir", "HEAD" },
+ { "HEAD", "commondir" },
+ };
+ git_buf path = GIT_BUF_INIT;
+ git_strarray wts;
+ unsigned i, j, len;
+
+ cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid",
+ fixture.repo->commondir));
+ cl_git_pass(p_mkdir(path.ptr, 0755));
+
+ len = path.size;
+
+ for (i = 0; i < ARRAY_SIZE(filesets); i++) {
+
+ for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
+ git_buf_truncate(&path, len);
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
+ cl_git_pass(p_close(p_creat(path.ptr, 0644)));
+ }
+
+ cl_git_pass(git_worktree_list(&wts, fixture.worktree));
+ cl_assert_equal_i(wts.count, 1);
+ cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
+ git_strarray_free(&wts);
+
+ for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) {
+ git_buf_truncate(&path, len);
+ cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j]));
+ p_unlink(path.ptr);
+ }
+ }
+
+ git_buf_free(&path);
+}
+
+void test_worktree_worktree__list_in_worktree_repo(void)
+{
+ git_strarray wts;
+
+ cl_git_pass(git_worktree_list(&wts, fixture.worktree));
+ cl_assert_equal_i(wts.count, 1);
+ cl_assert_equal_s(wts.strings[0], "testrepo-worktree");
+
+ git_strarray_free(&wts);
+}
+
+void test_worktree_worktree__list_bare(void)
+{
+ git_repository *repo;
+ git_strarray wts;
+
+ repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_pass(git_worktree_list(&wts, repo));
+ cl_assert_equal_i(wts.count, 0);
+
+ git_repository_free(repo);
+}
+
+void test_worktree_worktree__list_without_worktrees(void)
+{
+ git_repository *repo;
+ git_strarray wts;
+
+ repo = cl_git_sandbox_init("testrepo2");
+ cl_git_pass(git_worktree_list(&wts, repo));
+ cl_assert_equal_i(wts.count, 0);
+
+ git_repository_free(repo);
+}
+
+void test_worktree_worktree__lookup(void)
+{
+ git_worktree *wt;
+ git_buf gitdir_path = GIT_BUF_INIT;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+
+ git_buf_printf(&gitdir_path, "%s/worktrees/%s", fixture.repo->commondir, "testrepo-worktree");
+
+ cl_assert_equal_s(wt->gitdir_path, gitdir_path.ptr);
+ cl_assert_equal_s(wt->parent_path, fixture.repo->gitdir);
+ cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink);
+ cl_assert_equal_s(wt->commondir_path, fixture.repo->commondir);
+
+ git_buf_free(&gitdir_path);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__lookup_nonexistent_worktree(void)
+{
+ git_worktree *wt;
+
+ cl_git_fail(git_worktree_lookup(&wt, fixture.repo, "nonexistent"));
+ cl_assert_equal_p(wt, NULL);
+}
+
+void test_worktree_worktree__open(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+
+ cl_git_pass(git_repository_open_from_worktree(&repo, wt));
+ cl_assert_equal_s(git_repository_workdir(repo),
+ git_repository_workdir(fixture.worktree));
+
+ git_repository_free(repo);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__open_invalid_commondir(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+ git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/commondir"));
+ cl_git_pass(git_buf_printf(&path,
+ "%s/worktrees/testrepo-worktree/commondir",
+ fixture.repo->commondir));
+ cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_fail(git_repository_open_from_worktree(&repo, wt));
+
+ git_buf_free(&buf);
+ git_buf_free(&path);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__open_invalid_gitdir(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+ git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
+ cl_git_pass(git_buf_printf(&path,
+ "%s/worktrees/testrepo-worktree/gitdir",
+ fixture.repo->commondir));
+ cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644));
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_fail(git_repository_open_from_worktree(&repo, wt));
+
+ git_buf_free(&buf);
+ git_buf_free(&path);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__open_invalid_parent(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_sets(&buf, "/path/to/nonexistent/gitdir"));
+ cl_git_pass(git_futils_writebuffer(&buf,
+ fixture.worktree->gitlink, O_RDWR, 0644));
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_fail(git_repository_open_from_worktree(&repo, wt));
+
+ git_buf_free(&buf);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__init(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+ git_reference *branch;
+ git_buf path = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+
+ /* Open and verify created repo */
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-new/") == 0);
+ cl_git_pass(git_branch_lookup(&branch, repo, "worktree-new", GIT_BRANCH_LOCAL));
+
+ git_buf_free(&path);
+ git_worktree_free(wt);
+ git_reference_free(branch);
+ git_repository_free(repo);
+}
+
+void test_worktree_worktree__init_existing_branch(void)
+{
+ git_reference *head, *branch;
+ git_commit *commit;
+ git_worktree *wt;
+ git_buf path = GIT_BUF_INIT;
+
+ cl_git_pass(git_repository_head(&head, fixture.repo));
+ cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid));
+ cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new", commit, false));
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+
+ git_buf_free(&path);
+ git_commit_free(commit);
+ git_reference_free(head);
+ git_reference_free(branch);
+}
+
+void test_worktree_worktree__init_existing_worktree(void)
+{
+ git_worktree *wt;
+ git_buf path = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "testrepo-worktree", path.ptr));
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink);
+
+ git_buf_free(&path);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__init_existing_path(void)
+{
+ const char *wtfiles[] = { "HEAD", "commondir", "gitdir", "index" };
+ git_worktree *wt;
+ git_buf path = GIT_BUF_INIT;
+ unsigned i;
+
+ /* Delete files to verify they have not been created by
+ * the init call */
+ for (i = 0; i < ARRAY_SIZE(wtfiles); i++) {
+ cl_git_pass(git_buf_joinpath(&path,
+ fixture.worktree->gitdir, wtfiles[i]));
+ cl_git_pass(p_unlink(path.ptr));
+ }
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../testrepo-worktree"));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+
+ /* Verify files have not been re-created */
+ for (i = 0; i < ARRAY_SIZE(wtfiles); i++) {
+ cl_git_pass(git_buf_joinpath(&path,
+ fixture.worktree->gitdir, wtfiles[i]));
+ cl_assert(!git_path_exists(path.ptr));
+ }
+
+ git_buf_free(&path);
+}
+
+void test_worktree_worktree__init_submodule(void)
+{
+ git_repository *repo, *sm, *wt;
+ git_worktree *worktree;
+ git_buf path = GIT_BUF_INIT;
+
+ cleanup_fixture_worktree(&fixture);
+ repo = setup_fixture_submod2();
+
+ cl_git_pass(git_buf_joinpath(&path, repo->workdir, "sm_unchanged"));
+ cl_git_pass(git_repository_open(&sm, path.ptr));
+ cl_git_pass(git_buf_joinpath(&path, repo->workdir, "../worktree/"));
+ cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr));
+ cl_git_pass(git_repository_open_from_worktree(&wt, worktree));
+
+ cl_assert_equal_s(path.ptr, wt->workdir);
+ cl_assert_equal_s(sm->commondir, wt->commondir);
+
+ cl_git_pass(git_buf_joinpath(&path, sm->gitdir, "worktrees/repo-worktree/"));
+ cl_assert_equal_s(path.ptr, wt->gitdir);
+
+ git_buf_free(&path);
+ git_worktree_free(worktree);
+ git_repository_free(sm);
+ git_repository_free(wt);
+}
+
+void test_worktree_worktree__validate(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_validate(wt));
+
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__validate_invalid_commondir(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ git__free(wt->commondir_path);
+ wt->commondir_path = "/path/to/invalid/commondir";
+
+ cl_git_fail(git_worktree_validate(wt));
+
+ wt->commondir_path = NULL;
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__validate_invalid_gitdir(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ git__free(wt->gitdir_path);
+ wt->gitdir_path = "/path/to/invalid/gitdir";
+ cl_git_fail(git_worktree_validate(wt));
+
+ wt->gitdir_path = NULL;
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__validate_invalid_parent(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ git__free(wt->parent_path);
+ wt->parent_path = "/path/to/invalid/parent";
+ cl_git_fail(git_worktree_validate(wt));
+
+ wt->parent_path = NULL;
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__lock_with_reason(void)
+{
+ git_worktree *wt;
+ git_buf reason = GIT_BUF_INIT;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+
+ cl_assert(!git_worktree_is_locked(NULL, wt));
+ cl_git_pass(git_worktree_lock(wt, "because"));
+ cl_assert(git_worktree_is_locked(&reason, wt) > 0);
+ cl_assert_equal_s(reason.ptr, "because");
+ cl_assert(wt->locked);
+
+ git_buf_free(&reason);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__lock_without_reason(void)
+{
+ git_worktree *wt;
+ git_buf reason = GIT_BUF_INIT;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+
+ cl_assert(!git_worktree_is_locked(NULL, wt));
+ cl_git_pass(git_worktree_lock(wt, NULL));
+ cl_assert(git_worktree_is_locked(&reason, wt) > 0);
+ cl_assert_equal_i(reason.size, 0);
+ cl_assert(wt->locked);
+
+ git_buf_free(&reason);
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__unlock_unlocked_worktree(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_assert(!git_worktree_is_locked(NULL, wt));
+ cl_assert(git_worktree_unlock(wt) == 0);
+ cl_assert(!wt->locked);
+
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__unlock_locked_worktree(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_lock(wt, NULL));
+ cl_assert(git_worktree_is_locked(NULL, wt));
+ cl_git_pass(git_worktree_unlock(wt));
+ cl_assert(!wt->locked);
+
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__prune_valid(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
+
+ /* Assert the repository is not valid anymore */
+ cl_git_fail(git_repository_open_from_worktree(&repo, wt));
+
+ git_worktree_free(wt);
+ git_repository_free(repo);
+}
+
+void test_worktree_worktree__prune_locked(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_lock(wt, NULL));
+ cl_git_fail(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
+ cl_git_fail(git_worktree_prune(wt, ~GIT_WORKTREE_PRUNE_LOCKED));
+
+ /* Assert the repository is still valid */
+ cl_git_pass(git_repository_open_from_worktree(&repo, wt));
+
+ git_worktree_free(wt);
+ git_repository_free(repo);
+}
+
+void test_worktree_worktree__prune_gitdir(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
+
+ cl_assert(!git_path_exists(wt->gitdir_path));
+ cl_assert(git_path_exists(wt->gitlink_path));
+
+ git_worktree_free(wt);
+}
+
+void test_worktree_worktree__prune_both(void)
+{
+ git_worktree *wt;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_WORKING_TREE | GIT_WORKTREE_PRUNE_VALID));
+
+ cl_assert(!git_path_exists(wt->gitdir_path));
+ cl_assert(!git_path_exists(wt->gitlink_path));
+
+ git_worktree_free(wt);
+}
diff --git a/tests/worktree/worktree_helpers.c b/tests/worktree/worktree_helpers.c
new file mode 100644
index 0000000..6d4cdba
--- /dev/null
+++ b/tests/worktree/worktree_helpers.c
@@ -0,0 +1,30 @@
+#include "clar_libgit2.h"
+#include "worktree_helpers.h"
+
+void cleanup_fixture_worktree(worktree_fixture *fixture)
+{
+ if (!fixture)
+ return;
+
+ if (fixture->repo) {
+ git_repository_free(fixture->repo);
+ fixture->repo = NULL;
+ }
+ if (fixture->worktree) {
+ git_repository_free(fixture->worktree);
+ fixture->worktree = NULL;
+ }
+
+ if (fixture->reponame)
+ cl_fixture_cleanup(fixture->reponame);
+ if (fixture->worktreename)
+ cl_fixture_cleanup(fixture->worktreename);
+}
+
+void setup_fixture_worktree(worktree_fixture *fixture)
+{
+ if (fixture->reponame)
+ fixture->repo = cl_git_sandbox_init(fixture->reponame);
+ if (fixture->worktreename)
+ fixture->worktree = cl_git_sandbox_init(fixture->worktreename);
+}
diff --git a/tests/worktree/worktree_helpers.h b/tests/worktree/worktree_helpers.h
new file mode 100644
index 0000000..35ea9ed
--- /dev/null
+++ b/tests/worktree/worktree_helpers.h
@@ -0,0 +1,11 @@
+typedef struct {
+ const char *reponame;
+ const char *worktreename;
+ git_repository *repo;
+ git_repository *worktree;
+} worktree_fixture;
+
+#define WORKTREE_FIXTURE_INIT(repo, worktree) { (repo), (worktree), NULL, NULL }
+
+void cleanup_fixture_worktree(worktree_fixture *fixture);
+void setup_fixture_worktree(worktree_fixture *fixture);