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
|
# test_index.py -- Tests for the git index
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@jelmer.uk>
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Tests for the index."""
import os
import shutil
import stat
import struct
import sys
import tempfile
from io import BytesIO
from dulwich.config import ConfigDict
from dulwich.diff_tree import (
CHANGE_ADD,
CHANGE_COPY,
CHANGE_DELETE,
CHANGE_MODIFY,
CHANGE_RENAME,
TreeChange,
tree_changes,
)
from dulwich.index import (
Index,
IndexEntry,
SerializedIndexEntry,
_compress_path,
_decode_varint,
_decompress_path,
_encode_varint,
_fs_to_tree_path,
_tree_to_fs_path,
build_index_from_tree,
cleanup_mode,
commit_tree,
detect_case_only_renames,
get_unstaged_changes,
index_entry_from_directory,
index_entry_from_path,
index_entry_from_stat,
iter_fresh_entries,
read_index,
read_index_dict,
update_working_tree,
validate_path_element_default,
validate_path_element_hfs,
validate_path_element_ntfs,
write_cache_time,
write_index,
write_index_dict,
)
from dulwich.object_store import MemoryObjectStore
from dulwich.objects import S_IFGITLINK, ZERO_SHA, Blob, Tree, TreeEntry
from dulwich.repo import Repo
from dulwich.tests.utils import make_commit
from . import TestCase, skipIf
def can_symlink() -> bool:
"""Return whether running process can create symlinks."""
if sys.platform != "win32":
# Platforms other than Windows should allow symlinks without issues.
return True
test_source = tempfile.mkdtemp()
test_target = test_source + "can_symlink"
try:
os.symlink(test_source, test_target)
except (NotImplementedError, OSError):
return False
return True
class IndexTestCase(TestCase):
datadir = os.path.join(os.path.dirname(__file__), "../testdata/indexes")
def get_simple_index(self, name):
return Index(os.path.join(self.datadir, name))
class SimpleIndexTestCase(IndexTestCase):
def test_len(self) -> None:
self.assertEqual(1, len(self.get_simple_index("index")))
def test_iter(self) -> None:
self.assertEqual([b"bla"], list(self.get_simple_index("index")))
def test_iter_skip_hash(self) -> None:
self.assertEqual([b"bla"], list(self.get_simple_index("index_skip_hash")))
def test_iterobjects(self) -> None:
self.assertEqual(
[(b"bla", b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 33188)],
list(self.get_simple_index("index").iterobjects()),
)
def test_getitem(self) -> None:
self.assertEqual(
IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
0,
0,
),
self.get_simple_index("index")[b"bla"],
)
def test_empty(self) -> None:
i = self.get_simple_index("notanindex")
self.assertEqual(0, len(i))
self.assertFalse(os.path.exists(i._filename))
def test_against_empty_tree(self) -> None:
i = self.get_simple_index("index")
changes = list(i.changes_from_tree(MemoryObjectStore(), None))
self.assertEqual(1, len(changes))
(_oldname, newname), (_oldmode, _newmode), (_oldsha, newsha) = changes[0]
self.assertEqual(b"bla", newname)
self.assertEqual(b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", newsha)
def test_index_pathlib(self) -> None:
import tempfile
from pathlib import Path
# Create a temporary index file
with tempfile.NamedTemporaryFile(suffix=".index", delete=False) as f:
temp_path = f.name
self.addCleanup(os.unlink, temp_path)
# Test creating Index with pathlib.Path
path_obj = Path(temp_path)
index = Index(path_obj, read=False)
self.assertEqual(str(path_obj), index.path)
# Add an entry and write
index[b"test"] = IndexEntry(
ctime=(0, 0),
mtime=(0, 0),
dev=0,
ino=0,
mode=33188,
uid=0,
gid=0,
size=0,
sha=b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
)
index.write()
# Read it back with pathlib.Path
index2 = Index(path_obj)
self.assertIn(b"test", index2)
class SimpleIndexWriterTestCase(IndexTestCase):
def setUp(self) -> None:
IndexTestCase.setUp(self)
self.tempdir = tempfile.mkdtemp()
def tearDown(self) -> None:
IndexTestCase.tearDown(self)
shutil.rmtree(self.tempdir)
def test_simple_write(self) -> None:
entries = [
(
SerializedIndexEntry(
b"barbla",
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
0,
0,
)
)
]
filename = os.path.join(self.tempdir, "test-simple-write-index")
with open(filename, "wb+") as x:
write_index(x, entries)
with open(filename, "rb") as x:
self.assertEqual(entries, list(read_index(x)))
class ReadIndexDictTests(IndexTestCase):
def setUp(self) -> None:
IndexTestCase.setUp(self)
self.tempdir = tempfile.mkdtemp()
def tearDown(self) -> None:
IndexTestCase.tearDown(self)
shutil.rmtree(self.tempdir)
def test_simple_write(self) -> None:
entries = {
b"barbla": IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
0,
0,
)
}
filename = os.path.join(self.tempdir, "test-simple-write-index")
with open(filename, "wb+") as x:
write_index_dict(x, entries)
with open(filename, "rb") as x:
self.assertEqual(entries, read_index_dict(x))
class CommitTreeTests(TestCase):
def setUp(self) -> None:
super().setUp()
self.store = MemoryObjectStore()
def test_single_blob(self) -> None:
blob = Blob()
blob.data = b"foo"
self.store.add_object(blob)
blobs = [(b"bla", blob.id, stat.S_IFREG)]
rootid = commit_tree(self.store, blobs)
self.assertEqual(rootid, b"1a1e80437220f9312e855c37ac4398b68e5c1d50")
self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid][b"bla"])
self.assertEqual({rootid, blob.id}, set(self.store._data.keys()))
def test_nested(self) -> None:
blob = Blob()
blob.data = b"foo"
self.store.add_object(blob)
blobs = [(b"bla/bar", blob.id, stat.S_IFREG)]
rootid = commit_tree(self.store, blobs)
self.assertEqual(rootid, b"d92b959b216ad0d044671981196781b3258fa537")
dirid = self.store[rootid][b"bla"][1]
self.assertEqual(dirid, b"c1a1deb9788150829579a8b4efa6311e7b638650")
self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid][b"bla"])
self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid][b"bar"])
self.assertEqual({rootid, dirid, blob.id}, set(self.store._data.keys()))
class CleanupModeTests(TestCase):
def assertModeEqual(self, expected, got) -> None:
self.assertEqual(expected, got, f"{expected:o} != {got:o}")
def test_file(self) -> None:
self.assertModeEqual(0o100644, cleanup_mode(0o100000))
def test_executable(self) -> None:
self.assertModeEqual(0o100755, cleanup_mode(0o100711))
self.assertModeEqual(0o100755, cleanup_mode(0o100700))
def test_symlink(self) -> None:
self.assertModeEqual(0o120000, cleanup_mode(0o120711))
def test_dir(self) -> None:
self.assertModeEqual(0o040000, cleanup_mode(0o40531))
def test_submodule(self) -> None:
self.assertModeEqual(0o160000, cleanup_mode(0o160744))
class WriteCacheTimeTests(TestCase):
def test_write_string(self) -> None:
f = BytesIO()
self.assertRaises(TypeError, write_cache_time, f, "foo")
def test_write_int(self) -> None:
f = BytesIO()
write_cache_time(f, 434343)
self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
def test_write_tuple(self) -> None:
f = BytesIO()
write_cache_time(f, (434343, 21))
self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
def test_write_float(self) -> None:
f = BytesIO()
write_cache_time(f, 434343.000000021)
self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
class IndexEntryFromStatTests(TestCase):
def test_simple(self) -> None:
st = os.stat_result(
(
16877,
131078,
64769,
154,
1000,
1000,
12288,
1323629595,
1324180496,
1324180496,
)
)
entry = index_entry_from_stat(st, b"22" * 20)
self.assertEqual(
entry,
IndexEntry(
1324180496,
1324180496,
64769,
131078,
16384,
1000,
1000,
12288,
b"2222222222222222222222222222222222222222",
0,
0,
),
)
def test_override_mode(self) -> None:
st = os.stat_result(
(
stat.S_IFREG + 0o644,
131078,
64769,
154,
1000,
1000,
12288,
1323629595,
1324180496,
1324180496,
)
)
entry = index_entry_from_stat(st, b"22" * 20, mode=stat.S_IFREG + 0o755)
self.assertEqual(
entry,
IndexEntry(
1324180496,
1324180496,
64769,
131078,
33261,
1000,
1000,
12288,
b"2222222222222222222222222222222222222222",
0,
0,
),
)
class BuildIndexTests(TestCase):
def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha) -> None:
self.assertEqual(index_entry.mode, mode) # mode
self.assertEqual(index_entry.size, filesize) # filesize
self.assertEqual(index_entry.sha, sha) # sha
def assertFileContents(self, path, contents, symlink=False) -> None:
if symlink:
self.assertEqual(os.readlink(path), contents)
else:
with open(path, "rb") as f:
self.assertEqual(f.read(), contents)
def test_empty(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
tree = Tree()
repo.object_store.add_object(tree)
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
self.assertEqual(len(index), 0)
# Verify no files
self.assertEqual([".git"], os.listdir(repo.path))
def test_git_dir(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Populate repo
filea = Blob.from_string(b"file a")
filee = Blob.from_string(b"d")
tree = Tree()
tree[b".git/a"] = (stat.S_IFREG | 0o644, filea.id)
tree[b"c/e"] = (stat.S_IFREG | 0o644, filee.id)
repo.object_store.add_objects([(o, None) for o in [filea, filee, tree]])
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
self.assertEqual(len(index), 1)
# filea
apath = os.path.join(repo.path, ".git", "a")
self.assertFalse(os.path.exists(apath))
# filee
epath = os.path.join(repo.path, "c", "e")
self.assertTrue(os.path.exists(epath))
self.assertReasonableIndexEntry(
index[b"c/e"], stat.S_IFREG | 0o644, 1, filee.id
)
self.assertFileContents(epath, b"d")
def test_nonempty(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Populate repo
filea = Blob.from_string(b"file a")
fileb = Blob.from_string(b"file b")
filed = Blob.from_string(b"file d")
tree = Tree()
tree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
tree[b"b"] = (stat.S_IFREG | 0o644, fileb.id)
tree[b"c/d"] = (stat.S_IFREG | 0o644, filed.id)
repo.object_store.add_objects(
[(o, None) for o in [filea, fileb, filed, tree]]
)
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
self.assertEqual(len(index), 3)
# filea
apath = os.path.join(repo.path, "a")
self.assertTrue(os.path.exists(apath))
self.assertReasonableIndexEntry(
index[b"a"], stat.S_IFREG | 0o644, 6, filea.id
)
self.assertFileContents(apath, b"file a")
# fileb
bpath = os.path.join(repo.path, "b")
self.assertTrue(os.path.exists(bpath))
self.assertReasonableIndexEntry(
index[b"b"], stat.S_IFREG | 0o644, 6, fileb.id
)
self.assertFileContents(bpath, b"file b")
# filed
dpath = os.path.join(repo.path, "c", "d")
self.assertTrue(os.path.exists(dpath))
self.assertReasonableIndexEntry(
index[b"c/d"], stat.S_IFREG | 0o644, 6, filed.id
)
self.assertFileContents(dpath, b"file d")
# Verify no extra files
self.assertEqual([".git", "a", "b", "c"], sorted(os.listdir(repo.path)))
self.assertEqual(["d"], sorted(os.listdir(os.path.join(repo.path, "c"))))
@skipIf(not getattr(os, "sync", None), "Requires sync support")
def test_norewrite(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Populate repo
filea = Blob.from_string(b"file a")
filea_path = os.path.join(repo_dir, "a")
tree = Tree()
tree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
repo.object_store.add_objects([(o, None) for o in [filea, tree]])
# First Write
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Use sync as metadata can be cached on some FS
os.sync()
mtime = os.stat(filea_path).st_mtime
# Test Rewrite
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
os.sync()
self.assertEqual(mtime, os.stat(filea_path).st_mtime)
# Modify content
with open(filea_path, "wb") as fh:
fh.write(b"test a")
os.sync()
mtime = os.stat(filea_path).st_mtime
# Test rewrite
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
os.sync()
with open(filea_path, "rb") as fh:
self.assertEqual(b"file a", fh.read())
@skipIf(not can_symlink(), "Requires symlink support")
def test_symlink(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Populate repo
filed = Blob.from_string(b"file d")
filee = Blob.from_string(b"d")
tree = Tree()
tree[b"c/d"] = (stat.S_IFREG | 0o644, filed.id)
tree[b"c/e"] = (stat.S_IFLNK, filee.id) # symlink
repo.object_store.add_objects([(o, None) for o in [filed, filee, tree]])
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
# symlink to d
epath = os.path.join(repo.path, "c", "e")
self.assertTrue(os.path.exists(epath))
self.assertReasonableIndexEntry(
index[b"c/e"],
stat.S_IFLNK,
0 if sys.platform == "win32" else 1,
filee.id,
)
self.assertFileContents(epath, "d", symlink=True)
def test_no_decode_encode(self) -> None:
repo_dir = tempfile.mkdtemp()
repo_dir_bytes = os.fsencode(repo_dir)
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Populate repo
file = Blob.from_string(b"foo")
tree = Tree()
latin1_name = "À".encode("latin1")
try:
latin1_path = os.path.join(repo_dir_bytes, latin1_name)
except UnicodeDecodeError:
self.skipTest("can not decode as latin1")
utf8_name = "À".encode()
utf8_path = os.path.join(repo_dir_bytes, utf8_name)
tree[latin1_name] = (stat.S_IFREG | 0o644, file.id)
tree[utf8_name] = (stat.S_IFREG | 0o644, file.id)
repo.object_store.add_objects([(o, None) for o in [file, tree]])
try:
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
except OSError as e:
if e.errno == 92 and sys.platform == "darwin":
# Our filename isn't supported by the platform :(
self.skipTest(f"can not write filename {e.filename!r}")
else:
raise
except UnicodeDecodeError:
# This happens e.g. with python3.6 on Windows.
# It implicitly decodes using utf8, which doesn't work.
self.skipTest("can not implicitly convert as utf8")
# Verify index entries
index = repo.open_index()
self.assertIn(latin1_name, index)
self.assertIn(utf8_name, index)
self.assertTrue(os.path.exists(latin1_path))
self.assertTrue(os.path.exists(utf8_path))
def test_windows_unicode_filename_encoding(self) -> None:
"""Test that Unicode filenames are handled correctly on Windows.
This test verifies the fix for GitHub issue #203, where filenames
containing Unicode characters like 'À' were incorrectly encoded/decoded
on Windows, resulting in corruption like 'À' -> 'À'.
"""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Create a blob
file_content = b"test file content"
blob = Blob.from_string(file_content)
# Create a tree with a Unicode filename
tree = Tree()
unicode_filename = "À" # This is the character from GitHub issue #203
utf8_filename_bytes = unicode_filename.encode(
"utf-8"
) # This is how it's stored in git trees
tree[utf8_filename_bytes] = (stat.S_IFREG | 0o644, blob.id)
repo.object_store.add_objects([(blob, None), (tree, None)])
# Build index from tree (this is what happens during checkout/clone)
try:
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
except (OSError, UnicodeError) as e:
if sys.platform == "win32" and "cannot" in str(e).lower():
self.skipTest(f"Platform doesn't support filename: {e}")
raise
# Check that the file was created correctly
expected_file_path = os.path.join(repo.path, unicode_filename)
self.assertTrue(
os.path.exists(expected_file_path),
f"File should exist at {expected_file_path}",
)
# Verify the file content is correct
with open(expected_file_path, "rb") as f:
actual_content = f.read()
self.assertEqual(actual_content, file_content)
# Test the reverse: adding a Unicode filename to the index
if sys.platform == "win32":
# On Windows, test that _tree_to_fs_path and _fs_to_tree_path
# handle UTF-8 encoded tree paths correctly
from dulwich.index import _fs_to_tree_path, _tree_to_fs_path
repo_path_bytes = os.fsencode(repo.path)
# Test tree path to filesystem path conversion
fs_path = _tree_to_fs_path(repo_path_bytes, utf8_filename_bytes)
expected_fs_path = os.path.join(
repo_path_bytes, os.fsencode(unicode_filename)
)
self.assertEqual(fs_path, expected_fs_path)
# Test filesystem path to tree path conversion
# _fs_to_tree_path expects relative paths, not absolute paths
# Extract just the filename from the full path
filename_only = os.path.basename(fs_path)
reconstructed_tree_path = _fs_to_tree_path(
filename_only, tree_encoding="utf-8"
)
self.assertEqual(reconstructed_tree_path, utf8_filename_bytes)
def test_git_submodule(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
filea = Blob.from_string(b"file alalala")
subtree = Tree()
subtree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
c = make_commit(
tree=subtree.id,
author=b"Somebody <somebody@example.com>",
committer=b"Somebody <somebody@example.com>",
author_time=42342,
commit_time=42342,
author_timezone=0,
commit_timezone=0,
parents=[],
message=b"Subcommit",
)
tree = Tree()
tree[b"c"] = (S_IFGITLINK, c.id)
repo.object_store.add_objects([(o, None) for o in [tree]])
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
self.assertEqual(len(index), 1)
# filea
apath = os.path.join(repo.path, "c/a")
self.assertFalse(os.path.exists(apath))
# dir c
cpath = os.path.join(repo.path, "c")
self.assertTrue(os.path.isdir(cpath))
self.assertEqual(index[b"c"].mode, S_IFGITLINK) # mode
self.assertEqual(index[b"c"].sha, c.id) # sha
def test_git_submodule_exists(self) -> None:
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
filea = Blob.from_string(b"file alalala")
subtree = Tree()
subtree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
c = make_commit(
tree=subtree.id,
author=b"Somebody <somebody@example.com>",
committer=b"Somebody <somebody@example.com>",
author_time=42342,
commit_time=42342,
author_timezone=0,
commit_timezone=0,
parents=[],
message=b"Subcommit",
)
tree = Tree()
tree[b"c"] = (S_IFGITLINK, c.id)
os.mkdir(os.path.join(repo_dir, "c"))
repo.object_store.add_objects([(o, None) for o in [tree]])
build_index_from_tree(
repo.path, repo.index_path(), repo.object_store, tree.id
)
# Verify index entries
index = repo.open_index()
self.assertEqual(len(index), 1)
# filea
apath = os.path.join(repo.path, "c/a")
self.assertFalse(os.path.exists(apath))
# dir c
cpath = os.path.join(repo.path, "c")
self.assertTrue(os.path.isdir(cpath))
self.assertEqual(index[b"c"].mode, S_IFGITLINK) # mode
self.assertEqual(index[b"c"].sha, c.id) # sha
def test_with_line_ending_normalization(self) -> None:
"""Test that build_index_from_tree applies line-ending normalization."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
from dulwich.line_ending import BlobNormalizer
with Repo.init(repo_dir) as repo:
# Set up autocrlf config
config = repo.get_config()
config.set((b"core",), b"autocrlf", b"true")
config.write_to_path()
# Create blob with LF line endings
content_lf = b"line1\nline2\nline3\n"
blob = Blob.from_string(content_lf)
tree = Tree()
tree[b"test.txt"] = (stat.S_IFREG | 0o644, blob.id)
repo.object_store.add_objects([(blob, None), (tree, None)])
# Create blob normalizer
autocrlf = config.get((b"core",), b"autocrlf")
blob_normalizer = BlobNormalizer(config, {}, autocrlf=autocrlf)
# Build index with normalization
build_index_from_tree(
repo.path,
repo.index_path(),
repo.object_store,
tree.id,
blob_normalizer=blob_normalizer,
)
# On Windows with autocrlf=true, file should have CRLF line endings
test_file = os.path.join(repo.path, "test.txt")
with open(test_file, "rb") as f:
content = f.read()
# autocrlf=true means LF -> CRLF on checkout (on all platforms for testing)
expected_content = b"line1\r\nline2\r\nline3\r\n"
self.assertEqual(content, expected_content)
class GetUnstagedChangesTests(TestCase):
def test_get_unstaged_changes(self) -> None:
"""Unit test for get_unstaged_changes."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Commit a dummy file then modify it
foo1_fullpath = os.path.join(repo_dir, "foo1")
with open(foo1_fullpath, "wb") as f:
f.write(b"origstuff")
foo2_fullpath = os.path.join(repo_dir, "foo2")
with open(foo2_fullpath, "wb") as f:
f.write(b"origstuff")
repo.get_worktree().stage(["foo1", "foo2"])
repo.get_worktree().commit(
message=b"test status",
committer=b"committer <email>",
author=b"author <email>",
)
with open(foo1_fullpath, "wb") as f:
f.write(b"newstuff")
# modify access and modify time of path
os.utime(foo1_fullpath, (0, 0))
changes = get_unstaged_changes(repo.open_index(), repo_dir)
self.assertEqual(list(changes), [b"foo1"])
def test_get_unstaged_changes_with_preload(self) -> None:
"""Unit test for get_unstaged_changes with preload_index=True."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Create multiple files to test parallel processing
files = []
for i in range(10):
filename = f"foo{i}"
fullpath = os.path.join(repo_dir, filename)
with open(fullpath, "wb") as f:
f.write(b"origstuff" + str(i).encode())
files.append(filename)
repo.get_worktree().stage(files)
repo.get_worktree().commit(
b"test status",
author=b"author <email>",
committer=b"committer <email>",
)
# Modify some files
modified_files = [b"foo1", b"foo3", b"foo5", b"foo7"]
for filename in modified_files:
fullpath = os.path.join(repo_dir, filename.decode())
with open(fullpath, "wb") as f:
f.write(b"newstuff")
os.utime(fullpath, (0, 0))
# Test with preload_index=False (serial)
changes_serial = list(
get_unstaged_changes(repo.open_index(), repo_dir, preload_index=False)
)
changes_serial.sort()
# Test with preload_index=True (parallel)
changes_parallel = list(
get_unstaged_changes(repo.open_index(), repo_dir, preload_index=True)
)
changes_parallel.sort()
# Both should return the same results
self.assertEqual(changes_serial, changes_parallel)
self.assertEqual(changes_serial, sorted(modified_files))
def test_get_unstaged_changes_nanosecond_precision(self) -> None:
"""Test that nanosecond precision mtime is used for change detection."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Commit a file
foo_fullpath = os.path.join(repo_dir, "foo")
with open(foo_fullpath, "wb") as f:
f.write(b"original content")
repo.get_worktree().stage(["foo"])
repo.get_worktree().commit(
message=b"initial commit",
committer=b"committer <email>",
author=b"author <email>",
)
# Get the current index entry
index = repo.open_index()
entry = index[b"foo"]
# Modify the file with the same size but different content
# This simulates a very fast change within the same second
with open(foo_fullpath, "wb") as f:
f.write(b"modified content")
# Set mtime to match the index entry exactly (same second)
# but with different nanoseconds if the filesystem supports it
st = os.stat(foo_fullpath)
if isinstance(entry.mtime, tuple) and hasattr(st, "st_mtime_ns"):
# Set the mtime to the same second as the index entry
# but with a slightly different nanosecond value
entry_sec = entry.mtime[0]
entry_nsec = entry.mtime[1]
new_mtime_ns = entry_sec * 1_000_000_000 + entry_nsec + 1000
new_mtime = new_mtime_ns / 1_000_000_000
os.utime(foo_fullpath, (st.st_atime, new_mtime))
# The file should be detected as changed due to nanosecond difference
changes = list(get_unstaged_changes(repo.open_index(), repo_dir))
self.assertEqual(changes, [b"foo"])
else:
# If nanosecond precision is not available, skip this test
self.skipTest("Nanosecond precision not available on this system")
def test_get_unstaged_deleted_changes(self) -> None:
"""Unit test for get_unstaged_changes."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Commit a dummy file then remove it
foo1_fullpath = os.path.join(repo_dir, "foo1")
with open(foo1_fullpath, "wb") as f:
f.write(b"origstuff")
repo.get_worktree().stage(["foo1"])
repo.get_worktree().commit(
message=b"test status",
committer=b"committer <email>",
author=b"author <email>",
)
os.unlink(foo1_fullpath)
changes = get_unstaged_changes(repo.open_index(), repo_dir)
self.assertEqual(list(changes), [b"foo1"])
def test_get_unstaged_changes_removed_replaced_by_directory(self) -> None:
"""Unit test for get_unstaged_changes."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Commit a dummy file then modify it
foo1_fullpath = os.path.join(repo_dir, "foo1")
with open(foo1_fullpath, "wb") as f:
f.write(b"origstuff")
repo.get_worktree().stage(["foo1"])
repo.get_worktree().commit(
message=b"test status",
committer=b"committer <email>",
author=b"author <email>",
)
os.remove(foo1_fullpath)
os.mkdir(foo1_fullpath)
changes = get_unstaged_changes(repo.open_index(), repo_dir)
self.assertEqual(list(changes), [b"foo1"])
@skipIf(not can_symlink(), "Requires symlink support")
def test_get_unstaged_changes_removed_replaced_by_link(self) -> None:
"""Unit test for get_unstaged_changes."""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Commit a dummy file then modify it
foo1_fullpath = os.path.join(repo_dir, "foo1")
with open(foo1_fullpath, "wb") as f:
f.write(b"origstuff")
repo.get_worktree().stage(["foo1"])
repo.get_worktree().commit(
message=b"test status",
committer=b"committer <email>",
author=b"author <email>",
)
os.remove(foo1_fullpath)
os.symlink(os.path.dirname(foo1_fullpath), foo1_fullpath)
changes = get_unstaged_changes(repo.open_index(), repo_dir)
self.assertEqual(list(changes), [b"foo1"])
class TestValidatePathElement(TestCase):
def test_default(self) -> None:
self.assertTrue(validate_path_element_default(b"bla"))
self.assertTrue(validate_path_element_default(b".bla"))
self.assertFalse(validate_path_element_default(b".git"))
self.assertFalse(validate_path_element_default(b".giT"))
self.assertFalse(validate_path_element_default(b".."))
self.assertTrue(validate_path_element_default(b"git~1"))
def test_ntfs(self) -> None:
self.assertTrue(validate_path_element_ntfs(b"bla"))
self.assertTrue(validate_path_element_ntfs(b".bla"))
self.assertFalse(validate_path_element_ntfs(b".git"))
self.assertFalse(validate_path_element_ntfs(b".giT"))
self.assertFalse(validate_path_element_ntfs(b".."))
self.assertFalse(validate_path_element_ntfs(b"git~1"))
def test_hfs(self) -> None:
# Normal paths should pass
self.assertTrue(validate_path_element_hfs(b"bla"))
self.assertTrue(validate_path_element_hfs(b".bla"))
# Basic .git variations should fail
self.assertFalse(validate_path_element_hfs(b".git"))
self.assertFalse(validate_path_element_hfs(b".giT"))
self.assertFalse(validate_path_element_hfs(b".GIT"))
self.assertFalse(validate_path_element_hfs(b".."))
# git~1 should also fail on HFS+
self.assertFalse(validate_path_element_hfs(b"git~1"))
# Test HFS+ Unicode normalization attacks
# .g\u200cit (zero-width non-joiner)
self.assertFalse(validate_path_element_hfs(b".g\xe2\x80\x8cit"))
# .gi\u200dt (zero-width joiner)
self.assertFalse(validate_path_element_hfs(b".gi\xe2\x80\x8dt"))
# Test other ignorable characters
# .g\ufeffit (zero-width no-break space)
self.assertFalse(validate_path_element_hfs(b".g\xef\xbb\xbfit"))
# Valid Unicode that shouldn't be confused with .git
self.assertTrue(validate_path_element_hfs(b".g\xc3\xaft")) # .gït
self.assertTrue(validate_path_element_hfs(b"git")) # git without dot
class TestTreeFSPathConversion(TestCase):
def test_tree_to_fs_path(self) -> None:
tree_path = "délwíçh/foo".encode()
fs_path = _tree_to_fs_path(b"/prefix/path", tree_path)
self.assertEqual(
fs_path,
os.fsencode(os.path.join("/prefix/path", "délwíçh", "foo")),
)
def test_tree_to_fs_path_windows_separator(self) -> None:
tree_path = b"path/with/slash"
original_sep = os.sep.encode("ascii")
# Temporarily modify os_sep_bytes to test Windows path conversion
# This simulates Windows behavior on all platforms for testing
import dulwich.index
dulwich.index.os_sep_bytes = b"\\"
self.addCleanup(setattr, dulwich.index, "os_sep_bytes", original_sep)
fs_path = _tree_to_fs_path(b"/prefix/path", tree_path)
# The function should join the prefix path with the converted tree path
# The expected behavior is that the path separators in the tree_path are
# converted to the platform-specific separator (which we've set to backslash)
expected_path = os.path.join(b"/prefix/path", b"path\\with\\slash")
self.assertEqual(fs_path, expected_path)
def test_fs_to_tree_path_str(self) -> None:
fs_path = os.path.join(os.path.join("délwíçh", "foo"))
tree_path = _fs_to_tree_path(fs_path)
self.assertEqual(tree_path, "délwíçh/foo".encode())
def test_fs_to_tree_path_bytes(self) -> None:
fs_path = os.path.join(os.fsencode(os.path.join("délwíçh", "foo")))
tree_path = _fs_to_tree_path(fs_path)
self.assertEqual(tree_path, "délwíçh/foo".encode())
def test_fs_to_tree_path_windows_separator(self) -> None:
# Test conversion of Windows paths to tree paths
fs_path = b"path\\with\\backslash"
original_sep = os.sep.encode("ascii")
# Temporarily modify os_sep_bytes to test Windows path conversion
import dulwich.index
dulwich.index.os_sep_bytes = b"\\"
self.addCleanup(setattr, dulwich.index, "os_sep_bytes", original_sep)
tree_path = _fs_to_tree_path(fs_path)
self.assertEqual(tree_path, b"path/with/backslash")
class TestIndexEntryFromPath(TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
def test_index_entry_from_path_file(self) -> None:
"""Test creating index entry from a regular file."""
# Create a test file
test_file = os.path.join(self.tempdir, "testfile")
with open(test_file, "wb") as f:
f.write(b"test content")
# Get the index entry
entry = index_entry_from_path(os.fsencode(test_file))
# Verify the entry was created with the right mode
self.assertIsNotNone(entry)
self.assertEqual(cleanup_mode(os.stat(test_file).st_mode), entry.mode)
@skipIf(not can_symlink(), "Requires symlink support")
def test_index_entry_from_path_symlink(self) -> None:
"""Test creating index entry from a symlink."""
# Create a target file
target_file = os.path.join(self.tempdir, "target")
with open(target_file, "wb") as f:
f.write(b"target content")
# Create a symlink
link_file = os.path.join(self.tempdir, "symlink")
os.symlink(target_file, link_file)
# Get the index entry
entry = index_entry_from_path(os.fsencode(link_file))
# Verify the entry was created with the right mode
self.assertIsNotNone(entry)
self.assertEqual(cleanup_mode(os.lstat(link_file).st_mode), entry.mode)
def test_index_entry_from_path_directory(self) -> None:
"""Test creating index entry from a directory (should return None)."""
# Create a directory
test_dir = os.path.join(self.tempdir, "testdir")
os.mkdir(test_dir)
# Get the index entry for a directory
entry = index_entry_from_path(os.fsencode(test_dir))
# Should return None for regular directories
self.assertIsNone(entry)
def test_index_entry_from_directory_regular(self) -> None:
"""Test index_entry_from_directory with a regular directory."""
# Create a directory
test_dir = os.path.join(self.tempdir, "testdir")
os.mkdir(test_dir)
# Get stat for the directory
st = os.lstat(test_dir)
# Get the index entry for a regular directory
entry = index_entry_from_directory(st, os.fsencode(test_dir))
# Should return None for regular directories
self.assertIsNone(entry)
def test_index_entry_from_directory_git_submodule(self) -> None:
"""Test index_entry_from_directory with a Git submodule."""
# Create a git repository that will be a submodule
sub_repo_dir = os.path.join(self.tempdir, "subrepo")
os.mkdir(sub_repo_dir)
# Create the .git directory to make it look like a git repo
git_dir = os.path.join(sub_repo_dir, ".git")
os.mkdir(git_dir)
# Create HEAD file with a fake commit SHA
head_sha = b"1234567890" * 4 # 40-char fake SHA
with open(os.path.join(git_dir, "HEAD"), "wb") as f:
f.write(head_sha)
# Get stat for the submodule directory
st = os.lstat(sub_repo_dir)
# Get the index entry for a git submodule directory
entry = index_entry_from_directory(st, os.fsencode(sub_repo_dir))
# Since we don't have a proper git setup, this might still return None
# This test just ensures the code path is executed
if entry is not None:
# If an entry is returned, it should have the gitlink mode
self.assertEqual(entry.mode, S_IFGITLINK)
def test_index_entry_from_path_with_object_store(self) -> None:
"""Test creating index entry with object store."""
# Create a test file
test_file = os.path.join(self.tempdir, "testfile")
with open(test_file, "wb") as f:
f.write(b"test content")
# Create a memory object store
object_store = MemoryObjectStore()
# Get the index entry and add to object store
entry = index_entry_from_path(os.fsencode(test_file), object_store)
# Verify we can access the blob from the object store
self.assertIsNotNone(entry)
blob = object_store[entry.sha]
self.assertEqual(b"test content", blob.data)
def test_iter_fresh_entries(self) -> None:
"""Test iterating over fresh entries."""
# Create some test files
file1 = os.path.join(self.tempdir, "file1")
with open(file1, "wb") as f:
f.write(b"file1 content")
file2 = os.path.join(self.tempdir, "file2")
with open(file2, "wb") as f:
f.write(b"file2 content")
# Create a memory object store
object_store = MemoryObjectStore()
# Get fresh entries
paths = [b"file1", b"file2", b"nonexistent"]
entries = dict(
iter_fresh_entries(paths, os.fsencode(self.tempdir), object_store)
)
# Verify both files got entries but nonexistent file is None
self.assertIn(b"file1", entries)
self.assertIn(b"file2", entries)
self.assertIn(b"nonexistent", entries)
self.assertIsNotNone(entries[b"file1"])
self.assertIsNotNone(entries[b"file2"])
self.assertIsNone(entries[b"nonexistent"])
# Check that blobs were added to object store
blob1 = object_store[entries[b"file1"].sha]
self.assertEqual(b"file1 content", blob1.data)
blob2 = object_store[entries[b"file2"].sha]
self.assertEqual(b"file2 content", blob2.data)
def test_read_submodule_head(self) -> None:
"""Test reading the HEAD of a submodule."""
from dulwich.index import read_submodule_head
# Create a test repo that will be our "submodule"
sub_repo_dir = os.path.join(self.tempdir, "subrepo")
os.mkdir(sub_repo_dir)
submodule_repo = Repo.init(sub_repo_dir)
# Create a file and commit it to establish a HEAD
test_file = os.path.join(sub_repo_dir, "testfile")
with open(test_file, "wb") as f:
f.write(b"test content")
submodule_repo.get_worktree().stage(["testfile"])
commit_id = submodule_repo.get_worktree().commit(
message=b"Test commit for submodule",
)
# Test reading the HEAD
head_sha = read_submodule_head(sub_repo_dir)
self.assertEqual(commit_id, head_sha)
# Test with bytes path
head_sha_bytes = read_submodule_head(os.fsencode(sub_repo_dir))
self.assertEqual(commit_id, head_sha_bytes)
# Test with non-existent path
non_repo_dir = os.path.join(self.tempdir, "nonrepo")
os.mkdir(non_repo_dir)
self.assertIsNone(read_submodule_head(non_repo_dir))
# Test with path that doesn't have a .git directory
not_git_dir = os.path.join(self.tempdir, "notgit")
os.mkdir(not_git_dir)
self.assertIsNone(read_submodule_head(not_git_dir))
def test_has_directory_changed(self) -> None:
"""Test checking if a directory has changed."""
from dulwich.index import IndexEntry, _has_directory_changed
# Setup mock IndexEntry
mock_entry = IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
0,
0,
)
# Test with a regular directory (not a submodule)
reg_dir = os.path.join(self.tempdir, "regular_dir")
os.mkdir(reg_dir)
# Should return True for regular directory
self.assertTrue(_has_directory_changed(os.fsencode(reg_dir), mock_entry))
# Create a git repository to test submodule scenarios
sub_repo_dir = os.path.join(self.tempdir, "subrepo")
os.mkdir(sub_repo_dir)
submodule_repo = Repo.init(sub_repo_dir)
# Create a file and commit it to establish a HEAD
test_file = os.path.join(sub_repo_dir, "testfile")
with open(test_file, "wb") as f:
f.write(b"test content")
submodule_repo.get_worktree().stage(["testfile"])
commit_id = submodule_repo.get_worktree().commit(
message=b"Test commit for submodule",
)
# Create an entry with the correct commit SHA
correct_entry = IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
commit_id,
0,
0,
)
# Create an entry with an incorrect commit SHA
incorrect_entry = IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"0000000000000000000000000000000000000000",
0,
0,
)
# Should return False for submodule with correct SHA
self.assertFalse(
_has_directory_changed(os.fsencode(sub_repo_dir), correct_entry)
)
# Should return True for submodule with incorrect SHA
self.assertTrue(
_has_directory_changed(os.fsencode(sub_repo_dir), incorrect_entry)
)
def test_get_unstaged_changes(self) -> None:
"""Test detecting unstaged changes in a working tree."""
from dulwich.index import (
ConflictedIndexEntry,
Index,
IndexEntry,
get_unstaged_changes,
)
# Create a test repo
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
# Create test index
index = Index(os.path.join(repo_dir, "index"))
# Create an actual hash of our test content
from dulwich.objects import Blob
test_blob = Blob()
test_blob.data = b"initial content"
# Create some test files with known contents
file1_path = os.path.join(repo_dir, "file1")
with open(file1_path, "wb") as f:
f.write(b"initial content")
file2_path = os.path.join(repo_dir, "file2")
with open(file2_path, "wb") as f:
f.write(b"initial content")
# Add them to index
entry1 = IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", # Not matching actual content
0,
0,
)
entry2 = IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
test_blob.id, # Will be content's real hash
0,
0,
)
# Add a file that has a conflict
entry_conflict = ConflictedIndexEntry(b"conflict", {0: None, 1: None, 2: None})
index._byname = {
b"file1": entry1,
b"file2": entry2,
b"file3": IndexEntry(
(1230680220, 0),
(1230680220, 0),
2050,
3761020,
33188,
1000,
1000,
0,
b"0000000000000000000000000000000000000000",
0,
0,
),
b"conflict": entry_conflict,
}
# Get unstaged changes
changes = list(get_unstaged_changes(index, repo_dir))
# File1 should be unstaged (content doesn't match hash)
# File3 doesn't exist (deleted)
# Conflict is always unstaged
self.assertEqual(sorted(changes), [b"conflict", b"file1", b"file3"])
# Create directory where there should be a file
os.mkdir(os.path.join(repo_dir, "file4"))
index._byname[b"file4"] = entry1
# Get unstaged changes again
changes = list(get_unstaged_changes(index, repo_dir))
# Now file4 should also be unstaged because it's a directory instead of a file
self.assertEqual(sorted(changes), [b"conflict", b"file1", b"file3", b"file4"])
# Create a custom blob filter function
def filter_blob_callback(blob, path):
# Modify blob data to make it look changed
result_blob = Blob()
result_blob.data = b"modified " + blob.data
return result_blob
# Get unstaged changes with blob filter
changes = list(get_unstaged_changes(index, repo_dir, filter_blob_callback))
# Now both file1 and file2 should be unstaged due to the filter
self.assertEqual(
sorted(changes), [b"conflict", b"file1", b"file2", b"file3", b"file4"]
)
def test_get_unstaged_changes_with_blob_filter(self) -> None:
"""Test get_unstaged_changes with filter that expects Blob objects.
This reproduces issue #2010 where passing blob.data instead of blob
to the filter callback causes AttributeError when the callback expects
a Blob object (like checkin_normalize does).
"""
repo_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, repo_dir)
with Repo.init(repo_dir) as repo:
# Create and commit a test file
test_file = os.path.join(repo_dir, "test.txt")
with open(test_file, "wb") as f:
f.write(b"original content")
repo.get_worktree().stage(["test.txt"])
repo.get_worktree().commit(
message=b"Initial commit",
committer=b"Test <test@example.com>",
author=b"Test <test@example.com>",
)
# Create a .gitattributes file
gitattributes_file = os.path.join(repo_dir, ".gitattributes")
with open(gitattributes_file, "wb") as f:
f.write(b"*.txt text\n")
# Modify the test file
with open(test_file, "wb") as f:
f.write(b"modified content")
# Force mtime change to ensure stat doesn't match
os.utime(test_file, (0, 0))
# Create a filter callback that expects Blob objects (like checkin_normalize)
def blob_filter_callback(blob: Blob, path: bytes) -> Blob:
"""Filter that expects a Blob object, not bytes."""
# This should receive a Blob object with a .data attribute
self.assertIsInstance(blob, Blob)
self.assertTrue(hasattr(blob, "data"))
# Return the blob unchanged for this test
return blob
# This should not raise AttributeError: 'bytes' object has no attribute 'data'
changes = list(
get_unstaged_changes(repo.open_index(), repo_dir, blob_filter_callback)
)
# Should detect the change in test.txt
self.assertIn(b"test.txt", changes)
class TestManyFilesFeature(TestCase):
"""Tests for the manyFiles feature (index version 4 and skipHash)."""
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
def test_index_version_4_parsing(self):
"""Test that index version 4 files can be parsed."""
index_path = os.path.join(self.tempdir, "index")
# Create an index with version 4
index = Index(index_path, read=False, version=4)
# Add some entries
entry = IndexEntry(
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=1,
mode=0o100644,
uid=1000,
gid=1000,
size=5,
sha=ZERO_SHA,
)
index[b"test.txt"] = entry
# Write and read back
index.write()
# Read the index back
index2 = Index(index_path)
self.assertEqual(index2._version, 4)
self.assertIn(b"test.txt", index2)
def test_skip_hash_feature(self):
"""Test that skipHash feature works correctly."""
index_path = os.path.join(self.tempdir, "index")
# Create an index with skipHash enabled
index = Index(index_path, read=False, skip_hash=True)
# Add some entries
entry = IndexEntry(
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=1,
mode=0o100644,
uid=1000,
gid=1000,
size=5,
sha=ZERO_SHA,
)
index[b"test.txt"] = entry
# Write the index
index.write()
# Verify the file was written with zero hash
with open(index_path, "rb") as f:
f.seek(-20, 2) # Seek to last 20 bytes
trailing_hash = f.read(20)
self.assertEqual(trailing_hash, b"\x00" * 20)
# Verify we can still read it back
index2 = Index(index_path)
self.assertIn(b"test.txt", index2)
def test_version_4_no_padding(self):
"""Test that version 4 entries have no padding."""
# Create entries with names that would show compression benefits
entries = [
SerializedIndexEntry(
name=b"src/main/java/com/example/Service.java",
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=1,
mode=0o100644,
uid=1000,
gid=1000,
size=5,
sha=ZERO_SHA,
flags=0,
extended_flags=0,
),
SerializedIndexEntry(
name=b"src/main/java/com/example/Controller.java",
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=2,
mode=0o100644,
uid=1000,
gid=1000,
size=5,
sha=b"1" * 40,
flags=0,
extended_flags=0,
),
]
# Test version 2 (with padding, full paths)
buf_v2 = BytesIO()
from dulwich.index import write_cache_entry
previous_path = b""
for entry in entries:
# Set proper flags for v2
entry_v2 = SerializedIndexEntry(
entry.name,
entry.ctime,
entry.mtime,
entry.dev,
entry.ino,
entry.mode,
entry.uid,
entry.gid,
entry.size,
entry.sha,
len(entry.name),
entry.extended_flags,
)
write_cache_entry(buf_v2, entry_v2, version=2, previous_path=previous_path)
previous_path = entry.name
v2_data = buf_v2.getvalue()
# Test version 4 (path compression, no padding)
buf_v4 = BytesIO()
previous_path = b""
for entry in entries:
write_cache_entry(buf_v4, entry, version=4, previous_path=previous_path)
previous_path = entry.name
v4_data = buf_v4.getvalue()
# Version 4 should be shorter due to compression and no padding
self.assertLess(len(v4_data), len(v2_data))
# Both should parse correctly
buf_v2.seek(0)
from dulwich.index import read_cache_entry
previous_path = b""
parsed_v2_entries = []
for _ in entries:
parsed = read_cache_entry(buf_v2, version=2, previous_path=previous_path)
parsed_v2_entries.append(parsed)
previous_path = parsed.name
buf_v4.seek(0)
previous_path = b""
parsed_v4_entries = []
for _ in entries:
parsed = read_cache_entry(buf_v4, version=4, previous_path=previous_path)
parsed_v4_entries.append(parsed)
previous_path = parsed.name
# Both should have the same paths
for v2_entry, v4_entry in zip(parsed_v2_entries, parsed_v4_entries):
self.assertEqual(v2_entry.name, v4_entry.name)
self.assertEqual(v2_entry.sha, v4_entry.sha)
class TestManyFilesRepoIntegration(TestCase):
"""Tests for manyFiles feature integration with Repo."""
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
def test_repo_with_manyfiles_config(self):
"""Test that a repository with feature.manyFiles=true uses the right settings."""
# Create a new repository
repo = Repo.init(self.tempdir)
# Set feature.manyFiles=true in config
config = repo.get_config()
config.set(b"feature", b"manyFiles", b"true")
config.write_to_path()
# Open the index - should have skipHash enabled and version 4
index = repo.open_index()
self.assertTrue(index._skip_hash)
self.assertEqual(index._version, 4)
def test_repo_with_explicit_index_settings(self):
"""Test that explicit index.version and index.skipHash work."""
# Create a new repository
repo = Repo.init(self.tempdir)
# Set explicit index settings
config = repo.get_config()
config.set(b"index", b"version", b"3")
config.set(b"index", b"skipHash", b"false")
config.write_to_path()
# Open the index - should respect explicit settings
index = repo.open_index()
self.assertFalse(index._skip_hash)
self.assertEqual(index._version, 3)
class TestPathPrefixCompression(TestCase):
"""Tests for index version 4 path prefix compression."""
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
def test_varint_encoding_decoding(self):
"""Test variable-width integer encoding and decoding."""
test_values = [0, 1, 127, 128, 255, 256, 16383, 16384, 65535, 65536]
for value in test_values:
encoded = _encode_varint(value)
decoded, _ = _decode_varint(encoded, 0)
self.assertEqual(value, decoded, f"Failed for value {value}")
def test_path_compression_simple(self):
"""Test simple path compression cases."""
# Test case 1: No common prefix
compressed = _compress_path(b"file1.txt", b"")
decompressed, _ = _decompress_path(compressed, 0, b"")
self.assertEqual(b"file1.txt", decompressed)
# Test case 2: Common prefix
compressed = _compress_path(b"src/file2.txt", b"src/file1.txt")
decompressed, _ = _decompress_path(compressed, 0, b"src/file1.txt")
self.assertEqual(b"src/file2.txt", decompressed)
# Test case 3: Completely different paths
compressed = _compress_path(b"docs/readme.md", b"src/file1.txt")
decompressed, _ = _decompress_path(compressed, 0, b"src/file1.txt")
self.assertEqual(b"docs/readme.md", decompressed)
def test_path_compression_deep_directories(self):
"""Test compression with deep directory structures."""
path1 = b"src/main/java/com/example/service/UserService.java"
path2 = b"src/main/java/com/example/service/OrderService.java"
path3 = b"src/main/java/com/example/model/User.java"
# Compress path2 relative to path1
compressed = _compress_path(path2, path1)
decompressed, _ = _decompress_path(compressed, 0, path1)
self.assertEqual(path2, decompressed)
# Compress path3 relative to path2
compressed = _compress_path(path3, path2)
decompressed, _ = _decompress_path(compressed, 0, path2)
self.assertEqual(path3, decompressed)
def test_index_version_4_with_compression(self):
"""Test full index version 4 write/read with path compression."""
index_path = os.path.join(self.tempdir, "index")
# Create an index with version 4
index = Index(index_path, read=False, version=4)
# Add multiple entries with common prefixes
paths = [
b"src/main/java/App.java",
b"src/main/java/Utils.java",
b"src/main/resources/config.properties",
b"src/test/java/AppTest.java",
b"docs/README.md",
b"docs/INSTALL.md",
]
for i, path in enumerate(paths):
entry = IndexEntry(
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=i + 1,
mode=0o100644,
uid=1000,
gid=1000,
size=10,
sha=f"{i:040d}".encode(),
)
index[path] = entry
# Write and read back
index.write()
# Read the index back
index2 = Index(index_path)
self.assertEqual(index2._version, 4)
# Verify all paths were preserved correctly
for path in paths:
self.assertIn(path, index2)
# Verify the index file is smaller than version 2 would be
with open(index_path, "rb") as f:
v4_size = len(f.read())
# Create equivalent version 2 index for comparison
index_v2_path = os.path.join(self.tempdir, "index_v2")
index_v2 = Index(index_v2_path, read=False, version=2)
for path in paths:
entry = IndexEntry(
ctime=(1234567890, 0),
mtime=(1234567890, 0),
dev=1,
ino=1,
mode=0o100644,
uid=1000,
gid=1000,
size=10,
sha=ZERO_SHA,
)
index_v2[path] = entry
index_v2.write()
with open(index_v2_path, "rb") as f:
v2_size = len(f.read())
# Version 4 should be smaller due to compression
self.assertLess(
v4_size, v2_size, "Version 4 index should be smaller than version 2"
)
def test_path_compression_edge_cases(self):
"""Test edge cases in path compression."""
# Empty paths
compressed = _compress_path(b"", b"")
decompressed, _ = _decompress_path(compressed, 0, b"")
self.assertEqual(b"", decompressed)
# Path identical to previous
compressed = _compress_path(b"same.txt", b"same.txt")
decompressed, _ = _decompress_path(compressed, 0, b"same.txt")
self.assertEqual(b"same.txt", decompressed)
# Path shorter than previous
compressed = _compress_path(b"short", b"very/long/path/file.txt")
decompressed, _ = _decompress_path(compressed, 0, b"very/long/path/file.txt")
self.assertEqual(b"short", decompressed)
class TestDetectCaseOnlyRenames(TestCase):
"""Tests for detect_case_only_renames function."""
def setUp(self):
self.config = ConfigDict()
def test_no_renames(self):
"""Test when there are no renames."""
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"file1.txt", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"file2.txt", 0o100644, b"b" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# No case-only renames, so should return original changes
self.assertEqual(changes, result)
def test_simple_case_rename(self):
"""Test simple case-only rename detection."""
# Default config uses case-insensitive comparison
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"README.txt", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"readme.txt", 0o100644, b"a" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return one CHANGE_RENAME instead of ADD/DELETE pair
self.assertEqual(1, len(result))
self.assertEqual(CHANGE_RENAME, result[0].type)
self.assertEqual(b"README.txt", result[0].old.path)
self.assertEqual(b"readme.txt", result[0].new.path)
def test_nested_path_case_rename(self):
"""Test case-only rename in nested paths."""
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"src/Main.java", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"src/main.java", 0o100644, b"a" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return one CHANGE_RENAME instead of ADD/DELETE pair
self.assertEqual(1, len(result))
self.assertEqual(CHANGE_RENAME, result[0].type)
self.assertEqual(b"src/Main.java", result[0].old.path)
self.assertEqual(b"src/main.java", result[0].new.path)
def test_multiple_case_renames(self):
"""Test multiple case-only renames."""
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"File1.txt", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_DELETE,
TreeEntry(b"File2.TXT", 0o100644, b"b" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"file1.txt", 0o100644, b"a" * 40),
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"file2.txt", 0o100644, b"b" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return two CHANGE_RENAME instead of ADD/DELETE pairs
self.assertEqual(2, len(result))
rename_changes = [c for c in result if c.type == CHANGE_RENAME]
self.assertEqual(2, len(rename_changes))
# Check that the renames are correct (order may vary)
rename_map = {c.old.path: c.new.path for c in rename_changes}
self.assertEqual(
{b"File1.txt": b"file1.txt", b"File2.TXT": b"file2.txt"}, rename_map
)
def test_case_rename_with_modify(self):
"""Test case rename detection with CHANGE_MODIFY."""
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"README.md", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_MODIFY,
TreeEntry(b"readme.md", 0o100644, b"a" * 40),
TreeEntry(b"readme.md", 0o100644, b"b" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return one CHANGE_RENAME instead of DELETE/MODIFY pair
self.assertEqual(1, len(result))
self.assertEqual(CHANGE_RENAME, result[0].type)
self.assertEqual(b"README.md", result[0].old.path)
self.assertEqual(b"readme.md", result[0].new.path)
def test_hfs_normalization(self):
"""Test case rename detection with HFS+ normalization."""
# Configure for HFS+ (macOS)
self.config.set((b"core",), b"protectHFS", b"true")
self.config.set((b"core",), b"protectNTFS", b"false")
# Test with composed vs decomposed Unicode
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry("café.txt".encode(), 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry("CAFÉ.txt".encode(), 0o100644, b"a" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return one CHANGE_RENAME for the case-only rename
self.assertEqual(1, len(result))
self.assertEqual(CHANGE_RENAME, result[0].type)
self.assertEqual("café.txt".encode(), result[0].old.path)
self.assertEqual("CAFÉ.txt".encode(), result[0].new.path)
def test_ntfs_normalization(self):
"""Test case rename detection with NTFS normalization."""
# Configure for NTFS (Windows)
self.config.set((b"core",), b"protectNTFS", b"true")
self.config.set((b"core",), b"protectHFS", b"false")
# NTFS strips trailing dots and spaces
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"file.txt.", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"FILE.TXT", 0o100644, b"a" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# Should return one CHANGE_RENAME for the case-only rename
self.assertEqual(1, len(result))
self.assertEqual(CHANGE_RENAME, result[0].type)
self.assertEqual(b"file.txt.", result[0].old.path)
self.assertEqual(b"FILE.TXT", result[0].new.path)
def test_invalid_utf8_handling(self):
"""Test handling of invalid UTF-8 in paths."""
# Invalid UTF-8 sequence
invalid_path = b"\xff\xfe"
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(invalid_path, 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_ADD,
None,
TreeEntry(b"valid.txt", 0o100644, b"b" * 40),
),
]
# Should not crash, just skip invalid paths
result = detect_case_only_renames(changes, self.config)
# No case-only renames detected, returns original changes
self.assertEqual(changes, result)
def test_rename_and_copy_changes(self):
"""Test case rename detection with CHANGE_RENAME and CHANGE_COPY."""
changes = [
TreeChange(
CHANGE_DELETE,
TreeEntry(b"OldFile.txt", 0o100644, b"a" * 40),
None,
),
TreeChange(
CHANGE_RENAME,
TreeEntry(b"other.txt", 0o100644, b"b" * 40),
TreeEntry(b"oldfile.txt", 0o100644, b"a" * 40),
),
TreeChange(
CHANGE_COPY,
TreeEntry(b"source.txt", 0o100644, b"c" * 40),
TreeEntry(b"OLDFILE.TXT", 0o100644, b"a" * 40),
),
]
result = detect_case_only_renames(changes, self.config)
# The DELETE of OldFile.txt and COPY to OLDFILE.TXT are detected as a case-only rename
# The original RENAME (other.txt -> oldfile.txt) remains
# The COPY is consumed by the case-only rename detection
self.assertEqual(2, len(result))
# Find the changes
rename_changes = [c for c in result if c.type == CHANGE_RENAME]
self.assertEqual(2, len(rename_changes))
# Check for the case-only rename
case_rename = None
for change in rename_changes:
if change.old.path == b"OldFile.txt" and change.new.path == b"OLDFILE.TXT":
case_rename = change
break
self.assertIsNotNone(case_rename)
self.assertEqual(b"OldFile.txt", case_rename.old.path)
self.assertEqual(b"OLDFILE.TXT", case_rename.new.path)
class TestUpdateWorkingTree(TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
def cleanup_tempdir():
"""Remove tempdir, handling read-only files on Windows."""
def remove_readonly(func, path, excinfo):
"""Error handler for Windows read-only files."""
import stat
if sys.platform == "win32" and excinfo[0] is PermissionError:
os.chmod(path, stat.S_IWRITE)
func(path)
else:
raise
shutil.rmtree(self.tempdir, onerror=remove_readonly)
self.addCleanup(cleanup_tempdir)
self.repo = Repo.init(self.tempdir)
def test_update_working_tree_with_blob_normalizer(self):
"""Test update_working_tree with a blob normalizer."""
# Create a simple blob normalizer that converts CRLF to LF
class TestBlobNormalizer:
def checkout_normalize(self, blob, path):
# Convert CRLF to LF during checkout
new_blob = Blob()
new_blob.data = blob.data.replace(b"\r\n", b"\n")
return new_blob
# Create a tree with a file containing CRLF
blob = Blob()
blob.data = b"Hello\r\nWorld\r\n"
self.repo.object_store.add_object(blob)
tree = Tree()
tree[b"test.txt"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree)
# Update working tree with normalizer
normalizer = TestBlobNormalizer()
changes = tree_changes(self.repo.object_store, None, tree.id)
update_working_tree(
self.repo,
None, # old_tree_id
tree.id, # new_tree_id
change_iterator=changes,
blob_normalizer=normalizer,
)
# Check that the file was written with LF line endings
test_file = os.path.join(self.tempdir, "test.txt")
with open(test_file, "rb") as f:
content = f.read()
self.assertEqual(b"Hello\nWorld\n", content)
# Check that the index has the original blob SHA
index = self.repo.open_index()
self.assertEqual(blob.id, index[b"test.txt"].sha)
def test_update_working_tree_without_blob_normalizer(self):
"""Test update_working_tree without a blob normalizer."""
# Create a tree with a file containing CRLF
blob = Blob()
blob.data = b"Hello\r\nWorld\r\n"
self.repo.object_store.add_object(blob)
tree = Tree()
tree[b"test.txt"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree)
# Update working tree without normalizer
changes = tree_changes(self.repo.object_store, None, tree.id)
update_working_tree(
self.repo,
None, # old_tree_id
tree.id, # new_tree_id
change_iterator=changes,
blob_normalizer=None,
)
# Check that the file was written with original CRLF line endings
test_file = os.path.join(self.tempdir, "test.txt")
with open(test_file, "rb") as f:
content = f.read()
self.assertEqual(b"Hello\r\nWorld\r\n", content)
# Check that the index has the blob SHA
index = self.repo.open_index()
self.assertEqual(blob.id, index[b"test.txt"].sha)
def test_update_working_tree_remove_directory(self):
"""Test that update_working_tree properly removes directories."""
# Create initial tree with a directory containing files
blob1 = Blob()
blob1.data = b"content1"
self.repo.object_store.add_object(blob1)
blob2 = Blob()
blob2.data = b"content2"
self.repo.object_store.add_object(blob2)
tree1 = Tree()
tree1[b"dir/file1.txt"] = (0o100644, blob1.id)
tree1[b"dir/file2.txt"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree1)
# Update to tree1 (create directory with files)
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify directory and files exist
dir_path = os.path.join(self.tempdir, "dir")
self.assertTrue(os.path.isdir(dir_path))
self.assertTrue(os.path.exists(os.path.join(dir_path, "file1.txt")))
self.assertTrue(os.path.exists(os.path.join(dir_path, "file2.txt")))
# Create empty tree (remove everything)
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update to empty tree
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Verify directory was removed
self.assertFalse(os.path.exists(dir_path))
def test_update_working_tree_submodule_to_file(self):
"""Test replacing a submodule directory with a file."""
# Create tree with submodule
submodule_sha = b"a" * 40
tree1 = Tree()
tree1[b"submodule"] = (S_IFGITLINK, submodule_sha)
self.repo.object_store.add_object(tree1)
# Update to tree with submodule
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify submodule directory exists with .git file
submodule_path = os.path.join(self.tempdir, "submodule")
self.assertTrue(os.path.isdir(submodule_path))
self.assertTrue(os.path.exists(os.path.join(submodule_path, ".git")))
# Create tree with file at same path
blob = Blob()
blob.data = b"file content"
self.repo.object_store.add_object(blob)
tree2 = Tree()
tree2[b"submodule"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree2)
# Update to tree with file (should remove submodule directory and create file)
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Verify it's now a file
self.assertTrue(os.path.isfile(submodule_path))
with open(submodule_path, "rb") as f:
self.assertEqual(b"file content", f.read())
def test_update_working_tree_directory_with_nested_subdir(self):
"""Test removing directory with nested subdirectories."""
# Create tree with nested directories
blob = Blob()
blob.data = b"deep content"
self.repo.object_store.add_object(blob)
tree1 = Tree()
tree1[b"a/b/c/file.txt"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify nested structure exists
path_a = os.path.join(self.tempdir, "a")
path_b = os.path.join(path_a, "b")
path_c = os.path.join(path_b, "c")
file_path = os.path.join(path_c, "file.txt")
self.assertTrue(os.path.exists(file_path))
# Create empty tree
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update to empty tree
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Verify all directories were removed
self.assertFalse(os.path.exists(path_a))
def test_update_working_tree_file_replaced_by_dir_not_removed(self):
"""Test that a directory replacing a git file is left alone if not empty."""
# Create tree with a file
blob = Blob()
blob.data = b"file content"
self.repo.object_store.add_object(blob)
tree1 = Tree()
tree1[b"path"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify file exists
file_path = os.path.join(self.tempdir, "path")
self.assertTrue(os.path.isfile(file_path))
# Manually replace file with directory containing untracked file
os.remove(file_path)
os.mkdir(file_path)
with open(os.path.join(file_path, "untracked.txt"), "w") as f:
f.write("untracked content")
# Create empty tree
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update should succeed but leave the directory alone
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Directory should still exist with its contents
self.assertTrue(os.path.isdir(file_path))
self.assertTrue(os.path.exists(os.path.join(file_path, "untracked.txt")))
def test_update_working_tree_file_replaced_by_empty_dir_removed(self):
"""Test that an empty directory replacing a git file is removed."""
# Create tree with a file
blob = Blob()
blob.data = b"file content"
self.repo.object_store.add_object(blob)
tree1 = Tree()
tree1[b"path"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify file exists
file_path = os.path.join(self.tempdir, "path")
self.assertTrue(os.path.isfile(file_path))
# Manually replace file with empty directory
os.remove(file_path)
os.mkdir(file_path)
# Create empty tree
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update should remove the empty directory
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Directory should be gone
self.assertFalse(os.path.exists(file_path))
def test_update_working_tree_symlink_transitions(self):
"""Test transitions involving symlinks."""
# Skip on Windows where symlinks might not be supported
if sys.platform == "win32":
self.skipTest("Symlinks not fully supported on Windows")
# Create tree with symlink
blob1 = Blob()
blob1.data = b"target/path"
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"link"] = (0o120000, blob1.id) # Symlink mode
self.repo.object_store.add_object(tree1)
# Update to tree with symlink
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
link_path = os.path.join(self.tempdir, "link")
self.assertTrue(os.path.islink(link_path))
self.assertEqual(b"target/path", os.readlink(link_path).encode())
# Test 1: Replace symlink with regular file
blob2 = Blob()
blob2.data = b"file content"
self.repo.object_store.add_object(blob2)
tree2 = Tree()
tree2[b"link"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree2)
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
self.assertFalse(os.path.islink(link_path))
self.assertTrue(os.path.isfile(link_path))
with open(link_path, "rb") as f:
self.assertEqual(b"file content", f.read())
# Test 2: Replace file with symlink
changes = tree_changes(self.repo.object_store, tree2.id, tree1.id)
update_working_tree(self.repo, tree2.id, tree1.id, change_iterator=changes)
self.assertTrue(os.path.islink(link_path))
self.assertEqual(b"target/path", os.readlink(link_path).encode())
# Test 3: Replace symlink with directory (manually)
os.unlink(link_path)
os.mkdir(link_path)
# Create empty tree
tree3 = Tree()
self.repo.object_store.add_object(tree3)
# Should remove empty directory
changes = tree_changes(self.repo.object_store, tree1.id, tree3.id)
update_working_tree(self.repo, tree1.id, tree3.id, change_iterator=changes)
self.assertFalse(os.path.exists(link_path))
def test_update_working_tree_modified_file_to_dir_transition(self):
"""Test that modified files are not removed when they should be directories."""
# Create tree with file
blob1 = Blob()
blob1.data = b"original content"
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"path"] = (0o100644, blob1.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
file_path = os.path.join(self.tempdir, "path")
# Modify the file locally
with open(file_path, "w") as f:
f.write("modified content")
# Create tree where path is a directory with file
blob2 = Blob()
blob2.data = b"subfile content"
self.repo.object_store.add_object(blob2)
tree2 = Tree()
tree2[b"path/subfile"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree2)
# Update should fail because can't create directory where modified file exists
with self.assertRaises(IOError):
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# File should still exist with modifications
self.assertTrue(os.path.isfile(file_path))
with open(file_path) as f:
self.assertEqual("modified content", f.read())
def test_update_working_tree_executable_transitions(self):
"""Test transitions involving executable bit changes."""
# Skip on Windows where executable bit is not supported
if sys.platform == "win32":
self.skipTest("Executable bit not supported on Windows")
# Create tree with non-executable file
blob = Blob()
blob.data = b"#!/bin/sh\necho hello"
self.repo.object_store.add_object(blob)
tree1 = Tree()
tree1[b"script.sh"] = (0o100644, blob.id) # Non-executable
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
script_path = os.path.join(self.tempdir, "script.sh")
self.assertTrue(os.path.isfile(script_path))
# Check it's not executable
mode = os.stat(script_path).st_mode
self.assertFalse(mode & stat.S_IXUSR)
# Create tree with executable file (same content)
tree2 = Tree()
tree2[b"script.sh"] = (0o100755, blob.id) # Executable
self.repo.object_store.add_object(tree2)
# Update to tree2
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Check it's now executable
mode = os.stat(script_path).st_mode
self.assertTrue(mode & stat.S_IXUSR)
def test_update_working_tree_submodule_with_untracked_files(self):
"""Test that submodules with untracked files are not removed."""
from dulwich.objects import S_IFGITLINK, Tree
# Create tree with submodule
submodule_sha = b"a" * 40
tree1 = Tree()
tree1[b"submodule"] = (S_IFGITLINK, submodule_sha)
self.repo.object_store.add_object(tree1)
# Update to tree with submodule
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Add untracked file to submodule directory
submodule_path = os.path.join(self.tempdir, "submodule")
untracked_path = os.path.join(submodule_path, "untracked.txt")
with open(untracked_path, "w") as f:
f.write("untracked content")
# Create empty tree
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update should not remove submodule directory with untracked files
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Directory should still exist with untracked file
self.assertTrue(os.path.isdir(submodule_path))
self.assertTrue(os.path.exists(untracked_path))
def test_update_working_tree_dir_to_file_with_subdir(self):
"""Test replacing directory structure with a file."""
# Create tree with nested directory structure
blob1 = Blob()
blob1.data = b"content1"
self.repo.object_store.add_object(blob1)
blob2 = Blob()
blob2.data = b"content2"
self.repo.object_store.add_object(blob2)
tree1 = Tree()
tree1[b"dir/subdir/file1"] = (0o100644, blob1.id)
tree1[b"dir/subdir/file2"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify structure exists
dir_path = os.path.join(self.tempdir, "dir")
self.assertTrue(os.path.isdir(dir_path))
# Add an untracked file to make directory truly non-empty
untracked_path = os.path.join(dir_path, "untracked.txt")
with open(untracked_path, "w") as f:
f.write("untracked content")
# Create tree with file at "dir" path
blob3 = Blob()
blob3.data = b"replacement file"
self.repo.object_store.add_object(blob3)
tree2 = Tree()
tree2[b"dir"] = (0o100644, blob3.id)
self.repo.object_store.add_object(tree2)
# Update should fail because directory is not empty
with self.assertRaises(IsADirectoryError):
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Directory should still exist
self.assertTrue(os.path.isdir(dir_path))
def test_update_working_tree_case_sensitivity(self):
"""Test handling of case-sensitive filename changes."""
# Detect if filesystem is case-insensitive by testing
test_file = os.path.join(self.tempdir, "TeSt.tmp")
with open(test_file, "w") as f:
f.write("test")
is_case_insensitive = os.path.exists(os.path.join(self.tempdir, "test.tmp"))
os.unlink(test_file)
# Set core.ignorecase to match actual filesystem behavior
# (This ensures test works correctly regardless of platform defaults)
config = self.repo.get_config()
config.set((b"core",), b"ignorecase", is_case_insensitive)
config.write_to_path()
# Create tree with lowercase file
blob1 = Blob()
blob1.data = b"lowercase content"
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"readme.txt"] = (0o100644, blob1.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Create tree with uppercase file (different content)
blob2 = Blob()
blob2.data = b"uppercase content"
self.repo.object_store.add_object(blob2)
tree2 = Tree()
tree2[b"README.txt"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree2)
# Update to tree2
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Check what exists (behavior depends on filesystem)
lowercase_path = os.path.join(self.tempdir, "readme.txt")
uppercase_path = os.path.join(self.tempdir, "README.txt")
if is_case_insensitive:
# On case-insensitive filesystems, should have one file with new content
# The exact case of the filename may vary by OS
self.assertTrue(
os.path.exists(lowercase_path) or os.path.exists(uppercase_path)
)
# Verify content is the new content
if os.path.exists(lowercase_path):
with open(lowercase_path, "rb") as f:
self.assertEqual(b"uppercase content", f.read())
else:
with open(uppercase_path, "rb") as f:
self.assertEqual(b"uppercase content", f.read())
else:
# On case-sensitive filesystems, only the uppercase file should exist
self.assertFalse(os.path.exists(lowercase_path))
self.assertTrue(os.path.exists(uppercase_path))
with open(uppercase_path, "rb") as f:
self.assertEqual(b"uppercase content", f.read())
def test_update_working_tree_case_rename_updates_filename(self):
"""Test that case-only renames update the actual filename on case-insensitive FS."""
# Detect if filesystem is case-insensitive by testing
test_file = os.path.join(self.tempdir, "TeSt.tmp")
with open(test_file, "w") as f:
f.write("test")
is_case_insensitive = os.path.exists(os.path.join(self.tempdir, "test.tmp"))
os.unlink(test_file)
if not is_case_insensitive:
self.skipTest("Test only relevant on case-insensitive filesystems")
# Set core.ignorecase to match actual filesystem behavior
config = self.repo.get_config()
config.set((b"core",), b"ignorecase", True)
config.write_to_path()
# Create tree with lowercase file
blob1 = Blob()
blob1.data = b"same content" # Using same content to test pure case rename
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"readme.txt"] = (0o100644, blob1.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify initial state
files = [f for f in os.listdir(self.tempdir) if not f.startswith(".git")]
self.assertEqual(["readme.txt"], files)
# Create tree with uppercase file (same content, same blob)
tree2 = Tree()
tree2[b"README.txt"] = (0o100644, blob1.id) # Same blob!
self.repo.object_store.add_object(tree2)
# Update to tree2 (case-only rename)
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# On case-insensitive filesystems, should have one file with updated case
files = [f for f in os.listdir(self.tempdir) if not f.startswith(".git")]
self.assertEqual(
1, len(files), "Should have exactly one file after case rename"
)
# The file should now have the new case in the directory listing
actual_filename = files[0]
self.assertEqual(
"README.txt",
actual_filename,
"Filename case should be updated in directory listing",
)
# Verify content is preserved
file_path = os.path.join(self.tempdir, actual_filename)
with open(file_path, "rb") as f:
self.assertEqual(b"same content", f.read())
# Both old and new case should access the same file
lowercase_path = os.path.join(self.tempdir, "readme.txt")
uppercase_path = os.path.join(self.tempdir, "README.txt")
self.assertTrue(os.path.exists(lowercase_path))
self.assertTrue(os.path.exists(uppercase_path))
def test_update_working_tree_deeply_nested_removal(self):
"""Test removal of deeply nested directory structures."""
# Create deeply nested structure
blob = Blob()
blob.data = b"deep content"
self.repo.object_store.add_object(blob)
tree1 = Tree()
# Create a very deep path
deep_path = b"/".join([b"level%d" % i for i in range(10)])
tree1[deep_path + b"/file.txt"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Verify deep structure exists
current_path = self.tempdir
for i in range(10):
current_path = os.path.join(current_path, f"level{i}")
self.assertTrue(os.path.isdir(current_path))
# Create empty tree
tree2 = Tree()
self.repo.object_store.add_object(tree2)
# Update should remove all empty directories
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Verify top level directory is gone
top_level = os.path.join(self.tempdir, "level0")
self.assertFalse(os.path.exists(top_level))
def test_update_working_tree_read_only_files(self):
"""Test handling of read-only files during updates."""
# Create tree with file
blob1 = Blob()
blob1.data = b"original content"
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"readonly.txt"] = (0o100644, blob1.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Make file read-only
file_path = os.path.join(self.tempdir, "readonly.txt")
os.chmod(file_path, 0o444) # Read-only
# Create tree with modified file
blob2 = Blob()
blob2.data = b"new content"
self.repo.object_store.add_object(blob2)
tree2 = Tree()
tree2[b"readonly.txt"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree2)
# Update should handle read-only file
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
# Verify content was updated
with open(file_path, "rb") as f:
self.assertEqual(b"new content", f.read())
def test_update_working_tree_invalid_filenames(self):
"""Test handling of invalid filenames for the platform."""
# Create tree with potentially problematic filenames
blob = Blob()
blob.data = b"content"
self.repo.object_store.add_object(blob)
tree = Tree()
# Add files with names that might be invalid on some platforms
tree[b"valid.txt"] = (0o100644, blob.id)
if sys.platform != "win32":
# These are invalid on Windows but valid on Unix
tree[b"file:with:colons.txt"] = (0o100644, blob.id)
tree[b"file<with>brackets.txt"] = (0o100644, blob.id)
self.repo.object_store.add_object(tree)
# Update should skip invalid files based on validation
changes = tree_changes(self.repo.object_store, None, tree.id)
update_working_tree(self.repo, None, tree.id, change_iterator=changes)
# Valid file should exist
self.assertTrue(os.path.exists(os.path.join(self.tempdir, "valid.txt")))
def test_update_working_tree_symlink_to_directory(self):
"""Test replacing a symlink pointing to a directory with a real directory."""
if sys.platform == "win32":
self.skipTest("Symlinks not fully supported on Windows")
# Create a target directory
target_dir = os.path.join(self.tempdir, "target")
os.mkdir(target_dir)
with open(os.path.join(target_dir, "file.txt"), "w") as f:
f.write("target file")
# Create tree with symlink pointing to directory
blob1 = Blob()
blob1.data = b"target" # Relative path to target directory
self.repo.object_store.add_object(blob1)
tree1 = Tree()
tree1[b"link"] = (0o120000, blob1.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
link_path = os.path.join(self.tempdir, "link")
self.assertTrue(os.path.islink(link_path))
# Create tree with actual directory at same path
blob2 = Blob()
blob2.data = b"new file content"
self.repo.object_store.add_object(blob2)
tree2 = Tree()
tree2[b"link/newfile.txt"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree2)
# Update should replace symlink with actual directory
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
self.assertFalse(os.path.islink(link_path))
self.assertTrue(os.path.isdir(link_path))
self.assertTrue(os.path.exists(os.path.join(link_path, "newfile.txt")))
def test_update_working_tree_comprehensive_transitions(self):
"""Test all possible file type transitions comprehensively."""
# Skip on Windows where symlinks might not be supported
if sys.platform == "win32":
self.skipTest("Symlinks not fully supported on Windows")
# Create blobs for different file types
file_blob = Blob()
file_blob.data = b"regular file content"
self.repo.object_store.add_object(file_blob)
exec_blob = Blob()
exec_blob.data = b"#!/bin/sh\necho executable"
self.repo.object_store.add_object(exec_blob)
link_blob = Blob()
link_blob.data = b"target/path"
self.repo.object_store.add_object(link_blob)
submodule_sha = b"a" * 40
# Test 1: Regular file → Submodule
tree1 = Tree()
tree1[b"item"] = (0o100644, file_blob.id)
self.repo.object_store.add_object(tree1)
tree2 = Tree()
tree2[b"item"] = (S_IFGITLINK, submodule_sha)
self.repo.object_store.add_object(tree2)
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
self.assertTrue(os.path.isfile(os.path.join(self.tempdir, "item")))
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
self.assertTrue(os.path.isdir(os.path.join(self.tempdir, "item")))
# Test 2: Submodule → Executable file
tree3 = Tree()
tree3[b"item"] = (0o100755, exec_blob.id)
self.repo.object_store.add_object(tree3)
changes = tree_changes(self.repo.object_store, tree2.id, tree3.id)
update_working_tree(self.repo, tree2.id, tree3.id, change_iterator=changes)
item_path = os.path.join(self.tempdir, "item")
self.assertTrue(os.path.isfile(item_path))
if sys.platform != "win32":
self.assertTrue(os.access(item_path, os.X_OK))
# Test 3: Executable file → Symlink
tree4 = Tree()
tree4[b"item"] = (0o120000, link_blob.id)
self.repo.object_store.add_object(tree4)
changes = tree_changes(self.repo.object_store, tree3.id, tree4.id)
update_working_tree(self.repo, tree3.id, tree4.id, change_iterator=changes)
self.assertTrue(os.path.islink(item_path))
# Test 4: Symlink → Submodule
tree5 = Tree()
tree5[b"item"] = (S_IFGITLINK, submodule_sha)
self.repo.object_store.add_object(tree5)
changes = tree_changes(self.repo.object_store, tree4.id, tree5.id)
update_working_tree(self.repo, tree4.id, tree5.id, change_iterator=changes)
self.assertTrue(os.path.isdir(item_path))
# Test 5: Clean up - Submodule → absent
tree6 = Tree()
self.repo.object_store.add_object(tree6)
changes = tree_changes(self.repo.object_store, tree5.id, tree6.id)
update_working_tree(self.repo, tree5.id, tree6.id, change_iterator=changes)
self.assertFalse(os.path.exists(item_path))
# Test 6: Symlink → Executable file
tree7 = Tree()
tree7[b"item2"] = (0o120000, link_blob.id)
self.repo.object_store.add_object(tree7)
changes = tree_changes(self.repo.object_store, tree6.id, tree7.id)
update_working_tree(self.repo, tree6.id, tree7.id, change_iterator=changes)
item2_path = os.path.join(self.tempdir, "item2")
self.assertTrue(os.path.islink(item2_path))
tree8 = Tree()
tree8[b"item2"] = (0o100755, exec_blob.id)
self.repo.object_store.add_object(tree8)
changes = tree_changes(self.repo.object_store, tree7.id, tree8.id)
update_working_tree(self.repo, tree7.id, tree8.id, change_iterator=changes)
self.assertTrue(os.path.isfile(item2_path))
if sys.platform != "win32":
self.assertTrue(os.access(item2_path, os.X_OK))
def test_update_working_tree_partial_update_failure(self):
"""Test handling when update fails partway through."""
# Create initial tree
blob1 = Blob()
blob1.data = b"file1 content"
self.repo.object_store.add_object(blob1)
blob2 = Blob()
blob2.data = b"file2 content"
self.repo.object_store.add_object(blob2)
tree1 = Tree()
tree1[b"file1.txt"] = (0o100644, blob1.id)
tree1[b"file2.txt"] = (0o100644, blob2.id)
self.repo.object_store.add_object(tree1)
# Update to tree1
changes = tree_changes(self.repo.object_store, None, tree1.id)
update_working_tree(self.repo, None, tree1.id, change_iterator=changes)
# Create a directory where file2.txt is, to cause a conflict
file2_path = os.path.join(self.tempdir, "file2.txt")
os.remove(file2_path)
os.mkdir(file2_path)
# Add untracked file to prevent removal
with open(os.path.join(file2_path, "blocker.txt"), "w") as f:
f.write("blocking content")
# Create tree with updates to both files
blob3 = Blob()
blob3.data = b"file1 updated"
self.repo.object_store.add_object(blob3)
blob4 = Blob()
blob4.data = b"file2 updated"
self.repo.object_store.add_object(blob4)
tree2 = Tree()
tree2[b"file1.txt"] = (0o100644, blob3.id)
tree2[b"file2.txt"] = (0o100644, blob4.id)
self.repo.object_store.add_object(tree2)
# Update should partially succeed - file1 updated, file2 blocked
try:
changes = tree_changes(self.repo.object_store, tree1.id, tree2.id)
update_working_tree(self.repo, tree1.id, tree2.id, change_iterator=changes)
except IsADirectoryError:
# Expected to fail on file2 because it's a directory
pass
# file1 should be updated
with open(os.path.join(self.tempdir, "file1.txt"), "rb") as f:
self.assertEqual(b"file1 updated", f.read())
# file2 should still be a directory
self.assertTrue(os.path.isdir(file2_path))
def test_ensure_parent_dir_exists_windows_drive(self):
"""Test that _ensure_parent_dir_exists handles Windows drive letters correctly."""
from dulwich.index import _ensure_parent_dir_exists
# Create a temporary directory to work with
with tempfile.TemporaryDirectory() as tmpdir:
# Test normal case (creates directory)
test_path = os.path.join(tmpdir, "subdir", "file.txt").encode()
_ensure_parent_dir_exists(test_path)
self.assertTrue(os.path.exists(os.path.dirname(test_path)))
# Test when parent is a file (should raise error)
file_path = os.path.join(tmpdir, "testfile").encode()
with open(file_path, "wb") as f:
f.write(b"test")
invalid_path = os.path.join(
tmpdir.encode(), b"testfile", b"subdir", b"file.txt"
)
with self.assertRaisesRegex(
OSError, "Cannot create directory, parent path is a file"
):
_ensure_parent_dir_exists(invalid_path)
# Test with nested subdirectories
nested_path = os.path.join(tmpdir, "a", "b", "c", "d", "file.txt").encode()
_ensure_parent_dir_exists(nested_path)
self.assertTrue(os.path.exists(os.path.dirname(nested_path)))
# Test that various path formats are handled correctly by os.path.dirname
# This includes Windows drive letters, UNC paths, etc.
# The key is that we're using os.path.dirname which handles these correctly
import platform
if platform.system() == "Windows":
# Test Windows-specific paths only on Windows
test_cases = [
b"C:\\temp\\test\\file.txt",
b"D:\\file.txt",
b"\\\\server\\share\\folder\\file.txt",
]
for path in test_cases:
# Just verify os.path.dirname handles these without errors
parent = os.path.dirname(path)
# We're not creating these directories, just testing the logic doesn't fail
self.assertIsInstance(parent, bytes)
class TestSparseIndex(TestCase):
"""Tests for sparse index support."""
def test_serialized_index_entry_is_sparse_dir(self):
"""Test SerializedIndexEntry.is_sparse_dir() method."""
from dulwich.index import EXTENDED_FLAG_SKIP_WORKTREE
# Regular file entry - not sparse
regular_entry = SerializedIndexEntry(
name=b"file.txt",
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=0o100644,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
flags=0,
extended_flags=0,
)
self.assertFalse(regular_entry.is_sparse_dir())
# Directory mode but no skip-worktree flag - not sparse
dir_entry = SerializedIndexEntry(
name=b"dir/",
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=stat.S_IFDIR,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
flags=0,
extended_flags=0,
)
self.assertFalse(dir_entry.is_sparse_dir())
# Skip-worktree flag but not directory - not sparse
skip_file = SerializedIndexEntry(
name=b"file.txt",
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=0o100644,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
flags=0,
extended_flags=EXTENDED_FLAG_SKIP_WORKTREE,
)
self.assertFalse(skip_file.is_sparse_dir())
# Directory mode + skip-worktree + trailing slash - sparse!
sparse_dir = SerializedIndexEntry(
name=b"sparse_dir/",
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=stat.S_IFDIR,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
flags=0,
extended_flags=EXTENDED_FLAG_SKIP_WORKTREE,
)
self.assertTrue(sparse_dir.is_sparse_dir())
def test_index_entry_is_sparse_dir(self):
"""Test IndexEntry.is_sparse_dir() method."""
from dulwich.index import EXTENDED_FLAG_SKIP_WORKTREE
# Regular file - not sparse
regular = IndexEntry(
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=0o100644,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
extended_flags=0,
)
self.assertFalse(regular.is_sparse_dir(b"file.txt"))
# Sparse directory entry
sparse = IndexEntry(
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=stat.S_IFDIR,
uid=0,
gid=0,
size=0,
sha=b"\x00" * 20,
extended_flags=EXTENDED_FLAG_SKIP_WORKTREE,
)
self.assertTrue(sparse.is_sparse_dir(b"dir/"))
self.assertFalse(sparse.is_sparse_dir(b"dir")) # No trailing slash
def test_sparse_dir_extension(self):
"""Test SparseDirExtension serialization."""
from dulwich.index import SDIR_EXTENSION, SparseDirExtension
ext = SparseDirExtension()
self.assertEqual(ext.signature, SDIR_EXTENSION)
self.assertEqual(ext.to_bytes(), b"")
# Test round-trip
ext2 = SparseDirExtension.from_bytes(b"")
self.assertEqual(ext2.signature, SDIR_EXTENSION)
self.assertEqual(ext2.to_bytes(), b"")
def test_index_is_sparse(self):
"""Test Index.is_sparse() method."""
from dulwich.index import SparseDirExtension
with tempfile.TemporaryDirectory() as tmpdir:
index_path = os.path.join(tmpdir, "index")
idx = Index(index_path, read=False)
# Initially not sparse
self.assertFalse(idx.is_sparse())
# Add sparse directory extension
idx._extensions.append(SparseDirExtension())
self.assertTrue(idx.is_sparse())
def test_index_expansion(self):
"""Test Index.ensure_full_index() expands sparse directories."""
from dulwich.index import EXTENDED_FLAG_SKIP_WORKTREE, SparseDirExtension
from dulwich.object_store import MemoryObjectStore
from dulwich.objects import Blob, Tree
# Create a tree structure
store = MemoryObjectStore()
blob1 = Blob()
blob1.data = b"file1"
store.add_object(blob1)
blob2 = Blob()
blob2.data = b"file2"
store.add_object(blob2)
subtree = Tree()
subtree[b"file1.txt"] = (0o100644, blob1.id)
subtree[b"file2.txt"] = (0o100644, blob2.id)
store.add_object(subtree)
# Create an index with a sparse directory entry
with tempfile.TemporaryDirectory() as tmpdir:
index_path = os.path.join(tmpdir, "index")
idx = Index(index_path, read=False)
# Add sparse directory entry
sparse_entry = IndexEntry(
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=stat.S_IFDIR,
uid=0,
gid=0,
size=0,
sha=subtree.id,
extended_flags=EXTENDED_FLAG_SKIP_WORKTREE,
)
idx[b"subdir/"] = sparse_entry
idx._extensions.append(SparseDirExtension())
self.assertTrue(idx.is_sparse())
self.assertEqual(len(idx), 1)
# Expand the index
idx.ensure_full_index(store)
# Should no longer be sparse
self.assertFalse(idx.is_sparse())
# Should have 2 entries now (the files)
self.assertEqual(len(idx), 2)
self.assertIn(b"subdir/file1.txt", idx)
self.assertIn(b"subdir/file2.txt", idx)
# Entries should point to the correct blobs
self.assertEqual(idx[b"subdir/file1.txt"].sha, blob1.id)
self.assertEqual(idx[b"subdir/file2.txt"].sha, blob2.id)
def test_index_collapse(self):
"""Test Index.convert_to_sparse() collapses directories."""
from dulwich.object_store import MemoryObjectStore
from dulwich.objects import Blob, Tree
# Create a tree structure
store = MemoryObjectStore()
blob1 = Blob()
blob1.data = b"file1"
store.add_object(blob1)
blob2 = Blob()
blob2.data = b"file2"
store.add_object(blob2)
subtree = Tree()
subtree[b"file1.txt"] = (0o100644, blob1.id)
subtree[b"file2.txt"] = (0o100644, blob2.id)
store.add_object(subtree)
tree = Tree()
tree[b"subdir"] = (stat.S_IFDIR, subtree.id)
store.add_object(tree)
# Create an index with full entries
with tempfile.TemporaryDirectory() as tmpdir:
index_path = os.path.join(tmpdir, "index")
idx = Index(index_path, read=False)
idx[b"subdir/file1.txt"] = IndexEntry(
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=0o100644,
uid=0,
gid=0,
size=5,
sha=blob1.id,
extended_flags=0,
)
idx[b"subdir/file2.txt"] = IndexEntry(
ctime=0,
mtime=0,
dev=0,
ino=0,
mode=0o100644,
uid=0,
gid=0,
size=5,
sha=blob2.id,
extended_flags=0,
)
self.assertEqual(len(idx), 2)
self.assertFalse(idx.is_sparse())
# Collapse subdir to sparse
idx.convert_to_sparse(store, tree.id, {b"subdir/"})
# Should now be sparse
self.assertTrue(idx.is_sparse())
# Should have 1 entry (the sparse dir)
self.assertEqual(len(idx), 1)
self.assertIn(b"subdir/", idx)
# Entry should be a sparse directory
entry = idx[b"subdir/"]
self.assertTrue(entry.is_sparse_dir(b"subdir/"))
self.assertEqual(entry.sha, subtree.id)
|