git_diff_generated: abstract generated diffs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752
diff --git a/src/checkout.c b/src/checkout.c
index b3e95df..6e3b93f 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -26,6 +26,7 @@
#include "filter.h"
#include "blob.h"
#include "diff.h"
+#include "diff_generate.h"
#include "pathspec.h"
#include "buf_text.h"
#include "diff_xdiff.h"
diff --git a/src/diff.c b/src/diff.c
index 9c27511..c54d357 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -4,279 +4,21 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
+#include "git2/version.h"
#include "common.h"
#include "diff.h"
-#include "fileops.h"
-#include "config.h"
-#include "attr_file.h"
-#include "filter.h"
-#include "pathspec.h"
+#include "diff_generate.h"
+#include "patch.h"
+#include "commit.h"
#include "index.h"
-#include "odb.h"
-#include "submodule.h"
-#define DIFF_FLAG_IS_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) != 0)
-#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) == 0)
+#define DIFF_FLAG_IS_SET(DIFF,FLAG) \
+ (((DIFF)->opts.flags & (FLAG)) != 0)
+#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
+ (((DIFF)->opts.flags & (FLAG)) == 0)
#define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->opts.flags = \
(VAL) ? ((DIFF)->opts.flags | (FLAG)) : ((DIFF)->opts.flags & ~(VAL))
-static git_diff_delta *diff_delta__alloc(
- git_diff *diff,
- git_delta_t status,
- const char *path)
-{
- git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
- if (!delta)
- return NULL;
-
- delta->old_file.path = git_pool_strdup(&diff->pool, path);
- if (delta->old_file.path == NULL) {
- git__free(delta);
- return NULL;
- }
-
- delta->new_file.path = delta->old_file.path;
-
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
- switch (status) {
- case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
- case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
- default: break; /* leave other status values alone */
- }
- }
- delta->status = status;
-
- return delta;
-}
-
-static int diff_insert_delta(
- git_diff *diff, git_diff_delta *delta, const char *matched_pathspec)
-{
- int error = 0;
-
- if (diff->opts.notify_cb) {
- error = diff->opts.notify_cb(
- diff, delta, matched_pathspec, diff->opts.payload);
-
- if (error) {
- git__free(delta);
-
- if (error > 0) /* positive value means to skip this delta */
- return 0;
- else /* negative value means to cancel diff */
- return giterr_set_after_callback_function(error, "git_diff");
- }
- }
-
- if ((error = git_vector_insert(&diff->deltas, delta)) < 0)
- git__free(delta);
-
- return error;
-}
-
-static bool diff_pathspec_match(
- const char **matched_pathspec,
- git_diff *diff,
- const git_index_entry *entry)
-{
- bool disable_pathspec_match =
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
-
- /* If we're disabling fnmatch, then the iterator has already applied
- * the filters to the files for us and we don't have to do anything.
- * However, this only applies to *files* - the iterator will include
- * directories that we need to recurse into when not autoexpanding,
- * so we still need to apply the pathspec match to directories.
- */
- if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
- disable_pathspec_match) {
- *matched_pathspec = entry->path;
- return true;
- }
-
- return git_pathspec__match(
- &diff->pathspec, entry->path, disable_pathspec_match,
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
- matched_pathspec, NULL);
-}
-
-static int diff_delta__from_one(
- git_diff *diff,
- git_delta_t status,
- const git_index_entry *oitem,
- const git_index_entry *nitem)
-{
- const git_index_entry *entry = nitem;
- bool has_old = false;
- git_diff_delta *delta;
- const char *matched_pathspec;
-
- assert((oitem != NULL) ^ (nitem != NULL));
-
- if (oitem) {
- entry = oitem;
- has_old = true;
- }
-
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
- has_old = !has_old;
-
- if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
- return 0;
-
- if (status == GIT_DELTA_IGNORED &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
- return 0;
-
- if (status == GIT_DELTA_UNTRACKED &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
- return 0;
-
- if (status == GIT_DELTA_UNREADABLE &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
- return 0;
-
- if (!diff_pathspec_match(&matched_pathspec, diff, entry))
- return 0;
-
- delta = diff_delta__alloc(diff, status, entry->path);
- GITERR_CHECK_ALLOC(delta);
-
- /* This fn is just for single-sided diffs */
- assert(status != GIT_DELTA_MODIFIED);
- delta->nfiles = 1;
-
- if (has_old) {
- delta->old_file.mode = entry->mode;
- delta->old_file.size = entry->file_size;
- delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
- git_oid_cpy(&delta->old_file.id, &entry->id);
- delta->old_file.id_abbrev = GIT_OID_HEXSZ;
- } else /* ADDED, IGNORED, UNTRACKED */ {
- delta->new_file.mode = entry->mode;
- delta->new_file.size = entry->file_size;
- delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
- git_oid_cpy(&delta->new_file.id, &entry->id);
- delta->new_file.id_abbrev = GIT_OID_HEXSZ;
- }
-
- delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
-
- if (has_old || !git_oid_iszero(&delta->new_file.id))
- delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
-
- return diff_insert_delta(diff, delta, matched_pathspec);
-}
-
-static int diff_delta__from_two(
- git_diff *diff,
- git_delta_t status,
- const git_index_entry *old_entry,
- uint32_t old_mode,
- const git_index_entry *new_entry,
- uint32_t new_mode,
- const git_oid *new_id,
- const char *matched_pathspec)
-{
- const git_oid *old_id = &old_entry->id;
- git_diff_delta *delta;
- const char *canonical_path = old_entry->path;
-
- if (status == GIT_DELTA_UNMODIFIED &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
- return 0;
-
- if (!new_id)
- new_id = &new_entry->id;
-
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
- uint32_t temp_mode = old_mode;
- const git_index_entry *temp_entry = old_entry;
- const git_oid *temp_id = old_id;
-
- old_entry = new_entry;
- new_entry = temp_entry;
- old_mode = new_mode;
- new_mode = temp_mode;
- old_id = new_id;
- new_id = temp_id;
- }
-
- delta = diff_delta__alloc(diff, status, canonical_path);
- GITERR_CHECK_ALLOC(delta);
- delta->nfiles = 2;
-
- if (!git_index_entry_is_conflict(old_entry)) {
- delta->old_file.size = old_entry->file_size;
- delta->old_file.mode = old_mode;
- git_oid_cpy(&delta->old_file.id, old_id);
- delta->old_file.id_abbrev = GIT_OID_HEXSZ;
- delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
- GIT_DIFF_FLAG_EXISTS;
- }
-
- if (!git_index_entry_is_conflict(new_entry)) {
- git_oid_cpy(&delta->new_file.id, new_id);
- delta->new_file.id_abbrev = GIT_OID_HEXSZ;
- delta->new_file.size = new_entry->file_size;
- delta->new_file.mode = new_mode;
- delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
- delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
-
- if (!git_oid_iszero(&new_entry->id))
- delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
- }
-
- return diff_insert_delta(diff, delta, matched_pathspec);
-}
-
-static git_diff_delta *diff_delta__last_for_item(
- git_diff *diff,
- const git_index_entry *item)
-{
- git_diff_delta *delta = git_vector_last(&diff->deltas);
- if (!delta)
- return NULL;
-
- switch (delta->status) {
- case GIT_DELTA_UNMODIFIED:
- case GIT_DELTA_DELETED:
- if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
- return delta;
- break;
- case GIT_DELTA_ADDED:
- if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
- return delta;
- break;
- case GIT_DELTA_UNREADABLE:
- case GIT_DELTA_UNTRACKED:
- if (diff->strcomp(delta->new_file.path, item->path) == 0 &&
- git_oid__cmp(&delta->new_file.id, &item->id) == 0)
- return delta;
- break;
- case GIT_DELTA_MODIFIED:
- if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
- git_oid__cmp(&delta->new_file.id, &item->id) == 0)
- return delta;
- break;
- default:
- break;
- }
-
- return NULL;
-}
-
-static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
-{
- size_t len = strlen(prefix);
-
- /* append '/' at end if needed */
- if (len > 0 && prefix[len - 1] != '/')
- return git_pool_strcat(pool, prefix, "/");
- else
- return git_pool_strndup(pool, prefix, len + 1);
-}
-
GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta)
{
const char *str = delta->old_file.path;
@@ -309,72 +51,6 @@ int git_diff_delta__casecmp(const void *a, const void *b)
return val ? val : ((int)da->status - (int)db->status);
}
-GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
-{
- return delta->old_file.path ?
- delta->old_file.path : delta->new_file.path;
-}
-
-int git_diff_delta__i2w_cmp(const void *a, const void *b)
-{
- const git_diff_delta *da = a, *db = b;
- int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
- return val ? val : ((int)da->status - (int)db->status);
-}
-
-int git_diff_delta__i2w_casecmp(const void *a, const void *b)
-{
- const git_diff_delta *da = a, *db = b;
- int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
- return val ? val : ((int)da->status - (int)db->status);
-}
-
-bool git_diff_delta__should_skip(
- const git_diff_options *opts, const git_diff_delta *delta)
-{
- uint32_t flags = opts ? opts->flags : 0;
-
- if (delta->status == GIT_DELTA_UNMODIFIED &&
- (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
- return true;
-
- if (delta->status == GIT_DELTA_IGNORED &&
- (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
- return true;
-
- if (delta->status == GIT_DELTA_UNTRACKED &&
- (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
- return true;
-
- if (delta->status == GIT_DELTA_UNREADABLE &&
- (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
- return true;
-
- return false;
-}
-
-
-static const char *diff_mnemonic_prefix(
- git_iterator_type_t type, bool left_side)
-{
- const char *pfx = "";
-
- switch (type) {
- case GIT_ITERATOR_TYPE_EMPTY: pfx = "c"; break;
- case GIT_ITERATOR_TYPE_TREE: pfx = "c"; break;
- case GIT_ITERATOR_TYPE_INDEX: pfx = "i"; break;
- case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
- case GIT_ITERATOR_TYPE_FS: pfx = left_side ? "1" : "2"; break;
- default: break;
- }
-
- /* note: without a deeper look at pathspecs, there is no easy way
- * to get the (o)bject / (w)ork tree mnemonics working...
- */
-
- return pfx;
-}
-
static int diff_entry_cmp(const void *a, const void *b)
{
const git_index_entry *entry_a = a;
@@ -391,198 +67,12 @@ static int diff_entry_icmp(const void *a, const void *b)
return strcasecmp(entry_a->path, entry_b->path);
}
-static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
-{
- if (!ignore_case) {
- diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
-
- diff->strcomp = git__strcmp;
- diff->strncomp = git__strncmp;
- diff->pfxcomp = git__prefixcmp;
- diff->entrycomp = diff_entry_cmp;
-
- git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
- } else {
- diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
-
- diff->strcomp = git__strcasecmp;
- diff->strncomp = git__strncasecmp;
- diff->pfxcomp = git__prefixcmp_icase;
- diff->entrycomp = diff_entry_icmp;
-
- git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
- }
-
- git_vector_sort(&diff->deltas);
-}
-
-static git_diff *diff_list_alloc(
- git_repository *repo,
- git_iterator *old_iter,
- git_iterator *new_iter)
-{
- git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
- git_diff *diff = git__calloc(1, sizeof(git_diff));
- if (!diff)
- return NULL;
-
- assert(repo && old_iter && new_iter);
-
- GIT_REFCOUNT_INC(diff);
- diff->repo = repo;
- diff->old_src = old_iter->type;
- diff->new_src = new_iter->type;
- memcpy(&diff->opts, &dflt, sizeof(diff->opts));
-
- git_pool_init(&diff->pool, 1);
-
- if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0) {
- git_diff_free(diff);
- return NULL;
- }
-
- /* Use case-insensitive compare if either iterator has
- * the ignore_case bit set */
- diff_set_ignore_case(
- diff,
- git_iterator_ignore_case(old_iter) ||
- git_iterator_ignore_case(new_iter));
-
- return diff;
-}
-
-static int diff_list_apply_options(
- git_diff *diff,
- const git_diff_options *opts)
-{
- git_config *cfg = NULL;
- git_repository *repo = diff->repo;
- git_pool *pool = &diff->pool;
- int val;
-
- if (opts) {
- /* copy user options (except case sensitivity info from iterators) */
- bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
- memcpy(&diff->opts, opts, sizeof(diff->opts));
- DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
-
- /* initialize pathspec from options */
- if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
- return -1;
- }
-
- /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
- diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
-
- /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
- diff->opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
-
- /* load config values that affect diff behavior */
- if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
- return val;
-
- if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
- diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_HAS_SYMLINKS;
-
- if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
- diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_IGNORE_STAT;
-
- if ((diff->opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
- !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
- diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_MODE_BITS;
-
- if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
- diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_CTIME;
-
- /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
-
- /* If not given explicit `opts`, check `diff.xyz` configs */
- if (!opts) {
- int context = git_config__get_int_force(cfg, "diff.context", 3);
- diff->opts.context_lines = context >= 0 ? (uint32_t)context : 3;
-
- /* add other defaults here */
- }
-
- /* Reverse src info if diff is reversed */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
- git_iterator_type_t tmp_src = diff->old_src;
- diff->old_src = diff->new_src;
- diff->new_src = tmp_src;
- }
-
- /* Unset UPDATE_INDEX unless diffing workdir and index */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
- (!(diff->old_src == GIT_ITERATOR_TYPE_WORKDIR ||
- diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
- !(diff->old_src == GIT_ITERATOR_TYPE_INDEX ||
- diff->new_src == GIT_ITERATOR_TYPE_INDEX)))
- diff->opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
-
- /* if ignore_submodules not explicitly set, check diff config */
- if (diff->opts.ignore_submodules <= 0) {
- git_config_entry *entry;
- git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
-
- if (entry && git_submodule_parse_ignore(
- &diff->opts.ignore_submodules, entry->value) < 0)
- giterr_clear();
- git_config_entry_free(entry);
- }
-
- /* if either prefix is not set, figure out appropriate value */
- if (!diff->opts.old_prefix || !diff->opts.new_prefix) {
- const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
- const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
-
- if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
- use_old = use_new = "";
- else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
- use_old = diff_mnemonic_prefix(diff->old_src, true);
- use_new = diff_mnemonic_prefix(diff->new_src, false);
- }
-
- if (!diff->opts.old_prefix)
- diff->opts.old_prefix = use_old;
- if (!diff->opts.new_prefix)
- diff->opts.new_prefix = use_new;
- }
-
- /* strdup prefix from pool so we're not dependent on external data */
- diff->opts.old_prefix = diff_strdup_prefix(pool, diff->opts.old_prefix);
- diff->opts.new_prefix = diff_strdup_prefix(pool, diff->opts.new_prefix);
-
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
- const char *tmp_prefix = diff->opts.old_prefix;
- diff->opts.old_prefix = diff->opts.new_prefix;
- diff->opts.new_prefix = tmp_prefix;
- }
-
- git_config_free(cfg);
-
- /* check strdup results for error */
- return (!diff->opts.old_prefix || !diff->opts.new_prefix) ? -1 : 0;
-}
-
-static void diff_list_free(git_diff *diff)
-{
- git_vector_free_deep(&diff->deltas);
-
- git_pathspec__vfree(&diff->pathspec);
- git_pool_clear(&diff->pool);
-
- git__memzero(diff, sizeof(*diff));
- git__free(diff);
-}
-
void git_diff_free(git_diff *diff)
{
if (!diff)
return;
- GIT_REFCOUNT_DEC(diff, diff_list_free);
+ GIT_REFCOUNT_DEC(diff, diff->free_fn);
}
void git_diff_addref(git_diff *diff)
@@ -590,896 +80,6 @@ void git_diff_addref(git_diff *diff)
GIT_REFCOUNT_INC(diff);
}
-int git_diff__oid_for_file(
- git_oid *out,
- git_diff *diff,
- const char *path,
- uint16_t mode,
- git_off_t size)
-{
- git_index_entry entry;
-
- memset(&entry, 0, sizeof(entry));
- entry.mode = mode;
- entry.file_size = size;
- entry.path = (char *)path;
-
- return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
-}
-
-int git_diff__oid_for_entry(
- git_oid *out,
- git_diff *diff,
- const git_index_entry *src,
- uint16_t mode,
- const git_oid *update_match)
-{
- int error = 0;
- git_buf full_path = GIT_BUF_INIT;
- git_index_entry entry = *src;
- git_filter_list *fl = NULL;
-
- memset(out, 0, sizeof(*out));
-
- if (git_buf_joinpath(
- &full_path, git_repository_workdir(diff->repo), entry.path) < 0)
- return -1;
-
- if (!mode) {
- struct stat st;
-
- diff->perf.stat_calls++;
-
- if (p_stat(full_path.ptr, &st) < 0) {
- error = git_path_set_error(errno, entry.path, "stat");
- git_buf_free(&full_path);
- return error;
- }
-
- git_index_entry__init_from_stat(
- &entry, &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
- }
-
- /* calculate OID for file if possible */
- if (S_ISGITLINK(mode)) {
- git_submodule *sm;
-
- if (!git_submodule_lookup(&sm, diff->repo, entry.path)) {
- const git_oid *sm_oid = git_submodule_wd_id(sm);
- if (sm_oid)
- git_oid_cpy(out, sm_oid);
- git_submodule_free(sm);
- } else {
- /* if submodule lookup failed probably just in an intermediate
- * state where some init hasn't happened, so ignore the error
- */
- giterr_clear();
- }
- } else if (S_ISLNK(mode)) {
- error = git_odb__hashlink(out, full_path.ptr);
- diff->perf.oid_calculations++;
- } else if (!git__is_sizet(entry.file_size)) {
- giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
- entry.path);
- error = -1;
- } else if (!(error = git_filter_list_load(
- &fl, diff->repo, NULL, entry.path,
- GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
- {
- int fd = git_futils_open_ro(full_path.ptr);
- if (fd < 0)
- error = fd;
- else {
- error = git_odb__hashfd_filtered(
- out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
- p_close(fd);
- diff->perf.oid_calculations++;
- }
-
- git_filter_list_free(fl);
- }
-
- /* update index for entry if requested */
- if (!error && update_match && git_oid_equal(out, update_match)) {
- git_index *idx;
- git_index_entry updated_entry;
-
- memcpy(&updated_entry, &entry, sizeof(git_index_entry));
- updated_entry.mode = mode;
- git_oid_cpy(&updated_entry.id, out);
-
- if (!(error = git_repository_index__weakptr(&idx, diff->repo))) {
- error = git_index_add(idx, &updated_entry);
- diff->index_updated = true;
- }
- }
-
- git_buf_free(&full_path);
- return error;
-}
-
-typedef struct {
- git_repository *repo;
- git_iterator *old_iter;
- git_iterator *new_iter;
- const git_index_entry *oitem;
- const git_index_entry *nitem;
-} diff_in_progress;
-
-#define MODE_BITS_MASK 0000777
-
-static int maybe_modified_submodule(
- git_delta_t *status,
- git_oid *found_oid,
- git_diff *diff,
- diff_in_progress *info)
-{
- int error = 0;
- git_submodule *sub;
- unsigned int sm_status = 0;
- git_submodule_ignore_t ign = diff->opts.ignore_submodules;
-
- *status = GIT_DELTA_UNMODIFIED;
-
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
- ign == GIT_SUBMODULE_IGNORE_ALL)
- return 0;
-
- if ((error = git_submodule_lookup(
- &sub, diff->repo, info->nitem->path)) < 0) {
-
- /* GIT_EEXISTS means dir with .git in it was found - ignore it */
- if (error == GIT_EEXISTS) {
- giterr_clear();
- error = 0;
- }
- return error;
- }
-
- if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
- /* ignore it */;
- else if ((error = git_submodule__status(
- &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
- /* return error below */;
-
- /* check IS_WD_UNMODIFIED because this case is only used
- * when the new side of the diff is the working directory
- */
- else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
- *status = GIT_DELTA_MODIFIED;
-
- /* now that we have a HEAD OID, check if HEAD moved */
- else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
- !git_oid_equal(&info->oitem->id, found_oid))
- *status = GIT_DELTA_MODIFIED;
-
- git_submodule_free(sub);
- return error;
-}
-
-static int maybe_modified(
- git_diff *diff,
- diff_in_progress *info)
-{
- git_oid noid;
- git_delta_t status = GIT_DELTA_MODIFIED;
- const git_index_entry *oitem = info->oitem;
- const git_index_entry *nitem = info->nitem;
- unsigned int omode = oitem->mode;
- unsigned int nmode = nitem->mode;
- bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
- bool modified_uncertain = false;
- const char *matched_pathspec;
- int error = 0;
-
- if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
- return 0;
-
- memset(&noid, 0, sizeof(noid));
-
- /* on platforms with no symlinks, preserve mode of existing symlinks */
- if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
- !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
- nmode = omode;
-
- /* on platforms with no execmode, just preserve old mode */
- if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
- (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
- new_is_workdir)
- nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
-
- /* if one side is a conflict, mark the whole delta as conflicted */
- if (git_index_entry_is_conflict(oitem) ||
- git_index_entry_is_conflict(nitem)) {
- status = GIT_DELTA_CONFLICTED;
-
- /* support "assume unchanged" (poorly, b/c we still stat everything) */
- } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
- status = GIT_DELTA_UNMODIFIED;
-
- /* support "skip worktree" index bit */
- } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
- status = GIT_DELTA_UNMODIFIED;
-
- /* if basic type of file changed, then split into delete and add */
- } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
- status = GIT_DELTA_TYPECHANGE;
- }
-
- else if (nmode == GIT_FILEMODE_UNREADABLE) {
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
- error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
- return error;
- }
-
- else {
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
- error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
- return error;
- }
-
- /* if oids and modes match (and are valid), then file is unmodified */
- } else if (git_oid_equal(&oitem->id, &nitem->id) &&
- omode == nmode &&
- !git_oid_iszero(&oitem->id)) {
- status = GIT_DELTA_UNMODIFIED;
-
- /* if we have an unknown OID and a workdir iterator, then check some
- * circumstances that can accelerate things or need special handling
- */
- } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
- bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
- git_index *index = git_iterator_index(info->new_iter);
-
- status = GIT_DELTA_UNMODIFIED;
-
- if (S_ISGITLINK(nmode)) {
- if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
- return error;
- }
-
- /* if the stat data looks different, then mark modified - this just
- * means that the OID will be recalculated below to confirm change
- */
- else if (omode != nmode || oitem->file_size != nitem->file_size) {
- status = GIT_DELTA_MODIFIED;
- modified_uncertain =
- (oitem->file_size <= 0 && nitem->file_size > 0);
- }
- else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
- (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
- oitem->ino != nitem->ino ||
- oitem->uid != nitem->uid ||
- oitem->gid != nitem->gid ||
- git_index_entry_newer_than_index(nitem, index))
- {
- status = GIT_DELTA_MODIFIED;
- modified_uncertain = true;
- }
-
- /* if mode is GITLINK and submodules are ignored, then skip */
- } else if (S_ISGITLINK(nmode) &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
- status = GIT_DELTA_UNMODIFIED;
- }
-
- /* if we got here and decided that the files are modified, but we
- * haven't calculated the OID of the new item, then calculate it now
- */
- if (modified_uncertain && git_oid_iszero(&nitem->id)) {
- const git_oid *update_check =
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
- &oitem->id : NULL;
-
- if ((error = git_diff__oid_for_entry(
- &noid, diff, nitem, nmode, update_check)) < 0)
- return error;
-
- /* if oid matches, then mark unmodified (except submodules, where
- * the filesystem content may be modified even if the oid still
- * matches between the index and the workdir HEAD)
- */
- if (omode == nmode && !S_ISGITLINK(omode) &&
- git_oid_equal(&oitem->id, &noid))
- status = GIT_DELTA_UNMODIFIED;
- }
-
- /* If we want case changes, then break this into a delete of the old
- * and an add of the new so that consumers can act accordingly (eg,
- * checkout will update the case on disk.)
- */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
- strcmp(oitem->path, nitem->path) != 0) {
-
- if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
- error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
-
- return error;
- }
-
- return diff_delta__from_two(
- diff, status, oitem, omode, nitem, nmode,
- git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
-}
-
-static bool entry_is_prefixed(
- git_diff *diff,
- const git_index_entry *item,
- const git_index_entry *prefix_item)
-{
- size_t pathlen;
-
- if (!item || diff->pfxcomp(item->path, prefix_item->path) != 0)
- return false;
-
- pathlen = strlen(prefix_item->path);
-
- return (prefix_item->path[pathlen - 1] == '/' ||
- item->path[pathlen] == '\0' ||
- item->path[pathlen] == '/');
-}
-
-static int iterator_current(
- const git_index_entry **entry,
- git_iterator *iterator)
-{
- int error;
-
- if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
- *entry = NULL;
- error = 0;
- }
-
- return error;
-}
-
-static int iterator_advance(
- const git_index_entry **entry,
- git_iterator *iterator)
-{
- const git_index_entry *prev_entry = *entry;
- int cmp, error;
-
- /* if we're looking for conflicts, we only want to report
- * one conflict for each file, instead of all three sides.
- * so if this entry is a conflict for this file, and the
- * previous one was a conflict for the same file, skip it.
- */
- while ((error = git_iterator_advance(entry, iterator)) == 0) {
- if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
- !git_index_entry_is_conflict(prev_entry) ||
- !git_index_entry_is_conflict(*entry))
- break;
-
- cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
- strcasecmp(prev_entry->path, (*entry)->path) :
- strcmp(prev_entry->path, (*entry)->path);
-
- if (cmp)
- break;
- }
-
- if (error == GIT_ITEROVER) {
- *entry = NULL;
- error = 0;
- }
-
- return error;
-}
-
-static int iterator_advance_into(
- const git_index_entry **entry,
- git_iterator *iterator)
-{
- int error;
-
- if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
- *entry = NULL;
- error = 0;
- }
-
- return error;
-}
-
-static int iterator_advance_over(
- const git_index_entry **entry,
- git_iterator_status_t *status,
- git_iterator *iterator)
-{
- int error = git_iterator_advance_over(entry, status, iterator);
-
- if (error == GIT_ITEROVER) {
- *entry = NULL;
- error = 0;
- }
-
- return error;
-}
-
-static int handle_unmatched_new_item(
- git_diff *diff, diff_in_progress *info)
-{
- int error = 0;
- const git_index_entry *nitem = info->nitem;
- git_delta_t delta_type = GIT_DELTA_UNTRACKED;
- bool contains_oitem;
-
- /* check if this is a prefix of the other side */
- contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
-
- /* update delta_type if this item is conflicted */
- if (git_index_entry_is_conflict(nitem))
- delta_type = GIT_DELTA_CONFLICTED;
-
- /* update delta_type if this item is ignored */
- else if (git_iterator_current_is_ignored(info->new_iter))
- delta_type = GIT_DELTA_IGNORED;
-
- if (nitem->mode == GIT_FILEMODE_TREE) {
- bool recurse_into_dir = contains_oitem;
-
- /* check if user requests recursion into this type of dir */
- recurse_into_dir = contains_oitem ||
- (delta_type == GIT_DELTA_UNTRACKED &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
- (delta_type == GIT_DELTA_IGNORED &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
-
- /* do not advance into directories that contain a .git file */
- if (recurse_into_dir && !contains_oitem) {
- git_buf *full = NULL;
- if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
- return -1;
- if (full && git_path_contains(full, DOT_GIT)) {
- /* TODO: warning if not a valid git repository */
- recurse_into_dir = false;
- }
- }
-
- /* still have to look into untracked directories to match core git -
- * with no untracked files, directory is treated as ignored
- */
- if (!recurse_into_dir &&
- delta_type == GIT_DELTA_UNTRACKED &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
- {
- git_diff_delta *last;
- git_iterator_status_t untracked_state;
-
- /* attempt to insert record for this directory */
- if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
- return error;
-
- /* if delta wasn't created (because of rules), just skip ahead */
- last = diff_delta__last_for_item(diff, nitem);
- if (!last)
- return iterator_advance(&info->nitem, info->new_iter);
-
- /* iterate into dir looking for an actual untracked file */
- if ((error = iterator_advance_over(
- &info->nitem, &untracked_state, info->new_iter)) < 0)
- return error;
-
- /* if we found nothing that matched our pathlist filter, exclude */
- if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
- git_vector_pop(&diff->deltas);
- git__free(last);
- }
-
- /* if we found nothing or just ignored items, update the record */
- if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
- untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
- last->status = GIT_DELTA_IGNORED;
-
- /* remove the record if we don't want ignored records */
- if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
- git_vector_pop(&diff->deltas);
- git__free(last);
- }
- }
-
- return 0;
- }
-
- /* try to advance into directory if necessary */
- if (recurse_into_dir) {
- error = iterator_advance_into(&info->nitem, info->new_iter);
-
- /* if directory is empty, can't advance into it, so skip it */
- if (error == GIT_ENOTFOUND) {
- giterr_clear();
- error = iterator_advance(&info->nitem, info->new_iter);
- }
-
- return error;
- }
- }
-
- else if (delta_type == GIT_DELTA_IGNORED &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
- git_iterator_current_tree_is_ignored(info->new_iter))
- /* item contained in ignored directory, so skip over it */
- return iterator_advance(&info->nitem, info->new_iter);
-
- else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
- if (delta_type != GIT_DELTA_CONFLICTED)
- delta_type = GIT_DELTA_ADDED;
- }
-
- else if (nitem->mode == GIT_FILEMODE_COMMIT) {
- /* ignore things that are not actual submodules */
- if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
- giterr_clear();
- delta_type = GIT_DELTA_IGNORED;
-
- /* if this contains a tracked item, treat as normal TREE */
- if (contains_oitem) {
- error = iterator_advance_into(&info->nitem, info->new_iter);
- if (error != GIT_ENOTFOUND)
- return error;
-
- giterr_clear();
- return iterator_advance(&info->nitem, info->new_iter);
- }
- }
- }
-
- else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
- delta_type = GIT_DELTA_UNTRACKED;
- else
- delta_type = GIT_DELTA_UNREADABLE;
- }
-
- /* Actually create the record for this item if necessary */
- if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
- return error;
-
- /* If user requested TYPECHANGE records, then check for that instead of
- * just generating an ADDED/UNTRACKED record
- */
- if (delta_type != GIT_DELTA_IGNORED &&
- DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
- contains_oitem)
- {
- /* this entry was prefixed with a tree - make TYPECHANGE */
- git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
- if (last) {
- last->status = GIT_DELTA_TYPECHANGE;
- last->old_file.mode = GIT_FILEMODE_TREE;
- }
- }
-
- return iterator_advance(&info->nitem, info->new_iter);
-}
-
-static int handle_unmatched_old_item(
- git_diff *diff, diff_in_progress *info)
-{
- git_delta_t delta_type = GIT_DELTA_DELETED;
- int error;
-
- /* update delta_type if this item is conflicted */
- if (git_index_entry_is_conflict(info->oitem))
- delta_type = GIT_DELTA_CONFLICTED;
-
- if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
- return error;
-
- /* if we are generating TYPECHANGE records then check for that
- * instead of just generating a DELETE record
- */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
- entry_is_prefixed(diff, info->nitem, info->oitem))
- {
- /* this entry has become a tree! convert to TYPECHANGE */
- git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
- if (last) {
- last->status = GIT_DELTA_TYPECHANGE;
- last->new_file.mode = GIT_FILEMODE_TREE;
- }
-
- /* If new_iter is a workdir iterator, then this situation
- * will certainly be followed by a series of untracked items.
- * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
- */
- if (S_ISDIR(info->nitem->mode) &&
- DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
- return iterator_advance(&info->nitem, info->new_iter);
- }
-
- return iterator_advance(&info->oitem, info->old_iter);
-}
-
-static int handle_matched_item(
- git_diff *diff, diff_in_progress *info)
-{
- int error = 0;
-
- if ((error = maybe_modified(diff, info)) < 0)
- return error;
-
- if (!(error = iterator_advance(&info->oitem, info->old_iter)))
- error = iterator_advance(&info->nitem, info->new_iter);
-
- return error;
-}
-
-int git_diff__from_iterators(
- git_diff **diff_ptr,
- git_repository *repo,
- git_iterator *old_iter,
- git_iterator *new_iter,
- const git_diff_options *opts)
-{
- int error = 0;
- diff_in_progress info;
- git_diff *diff;
-
- *diff_ptr = NULL;
-
- diff = diff_list_alloc(repo, old_iter, new_iter);
- GITERR_CHECK_ALLOC(diff);
-
- info.repo = repo;
- info.old_iter = old_iter;
- info.new_iter = new_iter;
-
- /* make iterators have matching icase behavior */
- if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
- git_iterator_set_ignore_case(old_iter, true);
- git_iterator_set_ignore_case(new_iter, true);
- }
-
- /* finish initialization */
- if ((error = diff_list_apply_options(diff, opts)) < 0)
- goto cleanup;
-
- if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
- (error = iterator_current(&info.nitem, new_iter)) < 0)
- goto cleanup;
-
- /* run iterators building diffs */
- while (!error && (info.oitem || info.nitem)) {
- int cmp;
-
- /* report progress */
- if (opts && opts->progress_cb) {
- if ((error = opts->progress_cb(diff,
- info.oitem ? info.oitem->path : NULL,
- info.nitem ? info.nitem->path : NULL,
- opts->payload)))
- break;
- }
-
- cmp = info.oitem ?
- (info.nitem ? diff->entrycomp(info.oitem, info.nitem) : -1) : 1;
-
- /* create DELETED records for old items not matched in new */
- if (cmp < 0)
- error = handle_unmatched_old_item(diff, &info);
-
- /* create ADDED, TRACKED, or IGNORED records for new items not
- * matched in old (and/or descend into directories as needed)
- */
- else if (cmp > 0)
- error = handle_unmatched_new_item(diff, &info);
-
- /* otherwise item paths match, so create MODIFIED record
- * (or ADDED and DELETED pair if type changed)
- */
- else
- error = handle_matched_item(diff, &info);
- }
-
- diff->perf.stat_calls += old_iter->stat_calls + new_iter->stat_calls;
-
-cleanup:
- if (!error)
- *diff_ptr = diff;
- else
- git_diff_free(diff);
-
- return error;
-}
-
-#define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
- git_iterator *a = NULL, *b = NULL; \
- char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
- git_pathspec_prefix(&opts->pathspec) : NULL; \
- git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
- b_opts = GIT_ITERATOR_OPTIONS_INIT; \
- a_opts.flags = FLAGS_FIRST; \
- a_opts.start = pfx; \
- a_opts.end = pfx; \
- b_opts.flags = FLAGS_SECOND; \
- b_opts.start = pfx; \
- b_opts.end = pfx; \
- GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
- if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
- a_opts.pathlist.strings = opts->pathspec.strings; \
- a_opts.pathlist.count = opts->pathspec.count; \
- b_opts.pathlist.strings = opts->pathspec.strings; \
- b_opts.pathlist.count = opts->pathspec.count; \
- } \
- if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
- error = git_diff__from_iterators(diff, repo, a, b, opts); \
- git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
-} while (0)
-
-int git_diff_tree_to_tree(
- git_diff **diff,
- git_repository *repo,
- git_tree *old_tree,
- git_tree *new_tree,
- const git_diff_options *opts)
-{
- git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
- int error = 0;
-
- assert(diff && repo);
-
- /* for tree to tree diff, be case sensitive even if the index is
- * currently case insensitive, unless the user explicitly asked
- * for case insensitivity
- */
- if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
- iflag = GIT_ITERATOR_IGNORE_CASE;
-
- DIFF_FROM_ITERATORS(
- git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
- git_iterator_for_tree(&b, new_tree, &b_opts), iflag
- );
-
- return error;
-}
-
-static int diff_load_index(git_index **index, git_repository *repo)
-{
- int error = git_repository_index__weakptr(index, repo);
-
- /* reload the repository index when user did not pass one in */
- if (!error && git_index_read(*index, false) < 0)
- giterr_clear();
-
- return error;
-}
-
-int git_diff_tree_to_index(
- git_diff **diff,
- git_repository *repo,
- git_tree *old_tree,
- git_index *index,
- const git_diff_options *opts)
-{
- git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
- GIT_ITERATOR_INCLUDE_CONFLICTS;
- bool index_ignore_case = false;
- int error = 0;
-
- assert(diff && repo);
-
- if (!index && (error = diff_load_index(&index, repo)) < 0)
- return error;
-
- index_ignore_case = index->ignore_case;
-
- DIFF_FROM_ITERATORS(
- git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
- git_iterator_for_index(&b, repo, index, &b_opts), iflag
- );
-
- /* if index is in case-insensitive order, re-sort deltas to match */
- if (!error && index_ignore_case)
- diff_set_ignore_case(*diff, true);
-
- return error;
-}
-
-int git_diff_index_to_workdir(
- git_diff **diff,
- git_repository *repo,
- git_index *index,
- const git_diff_options *opts)
-{
- int error = 0;
-
- assert(diff && repo);
-
- if (!index && (error = diff_load_index(&index, repo)) < 0)
- return error;
-
- DIFF_FROM_ITERATORS(
- git_iterator_for_index(&a, repo, index, &a_opts),
- GIT_ITERATOR_INCLUDE_CONFLICTS,
-
- git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
- GIT_ITERATOR_DONT_AUTOEXPAND
- );
-
- if (!error && DIFF_FLAG_IS_SET(*diff, GIT_DIFF_UPDATE_INDEX) && (*diff)->index_updated)
- error = git_index_write(index);
-
- return error;
-}
-
-int git_diff_tree_to_workdir(
- git_diff **diff,
- git_repository *repo,
- git_tree *old_tree,
- const git_diff_options *opts)
-{
- int error = 0;
- git_index *index;
-
- assert(diff && repo);
-
- if ((error = git_repository_index__weakptr(&index, repo)))
- return error;
-
- DIFF_FROM_ITERATORS(
- git_iterator_for_tree(&a, old_tree, &a_opts), 0,
- git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
- );
-
- return error;
-}
-
-int git_diff_tree_to_workdir_with_index(
- git_diff **diff,
- git_repository *repo,
- git_tree *old_tree,
- const git_diff_options *opts)
-{
- int error = 0;
- git_diff *d1 = NULL, *d2 = NULL;
- git_index *index = NULL;
-
- assert(diff && repo);
-
- if ((error = diff_load_index(&index, repo)) < 0)
- return error;
-
- if (!(error = git_diff_tree_to_index(&d1, repo, old_tree, index, opts)) &&
- !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
- error = git_diff_merge(d1, d2);
-
- git_diff_free(d2);
-
- if (error) {
- git_diff_free(d1);
- d1 = NULL;
- }
-
- *diff = d1;
- return error;
-}
-
-int git_diff_index_to_index(
- git_diff **diff,
- git_repository *repo,
- git_index *old_index,
- git_index *new_index,
- const git_diff_options *opts)
-{
- int error = 0;
-
- assert(diff && old_index && new_index);
-
- DIFF_FROM_ITERATORS(
- git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
- git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
- );
-
- /* if index is in case-insensitive order, re-sort deltas to match */
- if (!error && (old_index->ignore_case || new_index->ignore_case))
- diff_set_ignore_case(*diff, true);
-
- return error;
-}
-
size_t git_diff_num_deltas(const git_diff *diff)
{
assert(diff);
@@ -1520,137 +120,6 @@ int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
return 0;
}
-int git_diff__paired_foreach(
- git_diff *head2idx,
- git_diff *idx2wd,
- int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
- void *payload)
-{
- int cmp, error = 0;
- git_diff_delta *h2i, *i2w;
- size_t i, j, i_max, j_max;
- int (*strcomp)(const char *, const char *) = git__strcmp;
- bool h2i_icase, i2w_icase, icase_mismatch;
-
- i_max = head2idx ? head2idx->deltas.length : 0;
- j_max = idx2wd ? idx2wd->deltas.length : 0;
- if (!i_max && !j_max)
- return 0;
-
- /* At some point, tree-to-index diffs will probably never ignore case,
- * even if that isn't true now. Index-to-workdir diffs may or may not
- * ignore case, but the index filename for the idx2wd diff should
- * still be using the canonical case-preserving name.
- *
- * Therefore the main thing we need to do here is make sure the diffs
- * are traversed in a compatible order. To do this, we temporarily
- * resort a mismatched diff to get the order correct.
- *
- * In order to traverse renames in the index->workdir, we need to
- * ensure that we compare the index name on both sides, so we
- * always sort by the old name in the i2w list.
- */
- h2i_icase = head2idx != NULL &&
- (head2idx->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
-
- i2w_icase = idx2wd != NULL &&
- (idx2wd->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
-
- icase_mismatch =
- (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
-
- if (icase_mismatch && h2i_icase) {
- git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
- git_vector_sort(&head2idx->deltas);
- }
-
- if (i2w_icase && !icase_mismatch) {
- strcomp = git__strcasecmp;
-
- git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
- git_vector_sort(&idx2wd->deltas);
- } else if (idx2wd != NULL) {
- git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
- git_vector_sort(&idx2wd->deltas);
- }
-
- for (i = 0, j = 0; i < i_max || j < j_max; ) {
- h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
- i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
-
- cmp = !i2w ? -1 : !h2i ? 1 :
- strcomp(h2i->new_file.path, i2w->old_file.path);
-
- if (cmp < 0) {
- i++; i2w = NULL;
- } else if (cmp > 0) {
- j++; h2i = NULL;
- } else {
- i++; j++;
- }
-
- if ((error = cb(h2i, i2w, payload)) != 0) {
- giterr_set_after_callback(error);
- break;
- }
- }
-
- /* restore case-insensitive delta sort */
- if (icase_mismatch && h2i_icase) {
- git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
- git_vector_sort(&head2idx->deltas);
- }
-
- /* restore idx2wd sort by new path */
- if (idx2wd != NULL) {
- git_vector_set_cmp(&idx2wd->deltas,
- i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
- git_vector_sort(&idx2wd->deltas);
- }
-
- return error;
-}
-
-int git_diff__commit(
- git_diff **diff,
- git_repository *repo,
- const git_commit *commit,
- const git_diff_options *opts)
-{
- git_commit *parent = NULL;
- git_diff *commit_diff = NULL;
- git_tree *old_tree = NULL, *new_tree = NULL;
- size_t parents;
- int error = 0;
-
- if ((parents = git_commit_parentcount(commit)) > 1) {
- char commit_oidstr[GIT_OID_HEXSZ + 1];
-
- error = -1;
- giterr_set(GITERR_INVALID, "Commit %s is a merge commit",
- git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
- goto on_error;
- }
-
- if (parents > 0)
- if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
- (error = git_commit_tree(&old_tree, parent)) < 0)
- goto on_error;
-
- if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
- (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
- goto on_error;
-
- *diff = commit_diff;
-
-on_error:
- git_tree_free(new_tree);
- git_tree_free(old_tree);
- git_commit_free(parent);
-
- return error;
-}
-
int git_diff_format_email__append_header_tobuf(
git_buf *out,
const git_oid *id,
@@ -1668,7 +137,8 @@ int git_diff_format_email__append_header_tobuf(
git_oid_fmt(idstr, id);
idstr[GIT_OID_HEXSZ] = '\0';
- if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str), &author->when)) < 0)
+ if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str),
+ &author->when)) < 0)
return error;
error = git_buf_printf(out,
@@ -1687,7 +157,8 @@ int git_diff_format_email__append_header_tobuf(
if (total_patches == 1) {
error = git_buf_puts(out, "[PATCH] ");
} else {
- error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ", patch_no, total_patches);
+ error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ",
+ patch_no, total_patches);
}
if (error < 0)
@@ -1745,16 +216,24 @@ int git_diff_format_email(
assert(out && diff && opts);
assert(opts->summary && opts->id && opts->author);
- GITERR_CHECK_VERSION(opts, GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, "git_format_email_options");
+ GITERR_CHECK_VERSION(opts,
+ GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
+ "git_format_email_options");
+
+ ignore_marker = (opts->flags &
+ GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0;
- if ((ignore_marker = opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) == false) {
+ if (!ignore_marker) {
if (opts->patch_no > opts->total_patches) {
- giterr_set(GITERR_INVALID, "patch %"PRIuZ" out of range. max %"PRIuZ, opts->patch_no, opts->total_patches);
+ giterr_set(GITERR_INVALID,
+ "patch %"PRIuZ" out of range. max %"PRIuZ,
+ opts->patch_no, opts->total_patches);
return -1;
}
if (opts->patch_no == 0) {
- giterr_set(GITERR_INVALID, "invalid patch no %"PRIuZ". should be >0", opts->patch_no);
+ giterr_set(GITERR_INVALID,
+ "invalid patch no %"PRIuZ". should be >0", opts->patch_no);
return -1;
}
}
@@ -1779,8 +258,8 @@ int git_diff_format_email(
}
error = git_diff_format_email__append_header_tobuf(out,
- opts->id, opts->author, summary == NULL ? opts->summary : summary,
- opts->body, opts->patch_no, opts->total_patches, ignore_marker);
+ opts->id, opts->author, summary == NULL ? opts->summary : summary,
+ opts->body, opts->patch_no, opts->total_patches, ignore_marker);
if (error < 0)
goto on_error;
@@ -1813,7 +292,8 @@ int git_diff_commit_as_email(
const git_diff_options *diff_opts)
{
git_diff *diff = NULL;
- git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
+ git_diff_format_email_options opts =
+ GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
int error;
assert (out && repo && commit);
@@ -1858,3 +338,4 @@ int git_diff_format_email_init_options(
GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
return 0;
}
+
diff --git a/src/diff.h b/src/diff.h
index 47743f8..153cd35 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -22,67 +22,29 @@
#define DIFF_OLD_PREFIX_DEFAULT "a/"
#define DIFF_NEW_PREFIX_DEFAULT "b/"
-enum {
- GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */
- GIT_DIFFCAPS_IGNORE_STAT = (1 << 1), /* use stat? */
- GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */
- GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */
- GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */
-};
-
-#define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
-#define DIFF_FLAGS_NOT_BINARY (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
-
-enum {
- GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */
- GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */
- GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */
- GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */
- GIT_DIFF_FLAG__FREE_BLOB = (1 << 11), /* release the blob when done */
- GIT_DIFF_FLAG__LOADED = (1 << 12), /* file data has been loaded */
-
- GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */
- GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */
- GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18),
- GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19),
- GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20),
-};
-
-#define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF)
-
-#define GIT_DIFF__VERBOSE (1 << 30)
+typedef enum {
+ GIT_DIFF_TYPE_UNKNOWN = 0,
+ GIT_DIFF_TYPE_GENERATED = 1,
+} git_diff_origin_t;
struct git_diff {
git_refcount rc;
git_repository *repo;
+ git_diff_origin_t type;
git_diff_options opts;
- git_vector pathspec;
git_vector deltas; /* vector of git_diff_delta */
git_pool pool;
git_iterator_type_t old_src;
git_iterator_type_t new_src;
- uint32_t diffcaps;
git_diff_perfdata perf;
- bool index_updated;
int (*strcomp)(const char *, const char *);
int (*strncomp)(const char *, const char *, size_t);
int (*pfxcomp)(const char *str, const char *pfx);
int (*entrycomp)(const void *a, const void *b);
-};
-
-extern void git_diff__cleanup_modes(
- uint32_t diffcaps, uint32_t *omode, uint32_t *nmode);
-
-extern void git_diff_addref(git_diff *diff);
-extern int git_diff_delta__cmp(const void *a, const void *b);
-extern int git_diff_delta__casecmp(const void *a, const void *b);
-
-extern const char *git_diff_delta__path(const git_diff_delta *delta);
-
-extern bool git_diff_delta__should_skip(
- const git_diff_options *opts, const git_diff_delta *delta);
+ void (*free_fn)(git_diff *diff);
+};
extern int git_diff_delta__format_file_header(
git_buf *out,
@@ -91,84 +53,8 @@ extern int git_diff_delta__format_file_header(
const char *newpfx,
int oid_strlen);
-extern int git_diff__oid_for_file(
- git_oid *out, git_diff *, const char *, uint16_t, git_off_t);
-extern int git_diff__oid_for_entry(
- git_oid *out, git_diff *, const git_index_entry *, uint16_t, const git_oid *update);
-
-extern int git_diff__from_iterators(
- git_diff **diff_ptr,
- git_repository *repo,
- git_iterator *old_iter,
- git_iterator *new_iter,
- const git_diff_options *opts);
-
-extern int git_diff__paired_foreach(
- git_diff *idx2head,
- git_diff *wd2idx,
- int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
- void *payload);
-
-extern int git_diff_find_similar__hashsig_for_file(
- void **out, const git_diff_file *f, const char *path, void *p);
-
-extern int git_diff_find_similar__hashsig_for_buf(
- void **out, const git_diff_file *f, const char *buf, size_t len, void *p);
-
-extern void git_diff_find_similar__hashsig_free(void *sig, void *payload);
-
-extern int git_diff_find_similar__calc_similarity(
- int *score, void *siga, void *sigb, void *payload);
-
-extern int git_diff__commit(
- git_diff **diff, git_repository *repo, const git_commit *commit, const git_diff_options *opts);
-
-/* Merge two `git_diff`s according to the callback given by `cb`. */
-
-typedef git_diff_delta *(*git_diff__merge_cb)(
- const git_diff_delta *left,
- const git_diff_delta *right,
- git_pool *pool);
-
-extern int git_diff__merge(
- git_diff *onto, const git_diff *from, git_diff__merge_cb cb);
-
-extern git_diff_delta *git_diff__merge_like_cgit(
- const git_diff_delta *a,
- const git_diff_delta *b,
- git_pool *pool);
-
-/* Duplicate a `git_diff_delta` out of the `git_pool` */
-extern git_diff_delta *git_diff__delta_dup(
- const git_diff_delta *d, git_pool *pool);
-
-/*
- * Sometimes a git_diff_file will have a zero size; this attempts to
- * fill in the size without loading the blob if possible. If that is
- * not possible, then it will return the git_odb_object that had to be
- * loaded and the caller can use it or dispose of it as needed.
- */
-GIT_INLINE(int) git_diff_file__resolve_zero_size(
- git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
-{
- int error;
- git_odb *odb;
- size_t len;
- git_otype type;
-
- if ((error = git_repository_odb(&odb, repo)) < 0)
- return error;
-
- error = git_odb__read_header_or_object(
- odb_obj, &len, &type, odb, &file->id);
-
- git_odb_free(odb);
-
- if (!error)
- file->size = (git_off_t)len;
-
- return error;
-}
+extern int git_diff_delta__cmp(const void *a, const void *b);
+extern int git_diff_delta__casecmp(const void *a, const void *b);
#endif
diff --git a/src/diff_file.c b/src/diff_file.c
index 8b945a5..cc10290 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -8,6 +8,7 @@
#include "git2/blob.h"
#include "git2/submodule.h"
#include "diff.h"
+#include "diff_generate.h"
#include "diff_file.h"
#include "odb.h"
#include "fileops.h"
diff --git a/src/diff_generate.c b/src/diff_generate.c
new file mode 100644
index 0000000..10bc154
--- /dev/null
+++ b/src/diff_generate.c
@@ -0,0 +1,1627 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "common.h"
+#include "diff.h"
+#include "diff_generate.h"
+#include "fileops.h"
+#include "config.h"
+#include "attr_file.h"
+#include "filter.h"
+#include "pathspec.h"
+#include "index.h"
+#include "odb.h"
+#include "submodule.h"
+
+#define DIFF_FLAG_IS_SET(DIFF,FLAG) \
+ (((DIFF)->base.opts.flags & (FLAG)) != 0)
+#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
+ (((DIFF)->base.opts.flags & (FLAG)) == 0)
+#define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \
+ (VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \
+ ((DIFF)->base.opts.flags & ~(VAL))
+
+typedef struct {
+ struct git_diff base;
+
+ git_vector pathspec;
+
+ uint32_t diffcaps;
+ bool index_updated;
+} git_diff_generated;
+
+static git_diff_delta *diff_delta__alloc(
+ git_diff_generated *diff,
+ git_delta_t status,
+ const char *path)
+{
+ git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
+ if (!delta)
+ return NULL;
+
+ delta->old_file.path = git_pool_strdup(&diff->base.pool, path);
+ if (delta->old_file.path == NULL) {
+ git__free(delta);
+ return NULL;
+ }
+
+ delta->new_file.path = delta->old_file.path;
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
+ switch (status) {
+ case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
+ case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
+ default: break; /* leave other status values alone */
+ }
+ }
+ delta->status = status;
+
+ return delta;
+}
+
+static int diff_insert_delta(
+ git_diff_generated *diff,
+ git_diff_delta *delta,
+ const char *matched_pathspec)
+{
+ int error = 0;
+
+ if (diff->base.opts.notify_cb) {
+ error = diff->base.opts.notify_cb(
+ &diff->base, delta, matched_pathspec, diff->base.opts.payload);
+
+ if (error) {
+ git__free(delta);
+
+ if (error > 0) /* positive value means to skip this delta */
+ return 0;
+ else /* negative value means to cancel diff */
+ return giterr_set_after_callback_function(error, "git_diff");
+ }
+ }
+
+ if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0)
+ git__free(delta);
+
+ return error;
+}
+
+static bool diff_pathspec_match(
+ const char **matched_pathspec,
+ git_diff_generated *diff,
+ const git_index_entry *entry)
+{
+ bool disable_pathspec_match =
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
+
+ /* If we're disabling fnmatch, then the iterator has already applied
+ * the filters to the files for us and we don't have to do anything.
+ * However, this only applies to *files* - the iterator will include
+ * directories that we need to recurse into when not autoexpanding,
+ * so we still need to apply the pathspec match to directories.
+ */
+ if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
+ disable_pathspec_match) {
+ *matched_pathspec = entry->path;
+ return true;
+ }
+
+ return git_pathspec__match(
+ &diff->pathspec, entry->path, disable_pathspec_match,
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
+ matched_pathspec, NULL);
+}
+
+static int diff_delta__from_one(
+ git_diff_generated *diff,
+ git_delta_t status,
+ const git_index_entry *oitem,
+ const git_index_entry *nitem)
+{
+ const git_index_entry *entry = nitem;
+ bool has_old = false;
+ git_diff_delta *delta;
+ const char *matched_pathspec;
+
+ assert((oitem != NULL) ^ (nitem != NULL));
+
+ if (oitem) {
+ entry = oitem;
+ has_old = true;
+ }
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
+ has_old = !has_old;
+
+ if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
+ return 0;
+
+ if (status == GIT_DELTA_IGNORED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
+ return 0;
+
+ if (status == GIT_DELTA_UNTRACKED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
+ return 0;
+
+ if (status == GIT_DELTA_UNREADABLE &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
+ return 0;
+
+ if (!diff_pathspec_match(&matched_pathspec, diff, entry))
+ return 0;
+
+ delta = diff_delta__alloc(diff, status, entry->path);
+ GITERR_CHECK_ALLOC(delta);
+
+ /* This fn is just for single-sided diffs */
+ assert(status != GIT_DELTA_MODIFIED);
+ delta->nfiles = 1;
+
+ if (has_old) {
+ delta->old_file.mode = entry->mode;
+ delta->old_file.size = entry->file_size;
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
+ git_oid_cpy(&delta->old_file.id, &entry->id);
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
+ } else /* ADDED, IGNORED, UNTRACKED */ {
+ delta->new_file.mode = entry->mode;
+ delta->new_file.size = entry->file_size;
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
+ git_oid_cpy(&delta->new_file.id, &entry->id);
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
+ }
+
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
+
+ if (has_old || !git_oid_iszero(&delta->new_file.id))
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
+
+ return diff_insert_delta(diff, delta, matched_pathspec);
+}
+
+static int diff_delta__from_two(
+ git_diff_generated *diff,
+ git_delta_t status,
+ const git_index_entry *old_entry,
+ uint32_t old_mode,
+ const git_index_entry *new_entry,
+ uint32_t new_mode,
+ const git_oid *new_id,
+ const char *matched_pathspec)
+{
+ const git_oid *old_id = &old_entry->id;
+ git_diff_delta *delta;
+ const char *canonical_path = old_entry->path;
+
+ if (status == GIT_DELTA_UNMODIFIED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
+ return 0;
+
+ if (!new_id)
+ new_id = &new_entry->id;
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
+ uint32_t temp_mode = old_mode;
+ const git_index_entry *temp_entry = old_entry;
+ const git_oid *temp_id = old_id;
+
+ old_entry = new_entry;
+ new_entry = temp_entry;
+ old_mode = new_mode;
+ new_mode = temp_mode;
+ old_id = new_id;
+ new_id = temp_id;
+ }
+
+ delta = diff_delta__alloc(diff, status, canonical_path);
+ GITERR_CHECK_ALLOC(delta);
+ delta->nfiles = 2;
+
+ if (!git_index_entry_is_conflict(old_entry)) {
+ delta->old_file.size = old_entry->file_size;
+ delta->old_file.mode = old_mode;
+ git_oid_cpy(&delta->old_file.id, old_id);
+ delta->old_file.id_abbrev = GIT_OID_HEXSZ;
+ delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
+ GIT_DIFF_FLAG_EXISTS;
+ }
+
+ if (!git_index_entry_is_conflict(new_entry)) {
+ git_oid_cpy(&delta->new_file.id, new_id);
+ delta->new_file.id_abbrev = GIT_OID_HEXSZ;
+ delta->new_file.size = new_entry->file_size;
+ delta->new_file.mode = new_mode;
+ delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
+ delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
+
+ if (!git_oid_iszero(&new_entry->id))
+ delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
+ }
+
+ return diff_insert_delta(diff, delta, matched_pathspec);
+}
+
+static git_diff_delta *diff_delta__last_for_item(
+ git_diff_generated *diff,
+ const git_index_entry *item)
+{
+ git_diff_delta *delta = git_vector_last(&diff->base.deltas);
+ if (!delta)
+ return NULL;
+
+ switch (delta->status) {
+ case GIT_DELTA_UNMODIFIED:
+ case GIT_DELTA_DELETED:
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
+ return delta;
+ break;
+ case GIT_DELTA_ADDED:
+ if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
+ return delta;
+ break;
+ case GIT_DELTA_UNREADABLE:
+ case GIT_DELTA_UNTRACKED:
+ if (diff->base.strcomp(delta->new_file.path, item->path) == 0 &&
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
+ return delta;
+ break;
+ case GIT_DELTA_MODIFIED:
+ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
+ git_oid__cmp(&delta->new_file.id, &item->id) == 0)
+ return delta;
+ break;
+ default:
+ break;
+ }
+
+ return NULL;
+}
+
+static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
+{
+ size_t len = strlen(prefix);
+
+ /* append '/' at end if needed */
+ if (len > 0 && prefix[len - 1] != '/')
+ return git_pool_strcat(pool, prefix, "/");
+ else
+ return git_pool_strndup(pool, prefix, len + 1);
+}
+
+GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
+{
+ return delta->old_file.path ?
+ delta->old_file.path : delta->new_file.path;
+}
+
+int git_diff_delta__i2w_cmp(const void *a, const void *b)
+{
+ const git_diff_delta *da = a, *db = b;
+ int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
+ return val ? val : ((int)da->status - (int)db->status);
+}
+
+int git_diff_delta__i2w_casecmp(const void *a, const void *b)
+{
+ const git_diff_delta *da = a, *db = b;
+ int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
+ return val ? val : ((int)da->status - (int)db->status);
+}
+
+bool git_diff_delta__should_skip(
+ const git_diff_options *opts, const git_diff_delta *delta)
+{
+ uint32_t flags = opts ? opts->flags : 0;
+
+ if (delta->status == GIT_DELTA_UNMODIFIED &&
+ (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
+ return true;
+
+ if (delta->status == GIT_DELTA_IGNORED &&
+ (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
+ return true;
+
+ if (delta->status == GIT_DELTA_UNTRACKED &&
+ (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
+ return true;
+
+ if (delta->status == GIT_DELTA_UNREADABLE &&
+ (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
+ return true;
+
+ return false;
+}
+
+
+static const char *diff_mnemonic_prefix(
+ git_iterator_type_t type, bool left_side)
+{
+ const char *pfx = "";
+
+ switch (type) {
+ case GIT_ITERATOR_TYPE_EMPTY: pfx = "c"; break;
+ case GIT_ITERATOR_TYPE_TREE: pfx = "c"; break;
+ case GIT_ITERATOR_TYPE_INDEX: pfx = "i"; break;
+ case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
+ case GIT_ITERATOR_TYPE_FS: pfx = left_side ? "1" : "2"; break;
+ default: break;
+ }
+
+ /* note: without a deeper look at pathspecs, there is no easy way
+ * to get the (o)bject / (w)ork tree mnemonics working...
+ */
+
+ return pfx;
+}
+
+static int diff_entry_cmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcmp(entry_a->path, entry_b->path);
+}
+
+static int diff_entry_icmp(const void *a, const void *b)
+{
+ const git_index_entry *entry_a = a;
+ const git_index_entry *entry_b = b;
+
+ return strcasecmp(entry_a->path, entry_b->path);
+}
+
+void git_diff__set_ignore_case(git_diff *diff, bool ignore_case)
+{
+ if (!ignore_case) {
+ diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
+
+ diff->strcomp = git__strcmp;
+ diff->strncomp = git__strncmp;
+ diff->pfxcomp = git__prefixcmp;
+ diff->entrycomp = diff_entry_cmp;
+
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
+ } else {
+ diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
+
+ diff->strcomp = git__strcasecmp;
+ diff->strncomp = git__strncasecmp;
+ diff->pfxcomp = git__prefixcmp_icase;
+ diff->entrycomp = diff_entry_icmp;
+
+ git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
+ }
+
+ git_vector_sort(&diff->deltas);
+}
+
+static void diff_generated_free(git_diff *d)
+{
+ git_diff_generated *diff = (git_diff_generated *)d;
+
+ git_vector_free_deep(&diff->base.deltas);
+
+ git_pathspec__vfree(&diff->pathspec);
+ git_pool_clear(&diff->base.pool);
+
+ git__memzero(diff, sizeof(*diff));
+ git__free(diff);
+}
+
+static git_diff_generated *diff_generated_alloc(
+ git_repository *repo,
+ git_iterator *old_iter,
+ git_iterator *new_iter)
+{
+ git_diff_generated *diff;
+ git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
+
+ assert(repo && old_iter && new_iter);
+
+ if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL)
+ return NULL;
+
+ GIT_REFCOUNT_INC(diff);
+ diff->base.type = GIT_DIFF_TYPE_GENERATED;
+ diff->base.repo = repo;
+ diff->base.old_src = old_iter->type;
+ diff->base.new_src = new_iter->type;
+ diff->base.free_fn = diff_generated_free;
+ memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
+
+ git_pool_init(&diff->base.pool, 1);
+
+ if (git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) {
+ git_diff_free(&diff->base);
+ return NULL;
+ }
+
+ /* Use case-insensitive compare if either iterator has
+ * the ignore_case bit set */
+ git_diff__set_ignore_case(
+ &diff->base,
+ git_iterator_ignore_case(old_iter) ||
+ git_iterator_ignore_case(new_iter));
+
+ return diff;
+}
+
+static int diff_generated_apply_options(
+ git_diff_generated *diff,
+ const git_diff_options *opts)
+{
+ git_config *cfg = NULL;
+ git_repository *repo = diff->base.repo;
+ git_pool *pool = &diff->base.pool;
+ int val;
+
+ if (opts) {
+ /* copy user options (except case sensitivity info from iterators) */
+ bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
+ memcpy(&diff->base.opts, opts, sizeof(diff->base.opts));
+ DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
+
+ /* initialize pathspec from options */
+ if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
+ return -1;
+ }
+
+ /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
+
+ /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
+ diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
+
+ /* load config values that affect diff behavior */
+ if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
+ return val;
+
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
+ diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS;
+
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
+ diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT;
+
+ if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
+ !git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS;
+
+ if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
+ diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME;
+
+ /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
+
+ /* If not given explicit `opts`, check `diff.xyz` configs */
+ if (!opts) {
+ int context = git_config__get_int_force(cfg, "diff.context", 3);
+ diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3;
+
+ /* add other defaults here */
+ }
+
+ /* Reverse src info if diff is reversed */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
+ git_iterator_type_t tmp_src = diff->base.old_src;
+ diff->base.old_src = diff->base.new_src;
+ diff->base.new_src = tmp_src;
+ }
+
+ /* Unset UPDATE_INDEX unless diffing workdir and index */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
+ (!(diff->base.old_src == GIT_ITERATOR_TYPE_WORKDIR ||
+ diff->base.new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
+ !(diff->base.old_src == GIT_ITERATOR_TYPE_INDEX ||
+ diff->base.new_src == GIT_ITERATOR_TYPE_INDEX)))
+ diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
+
+ /* if ignore_submodules not explicitly set, check diff config */
+ if (diff->base.opts.ignore_submodules <= 0) {
+ git_config_entry *entry;
+ git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
+
+ if (entry && git_submodule_parse_ignore(
+ &diff->base.opts.ignore_submodules, entry->value) < 0)
+ giterr_clear();
+ git_config_entry_free(entry);
+ }
+
+ /* if either prefix is not set, figure out appropriate value */
+ if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) {
+ const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
+ const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
+
+ if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
+ use_old = use_new = "";
+ else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
+ use_old = diff_mnemonic_prefix(diff->base.old_src, true);
+ use_new = diff_mnemonic_prefix(diff->base.new_src, false);
+ }
+
+ if (!diff->base.opts.old_prefix)
+ diff->base.opts.old_prefix = use_old;
+ if (!diff->base.opts.new_prefix)
+ diff->base.opts.new_prefix = use_new;
+ }
+
+ /* strdup prefix from pool so we're not dependent on external data */
+ diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix);
+ diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix);
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
+ const char *tmp_prefix = diff->base.opts.old_prefix;
+ diff->base.opts.old_prefix = diff->base.opts.new_prefix;
+ diff->base.opts.new_prefix = tmp_prefix;
+ }
+
+ git_config_free(cfg);
+
+ /* check strdup results for error */
+ return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0;
+}
+
+int git_diff__oid_for_file(
+ git_oid *out,
+ git_diff *diff,
+ const char *path,
+ uint16_t mode,
+ git_off_t size)
+{
+ git_index_entry entry;
+
+ memset(&entry, 0, sizeof(entry));
+ entry.mode = mode;
+ entry.file_size = size;
+ entry.path = (char *)path;
+
+ return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
+}
+
+int git_diff__oid_for_entry(
+ git_oid *out,
+ git_diff *d,
+ const git_index_entry *src,
+ uint16_t mode,
+ const git_oid *update_match)
+{
+ git_diff_generated *diff;
+ git_buf full_path = GIT_BUF_INIT;
+ git_index_entry entry = *src;
+ git_filter_list *fl = NULL;
+ int error = 0;
+
+ assert(d->type == GIT_DIFF_TYPE_GENERATED);
+ diff = (git_diff_generated *)d;
+
+ memset(out, 0, sizeof(*out));
+
+ if (git_buf_joinpath(&full_path,
+ git_repository_workdir(diff->base.repo), entry.path) < 0)
+ return -1;
+
+ if (!mode) {
+ struct stat st;
+
+ diff->base.perf.stat_calls++;
+
+ if (p_stat(full_path.ptr, &st) < 0) {
+ error = git_path_set_error(errno, entry.path, "stat");
+ git_buf_free(&full_path);
+ return error;
+ }
+
+ git_index_entry__init_from_stat(&entry,
+ &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
+ }
+
+ /* calculate OID for file if possible */
+ if (S_ISGITLINK(mode)) {
+ git_submodule *sm;
+
+ if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) {
+ const git_oid *sm_oid = git_submodule_wd_id(sm);
+ if (sm_oid)
+ git_oid_cpy(out, sm_oid);
+ git_submodule_free(sm);
+ } else {
+ /* if submodule lookup failed probably just in an intermediate
+ * state where some init hasn't happened, so ignore the error
+ */
+ giterr_clear();
+ }
+ } else if (S_ISLNK(mode)) {
+ error = git_odb__hashlink(out, full_path.ptr);
+ diff->base.perf.oid_calculations++;
+ } else if (!git__is_sizet(entry.file_size)) {
+ giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
+ entry.path);
+ error = -1;
+ } else if (!(error = git_filter_list_load(&fl,
+ diff->base.repo, NULL, entry.path,
+ GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
+ {
+ int fd = git_futils_open_ro(full_path.ptr);
+ if (fd < 0)
+ error = fd;
+ else {
+ error = git_odb__hashfd_filtered(
+ out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
+ p_close(fd);
+ diff->base.perf.oid_calculations++;
+ }
+
+ git_filter_list_free(fl);
+ }
+
+ /* update index for entry if requested */
+ if (!error && update_match && git_oid_equal(out, update_match)) {
+ git_index *idx;
+ git_index_entry updated_entry;
+
+ memcpy(&updated_entry, &entry, sizeof(git_index_entry));
+ updated_entry.mode = mode;
+ git_oid_cpy(&updated_entry.id, out);
+
+ if (!(error = git_repository_index__weakptr(&idx,
+ diff->base.repo))) {
+ error = git_index_add(idx, &updated_entry);
+ diff->index_updated = true;
+ }
+ }
+
+ git_buf_free(&full_path);
+ return error;
+}
+
+typedef struct {
+ git_repository *repo;
+ git_iterator *old_iter;
+ git_iterator *new_iter;
+ const git_index_entry *oitem;
+ const git_index_entry *nitem;
+} diff_in_progress;
+
+#define MODE_BITS_MASK 0000777
+
+static int maybe_modified_submodule(
+ git_delta_t *status,
+ git_oid *found_oid,
+ git_diff_generated *diff,
+ diff_in_progress *info)
+{
+ int error = 0;
+ git_submodule *sub;
+ unsigned int sm_status = 0;
+ git_submodule_ignore_t ign = diff->base.opts.ignore_submodules;
+
+ *status = GIT_DELTA_UNMODIFIED;
+
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
+ ign == GIT_SUBMODULE_IGNORE_ALL)
+ return 0;
+
+ if ((error = git_submodule_lookup(
+ &sub, diff->base.repo, info->nitem->path)) < 0) {
+
+ /* GIT_EEXISTS means dir with .git in it was found - ignore it */
+ if (error == GIT_EEXISTS) {
+ giterr_clear();
+ error = 0;
+ }
+ return error;
+ }
+
+ if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
+ /* ignore it */;
+ else if ((error = git_submodule__status(
+ &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
+ /* return error below */;
+
+ /* check IS_WD_UNMODIFIED because this case is only used
+ * when the new side of the diff is the working directory
+ */
+ else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
+ *status = GIT_DELTA_MODIFIED;
+
+ /* now that we have a HEAD OID, check if HEAD moved */
+ else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
+ !git_oid_equal(&info->oitem->id, found_oid))
+ *status = GIT_DELTA_MODIFIED;
+
+ git_submodule_free(sub);
+ return error;
+}
+
+static int maybe_modified(
+ git_diff_generated *diff,
+ diff_in_progress *info)
+{
+ git_oid noid;
+ git_delta_t status = GIT_DELTA_MODIFIED;
+ const git_index_entry *oitem = info->oitem;
+ const git_index_entry *nitem = info->nitem;
+ unsigned int omode = oitem->mode;
+ unsigned int nmode = nitem->mode;
+ bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
+ bool modified_uncertain = false;
+ const char *matched_pathspec;
+ int error = 0;
+
+ if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
+ return 0;
+
+ memset(&noid, 0, sizeof(noid));
+
+ /* on platforms with no symlinks, preserve mode of existing symlinks */
+ if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
+ !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
+ nmode = omode;
+
+ /* on platforms with no execmode, just preserve old mode */
+ if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
+ (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
+ new_is_workdir)
+ nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
+
+ /* if one side is a conflict, mark the whole delta as conflicted */
+ if (git_index_entry_is_conflict(oitem) ||
+ git_index_entry_is_conflict(nitem)) {
+ status = GIT_DELTA_CONFLICTED;
+
+ /* support "assume unchanged" (poorly, b/c we still stat everything) */
+ } else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
+ status = GIT_DELTA_UNMODIFIED;
+
+ /* support "skip worktree" index bit */
+ } else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
+ status = GIT_DELTA_UNMODIFIED;
+
+ /* if basic type of file changed, then split into delete and add */
+ } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
+ status = GIT_DELTA_TYPECHANGE;
+ }
+
+ else if (nmode == GIT_FILEMODE_UNREADABLE) {
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
+ return error;
+ }
+
+ else {
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
+ return error;
+ }
+
+ /* if oids and modes match (and are valid), then file is unmodified */
+ } else if (git_oid_equal(&oitem->id, &nitem->id) &&
+ omode == nmode &&
+ !git_oid_iszero(&oitem->id)) {
+ status = GIT_DELTA_UNMODIFIED;
+
+ /* if we have an unknown OID and a workdir iterator, then check some
+ * circumstances that can accelerate things or need special handling
+ */
+ } else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
+ bool use_ctime =
+ ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
+ git_index *index = git_iterator_index(info->new_iter);
+
+ status = GIT_DELTA_UNMODIFIED;
+
+ if (S_ISGITLINK(nmode)) {
+ if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
+ return error;
+ }
+
+ /* if the stat data looks different, then mark modified - this just
+ * means that the OID will be recalculated below to confirm change
+ */
+ else if (omode != nmode || oitem->file_size != nitem->file_size) {
+ status = GIT_DELTA_MODIFIED;
+ modified_uncertain =
+ (oitem->file_size <= 0 && nitem->file_size > 0);
+ }
+ else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
+ (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
+ oitem->ino != nitem->ino ||
+ oitem->uid != nitem->uid ||
+ oitem->gid != nitem->gid ||
+ git_index_entry_newer_than_index(nitem, index))
+ {
+ status = GIT_DELTA_MODIFIED;
+ modified_uncertain = true;
+ }
+
+ /* if mode is GITLINK and submodules are ignored, then skip */
+ } else if (S_ISGITLINK(nmode) &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
+ status = GIT_DELTA_UNMODIFIED;
+ }
+
+ /* if we got here and decided that the files are modified, but we
+ * haven't calculated the OID of the new item, then calculate it now
+ */
+ if (modified_uncertain && git_oid_iszero(&nitem->id)) {
+ const git_oid *update_check =
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
+ &oitem->id : NULL;
+
+ if ((error = git_diff__oid_for_entry(
+ &noid, &diff->base, nitem, nmode, update_check)) < 0)
+ return error;
+
+ /* if oid matches, then mark unmodified (except submodules, where
+ * the filesystem content may be modified even if the oid still
+ * matches between the index and the workdir HEAD)
+ */
+ if (omode == nmode && !S_ISGITLINK(omode) &&
+ git_oid_equal(&oitem->id, &noid))
+ status = GIT_DELTA_UNMODIFIED;
+ }
+
+ /* If we want case changes, then break this into a delete of the old
+ * and an add of the new so that consumers can act accordingly (eg,
+ * checkout will update the case on disk.)
+ */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
+ strcmp(oitem->path, nitem->path) != 0) {
+
+ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
+ error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
+
+ return error;
+ }
+
+ return diff_delta__from_two(
+ diff, status, oitem, omode, nitem, nmode,
+ git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
+}
+
+static bool entry_is_prefixed(
+ git_diff_generated *diff,
+ const git_index_entry *item,
+ const git_index_entry *prefix_item)
+{
+ size_t pathlen;
+
+ if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0)
+ return false;
+
+ pathlen = strlen(prefix_item->path);
+
+ return (prefix_item->path[pathlen - 1] == '/' ||
+ item->path[pathlen] == '\0' ||
+ item->path[pathlen] == '/');
+}
+
+static int iterator_current(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ int error;
+
+ if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ const git_index_entry *prev_entry = *entry;
+ int cmp, error;
+
+ /* if we're looking for conflicts, we only want to report
+ * one conflict for each file, instead of all three sides.
+ * so if this entry is a conflict for this file, and the
+ * previous one was a conflict for the same file, skip it.
+ */
+ while ((error = git_iterator_advance(entry, iterator)) == 0) {
+ if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
+ !git_index_entry_is_conflict(prev_entry) ||
+ !git_index_entry_is_conflict(*entry))
+ break;
+
+ cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
+ strcasecmp(prev_entry->path, (*entry)->path) :
+ strcmp(prev_entry->path, (*entry)->path);
+
+ if (cmp)
+ break;
+ }
+
+ if (error == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance_into(
+ const git_index_entry **entry,
+ git_iterator *iterator)
+{
+ int error;
+
+ if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int iterator_advance_over(
+ const git_index_entry **entry,
+ git_iterator_status_t *status,
+ git_iterator *iterator)
+{
+ int error = git_iterator_advance_over(entry, status, iterator);
+
+ if (error == GIT_ITEROVER) {
+ *entry = NULL;
+ error = 0;
+ }
+
+ return error;
+}
+
+static int handle_unmatched_new_item(
+ git_diff_generated *diff, diff_in_progress *info)
+{
+ int error = 0;
+ const git_index_entry *nitem = info->nitem;
+ git_delta_t delta_type = GIT_DELTA_UNTRACKED;
+ bool contains_oitem;
+
+ /* check if this is a prefix of the other side */
+ contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
+
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_is_conflict(nitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
+ /* update delta_type if this item is ignored */
+ else if (git_iterator_current_is_ignored(info->new_iter))
+ delta_type = GIT_DELTA_IGNORED;
+
+ if (nitem->mode == GIT_FILEMODE_TREE) {
+ bool recurse_into_dir = contains_oitem;
+
+ /* check if user requests recursion into this type of dir */
+ recurse_into_dir = contains_oitem ||
+ (delta_type == GIT_DELTA_UNTRACKED &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
+ (delta_type == GIT_DELTA_IGNORED &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
+
+ /* do not advance into directories that contain a .git file */
+ if (recurse_into_dir && !contains_oitem) {
+ git_buf *full = NULL;
+ if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
+ return -1;
+ if (full && git_path_contains(full, DOT_GIT)) {
+ /* TODO: warning if not a valid git repository */
+ recurse_into_dir = false;
+ }
+ }
+
+ /* still have to look into untracked directories to match core git -
+ * with no untracked files, directory is treated as ignored
+ */
+ if (!recurse_into_dir &&
+ delta_type == GIT_DELTA_UNTRACKED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
+ {
+ git_diff_delta *last;
+ git_iterator_status_t untracked_state;
+
+ /* attempt to insert record for this directory */
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
+ return error;
+
+ /* if delta wasn't created (because of rules), just skip ahead */
+ last = diff_delta__last_for_item(diff, nitem);
+ if (!last)
+ return iterator_advance(&info->nitem, info->new_iter);
+
+ /* iterate into dir looking for an actual untracked file */
+ if ((error = iterator_advance_over(
+ &info->nitem, &untracked_state, info->new_iter)) < 0)
+ return error;
+
+ /* if we found nothing that matched our pathlist filter, exclude */
+ if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
+ git_vector_pop(&diff->base.deltas);
+ git__free(last);
+ }
+
+ /* if we found nothing or just ignored items, update the record */
+ if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
+ untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
+ last->status = GIT_DELTA_IGNORED;
+
+ /* remove the record if we don't want ignored records */
+ if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
+ git_vector_pop(&diff->base.deltas);
+ git__free(last);
+ }
+ }
+
+ return 0;
+ }
+
+ /* try to advance into directory if necessary */
+ if (recurse_into_dir) {
+ error = iterator_advance_into(&info->nitem, info->new_iter);
+
+ /* if directory is empty, can't advance into it, so skip it */
+ if (error == GIT_ENOTFOUND) {
+ giterr_clear();
+ error = iterator_advance(&info->nitem, info->new_iter);
+ }
+
+ return error;
+ }
+ }
+
+ else if (delta_type == GIT_DELTA_IGNORED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
+ git_iterator_current_tree_is_ignored(info->new_iter))
+ /* item contained in ignored directory, so skip over it */
+ return iterator_advance(&info->nitem, info->new_iter);
+
+ else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
+ if (delta_type != GIT_DELTA_CONFLICTED)
+ delta_type = GIT_DELTA_ADDED;
+ }
+
+ else if (nitem->mode == GIT_FILEMODE_COMMIT) {
+ /* ignore things that are not actual submodules */
+ if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
+ giterr_clear();
+ delta_type = GIT_DELTA_IGNORED;
+
+ /* if this contains a tracked item, treat as normal TREE */
+ if (contains_oitem) {
+ error = iterator_advance_into(&info->nitem, info->new_iter);
+ if (error != GIT_ENOTFOUND)
+ return error;
+
+ giterr_clear();
+ return iterator_advance(&info->nitem, info->new_iter);
+ }
+ }
+ }
+
+ else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
+ delta_type = GIT_DELTA_UNTRACKED;
+ else
+ delta_type = GIT_DELTA_UNREADABLE;
+ }
+
+ /* Actually create the record for this item if necessary */
+ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
+ return error;
+
+ /* If user requested TYPECHANGE records, then check for that instead of
+ * just generating an ADDED/UNTRACKED record
+ */
+ if (delta_type != GIT_DELTA_IGNORED &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
+ contains_oitem)
+ {
+ /* this entry was prefixed with a tree - make TYPECHANGE */
+ git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
+ if (last) {
+ last->status = GIT_DELTA_TYPECHANGE;
+ last->old_file.mode = GIT_FILEMODE_TREE;
+ }
+ }
+
+ return iterator_advance(&info->nitem, info->new_iter);
+}
+
+static int handle_unmatched_old_item(
+ git_diff_generated *diff, diff_in_progress *info)
+{
+ git_delta_t delta_type = GIT_DELTA_DELETED;
+ int error;
+
+ /* update delta_type if this item is conflicted */
+ if (git_index_entry_is_conflict(info->oitem))
+ delta_type = GIT_DELTA_CONFLICTED;
+
+ if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
+ return error;
+
+ /* if we are generating TYPECHANGE records then check for that
+ * instead of just generating a DELETE record
+ */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
+ entry_is_prefixed(diff, info->nitem, info->oitem))
+ {
+ /* this entry has become a tree! convert to TYPECHANGE */
+ git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
+ if (last) {
+ last->status = GIT_DELTA_TYPECHANGE;
+ last->new_file.mode = GIT_FILEMODE_TREE;
+ }
+
+ /* If new_iter is a workdir iterator, then this situation
+ * will certainly be followed by a series of untracked items.
+ * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
+ */
+ if (S_ISDIR(info->nitem->mode) &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
+ return iterator_advance(&info->nitem, info->new_iter);
+ }
+
+ return iterator_advance(&info->oitem, info->old_iter);
+}
+
+static int handle_matched_item(
+ git_diff_generated *diff, diff_in_progress *info)
+{
+ int error = 0;
+
+ if ((error = maybe_modified(diff, info)) < 0)
+ return error;
+
+ if (!(error = iterator_advance(&info->oitem, info->old_iter)))
+ error = iterator_advance(&info->nitem, info->new_iter);
+
+ return error;
+}
+
+int git_diff__from_iterators(
+ git_diff **out,
+ git_repository *repo,
+ git_iterator *old_iter,
+ git_iterator *new_iter,
+ const git_diff_options *opts)
+{
+ git_diff_generated *diff;
+ diff_in_progress info;
+ int error = 0;
+
+ *out = NULL;
+
+ diff = diff_generated_alloc(repo, old_iter, new_iter);
+ GITERR_CHECK_ALLOC(diff);
+
+ info.repo = repo;
+ info.old_iter = old_iter;
+ info.new_iter = new_iter;
+
+ /* make iterators have matching icase behavior */
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
+ git_iterator_set_ignore_case(old_iter, true);
+ git_iterator_set_ignore_case(new_iter, true);
+ }
+
+ /* finish initialization */
+ if ((error = diff_generated_apply_options(diff, opts)) < 0)
+ goto cleanup;
+
+ if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
+ (error = iterator_current(&info.nitem, new_iter)) < 0)
+ goto cleanup;
+
+ /* run iterators building diffs */
+ while (!error && (info.oitem || info.nitem)) {
+ int cmp;
+
+ /* report progress */
+ if (opts && opts->progress_cb) {
+ if ((error = opts->progress_cb(&diff->base,
+ info.oitem ? info.oitem->path : NULL,
+ info.nitem ? info.nitem->path : NULL,
+ opts->payload)))
+ break;
+ }
+
+ cmp = info.oitem ?
+ (info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1;
+
+ /* create DELETED records for old items not matched in new */
+ if (cmp < 0)
+ error = handle_unmatched_old_item(diff, &info);
+
+ /* create ADDED, TRACKED, or IGNORED records for new items not
+ * matched in old (and/or descend into directories as needed)
+ */
+ else if (cmp > 0)
+ error = handle_unmatched_new_item(diff, &info);
+
+ /* otherwise item paths match, so create MODIFIED record
+ * (or ADDED and DELETED pair if type changed)
+ */
+ else
+ error = handle_matched_item(diff, &info);
+ }
+
+ diff->base.perf.stat_calls +=
+ old_iter->stat_calls + new_iter->stat_calls;
+
+cleanup:
+ if (!error)
+ *out = &diff->base;
+ else
+ git_diff_free(&diff->base);
+
+ return error;
+}
+
+#define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
+ git_iterator *a = NULL, *b = NULL; \
+ char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
+ git_pathspec_prefix(&opts->pathspec) : NULL; \
+ git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
+ b_opts = GIT_ITERATOR_OPTIONS_INIT; \
+ a_opts.flags = FLAGS_FIRST; \
+ a_opts.start = pfx; \
+ a_opts.end = pfx; \
+ b_opts.flags = FLAGS_SECOND; \
+ b_opts.start = pfx; \
+ b_opts.end = pfx; \
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
+ if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
+ a_opts.pathlist.strings = opts->pathspec.strings; \
+ a_opts.pathlist.count = opts->pathspec.count; \
+ b_opts.pathlist.strings = opts->pathspec.strings; \
+ b_opts.pathlist.count = opts->pathspec.count; \
+ } \
+ if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
+ error = git_diff__from_iterators(&diff, repo, a, b, opts); \
+ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
+} while (0)
+
+int git_diff_tree_to_tree(
+ git_diff **out,
+ git_repository *repo,
+ git_tree *old_tree,
+ git_tree *new_tree,
+ const git_diff_options *opts)
+{
+ git_diff *diff = NULL;
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
+ int error = 0;
+
+ assert(out && repo);
+
+ *out = NULL;
+
+ /* for tree to tree diff, be case sensitive even if the index is
+ * currently case insensitive, unless the user explicitly asked
+ * for case insensitivity
+ */
+ if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
+ iflag = GIT_ITERATOR_IGNORE_CASE;
+
+ DIFF_FROM_ITERATORS(
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
+ git_iterator_for_tree(&b, new_tree, &b_opts), iflag
+ );
+
+ if (!error)
+ *out = diff;
+
+ return error;
+}
+
+static int diff_load_index(git_index **index, git_repository *repo)
+{
+ int error = git_repository_index__weakptr(index, repo);
+
+ /* reload the repository index when user did not pass one in */
+ if (!error && git_index_read(*index, false) < 0)
+ giterr_clear();
+
+ return error;
+}
+
+int git_diff_tree_to_index(
+ git_diff **out,
+ git_repository *repo,
+ git_tree *old_tree,
+ git_index *index,
+ const git_diff_options *opts)
+{
+ git_diff *diff = NULL;
+ git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
+ GIT_ITERATOR_INCLUDE_CONFLICTS;
+ bool index_ignore_case = false;
+ int error = 0;
+
+ assert(out && repo);
+
+ *out = NULL;
+
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
+ return error;
+
+ index_ignore_case = index->ignore_case;
+
+ DIFF_FROM_ITERATORS(
+ git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
+ git_iterator_for_index(&b, repo, index, &b_opts), iflag
+ );
+
+ /* if index is in case-insensitive order, re-sort deltas to match */
+ if (!error && index_ignore_case)
+ git_diff__set_ignore_case(diff, true);
+
+ if (!error)
+ *out = diff;
+
+ return error;
+}
+
+int git_diff_index_to_workdir(
+ git_diff **out,
+ git_repository *repo,
+ git_index *index,
+ const git_diff_options *opts)
+{
+ git_diff *diff = NULL;
+ int error = 0;
+
+ assert(out && repo);
+
+ *out = NULL;
+
+ if (!index && (error = diff_load_index(&index, repo)) < 0)
+ return error;
+
+ DIFF_FROM_ITERATORS(
+ git_iterator_for_index(&a, repo, index, &a_opts),
+ GIT_ITERATOR_INCLUDE_CONFLICTS,
+
+ git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
+ GIT_ITERATOR_DONT_AUTOEXPAND
+ );
+
+ if (!error && (diff->opts.flags & GIT_DIFF_UPDATE_INDEX) != 0 &&
+ ((git_diff_generated *)diff)->index_updated)
+ error = git_index_write(index);
+
+ if (!error)
+ *out = diff;
+
+ return error;
+}
+
+int git_diff_tree_to_workdir(
+ git_diff **out,
+ git_repository *repo,
+ git_tree *old_tree,
+ const git_diff_options *opts)
+{
+ git_diff *diff = NULL;
+ git_index *index;
+ int error = 0;
+
+ assert(out && repo);
+
+ *out = NULL;
+
+ if ((error = git_repository_index__weakptr(&index, repo)))
+ return error;
+
+ DIFF_FROM_ITERATORS(
+ git_iterator_for_tree(&a, old_tree, &a_opts), 0,
+ git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
+ );
+
+ if (!error)
+ *out = diff;
+
+ return error;
+}
+
+int git_diff_tree_to_workdir_with_index(
+ git_diff **out,
+ git_repository *repo,
+ git_tree *tree,
+ const git_diff_options *opts)
+{
+ git_diff *d1 = NULL, *d2 = NULL;
+ git_index *index = NULL;
+ int error = 0;
+
+ assert(out && repo);
+
+ *out = NULL;
+
+ if ((error = diff_load_index(&index, repo)) < 0)
+ return error;
+
+ if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) &&
+ !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
+ error = git_diff_merge(d1, d2);
+
+ git_diff_free(d2);
+
+ if (error) {
+ git_diff_free(d1);
+ d1 = NULL;
+ }
+
+ *out = d1;
+ return error;
+}
+
+int git_diff_index_to_index(
+ git_diff **out,
+ git_repository *repo,
+ git_index *old_index,
+ git_index *new_index,
+ const git_diff_options *opts)
+{
+ git_diff *diff;
+ int error = 0;
+
+ assert(out && old_index && new_index);
+
+ *out = NULL;
+
+ DIFF_FROM_ITERATORS(
+ git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
+ git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
+ );
+
+ /* if index is in case-insensitive order, re-sort deltas to match */
+ if (!error && (old_index->ignore_case || new_index->ignore_case))
+ git_diff__set_ignore_case(diff, true);
+
+ if (!error)
+ *out = diff;
+
+ return error;
+}
+
+int git_diff__paired_foreach(
+ git_diff *head2idx,
+ git_diff *idx2wd,
+ int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
+ void *payload)
+{
+ int cmp, error = 0;
+ git_diff_delta *h2i, *i2w;
+ size_t i, j, i_max, j_max;
+ int (*strcomp)(const char *, const char *) = git__strcmp;
+ bool h2i_icase, i2w_icase, icase_mismatch;
+
+ i_max = head2idx ? head2idx->deltas.length : 0;
+ j_max = idx2wd ? idx2wd->deltas.length : 0;
+ if (!i_max && !j_max)
+ return 0;
+
+ /* At some point, tree-to-index diffs will probably never ignore case,
+ * even if that isn't true now. Index-to-workdir diffs may or may not
+ * ignore case, but the index filename for the idx2wd diff should
+ * still be using the canonical case-preserving name.
+ *
+ * Therefore the main thing we need to do here is make sure the diffs
+ * are traversed in a compatible order. To do this, we temporarily
+ * resort a mismatched diff to get the order correct.
+ *
+ * In order to traverse renames in the index->workdir, we need to
+ * ensure that we compare the index name on both sides, so we
+ * always sort by the old name in the i2w list.
+ */
+ h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx);
+ i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd);
+
+ icase_mismatch =
+ (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
+
+ if (icase_mismatch && h2i_icase) {
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
+ git_vector_sort(&head2idx->deltas);
+ }
+
+ if (i2w_icase && !icase_mismatch) {
+ strcomp = git__strcasecmp;
+
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
+ git_vector_sort(&idx2wd->deltas);
+ } else if (idx2wd != NULL) {
+ git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
+ git_vector_sort(&idx2wd->deltas);
+ }
+
+ for (i = 0, j = 0; i < i_max || j < j_max; ) {
+ h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
+ i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
+
+ cmp = !i2w ? -1 : !h2i ? 1 :
+ strcomp(h2i->new_file.path, i2w->old_file.path);
+
+ if (cmp < 0) {
+ i++; i2w = NULL;
+ } else if (cmp > 0) {
+ j++; h2i = NULL;
+ } else {
+ i++; j++;
+ }
+
+ if ((error = cb(h2i, i2w, payload)) != 0) {
+ giterr_set_after_callback(error);
+ break;
+ }
+ }
+
+ /* restore case-insensitive delta sort */
+ if (icase_mismatch && h2i_icase) {
+ git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
+ git_vector_sort(&head2idx->deltas);
+ }
+
+ /* restore idx2wd sort by new path */
+ if (idx2wd != NULL) {
+ git_vector_set_cmp(&idx2wd->deltas,
+ i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
+ git_vector_sort(&idx2wd->deltas);
+ }
+
+ return error;
+}
+
+int git_diff__commit(
+ git_diff **out,
+ git_repository *repo,
+ const git_commit *commit,
+ const git_diff_options *opts)
+{
+ git_commit *parent = NULL;
+ git_diff *commit_diff = NULL;
+ git_tree *old_tree = NULL, *new_tree = NULL;
+ size_t parents;
+ int error = 0;
+
+ *out = NULL;
+
+ if ((parents = git_commit_parentcount(commit)) > 1) {
+ char commit_oidstr[GIT_OID_HEXSZ + 1];
+
+ error = -1;
+ giterr_set(GITERR_INVALID, "Commit %s is a merge commit",
+ git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
+ goto on_error;
+ }
+
+ if (parents > 0)
+ if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
+ (error = git_commit_tree(&old_tree, parent)) < 0)
+ goto on_error;
+
+ if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
+ (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
+ goto on_error;
+
+ *out = commit_diff;
+
+on_error:
+ git_tree_free(new_tree);
+ git_tree_free(old_tree);
+ git_commit_free(parent);
+
+ return error;
+}
+
diff --git a/src/diff_generate.h b/src/diff_generate.h
new file mode 100644
index 0000000..63e7f05
--- /dev/null
+++ b/src/diff_generate.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_diff_generate_h__
+#define INCLUDE_diff_generate_h__
+
+enum {
+ GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */
+ GIT_DIFFCAPS_IGNORE_STAT = (1 << 1), /* use stat? */
+ GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */
+ GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */
+ GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */
+};
+
+#define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
+#define DIFF_FLAGS_NOT_BINARY (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
+
+enum {
+ GIT_DIFF_FLAG__FREE_PATH = (1 << 7), /* `path` is allocated memory */
+ GIT_DIFF_FLAG__FREE_DATA = (1 << 8), /* internal file data is allocated */
+ GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9), /* internal file data is mmap'ed */
+ GIT_DIFF_FLAG__NO_DATA = (1 << 10), /* file data should not be loaded */
+ GIT_DIFF_FLAG__FREE_BLOB = (1 << 11), /* release the blob when done */
+ GIT_DIFF_FLAG__LOADED = (1 << 12), /* file data has been loaded */
+
+ GIT_DIFF_FLAG__TO_DELETE = (1 << 16), /* delete entry during rename det. */
+ GIT_DIFF_FLAG__TO_SPLIT = (1 << 17), /* split entry during rename det. */
+ GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18),
+ GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19),
+ GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20),
+};
+
+#define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF)
+
+#define GIT_DIFF__VERBOSE (1 << 30)
+
+extern void git_diff_addref(git_diff *diff);
+
+extern bool git_diff_delta__should_skip(
+ const git_diff_options *opts, const git_diff_delta *delta);
+
+extern int git_diff__from_iterators(
+ git_diff **diff_ptr,
+ git_repository *repo,
+ git_iterator *old_iter,
+ git_iterator *new_iter,
+ const git_diff_options *opts);
+
+extern int git_diff__commit(
+ git_diff **diff, git_repository *repo, const git_commit *commit, const git_diff_options *opts);
+
+extern int git_diff__paired_foreach(
+ git_diff *idx2head,
+ git_diff *wd2idx,
+ int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
+ void *payload);
+
+/* Merge two `git_diff`s according to the callback given by `cb`. */
+
+typedef git_diff_delta *(*git_diff__merge_cb)(
+ const git_diff_delta *left,
+ const git_diff_delta *right,
+ git_pool *pool);
+
+extern int git_diff__merge(
+ git_diff *onto, const git_diff *from, git_diff__merge_cb cb);
+
+extern git_diff_delta *git_diff__merge_like_cgit(
+ const git_diff_delta *a,
+ const git_diff_delta *b,
+ git_pool *pool);
+
+/* Duplicate a `git_diff_delta` out of the `git_pool` */
+extern git_diff_delta *git_diff__delta_dup(
+ const git_diff_delta *d, git_pool *pool);
+
+extern int git_diff__oid_for_file(
+ git_oid *out,
+ git_diff *diff,
+ const char *path,
+ uint16_t mode,
+ git_off_t size);
+
+extern int git_diff__oid_for_entry(
+ git_oid *out,
+ git_diff *diff,
+ const git_index_entry *src,
+ uint16_t mode,
+ const git_oid *update_match);
+
+/*
+ * Sometimes a git_diff_file will have a zero size; this attempts to
+ * fill in the size without loading the blob if possible. If that is
+ * not possible, then it will return the git_odb_object that had to be
+ * loaded and the caller can use it or dispose of it as needed.
+ */
+GIT_INLINE(int) git_diff_file__resolve_zero_size(
+ git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
+{
+ int error;
+ git_odb *odb;
+ size_t len;
+ git_otype type;
+
+ if ((error = git_repository_odb(&odb, repo)) < 0)
+ return error;
+
+ error = git_odb__read_header_or_object(
+ odb_obj, &len, &type, odb, &file->id);
+
+ git_odb_free(odb);
+
+ if (!error)
+ file->size = (git_off_t)len;
+
+ return error;
+}
+
+#endif
+
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 6a6a628..e8848bd 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -11,6 +11,7 @@
#include "git2/sys/hashsig.h"
#include "diff.h"
+#include "diff_generate.h"
#include "path.h"
#include "fileops.h"
#include "config.h"
diff --git a/src/diff_tform.h b/src/diff_tform.h
new file mode 100644
index 0000000..5bd9712
--- /dev/null
+++ b/src/diff_tform.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_diff_tform_h__
+#define INCLUDE_diff_tform_h__
+
+extern int git_diff_find_similar__hashsig_for_file(
+ void **out, const git_diff_file *f, const char *path, void *p);
+
+extern int git_diff_find_similar__hashsig_for_buf(
+ void **out, const git_diff_file *f, const char *buf, size_t len, void *p);
+
+extern void git_diff_find_similar__hashsig_free(void *sig, void *payload);
+
+extern int git_diff_find_similar__calc_similarity(
+ int *score, void *siga, void *sigb, void *payload);
+
+#endif
+
diff --git a/src/merge.c b/src/merge.c
index b93851b..6934aa7 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -18,6 +18,8 @@
#include "iterator.h"
#include "refs.h"
#include "diff.h"
+#include "diff_generate.h"
+#include "diff_tform.h"
#include "checkout.h"
#include "tree.h"
#include "blob.h"
diff --git a/src/patch_generate.c b/src/patch_generate.c
index 80a5a55..98c1492 100644
--- a/src/patch_generate.c
+++ b/src/patch_generate.c
@@ -7,6 +7,7 @@
#include "common.h"
#include "git2/blob.h"
#include "diff.h"
+#include "diff_generate.h"
#include "diff_file.h"
#include "diff_driver.h"
#include "patch_generate.h"
diff --git a/src/stash.c b/src/stash.c
index 43a464e..f5f4f36 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -23,6 +23,7 @@
#include "iterator.h"
#include "merge.h"
#include "diff.h"
+#include "diff_generate.h"
static int create_error(int error, const char *msg)
{
diff --git a/src/status.c b/src/status.c
index b206b0e..e610f5f 100644
--- a/src/status.c
+++ b/src/status.c
@@ -19,6 +19,7 @@
#include "git2/diff.h"
#include "diff.h"
+#include "diff_generate.h"
static unsigned int index_delta2status(const git_diff_delta *head2idx)
{
diff --git a/tests/diff/format_email.c b/tests/diff/format_email.c
index e55afe9..2e6d036 100644
--- a/tests/diff/format_email.c
+++ b/tests/diff/format_email.c
@@ -4,6 +4,7 @@
#include "buffer.h"
#include "commit.h"
#include "diff.h"
+#include "diff_generate.h"
static git_repository *repo;
diff --git a/tests/diff/stats.c b/tests/diff/stats.c
index f731997..8f146e2 100644
--- a/tests/diff/stats.c
+++ b/tests/diff/stats.c
@@ -4,6 +4,7 @@
#include "buffer.h"
#include "commit.h"
#include "diff.h"
+#include "diff_generate.h"
static git_repository *_repo;
static git_diff_stats *_stats;
diff --git a/tests/merge/trees/treediff.c b/tests/merge/trees/treediff.c
index 3634568..cd2cf78 100644
--- a/tests/merge/trees/treediff.c
+++ b/tests/merge/trees/treediff.c
@@ -3,6 +3,7 @@
#include "merge.h"
#include "../merge_helpers.h"
#include "diff.h"
+#include "diff_tform.h"
#include "git2/sys/hashsig.h"
static git_repository *repo;