Merge pull request #1642 from arrbee/diff-function-context Diff code reorg plus function context in diff headers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085
diff --git a/docs/diff-internals.md b/docs/diff-internals.md
new file mode 100644
index 0000000..53e71f5
--- /dev/null
+++ b/docs/diff-internals.md
@@ -0,0 +1,88 @@
+Diff is broken into four phases:
+
+1. Building a list of things that have changed. These changes are called
+ deltas (git_diff_delta objects) and are grouped into a git_diff_list.
+2. Applying file similarity measurement for rename and copy detection (and
+ to potentially split files that have changed radically). This step is
+ optional.
+3. Computing the textual diff for each delta. Not all deltas have a
+ meaningful textual diff. For those that do, the textual diff can
+ either be generated on the fly and passed to output callbacks or can be
+ turned into a git_diff_patch object.
+4. Formatting the diff and/or patch into standard text formats (such as
+ patches, raw lists, etc).
+
+In the source code, step 1 is implemented in `src/diff.c`, step 2 in
+`src/diff_tform.c`, step 3 in `src/diff_patch.c`, and step 4 in
+`src/diff_print.c`. Additionally, when it comes to accessing file
+content, everything goes through diff drivers that are implemented in
+`src/diff_driver.c`.
+
+External Objects
+----------------
+
+* `git_diff_options` repesents user choices about how a diff should be
+ performed and is passed to most diff generating functions.
+* `git_diff_file` represents an item on one side of a possible delta
+* `git_diff_delta` represents a pair of items that have changed in some
+ way - it contains two `git_diff_file` plus a status and other stuff.
+* `git_diff_list` is a list of deltas along with information about how
+ those particular deltas were found.
+* `git_diff_patch` represents the actual diff between a pair of items. In
+ some cases, a delta may not have a corresponding patch, if the objects
+ are binary, for example. The content of a patch will be a set of hunks
+ and lines.
+* A `hunk` is range of lines described by a `git_diff_range` (i.e. "lines
+ 10-20 in the old file became lines 12-23 in the new"). It will have a
+ header that compactly represents that information, and it will have a
+ number of lines of context surrounding added and deleted lines.
+* A `line` is simple a line of data along with a `git_diff_line_t` value
+ that tells how the data should be interpretted (e.g. context or added).
+
+Internal Objects
+----------------
+
+* `git_diff_file_content` is an internal structure that represents the
+ data on one side of an item to be diffed; it is an augmented
+ `git_diff_file` with more flags and the actual file data.
+** it is created from a repository plus a) a git_diff_file, b) a git_blob,
+ or c) raw data and size
+** there are three main operations on git_diff_file_content:
+*** _initialization_ sets up the data structure and does what it can up to,
+ but not including loading and looking at the actual data
+*** _loading_ loads the data, preprocesses it (i.e. applies filters) and
+ potentially analyzes it (to decide if binary)
+*** _free_ releases loaded data and frees any allocated memory
+
+* The internal structure of a `git_diff_patch` stores the actual diff
+ between a pair of `git_diff_file_content` items
+** it may be "unset" if the items are not diffable
+** "empty" if the items are the same
+** otherwise it will consist of a set of hunks each of which covers some
+ number of lines of context, additions and deletions
+** a patch is created from two git_diff_file_content items
+** a patch is fully instantiated in three phases:
+*** initial creation and initialization
+*** loading of data and preliminary data examination
+*** diffing of data and optional storage of diffs
+** (TBD) if a patch is asked to store the diffs and the size of the diff
+ is significantly smaller than the raw data of the two sides, then the
+ patch may be flattened using a pool of string data
+
+* `git_diff_output` is an internal structure that represents an output
+ target for a `git_diff_patch`
+** It consists of file, hunk, and line callbacks, plus a payload
+** There is a standard flattened output that can be used for plain text output
+** Typically we use a `git_xdiff_output` which drives the callbacks via the
+ xdiff code taken from core Git.
+
+* `git_diff_driver` is an internal structure that encapsulates the logic
+ for a given type of file
+** a driver is looked up based on the name and mode of a file.
+** the driver can then be used to:
+*** determine if a file is binary (by attributes, by git_diff_options
+ settings, or by examining the content)
+*** give you a function pointer that is used to evaluate function context
+ for hunk headers
+** At some point, the logic for getting a filtered version of file content
+ or calculating the OID of a file may be moved into the driver.
diff --git a/include/git2/diff.h b/include/git2/diff.h
index d26456c..8113a56 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -148,6 +148,9 @@ typedef enum {
* Of course, ignore rules are still checked for the directory itself.
*/
GIT_DIFF_FAST_UNTRACKED_DIRS = (1 << 19),
+
+ /** Treat all files as binary, disabling text diffs */
+ GIT_DIFF_FORCE_BINARY = (1 << 20),
} git_diff_option_t;
/**
@@ -857,7 +860,7 @@ GIT_EXTERN(size_t) git_diff_patch_num_hunks(
* @param total_additions Count of addition lines in output, can be NULL.
* @param total_deletions Count of deletion lines in output, can be NULL.
* @param patch The git_diff_patch object
- * @return Number of lines in hunk or -1 if invalid hunk index
+ * @return 0 on success, <0 on error
*/
GIT_EXTERN(int) git_diff_patch_line_stats(
size_t *total_context,
@@ -998,6 +1001,26 @@ GIT_EXTERN(int) git_diff_blobs(
void *payload);
/**
+ * Directly generate a patch from the difference between two blobs.
+ *
+ * This is just like `git_diff_blobs()` except it generates a patch object
+ * for the difference instead of directly making callbacks. You can use the
+ * standard `git_diff_patch` accessor functions to read the patch data, and
+ * you must call `git_diff_patch_free()` on the patch when done.
+ *
+ * @param out The generated patch; NULL on error
+ * @param old_blob Blob for old side of diff, or NULL for empty blob
+ * @param new_blob Blob for new side of diff, or NULL for empty blob
+ * @param options Options for diff, or NULL for default options
+ * @return 0 on success or error code < 0
+ */
+GIT_EXTERN(int) git_diff_patch_from_blobs(
+ git_diff_patch **out,
+ const git_blob *old_blob,
+ const git_blob *new_blob,
+ const git_diff_options *opts);
+
+/**
* Directly run a diff between a blob and a buffer.
*
* As with `git_diff_blobs`, comparing a blob and buffer lacks some context,
@@ -1010,7 +1033,7 @@ GIT_EXTERN(int) git_diff_blobs(
* the reverse, with GIT_DELTA_REMOVED and blob content removed.
*
* @param old_blob Blob for old side of diff, or NULL for empty blob
- * @param buffer Raw data for new side of diff
+ * @param buffer Raw data for new side of diff, or NULL for empty
* @param buffer_len Length of raw data for new side of diff
* @param options Options for diff, or NULL for default options
* @param file_cb Callback for "file"; made once if there is a diff; can be NULL
@@ -1029,6 +1052,29 @@ GIT_EXTERN(int) git_diff_blob_to_buffer(
git_diff_data_cb data_cb,
void *payload);
+/**
+ * Directly generate a patch from the difference between a blob and a buffer.
+ *
+ * This is just like `git_diff_blob_to_buffer()` except it generates a patch
+ * object for the difference instead of directly making callbacks. You can
+ * use the standard `git_diff_patch` accessor functions to read the patch
+ * data, and you must call `git_diff_patch_free()` on the patch when done.
+ *
+ * @param out The generated patch; NULL on error
+ * @param old_blob Blob for old side of diff, or NULL for empty blob
+ * @param buffer Raw data for new side of diff, or NULL for empty
+ * @param buffer_len Length of raw data for new side of diff
+ * @param options Options for diff, or NULL for default options
+ * @return 0 on success or error code < 0
+ */
+GIT_EXTERN(int) git_diff_patch_from_blob_and_buffer(
+ git_diff_patch **out,
+ const git_blob *old_blob,
+ const char *buf,
+ size_t buflen,
+ const git_diff_options *opts);
+
+
GIT_END_DECL
/** @} */
diff --git a/src/array.h b/src/array.h
new file mode 100644
index 0000000..2d77c71
--- /dev/null
+++ b/src/array.h
@@ -0,0 +1,66 @@
+/*
+ * 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_array_h__
+#define INCLUDE_array_h__
+
+#include "util.h"
+
+/*
+ * Use this to declare a typesafe resizable array of items, a la:
+ *
+ * git_array_t(int) my_ints = GIT_ARRAY_INIT;
+ * ...
+ * int *i = git_array_alloc(my_ints);
+ * GITERR_CHECK_ALLOC(i);
+ * ...
+ * git_array_clear(my_ints);
+ *
+ * You may also want to do things like:
+ *
+ * typedef git_array_t(my_struct) my_struct_array_t;
+ */
+#define git_array_t(type) struct { type *ptr; uint32_t size, asize; }
+
+#define GIT_ARRAY_INIT { NULL, 0, 0 }
+
+#define git_array_init(a) \
+ do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
+
+#define git_array_clear(a) \
+ do { git__free((a).ptr); git_array_init(a); } while (0)
+
+#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
+
+
+typedef git_array_t(void) git_array_generic_t;
+
+/* use a generic array for growth so this can return the new item */
+GIT_INLINE(void *) git_array_grow(git_array_generic_t *a, size_t item_size)
+{
+ uint32_t new_size = (a->size < 8) ? 8 : a->asize * 3 / 2;
+ void *new_array = git__realloc(a->ptr, new_size * item_size);
+ if (!new_array) {
+ git_array_clear(*a);
+ return NULL;
+ } else {
+ a->ptr = new_array; a->asize = new_size; a->size++;
+ return (((char *)a->ptr) + (a->size - 1) * item_size);
+ }
+}
+
+#define git_array_alloc(a) \
+ ((a).size >= (a).asize) ? \
+ git_array_grow((git_array_generic_t *)&(a), sizeof(*(a).ptr)) : \
+ (a).ptr ? &(a).ptr[(a).size++] : NULL
+
+#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
+
+#define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : NULL)
+
+#define git_array_size(a) (a).size
+
+#endif
diff --git a/src/blob.c b/src/blob.c
index a68c4cc..2e4d5f4 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -11,6 +11,7 @@
#include "git2/odb_backend.h"
#include "common.h"
+#include "filebuf.h"
#include "blob.h"
#include "filter.h"
#include "buf_text.h"
diff --git a/src/checkout.c b/src/checkout.c
index 7a2e683..ede0be8 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -20,6 +20,7 @@
#include "refs.h"
#include "repository.h"
+#include "index.h"
#include "filter.h"
#include "blob.h"
#include "diff.h"
diff --git a/src/clone.c b/src/clone.c
index af3298f..5b6c6f7 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -21,6 +21,7 @@
#include "fileops.h"
#include "refs.h"
#include "path.h"
+#include "repository.h"
static int create_branch(
git_reference **branch,
diff --git a/src/crlf.c b/src/crlf.c
index 81268da..65039f9 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -5,14 +5,16 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
+#include "git2/attr.h"
+#include "git2/blob.h"
+#include "git2/index.h"
+
#include "common.h"
#include "fileops.h"
#include "hash.h"
#include "filter.h"
#include "buf_text.h"
#include "repository.h"
-#include "git2/attr.h"
-#include "git2/blob.h"
struct crlf_attrs {
int crlf_action;
diff --git a/src/diff.c b/src/diff.c
index 05ef4f1..97ccb3c 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -11,6 +11,8 @@
#include "attr_file.h"
#include "filter.h"
#include "pathspec.h"
+#include "index.h"
+#include "odb.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)
@@ -1170,3 +1172,73 @@ int git_diff_tree_to_workdir(
return error;
}
+
+size_t git_diff_num_deltas(git_diff_list *diff)
+{
+ assert(diff);
+ return (size_t)diff->deltas.length;
+}
+
+size_t git_diff_num_deltas_of_type(git_diff_list *diff, git_delta_t type)
+{
+ size_t i, count = 0;
+ git_diff_delta *delta;
+
+ assert(diff);
+
+ git_vector_foreach(&diff->deltas, i, delta) {
+ count += (delta->status == type);
+ }
+
+ return count;
+}
+
+int git_diff__paired_foreach(
+ git_diff_list *idx2head,
+ git_diff_list *wd2idx,
+ int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
+ void *payload)
+{
+ int cmp;
+ git_diff_delta *i2h, *w2i;
+ size_t i, j, i_max, j_max;
+ int (*strcomp)(const char *, const char *);
+
+ i_max = idx2head ? idx2head->deltas.length : 0;
+ j_max = wd2idx ? wd2idx->deltas.length : 0;
+
+ /* Get appropriate strcmp function */
+ strcomp = idx2head ? idx2head->strcomp : wd2idx ? wd2idx->strcomp : NULL;
+
+ /* Assert both iterators use matching ignore-case. If this function ever
+ * supports merging diffs that are not sorted by the same function, then
+ * it will need to spool and sort on one of the results before merging
+ */
+ if (idx2head && wd2idx) {
+ assert(idx2head->strcomp == wd2idx->strcomp);
+ }
+
+ for (i = 0, j = 0; i < i_max || j < j_max; ) {
+ i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL;
+ w2i = wd2idx ? GIT_VECTOR_GET(&wd2idx->deltas,j) : NULL;
+
+ cmp = !w2i ? -1 : !i2h ? 1 :
+ strcomp(i2h->old_file.path, w2i->old_file.path);
+
+ if (cmp < 0) {
+ if (cb(i2h, NULL, payload))
+ return GIT_EUSER;
+ i++;
+ } else if (cmp > 0) {
+ if (cb(NULL, w2i, payload))
+ return GIT_EUSER;
+ j++;
+ } else {
+ if (cb(i2h, w2i, payload))
+ return GIT_EUSER;
+ i++; j++;
+ }
+ }
+
+ return 0;
+}
diff --git a/src/diff.h b/src/diff.h
index ac8ab2a..ad12e77 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -29,11 +29,16 @@ enum {
GIT_DIFFCAPS_TRUST_NANOSECS = (1 << 5), /* use stat time nanoseconds */
};
+#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. */
@@ -83,6 +88,12 @@ extern int git_diff__from_iterators(
git_iterator *new_iter,
const git_diff_options *opts);
+extern int git_diff__paired_foreach(
+ git_diff_list *idx2head,
+ git_diff_list *wd2idx,
+ int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
+ void *payload);
+
int git_diff_find_similar__hashsig_for_file(
void **out, const git_diff_file *f, const char *path, void *p);
diff --git a/src/diff_driver.c b/src/diff_driver.c
new file mode 100644
index 0000000..ae2b7c3
--- /dev/null
+++ b/src/diff_driver.c
@@ -0,0 +1,405 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "common.h"
+
+#include "git2/attr.h"
+
+#include "diff.h"
+#include "diff_patch.h"
+#include "diff_driver.h"
+#include "strmap.h"
+#include "map.h"
+#include "buf_text.h"
+#include "repository.h"
+
+GIT__USE_STRMAP;
+
+typedef enum {
+ DIFF_DRIVER_AUTO = 0,
+ DIFF_DRIVER_BINARY = 1,
+ DIFF_DRIVER_TEXT = 2,
+ DIFF_DRIVER_PATTERNLIST = 3,
+} git_diff_driver_t;
+
+enum {
+ DIFF_CONTEXT_FIND_NORMAL = 0,
+ DIFF_CONTEXT_FIND_ICASE = (1 << 0),
+ DIFF_CONTEXT_FIND_EXT = (1 << 1),
+};
+
+/* data for finding function context for a given file type */
+struct git_diff_driver {
+ git_diff_driver_t type;
+ uint32_t binary_flags;
+ uint32_t other_flags;
+ git_array_t(regex_t) fn_patterns;
+ regex_t word_pattern;
+ char name[GIT_FLEX_ARRAY];
+};
+
+struct git_diff_driver_registry {
+ git_strmap *drivers;
+};
+
+#define FORCE_DIFFABLE (GIT_DIFF_FORCE_TEXT | GIT_DIFF_FORCE_BINARY)
+
+static git_diff_driver global_drivers[3] = {
+ { DIFF_DRIVER_AUTO, 0, 0, },
+ { DIFF_DRIVER_BINARY, GIT_DIFF_FORCE_BINARY, 0 },
+ { DIFF_DRIVER_TEXT, GIT_DIFF_FORCE_TEXT, 0 },
+};
+
+git_diff_driver_registry *git_diff_driver_registry_new()
+{
+ git_diff_driver_registry *reg =
+ git__calloc(1, sizeof(git_diff_driver_registry));
+ if (!reg)
+ return NULL;
+
+ if ((reg->drivers = git_strmap_alloc()) == NULL) {
+ git_diff_driver_registry_free(reg);
+ return NULL;
+ }
+
+ return reg;
+}
+
+void git_diff_driver_registry_free(git_diff_driver_registry *reg)
+{
+ git_diff_driver *drv;
+
+ if (!reg)
+ return;
+
+ git_strmap_foreach_value(reg->drivers, drv, git_diff_driver_free(drv));
+ git_strmap_free(reg->drivers);
+ git__free(reg);
+}
+
+static int diff_driver_add_funcname(
+ git_diff_driver *drv, const char *name, int regex_flags)
+{
+ int error;
+ regex_t re, *re_ptr;
+
+ if ((error = regcomp(&re, name, regex_flags)) != 0) {
+ /* TODO: warning about bad regex instead of failure */
+ error = giterr_set_regex(&re, error);
+ regfree(&re);
+ return error;
+ }
+
+ re_ptr = git_array_alloc(drv->fn_patterns);
+ GITERR_CHECK_ALLOC(re_ptr);
+
+ memcpy(re_ptr, &re, sizeof(re));
+ return 0;
+}
+
+static int diff_driver_xfuncname(const git_config_entry *entry, void *payload)
+{
+ return diff_driver_add_funcname(payload, entry->value, REG_EXTENDED);
+}
+
+static int diff_driver_funcname(const git_config_entry *entry, void *payload)
+{
+ return diff_driver_add_funcname(payload, entry->value, 0);
+}
+
+static git_diff_driver_registry *git_repository_driver_registry(
+ git_repository *repo)
+{
+ if (!repo->diff_drivers) {
+ git_diff_driver_registry *reg = git_diff_driver_registry_new();
+ reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);
+
+ if (reg != NULL) /* if we race, free losing allocation */
+ git_diff_driver_registry_free(reg);
+ }
+
+ if (!repo->diff_drivers)
+ giterr_set(GITERR_REPOSITORY, "Unable to create diff driver registry");
+
+ return repo->diff_drivers;
+}
+
+static int git_diff_driver_load(
+ git_diff_driver **out, git_repository *repo, const char *driver_name)
+{
+ int error = 0, bval;
+ git_diff_driver_registry *reg;
+ git_diff_driver *drv;
+ size_t namelen = strlen(driver_name);
+ khiter_t pos;
+ git_config *cfg;
+ git_buf name = GIT_BUF_INIT;
+ const char *val;
+ bool found_driver = false;
+
+ reg = git_repository_driver_registry(repo);
+ if (!reg)
+ return -1;
+ else {
+ pos = git_strmap_lookup_index(reg->drivers, driver_name);
+ if (git_strmap_valid_index(reg->drivers, pos)) {
+ *out = git_strmap_value_at(reg->drivers, pos);
+ return 0;
+ }
+ }
+
+ /* if you can't read config for repo, just use default driver */
+ if (git_repository_config__weakptr(&cfg, repo) < 0) {
+ giterr_clear();
+ return GIT_ENOTFOUND;
+ }
+
+ drv = git__calloc(1, sizeof(git_diff_driver) + namelen + 1);
+ GITERR_CHECK_ALLOC(drv);
+ drv->type = DIFF_DRIVER_AUTO;
+ memcpy(drv->name, driver_name, namelen);
+
+ if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
+ goto done;
+ if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto done;
+ /* diff.<driver>.binary unspecified, so just continue */
+ giterr_clear();
+ } else if (git_config_parse_bool(&bval, val) < 0) {
+ /* TODO: warn that diff.<driver>.binary has invalid value */
+ giterr_clear();
+ } else if (bval) {
+ /* if diff.<driver>.binary is true, just return the binary driver */
+ *out = &global_drivers[DIFF_DRIVER_BINARY];
+ goto done;
+ } else {
+ /* if diff.<driver>.binary is false, force binary checks off */
+ /* but still may have custom function context patterns, etc. */
+ drv->binary_flags = GIT_DIFF_FORCE_TEXT;
+ found_driver = true;
+ }
+
+ /* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
+
+ git_buf_truncate(&name, namelen + strlen("diff.."));
+ git_buf_put(&name, "xfuncname", strlen("xfuncname"));
+ if ((error = git_config_get_multivar(
+ cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto done;
+ giterr_clear(); /* no diff.<driver>.xfuncname, so just continue */
+ }
+
+ git_buf_truncate(&name, namelen + strlen("diff.."));
+ git_buf_put(&name, "funcname", strlen("funcname"));
+ if ((error = git_config_get_multivar(
+ cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto done;
+ giterr_clear(); /* no diff.<driver>.funcname, so just continue */
+ }
+
+ /* if we found any patterns, set driver type to use correct callback */
+ if (git_array_size(drv->fn_patterns) > 0) {
+ drv->type = DIFF_DRIVER_PATTERNLIST;
+ found_driver = true;
+ }
+
+ git_buf_truncate(&name, namelen + strlen("diff.."));
+ git_buf_put(&name, "wordregex", strlen("wordregex"));
+ if ((error = git_config_get_string(&val, cfg, name.ptr)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ goto done;
+ giterr_clear(); /* no diff.<driver>.wordregex, so just continue */
+ } else if ((error = regcomp(&drv->word_pattern, val, REG_EXTENDED)) != 0) {
+ /* TODO: warning about bad regex instead of failure */
+ error = giterr_set_regex(&drv->word_pattern, error);
+ goto done;
+ } else {
+ found_driver = true;
+ }
+
+ /* TODO: look up diff.<driver>.algorithm to turn on minimal / patience
+ * diff in drv->other_flags
+ */
+
+ /* if no driver config found at all, fall back on AUTO driver */
+ if (!found_driver)
+ goto done;
+
+ /* store driver in registry */
+ git_strmap_insert(reg->drivers, drv->name, drv, error);
+ if (error < 0)
+ goto done;
+
+ *out = drv;
+
+done:
+ git_buf_free(&name);
+
+ if (!*out)
+ *out = &global_drivers[DIFF_DRIVER_AUTO];
+
+ if (drv && drv != *out)
+ git_diff_driver_free(drv);
+
+ return error;
+}
+
+int git_diff_driver_lookup(
+ git_diff_driver **out, git_repository *repo, const char *path)
+{
+ int error = 0;
+ const char *value;
+
+ assert(out);
+
+ if (!repo || !path || !strlen(path))
+ goto use_auto;
+
+ if ((error = git_attr_get(&value, repo, 0, path, "diff")) < 0)
+ return error;
+
+ if (GIT_ATTR_UNSPECIFIED(value))
+ /* just use the auto value */;
+ else if (GIT_ATTR_FALSE(value))
+ *out = &global_drivers[DIFF_DRIVER_BINARY];
+ else if (GIT_ATTR_TRUE(value))
+ *out = &global_drivers[DIFF_DRIVER_TEXT];
+
+ /* otherwise look for driver information in config and build driver */
+ else if ((error = git_diff_driver_load(out, repo, value)) < 0) {
+ if (error != GIT_ENOTFOUND)
+ return error;
+ else
+ giterr_clear();
+ }
+
+use_auto:
+ if (!*out)
+ *out = &global_drivers[DIFF_DRIVER_AUTO];
+
+ return 0;
+}
+
+void git_diff_driver_free(git_diff_driver *driver)
+{
+ size_t i;
+
+ if (!driver)
+ return;
+
+ for (i = 0; i < git_array_size(driver->fn_patterns); ++i)
+ regfree(git_array_get(driver->fn_patterns, i));
+ git_array_clear(driver->fn_patterns);
+
+ regfree(&driver->word_pattern);
+
+ git__free(driver);
+}
+
+void git_diff_driver_update_options(
+ uint32_t *option_flags, git_diff_driver *driver)
+{
+ if ((*option_flags & FORCE_DIFFABLE) == 0)
+ *option_flags |= driver->binary_flags;
+
+ *option_flags |= driver->other_flags;
+}
+
+int git_diff_driver_content_is_binary(
+ git_diff_driver *driver, const char *content, size_t content_len)
+{
+ const git_buf search = { (char *)content, 0, min(content_len, 4000) };
+
+ GIT_UNUSED(driver);
+
+ /* TODO: provide encoding / binary detection callbacks that can
+ * be UTF-8 aware, etc. For now, instead of trying to be smart,
+ * let's just use the simple NUL-byte detection that core git uses.
+ */
+
+ /* previously was: if (git_buf_text_is_binary(&search)) */
+ if (git_buf_text_contains_nul(&search))
+ return 1;
+
+ return 0;
+}
+
+static int diff_context_line__simple(
+ git_diff_driver *driver, const char *line, long line_len)
+{
+ GIT_UNUSED(driver);
+ GIT_UNUSED(line_len);
+ return (git__isalpha(*line) || *line == '_' || *line == '$');
+}
+
+static int diff_context_line__pattern_match(
+ git_diff_driver *driver, const char *line, long line_len)
+{
+ size_t i;
+
+ GIT_UNUSED(line_len);
+
+ for (i = 0; i < git_array_size(driver->fn_patterns); ++i) {
+ if (!regexec(git_array_get(driver->fn_patterns, i), line, 0, NULL, 0))
+ return true;
+ }
+
+ return false;
+}
+
+static long diff_context_find(
+ const char *line,
+ long line_len,
+ char *out,
+ long out_size,
+ void *payload)
+{
+ git_diff_find_context_payload *ctxt = payload;
+
+ if (git_buf_set(&ctxt->line, line, (size_t)line_len) < 0)
+ return -1;
+ git_buf_rtrim(&ctxt->line);
+
+ if (!ctxt->line.size)
+ return -1;
+
+ if (!ctxt->match_line ||
+ !ctxt->match_line(ctxt->driver, ctxt->line.ptr, ctxt->line.size))
+ return -1;
+
+ git_buf_truncate(&ctxt->line, (size_t)out_size);
+ git_buf_copy_cstr(out, (size_t)out_size, &ctxt->line);
+
+ return (long)ctxt->line.size;
+}
+
+void git_diff_find_context_init(
+ git_diff_find_context_fn *findfn_out,
+ git_diff_find_context_payload *payload_out,
+ git_diff_driver *driver)
+{
+ *findfn_out = driver ? diff_context_find : NULL;
+
+ memset(payload_out, 0, sizeof(*payload_out));
+ if (driver) {
+ payload_out->driver = driver;
+ payload_out->match_line = (driver->type == DIFF_DRIVER_PATTERNLIST) ?
+ diff_context_line__pattern_match : diff_context_line__simple;
+ git_buf_init(&payload_out->line, 0);
+ }
+}
+
+void git_diff_find_context_clear(git_diff_find_context_payload *payload)
+{
+ if (payload) {
+ git_buf_free(&payload->line);
+ payload->driver = NULL;
+ }
+}
+
diff --git a/src/diff_driver.h b/src/diff_driver.h
new file mode 100644
index 0000000..3db7df0
--- /dev/null
+++ b/src/diff_driver.h
@@ -0,0 +1,49 @@
+/*
+ * 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_driver_h__
+#define INCLUDE_diff_driver_h__
+
+#include "common.h"
+#include "buffer.h"
+
+typedef struct git_diff_driver_registry git_diff_driver_registry;
+
+git_diff_driver_registry *git_diff_driver_registry_new(void);
+void git_diff_driver_registry_free(git_diff_driver_registry *);
+
+typedef struct git_diff_driver git_diff_driver;
+
+int git_diff_driver_lookup(git_diff_driver **, git_repository *, const char *);
+void git_diff_driver_free(git_diff_driver *);
+
+/* diff option flags to force off and on for this driver */
+void git_diff_driver_update_options(uint32_t *option_flags, git_diff_driver *);
+
+/* returns -1 meaning "unknown", 0 meaning not binary, 1 meaning binary */
+int git_diff_driver_content_is_binary(
+ git_diff_driver *, const char *content, size_t content_len);
+
+typedef long (*git_diff_find_context_fn)(
+ const char *, long, char *, long, void *);
+
+typedef int (*git_diff_find_context_line)(
+ git_diff_driver *, const char *, long);
+
+typedef struct {
+ git_diff_driver *driver;
+ git_diff_find_context_line match_line;
+ git_buf line;
+} git_diff_find_context_payload;
+
+void git_diff_find_context_init(
+ git_diff_find_context_fn *findfn_out,
+ git_diff_find_context_payload *payload_out,
+ git_diff_driver *driver);
+
+void git_diff_find_context_clear(git_diff_find_context_payload *);
+
+#endif
diff --git a/src/diff_file.c b/src/diff_file.c
new file mode 100644
index 0000000..4fd1177
--- /dev/null
+++ b/src/diff_file.c
@@ -0,0 +1,441 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "common.h"
+#include "git2/blob.h"
+#include "git2/submodule.h"
+#include "diff.h"
+#include "diff_file.h"
+#include "odb.h"
+#include "fileops.h"
+#include "filter.h"
+
+#define DIFF_MAX_FILESIZE 0x20000000
+
+static bool diff_file_content_binary_by_size(git_diff_file_content *fc)
+{
+ /* if we have diff opts, check max_size vs file size */
+ if ((fc->file.flags & DIFF_FLAGS_KNOWN_BINARY) == 0 &&
+ fc->opts_max_size > 0 &&
+ fc->file.size > fc->opts_max_size)
+ fc->file.flags |= GIT_DIFF_FLAG_BINARY;
+
+ return ((fc->file.flags & GIT_DIFF_FLAG_BINARY) != 0);
+}
+
+static void diff_file_content_binary_by_content(git_diff_file_content *fc)
+{
+ if ((fc->file.flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
+ return;
+
+ switch (git_diff_driver_content_is_binary(
+ fc->driver, fc->map.data, fc->map.len)) {
+ case 0: fc->file.flags |= GIT_DIFF_FLAG_NOT_BINARY; break;
+ case 1: fc->file.flags |= GIT_DIFF_FLAG_BINARY; break;
+ default: break;
+ }
+}
+
+static int diff_file_content_init_common(
+ git_diff_file_content *fc, const git_diff_options *opts)
+{
+ fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL;
+
+ if (opts && opts->max_size >= 0)
+ fc->opts_max_size = opts->max_size ?
+ opts->max_size : DIFF_MAX_FILESIZE;
+
+ if (!fc->driver) {
+ if (git_diff_driver_lookup(&fc->driver, fc->repo, "") < 0)
+ return -1;
+ fc->src = GIT_ITERATOR_TYPE_TREE;
+ }
+
+ /* give driver a chance to modify options */
+ git_diff_driver_update_options(&fc->opts_flags, fc->driver);
+
+ /* make sure file is conceivable mmap-able */
+ if ((git_off_t)((size_t)fc->file.size) != fc->file.size)
+ fc->file.flags |= GIT_DIFF_FLAG_BINARY;
+ /* check if user is forcing text diff the file */
+ else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
+ fc->file.flags &= ~GIT_DIFF_FLAG_BINARY;
+ fc->file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
+ }
+ /* check if user is forcing binary diff the file */
+ else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) {
+ fc->file.flags &= ~GIT_DIFF_FLAG_NOT_BINARY;
+ fc->file.flags |= GIT_DIFF_FLAG_BINARY;
+ }
+
+ diff_file_content_binary_by_size(fc);
+
+ if ((fc->file.flags & GIT_DIFF_FLAG__NO_DATA) != 0) {
+ fc->file.flags |= GIT_DIFF_FLAG__LOADED;
+ fc->map.len = 0;
+ fc->map.data = "";
+ }
+
+ if ((fc->file.flags & GIT_DIFF_FLAG__LOADED) != 0)
+ diff_file_content_binary_by_content(fc);
+
+ return 0;
+}
+
+int git_diff_file_content__init_from_diff(
+ git_diff_file_content *fc,
+ git_diff_list *diff,
+ size_t delta_index,
+ bool use_old)
+{
+ git_diff_delta *delta = git_vector_get(&diff->deltas, delta_index);
+ git_diff_file *file = use_old ? &delta->old_file : &delta->new_file;
+ bool has_data = true;
+
+ memset(fc, 0, sizeof(*fc));
+ fc->repo = diff->repo;
+ fc->src = use_old ? diff->old_src : diff->new_src;
+ memcpy(&fc->file, file, sizeof(fc->file));
+
+ if (git_diff_driver_lookup(&fc->driver, fc->repo, file->path) < 0)
+ return -1;
+
+ switch (delta->status) {
+ case GIT_DELTA_ADDED:
+ has_data = !use_old; break;
+ case GIT_DELTA_DELETED:
+ has_data = use_old; break;
+ case GIT_DELTA_UNTRACKED:
+ has_data = !use_old &&
+ (diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) != 0;
+ break;
+ case GIT_DELTA_MODIFIED:
+ case GIT_DELTA_COPIED:
+ case GIT_DELTA_RENAMED:
+ break;
+ default:
+ has_data = false;
+ break;
+ }
+
+ if (!has_data)
+ fc->file.flags |= GIT_DIFF_FLAG__NO_DATA;
+
+ return diff_file_content_init_common(fc, &diff->opts);
+}
+
+int git_diff_file_content__init_from_blob(
+ git_diff_file_content *fc,
+ git_repository *repo,
+ const git_diff_options *opts,
+ const git_blob *blob)
+{
+ memset(fc, 0, sizeof(*fc));
+ fc->repo = repo;
+ fc->blob = blob;
+
+ if (!blob) {
+ fc->file.flags |= GIT_DIFF_FLAG__NO_DATA;
+ } else {
+ fc->file.flags |= GIT_DIFF_FLAG__LOADED | GIT_DIFF_FLAG_VALID_OID;
+ fc->file.size = git_blob_rawsize(blob);
+ fc->file.mode = 0644;
+ git_oid_cpy(&fc->file.oid, git_blob_id(blob));
+
+ fc->map.len = (size_t)fc->file.size;
+ fc->map.data = (char *)git_blob_rawcontent(blob);
+ }
+
+ return diff_file_content_init_common(fc, opts);
+}
+
+int git_diff_file_content__init_from_raw(
+ git_diff_file_content *fc,
+ git_repository *repo,
+ const git_diff_options *opts,
+ const char *buf,
+ size_t buflen)
+{
+ memset(fc, 0, sizeof(*fc));
+ fc->repo = repo;
+
+ if (!buf) {
+ fc->file.flags |= GIT_DIFF_FLAG__NO_DATA;
+ } else {
+ fc->file.flags |= GIT_DIFF_FLAG__LOADED | GIT_DIFF_FLAG_VALID_OID;
+ fc->file.size = buflen;
+ fc->file.mode = 0644;
+ git_odb_hash(&fc->file.oid, buf, buflen, GIT_OBJ_BLOB);
+
+ fc->map.len = buflen;
+ fc->map.data = (char *)buf;
+ }
+
+ return diff_file_content_init_common(fc, opts);
+}
+
+static int diff_file_content_commit_to_str(
+ git_diff_file_content *fc, bool check_status)
+{
+ char oid[GIT_OID_HEXSZ+1];
+ git_buf content = GIT_BUF_INIT;
+ const char *status = "";
+
+ if (check_status) {
+ int error = 0;
+ git_submodule *sm = NULL;
+ unsigned int sm_status = 0;
+ const git_oid *sm_head;
+
+ if ((error = git_submodule_lookup(&sm, fc->repo, fc->file.path)) < 0 ||
+ (error = git_submodule_status(&sm_status, sm)) < 0) {
+ /* GIT_EEXISTS means a "submodule" that has not been git added */
+ if (error == GIT_EEXISTS)
+ error = 0;
+ return error;
+ }
+
+ /* update OID if we didn't have it previously */
+ if ((fc->file.flags & GIT_DIFF_FLAG_VALID_OID) == 0 &&
+ ((sm_head = git_submodule_wd_id(sm)) != NULL ||
+ (sm_head = git_submodule_head_id(sm)) != NULL))
+ {
+ git_oid_cpy(&fc->file.oid, sm_head);
+ fc->file.flags |= GIT_DIFF_FLAG_VALID_OID;
+ }
+
+ if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
+ status = "-dirty";
+ }
+
+ git_oid_tostr(oid, sizeof(oid), &fc->file.oid);
+ if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
+ return -1;
+
+ fc->map.len = git_buf_len(&content);
+ fc->map.data = git_buf_detach(&content);
+ fc->file.flags |= GIT_DIFF_FLAG__FREE_DATA;
+
+ return 0;
+}
+
+static int diff_file_content_load_blob(git_diff_file_content *fc)
+{
+ int error = 0;
+ git_odb_object *odb_obj = NULL;
+
+ if (git_oid_iszero(&fc->file.oid))
+ return 0;
+
+ if (fc->file.mode == GIT_FILEMODE_COMMIT)
+ return diff_file_content_commit_to_str(fc, false);
+
+ /* if we don't know size, try to peek at object header first */
+ if (!fc->file.size) {
+ git_odb *odb;
+ size_t len;
+ git_otype type;
+
+ if (!(error = git_repository_odb__weakptr(&odb, fc->repo))) {
+ error = git_odb__read_header_or_object(
+ &odb_obj, &len, &type, odb, &fc->file.oid);
+ git_odb_free(odb);
+ }
+ if (error)
+ return error;
+
+ fc->file.size = len;
+ }
+
+ if (diff_file_content_binary_by_size(fc))
+ return 0;
+
+ if (odb_obj != NULL) {
+ error = git_object__from_odb_object(
+ (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB);
+ git_odb_object_free(odb_obj);
+ } else {
+ error = git_blob_lookup(
+ (git_blob **)&fc->blob, fc->repo, &fc->file.oid);
+ }
+
+ if (!error) {
+ fc->file.flags |= GIT_DIFF_FLAG__FREE_BLOB;
+ fc->map.data = (void *)git_blob_rawcontent(fc->blob);
+ fc->map.len = (size_t)git_blob_rawsize(fc->blob);
+ }
+
+ return error;
+}
+
+static int diff_file_content_load_workdir_symlink(
+ git_diff_file_content *fc, git_buf *path)
+{
+ ssize_t alloc_len, read_len;
+
+ /* link path on disk could be UTF-16, so prepare a buffer that is
+ * big enough to handle some UTF-8 data expansion
+ */
+ alloc_len = (ssize_t)(fc->file.size * 2) + 1;
+
+ fc->map.data = git__calloc(alloc_len, sizeof(char));
+ GITERR_CHECK_ALLOC(fc->map.data);
+
+ fc->file.flags |= GIT_DIFF_FLAG__FREE_DATA;
+
+ read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
+ if (read_len < 0) {
+ giterr_set(GITERR_OS, "Failed to read symlink '%s'", fc->file.path);
+ return -1;
+ }
+
+ fc->map.len = read_len;
+ return 0;
+}
+
+static int diff_file_content_load_workdir_file(
+ git_diff_file_content *fc, git_buf *path)
+{
+ int error = 0;
+ git_vector filters = GIT_VECTOR_INIT;
+ git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
+ git_file fd = git_futils_open_ro(git_buf_cstr(path));
+
+ if (fd < 0)
+ return fd;
+
+ if (!fc->file.size &&
+ !(fc->file.size = git_futils_filesize(fd)))
+ goto cleanup;
+
+ if (diff_file_content_binary_by_size(fc))
+ goto cleanup;
+
+ if ((error = git_filters_load(
+ &filters, fc->repo, fc->file.path, GIT_FILTER_TO_ODB)) < 0)
+ goto cleanup;
+ /* error >= is a filter count */
+
+ if (error == 0) {
+ if (!(error = git_futils_mmap_ro(
+ &fc->map, fd, 0, (size_t)fc->file.size)))
+ fc->file.flags |= GIT_DIFF_FLAG__UNMAP_DATA;
+ else /* fall through to try readbuffer below */
+ giterr_clear();
+ }
+
+ if (error != 0) {
+ error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file.size);
+ if (error < 0)
+ goto cleanup;
+
+ if (!filters.length)
+ git_buf_swap(&filtered, &raw);
+ else
+ error = git_filters_apply(&filtered, &raw, &filters);
+
+ if (!error) {
+ fc->map.len = git_buf_len(&filtered);
+ fc->map.data = git_buf_detach(&filtered);
+ fc->file.flags |= GIT_DIFF_FLAG__FREE_DATA;
+ }
+
+ git_buf_free(&raw);
+ git_buf_free(&filtered);
+ }
+
+cleanup:
+ git_filters_free(&filters);
+ p_close(fd);
+
+ return error;
+}
+
+static int diff_file_content_load_workdir(git_diff_file_content *fc)
+{
+ int error = 0;
+ git_buf path = GIT_BUF_INIT;
+
+ if (fc->file.mode == GIT_FILEMODE_COMMIT)
+ return diff_file_content_commit_to_str(fc, true);
+
+ if (fc->file.mode == GIT_FILEMODE_TREE)
+ return 0;
+
+ if (git_buf_joinpath(
+ &path, git_repository_workdir(fc->repo), fc->file.path) < 0)
+ return -1;
+
+ if (S_ISLNK(fc->file.mode))
+ error = diff_file_content_load_workdir_symlink(fc, &path);
+ else
+ error = diff_file_content_load_workdir_file(fc, &path);
+
+ /* once data is loaded, update OID if we didn't have it previously */
+ if (!error && (fc->file.flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
+ error = git_odb_hash(
+ &fc->file.oid, fc->map.data, fc->map.len, GIT_OBJ_BLOB);
+ fc->file.flags |= GIT_DIFF_FLAG_VALID_OID;
+ }
+
+ git_buf_free(&path);
+ return error;
+}
+
+int git_diff_file_content__load(git_diff_file_content *fc)
+{
+ int error = 0;
+
+ if ((fc->file.flags & GIT_DIFF_FLAG__LOADED) != 0)
+ return 0;
+
+ if (fc->file.flags & GIT_DIFF_FLAG_BINARY)
+ return 0;
+
+ if (fc->src == GIT_ITERATOR_TYPE_WORKDIR)
+ error = diff_file_content_load_workdir(fc);
+ else
+ error = diff_file_content_load_blob(fc);
+ if (error)
+ return error;
+
+ fc->file.flags |= GIT_DIFF_FLAG__LOADED;
+
+ diff_file_content_binary_by_content(fc);
+
+ return 0;
+}
+
+void git_diff_file_content__unload(git_diff_file_content *fc)
+{
+ if (fc->file.flags & GIT_DIFF_FLAG__FREE_DATA) {
+ git__free(fc->map.data);
+ fc->map.data = "";
+ fc->map.len = 0;
+ fc->file.flags &= ~GIT_DIFF_FLAG__FREE_DATA;
+ }
+ else if (fc->file.flags & GIT_DIFF_FLAG__UNMAP_DATA) {
+ git_futils_mmap_free(&fc->map);
+ fc->map.data = "";
+ fc->map.len = 0;
+ fc->file.flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
+ }
+
+ if (fc->file.flags & GIT_DIFF_FLAG__FREE_BLOB) {
+ git_blob_free((git_blob *)fc->blob);
+ fc->blob = NULL;
+ fc->file.flags &= ~GIT_DIFF_FLAG__FREE_BLOB;
+ }
+
+ fc->file.flags &= ~GIT_DIFF_FLAG__LOADED;
+}
+
+void git_diff_file_content__clear(git_diff_file_content *fc)
+{
+ git_diff_file_content__unload(fc);
+
+ /* for now, nothing else to do */
+}
diff --git a/src/diff_file.h b/src/diff_file.h
new file mode 100644
index 0000000..afad851
--- /dev/null
+++ b/src/diff_file.h
@@ -0,0 +1,55 @@
+/*
+ * 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_file_h__
+#define INCLUDE_diff_file_h__
+
+#include "common.h"
+#include "diff.h"
+#include "diff_driver.h"
+#include "map.h"
+
+/* expanded information for one side of a delta */
+typedef struct {
+ git_repository *repo;
+ git_diff_file file;
+ git_diff_driver *driver;
+ uint32_t opts_flags;
+ git_off_t opts_max_size;
+ git_iterator_type_t src;
+ const git_blob *blob;
+ git_map map;
+} git_diff_file_content;
+
+extern int git_diff_file_content__init_from_diff(
+ git_diff_file_content *fc,
+ git_diff_list *diff,
+ size_t delta_index,
+ bool use_old);
+
+extern int git_diff_file_content__init_from_blob(
+ git_diff_file_content *fc,
+ git_repository *repo,
+ const git_diff_options *opts,
+ const git_blob *blob);
+
+extern int git_diff_file_content__init_from_raw(
+ git_diff_file_content *fc,
+ git_repository *repo,
+ const git_diff_options *opts,
+ const char *buf,
+ size_t buflen);
+
+/* this loads the blob/file-on-disk as needed */
+extern int git_diff_file_content__load(git_diff_file_content *fc);
+
+/* this releases the blob/file-in-memory */
+extern void git_diff_file_content__unload(git_diff_file_content *fc);
+
+/* this unloads and also releases any other resources */
+extern void git_diff_file_content__clear(git_diff_file_content *fc);
+
+#endif
diff --git a/src/diff_output.c b/src/diff_output.c
deleted file mode 100644
index 8dd110c..0000000
--- a/src/diff_output.c
+++ /dev/null
@@ -1,1895 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#include "common.h"
-#include "git2/attr.h"
-#include "git2/oid.h"
-#include "git2/submodule.h"
-#include "diff_output.h"
-#include <ctype.h>
-#include "fileops.h"
-#include "filter.h"
-#include "buf_text.h"
-
-static int read_next_int(const char **str, int *value)
-{
- const char *scan = *str;
- int v = 0, digits = 0;
- /* find next digit */
- for (scan = *str; *scan && !isdigit(*scan); scan++);
- /* parse next number */
- for (; isdigit(*scan); scan++, digits++)
- v = (v * 10) + (*scan - '0');
- *str = scan;
- *value = v;
- return (digits > 0) ? 0 : -1;
-}
-
-static int parse_hunk_header(git_diff_range *range, const char *header)
-{
- /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
- if (*header != '@')
- return -1;
- if (read_next_int(&header, &range->old_start) < 0)
- return -1;
- if (*header == ',') {
- if (read_next_int(&header, &range->old_lines) < 0)
- return -1;
- } else
- range->old_lines = 1;
- if (read_next_int(&header, &range->new_start) < 0)
- return -1;
- if (*header == ',') {
- if (read_next_int(&header, &range->new_lines) < 0)
- return -1;
- } else
- range->new_lines = 1;
- if (range->old_start < 0 || range->new_start < 0)
- return -1;
-
- return 0;
-}
-
-#define KNOWN_BINARY_FLAGS (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
-#define NOT_BINARY_FLAGS (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
-
-static int update_file_is_binary_by_attr(
- git_repository *repo, git_diff_file *file)
-{
- const char *value;
-
- /* because of blob diffs, cannot assume path is set */
- if (!file->path || !strlen(file->path))
- return 0;
-
- if (git_attr_get(&value, repo, 0, file->path, "diff") < 0)
- return -1;
-
- if (GIT_ATTR_FALSE(value))
- file->flags |= GIT_DIFF_FLAG_BINARY;
- else if (GIT_ATTR_TRUE(value))
- file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
- /* otherwise leave file->flags alone */
-
- return 0;
-}
-
-static void update_delta_is_binary(git_diff_delta *delta)
-{
- if ((delta->old_file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
- (delta->new_file.flags & GIT_DIFF_FLAG_BINARY) != 0)
- delta->flags |= GIT_DIFF_FLAG_BINARY;
-
- else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 &&
- (delta->new_file.flags & NOT_BINARY_FLAGS) != 0)
- delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
-
- /* otherwise leave delta->flags binary value untouched */
-}
-
-/* returns if we forced binary setting (and no further checks needed) */
-static bool diff_delta_is_binary_forced(
- diff_context *ctxt,
- git_diff_delta *delta)
-{
- /* return true if binary-ness has already been settled */
- if ((delta->flags & KNOWN_BINARY_FLAGS) != 0)
- return true;
-
- /* make sure files are conceivably mmap-able */
- if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
- (git_off_t)((size_t)delta->new_file.size) != delta->new_file.size) {
-
- delta->old_file.flags |= GIT_DIFF_FLAG_BINARY;
- delta->new_file.flags |= GIT_DIFF_FLAG_BINARY;
- delta->flags |= GIT_DIFF_FLAG_BINARY;
- return true;
- }
-
- /* check if user is forcing us to text diff these files */
- if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) {
- delta->old_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
- delta->new_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
- delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
- return true;
- }
-
- return false;
-}
-
-static int diff_delta_is_binary_by_attr(
- diff_context *ctxt, git_diff_patch *patch)
-{
- int error = 0, mirror_new;
- git_diff_delta *delta = patch->delta;
-
- if (diff_delta_is_binary_forced(ctxt, delta))
- return 0;
-
- /* check diff attribute +, -, or 0 */
- if (update_file_is_binary_by_attr(ctxt->repo, &delta->old_file) < 0)
- return -1;
-
- mirror_new = (delta->new_file.path == delta->old_file.path ||
- ctxt->diff->strcomp(delta->new_file.path, delta->old_file.path) == 0);
- if (mirror_new)
- delta->new_file.flags |= (delta->old_file.flags & KNOWN_BINARY_FLAGS);
- else
- error = update_file_is_binary_by_attr(ctxt->repo, &delta->new_file);
-
- update_delta_is_binary(delta);
-
- return error;
-}
-
-static int diff_delta_is_binary_by_content(
- diff_context *ctxt,
- git_diff_delta *delta,
- git_diff_file *file,
- const git_map *map)
-{
- const git_buf search = { map->data, 0, min(map->len, 4000) };
-
- if (diff_delta_is_binary_forced(ctxt, delta))
- return 0;
-
- /* TODO: provide encoding / binary detection callbacks that can
- * be UTF-8 aware, etc. For now, instead of trying to be smart,
- * let's just use the simple NUL-byte detection that core git uses.
- */
-
- /* previously was: if (git_buf_text_is_binary(&search)) */
- if (git_buf_text_contains_nul(&search))
- file->flags |= GIT_DIFF_FLAG_BINARY;
- else
- file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
-
- update_delta_is_binary(delta);
-
- return 0;
-}
-
-static int diff_delta_is_binary_by_size(
- diff_context *ctxt, git_diff_delta *delta, git_diff_file *file)
-{
- git_off_t threshold = MAX_DIFF_FILESIZE;
-
- if ((file->flags & KNOWN_BINARY_FLAGS) != 0)
- return 0;
-
- if (ctxt && ctxt->opts) {
- if (ctxt->opts->max_size < 0)
- return 0;
-
- if (ctxt->opts->max_size > 0)
- threshold = ctxt->opts->max_size;
- }
-
- if (file->size > threshold)
- file->flags |= GIT_DIFF_FLAG_BINARY;
-
- update_delta_is_binary(delta);
-
- return 0;
-}
-
-static void setup_xdiff_options(
- const git_diff_options *opts, xdemitconf_t *cfg, xpparam_t *param)
-{
- memset(cfg, 0, sizeof(xdemitconf_t));
- memset(param, 0, sizeof(xpparam_t));
-
- cfg->ctxlen =
- (!opts) ? 3 : opts->context_lines;
- cfg->interhunkctxlen =
- (!opts) ? 0 : opts->interhunk_lines;
-
- if (!opts)
- return;
-
- if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE)
- param->flags |= XDF_WHITESPACE_FLAGS;
- if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
- param->flags |= XDF_IGNORE_WHITESPACE_CHANGE;
- if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
- param->flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
-}
-
-
-static int get_blob_content(
- diff_context *ctxt,
- git_diff_delta *delta,
- git_diff_file *file,
- git_map *map,
- git_blob **blob)
-{
- int error;
- git_odb_object *odb_obj = NULL;
-
- if (git_oid_iszero(&file->oid))
- return 0;
-
- if (file->mode == GIT_FILEMODE_COMMIT) {
- char oidstr[GIT_OID_HEXSZ+1];
- git_buf content = GIT_BUF_INIT;
-
- git_oid_tostr(oidstr, sizeof(oidstr), &file->oid);
- git_buf_printf(&content, "Subproject commit %s\n", oidstr);
-
- map->data = git_buf_detach(&content);
- map->len = strlen(map->data);
-
- file->flags |= GIT_DIFF_FLAG__FREE_DATA;
- return 0;
- }
-
- if (!file->size) {
- git_odb *odb;
- size_t len;
- git_otype type;
-
- /* peek at object header to avoid loading if too large */
- if ((error = git_repository_odb__weakptr(&odb, ctxt->repo)) < 0 ||
- (error = git_odb__read_header_or_object(
- &odb_obj, &len, &type, odb, &file->oid)) < 0)
- return error;
-
- assert(type == GIT_OBJ_BLOB);
-
- file->size = len;
- }
-
- /* if blob is too large to diff, mark as binary */
- if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0)
- return error;
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- return 0;
-
- if (odb_obj != NULL) {
- error = git_object__from_odb_object(
- (git_object **)blob, ctxt->repo, odb_obj, GIT_OBJ_BLOB);
- git_odb_object_free(odb_obj);
- } else
- error = git_blob_lookup(blob, ctxt->repo, &file->oid);
-
- if (error)
- return error;
-
- map->data = (void *)git_blob_rawcontent(*blob);
- map->len = (size_t)git_blob_rawsize(*blob);
-
- return diff_delta_is_binary_by_content(ctxt, delta, file, map);
-}
-
-static int get_workdir_sm_content(
- diff_context *ctxt,
- git_diff_file *file,
- git_map *map)
-{
- int error = 0;
- git_buf content = GIT_BUF_INIT;
- git_submodule* sm = NULL;
- unsigned int sm_status = 0;
- const char* sm_status_text = "";
- char oidstr[GIT_OID_HEXSZ+1];
-
- if ((error = git_submodule_lookup(&sm, ctxt->repo, file->path)) < 0 ||
- (error = git_submodule_status(&sm_status, sm)) < 0) {
-
- /* GIT_EEXISTS means a "submodule" that has not been git added */
- if (error == GIT_EEXISTS)
- error = 0;
- return error;
- }
-
- /* update OID if we didn't have it previously */
- if ((file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
- const git_oid* sm_head;
-
- if ((sm_head = git_submodule_wd_id(sm)) != NULL ||
- (sm_head = git_submodule_head_id(sm)) != NULL) {
-
- git_oid_cpy(&file->oid, sm_head);
- file->flags |= GIT_DIFF_FLAG_VALID_OID;
- }
- }
-
- git_oid_tostr(oidstr, sizeof(oidstr), &file->oid);
-
- if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
- sm_status_text = "-dirty";
-
- git_buf_printf(
- &content, "Subproject commit %s%s\n", oidstr, sm_status_text);
-
- map->data = git_buf_detach(&content);
- map->len = strlen(map->data);
-
- file->flags |= GIT_DIFF_FLAG__FREE_DATA;
-
- return 0;
-}
-
-static int get_filtered(
- git_map *map, git_file fd, git_diff_file *file, git_vector *filters)
-{
- int error;
- git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
-
- if ((error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) < 0)
- return error;
-
- if (!filters->length)
- git_buf_swap(&filtered, &raw);
- else
- error = git_filters_apply(&filtered, &raw, filters);
-
- if (!error) {
- map->len = git_buf_len(&filtered);
- map->data = git_buf_detach(&filtered);
-
- file->flags |= GIT_DIFF_FLAG__FREE_DATA;
- }
-
- git_buf_free(&raw);
- git_buf_free(&filtered);
-
- return error;
-}
-
-static int get_workdir_content(
- diff_context *ctxt,
- git_diff_delta *delta,
- git_diff_file *file,
- git_map *map)
-{
- int error = 0;
- git_buf path = GIT_BUF_INIT;
- const char *wd = git_repository_workdir(ctxt->repo);
-
- if (S_ISGITLINK(file->mode))
- return get_workdir_sm_content(ctxt, file, map);
-
- if (S_ISDIR(file->mode))
- return 0;
-
- if (git_buf_joinpath(&path, wd, file->path) < 0)
- return -1;
-
- if (S_ISLNK(file->mode)) {
- ssize_t alloc_len, read_len;
-
- file->flags |= GIT_DIFF_FLAG__FREE_DATA;
- file->flags |= GIT_DIFF_FLAG_BINARY;
-
- /* link path on disk could be UTF-16, so prepare a buffer that is
- * big enough to handle some UTF-8 data expansion
- */
- alloc_len = (ssize_t)(file->size * 2) + 1;
-
- map->data = git__malloc(alloc_len);
- GITERR_CHECK_ALLOC(map->data);
-
- read_len = p_readlink(path.ptr, map->data, alloc_len);
- if (read_len < 0) {
- giterr_set(GITERR_OS, "Failed to read symlink '%s'", file->path);
- error = -1;
- goto cleanup;
- }
-
- map->len = read_len;
- }
- else {
- git_file fd = git_futils_open_ro(path.ptr);
- git_vector filters = GIT_VECTOR_INIT;
-
- if (fd < 0) {
- error = fd;
- goto cleanup;
- }
-
- if (!file->size && !(file->size = git_futils_filesize(fd)))
- goto close_and_cleanup;
-
- if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
- (delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto close_and_cleanup;
-
- error = git_filters_load(
- &filters, ctxt->repo, file->path, GIT_FILTER_TO_ODB);
- if (error < 0)
- goto close_and_cleanup;
-
- if (error == 0) { /* note: git_filters_load returns filter count */
- error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
- if (!error)
- file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
- }
- if (error != 0)
- error = get_filtered(map, fd, file, &filters);
-
-close_and_cleanup:
- git_filters_free(&filters);
- p_close(fd);
- }
-
- /* once data is loaded, update OID if we didn't have it previously */
- if (!error && (file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
- error = git_odb_hash(
- &file->oid, map->data, map->len, GIT_OBJ_BLOB);
- if (!error)
- file->flags |= GIT_DIFF_FLAG_VALID_OID;
- }
-
- if (!error)
- error = diff_delta_is_binary_by_content(ctxt, delta, file, map);
-
-cleanup:
- git_buf_free(&path);
- return error;
-}
-
-static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
-{
- if (blob != NULL)
- git_blob_free(blob);
-
- if (file->flags & GIT_DIFF_FLAG__FREE_DATA) {
- git__free(map->data);
- map->data = "";
- map->len = 0;
- file->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
- }
- else if (file->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
- git_futils_mmap_free(map);
- map->data = "";
- map->len = 0;
- file->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
- }
-}
-
-
-static int diff_context_init(
- diff_context *ctxt,
- git_diff_list *diff,
- git_repository *repo,
- const git_diff_options *opts,
- git_diff_file_cb file_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_data_cb data_cb,
- void *payload)
-{
- memset(ctxt, 0, sizeof(diff_context));
-
- if (!repo && diff)
- repo = diff->repo;
-
- if (!opts && diff)
- opts = &diff->opts;
-
- ctxt->repo = repo;
- ctxt->diff = diff;
- ctxt->opts = opts;
- ctxt->file_cb = file_cb;
- ctxt->hunk_cb = hunk_cb;
- ctxt->data_cb = data_cb;
- ctxt->payload = payload;
- ctxt->error = 0;
-
- setup_xdiff_options(ctxt->opts, &ctxt->xdiff_config, &ctxt->xdiff_params);
-
- return 0;
-}
-
-static int diff_delta_file_callback(
- diff_context *ctxt, git_diff_delta *delta, size_t idx)
-{
- float progress;
-
- if (!ctxt->file_cb)
- return 0;
-
- progress = ctxt->diff ? ((float)idx / ctxt->diff->deltas.length) : 1.0f;
-
- if (ctxt->file_cb(delta, progress, ctxt->payload) != 0)
- ctxt->error = GIT_EUSER;
-
- return ctxt->error;
-}
-
-static void diff_patch_init(
- diff_context *ctxt, git_diff_patch *patch)
-{
- memset(patch, 0, sizeof(git_diff_patch));
-
- patch->diff = ctxt->diff;
- patch->ctxt = ctxt;
-
- if (patch->diff) {
- patch->old_src = patch->diff->old_src;
- patch->new_src = patch->diff->new_src;
- } else {
- patch->old_src = patch->new_src = GIT_ITERATOR_TYPE_TREE;
- }
-}
-
-static git_diff_patch *diff_patch_alloc(
- diff_context *ctxt, git_diff_delta *delta)
-{
- git_diff_patch *patch = git__malloc(sizeof(git_diff_patch));
- if (!patch)
- return NULL;
-
- diff_patch_init(ctxt, patch);
-
- git_diff_list_addref(patch->diff);
-
- GIT_REFCOUNT_INC(patch);
-
- patch->delta = delta;
- patch->flags = GIT_DIFF_PATCH_ALLOCATED;
-
- return patch;
-}
-
-static int diff_patch_load(
- diff_context *ctxt, git_diff_patch *patch)
-{
- int error = 0;
- git_diff_delta *delta = patch->delta;
- bool check_if_unmodified = false;
-
- if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0)
- return 0;
-
- error = diff_delta_is_binary_by_attr(ctxt, patch);
-
- patch->old_data.data = "";
- patch->old_data.len = 0;
- patch->old_blob = NULL;
-
- patch->new_data.data = "";
- patch->new_data.len = 0;
- patch->new_blob = NULL;
-
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto cleanup;
-
- if (!ctxt->hunk_cb &&
- !ctxt->data_cb &&
- (ctxt->opts->flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0)
- goto cleanup;
-
- switch (delta->status) {
- case GIT_DELTA_ADDED:
- delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- break;
- case GIT_DELTA_DELETED:
- delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- break;
- case GIT_DELTA_MODIFIED:
- case GIT_DELTA_COPIED:
- case GIT_DELTA_RENAMED:
- break;
- case GIT_DELTA_UNTRACKED:
- delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)
- delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- break;
- default:
- delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
- break;
- }
-
-#define CHECK_UNMODIFIED (GIT_DIFF_FLAG__NO_DATA | GIT_DIFF_FLAG_VALID_OID)
-
- check_if_unmodified =
- (delta->old_file.flags & CHECK_UNMODIFIED) == 0 &&
- (delta->new_file.flags & CHECK_UNMODIFIED) == 0;
-
- /* Always try to load workdir content first, since it may need to be
- * filtered (and hence use 2x memory) and we want to minimize the max
- * memory footprint during diff.
- */
-
- if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
- patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = get_workdir_content(
- ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
- goto cleanup;
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto cleanup;
- }
-
- if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
- patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = get_workdir_content(
- ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
- goto cleanup;
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto cleanup;
- }
-
- if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
- patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = get_blob_content(
- ctxt, delta, &delta->old_file,
- &patch->old_data, &patch->old_blob)) < 0)
- goto cleanup;
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto cleanup;
- }
-
- if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
- patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = get_blob_content(
- ctxt, delta, &delta->new_file,
- &patch->new_data, &patch->new_blob)) < 0)
- goto cleanup;
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
- goto cleanup;
- }
-
- /* if we did not previously have the definitive oid, we may have
- * incorrect status and need to switch this to UNMODIFIED.
- */
- if (check_if_unmodified &&
- delta->old_file.mode == delta->new_file.mode &&
- !git_oid__cmp(&delta->old_file.oid, &delta->new_file.oid)) {
-
- delta->status = GIT_DELTA_UNMODIFIED;
-
- if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
- goto cleanup;
- }
-
-cleanup:
- if ((delta->flags & KNOWN_BINARY_FLAGS) == 0)
- update_delta_is_binary(delta);
-
- if (!error) {
- patch->flags |= GIT_DIFF_PATCH_LOADED;
-
- /* patch is diffable only for non-binary, modified files where at
- * least one side has data and there is actual change in the data
- */
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
- delta->status != GIT_DELTA_UNMODIFIED &&
- (patch->old_data.len || patch->new_data.len) &&
- (patch->old_data.len != patch->new_data.len ||
- !git_oid_equal(&delta->old_file.oid, &delta->new_file.oid)))
- patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
- }
-
- return error;
-}
-
-static int diff_patch_cb(void *priv, mmbuffer_t *bufs, int len)
-{
- git_diff_patch *patch = priv;
- diff_context *ctxt = patch->ctxt;
-
- if (len == 1) {
- ctxt->error = parse_hunk_header(&ctxt->range, bufs[0].ptr);
- if (ctxt->error < 0)
- return ctxt->error;
-
- if (ctxt->hunk_cb != NULL &&
- ctxt->hunk_cb(patch->delta, &ctxt->range,
- bufs[0].ptr, bufs[0].size, ctxt->payload))
- ctxt->error = GIT_EUSER;
- }
-
- if (len == 2 || len == 3) {
- /* expect " "/"-"/"+", then data */
- char origin =
- (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
- (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
- GIT_DIFF_LINE_CONTEXT;
-
- if (ctxt->data_cb != NULL &&
- ctxt->data_cb(patch->delta, &ctxt->range,
- origin, bufs[1].ptr, bufs[1].size, ctxt->payload))
- ctxt->error = GIT_EUSER;
- }
-
- if (len == 3 && !ctxt->error) {
- /* If we have a '+' and a third buf, then we have added a line
- * without a newline and the old code had one, so DEL_EOFNL.
- * If we have a '-' and a third buf, then we have removed a line
- * with out a newline but added a blank line, so ADD_EOFNL.
- */
- char origin =
- (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
- (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
- GIT_DIFF_LINE_CONTEXT_EOFNL;
-
- if (ctxt->data_cb != NULL &&
- ctxt->data_cb(patch->delta, &ctxt->range,
- origin, bufs[2].ptr, bufs[2].size, ctxt->payload))
- ctxt->error = GIT_EUSER;
- }
-
- return ctxt->error;
-}
-
-static int diff_patch_generate(
- diff_context *ctxt, git_diff_patch *patch)
-{
- int error = 0;
- xdemitcb_t xdiff_callback;
- mmfile_t old_xdiff_data, new_xdiff_data;
-
- if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
- return 0;
-
- if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0)
- if ((error = diff_patch_load(ctxt, patch)) < 0)
- return error;
-
- if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0)
- return 0;
-
- if (!ctxt->file_cb && !ctxt->hunk_cb)
- return 0;
-
- patch->ctxt = ctxt;
-
- memset(&xdiff_callback, 0, sizeof(xdiff_callback));
- xdiff_callback.outf = diff_patch_cb;
- xdiff_callback.priv = patch;
-
- old_xdiff_data.ptr = patch->old_data.data;
- old_xdiff_data.size = patch->old_data.len;
- new_xdiff_data.ptr = patch->new_data.data;
- new_xdiff_data.size = patch->new_data.len;
-
- xdl_diff(&old_xdiff_data, &new_xdiff_data,
- &ctxt->xdiff_params, &ctxt->xdiff_config, &xdiff_callback);
-
- error = ctxt->error;
-
- if (!error)
- patch->flags |= GIT_DIFF_PATCH_DIFFED;
-
- return error;
-}
-
-static void diff_patch_unload(git_diff_patch *patch)
-{
- if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0) {
- patch->flags = (patch->flags & ~GIT_DIFF_PATCH_DIFFED);
-
- patch->hunks_size = 0;
- patch->lines_size = 0;
- }
-
- if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0) {
- patch->flags = (patch->flags & ~GIT_DIFF_PATCH_LOADED);
-
- release_content(
- &patch->delta->old_file, &patch->old_data, patch->old_blob);
- release_content(
- &patch->delta->new_file, &patch->new_data, patch->new_blob);
- }
-}
-
-static void diff_patch_free(git_diff_patch *patch)
-{
- diff_patch_unload(patch);
-
- git__free(patch->lines);
- patch->lines = NULL;
- patch->lines_asize = 0;
-
- git__free(patch->hunks);
- patch->hunks = NULL;
- patch->hunks_asize = 0;
-
- if (!(patch->flags & GIT_DIFF_PATCH_ALLOCATED))
- return;
-
- patch->flags = 0;
-
- git_diff_list_free(patch->diff); /* decrements refcount */
-
- git__free(patch);
-}
-
-#define MAX_HUNK_STEP 128
-#define MIN_HUNK_STEP 8
-#define MAX_LINE_STEP 256
-#define MIN_LINE_STEP 8
-
-static int diff_patch_hunk_cb(
- const git_diff_delta *delta,
- const git_diff_range *range,
- const char *header,
- size_t header_len,
- void *payload)
-{
- git_diff_patch *patch = payload;
- diff_patch_hunk *hunk;
-
- GIT_UNUSED(delta);
-
- if (patch->hunks_size >= patch->hunks_asize) {
- size_t new_size;
- diff_patch_hunk *new_hunks;
-
- if (patch->hunks_asize > MAX_HUNK_STEP)
- new_size = patch->hunks_asize + MAX_HUNK_STEP;
- else
- new_size = patch->hunks_asize * 2;
- if (new_size < MIN_HUNK_STEP)
- new_size = MIN_HUNK_STEP;
-
- new_hunks = git__realloc(
- patch->hunks, new_size * sizeof(diff_patch_hunk));
- if (!new_hunks)
- return -1;
-
- patch->hunks = new_hunks;
- patch->hunks_asize = new_size;
- }
-
- hunk = &patch->hunks[patch->hunks_size++];
-
- memcpy(&hunk->range, range, sizeof(hunk->range));
-
- assert(header_len + 1 < sizeof(hunk->header));
- memcpy(&hunk->header, header, header_len);
- hunk->header[header_len] = '\0';
- hunk->header_len = header_len;
-
- hunk->line_start = patch->lines_size;
- hunk->line_count = 0;
-
- patch->oldno = range->old_start;
- patch->newno = range->new_start;
-
- return 0;
-}
-
-static int diff_patch_line_cb(
- const git_diff_delta *delta,
- const git_diff_range *range,
- char line_origin,
- const char *content,
- size_t content_len,
- void *payload)
-{
- git_diff_patch *patch = payload;
- diff_patch_hunk *hunk;
- diff_patch_line *line;
-
- GIT_UNUSED(delta);
- GIT_UNUSED(range);
-
- assert(patch->hunks_size > 0);
- assert(patch->hunks != NULL);
-
- hunk = &patch->hunks[patch->hunks_size - 1];
-
- if (patch->lines_size >= patch->lines_asize) {
- size_t new_size;
- diff_patch_line *new_lines;
-
- if (patch->lines_asize > MAX_LINE_STEP)
- new_size = patch->lines_asize + MAX_LINE_STEP;
- else
- new_size = patch->lines_asize * 2;
- if (new_size < MIN_LINE_STEP)
- new_size = MIN_LINE_STEP;
-
- new_lines = git__realloc(
- patch->lines, new_size * sizeof(diff_patch_line));
- if (!new_lines)
- return -1;
-
- patch->lines = new_lines;
- patch->lines_asize = new_size;
- }
-
- line = &patch->lines[patch->lines_size++];
-
- line->ptr = content;
- line->len = content_len;
- line->origin = line_origin;
-
- /* do some bookkeeping so we can provide old/new line numbers */
-
- for (line->lines = 0; content_len > 0; --content_len) {
- if (*content++ == '\n')
- ++line->lines;
- }
-
- switch (line_origin) {
- case GIT_DIFF_LINE_ADDITION:
- case GIT_DIFF_LINE_DEL_EOFNL:
- line->oldno = -1;
- line->newno = patch->newno;
- patch->newno += line->lines;
- break;
- case GIT_DIFF_LINE_DELETION:
- case GIT_DIFF_LINE_ADD_EOFNL:
- line->oldno = patch->oldno;
- line->newno = -1;
- patch->oldno += line->lines;
- break;
- default:
- line->oldno = patch->oldno;
- line->newno = patch->newno;
- patch->oldno += line->lines;
- patch->newno += line->lines;
- break;
- }
-
- hunk->line_count++;
-
- return 0;
-}
-
-static int diff_required(git_diff_list *diff, const char *action)
-{
- if (!diff) {
- giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
- return -1;
- }
-
- return 0;
-}
-
-int git_diff_foreach(
- git_diff_list *diff,
- git_diff_file_cb file_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_data_cb data_cb,
- void *payload)
-{
- int error = 0;
- diff_context ctxt;
- size_t idx;
- git_diff_patch patch;
-
- if (diff_required(diff, "git_diff_foreach") < 0)
- return -1;
-
- if (diff_context_init(
- &ctxt, diff, NULL, NULL, file_cb, hunk_cb, data_cb, payload) < 0)
- return -1;
-
- diff_patch_init(&ctxt, &patch);
-
- git_vector_foreach(&diff->deltas, idx, patch.delta) {
-
- /* check flags against patch status */
- if (git_diff_delta__should_skip(ctxt.opts, patch.delta))
- continue;
-
- if (!(error = diff_patch_load(&ctxt, &patch))) {
-
- /* invoke file callback */
- error = diff_delta_file_callback(&ctxt, patch.delta, idx);
-
- /* generate diffs and invoke hunk and line callbacks */
- if (!error)
- error = diff_patch_generate(&ctxt, &patch);
-
- diff_patch_unload(&patch);
- }
-
- if (error < 0)
- break;
- }
-
- if (error == GIT_EUSER)
- giterr_clear(); /* don't let error message leak */
-
- return error;
-}
-
-
-typedef struct {
- git_diff_list *diff;
- git_diff_data_cb print_cb;
- void *payload;
- git_buf *buf;
- int oid_strlen;
-} diff_print_info;
-
-static int diff_print_info_init(
- diff_print_info *pi,
- git_buf *out, git_diff_list *diff, git_diff_data_cb cb, void *payload)
-{
- assert(diff && diff->repo);
-
- pi->diff = diff;
- pi->print_cb = cb;
- pi->payload = payload;
- pi->buf = out;
-
- if (git_repository__cvar(&pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0)
- return -1;
-
- pi->oid_strlen += 1; /* for NUL byte */
-
- if (pi->oid_strlen < 2)
- pi->oid_strlen = 2;
- else if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
- pi->oid_strlen = GIT_OID_HEXSZ + 1;
-
- return 0;
-}
-
-static char pick_suffix(int mode)
-{
- if (S_ISDIR(mode))
- return '/';
- else if (mode & 0100) //-V536
- /* in git, modes are very regular, so we must have 0100755 mode */
- return '*';
- else
- return ' ';
-}
-
-char git_diff_status_char(git_delta_t status)
-{
- char code;
-
- switch (status) {
- case GIT_DELTA_ADDED: code = 'A'; break;
- case GIT_DELTA_DELETED: code = 'D'; break;
- case GIT_DELTA_MODIFIED: code = 'M'; break;
- case GIT_DELTA_RENAMED: code = 'R'; break;
- case GIT_DELTA_COPIED: code = 'C'; break;
- case GIT_DELTA_IGNORED: code = 'I'; break;
- case GIT_DELTA_UNTRACKED: code = '?'; break;
- default: code = ' '; break;
- }
-
- return code;
-}
-
-static int callback_error(void)
-{
- giterr_clear();
- return GIT_EUSER;
-}
-
-static int print_compact(
- const git_diff_delta *delta, float progress, void *data)
-{
- diff_print_info *pi = data;
- char old_suffix, new_suffix, code = git_diff_status_char(delta->status);
-
- GIT_UNUSED(progress);
-
- if (code == ' ')
- return 0;
-
- old_suffix = pick_suffix(delta->old_file.mode);
- new_suffix = pick_suffix(delta->new_file.mode);
-
- git_buf_clear(pi->buf);
-
- if (delta->old_file.path != delta->new_file.path &&
- pi->diff->strcomp(delta->old_file.path,delta->new_file.path) != 0)
- git_buf_printf(pi->buf, "%c\t%s%c -> %s%c\n", code,
- delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
- else if (delta->old_file.mode != delta->new_file.mode &&
- delta->old_file.mode != 0 && delta->new_file.mode != 0)
- git_buf_printf(pi->buf, "%c\t%s%c (%o -> %o)\n", code,
- delta->old_file.path, new_suffix, delta->old_file.mode, delta->new_file.mode);
- else if (old_suffix != ' ')
- git_buf_printf(pi->buf, "%c\t%s%c\n", code, delta->old_file.path, old_suffix);
- else
- git_buf_printf(pi->buf, "%c\t%s\n", code, delta->old_file.path);
-
- if (git_buf_oom(pi->buf))
- return -1;
-
- if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- return 0;
-}
-
-int git_diff_print_compact(
- git_diff_list *diff,
- git_diff_data_cb print_cb,
- void *payload)
-{
- int error;
- git_buf buf = GIT_BUF_INIT;
- diff_print_info pi;
-
- if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
- error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi);
-
- git_buf_free(&buf);
-
- return error;
-}
-
-static int print_raw(
- const git_diff_delta *delta, float progress, void *data)
-{
- diff_print_info *pi = data;
- char code = git_diff_status_char(delta->status);
- char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
-
- GIT_UNUSED(progress);
-
- if (code == ' ')
- return 0;
-
- git_buf_clear(pi->buf);
-
- git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid);
- git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid);
-
- git_buf_printf(
- pi->buf, ":%06o %06o %s... %s... %c",
- delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code);
-
- if (delta->similarity > 0)
- git_buf_printf(pi->buf, "%03u", delta->similarity);
-
- if (delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED)
- git_buf_printf(
- pi->buf, "\t%s %s\n", delta->old_file.path, delta->new_file.path);
- else
- git_buf_printf(
- pi->buf, "\t%s\n", delta->old_file.path ?
- delta->old_file.path : delta->new_file.path);
-
- if (git_buf_oom(pi->buf))
- return -1;
-
- if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- return 0;
-}
-
-int git_diff_print_raw(
- git_diff_list *diff,
- git_diff_data_cb print_cb,
- void *payload)
-{
- int error;
- git_buf buf = GIT_BUF_INIT;
- diff_print_info pi;
-
- if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
- error = git_diff_foreach(diff, print_raw, NULL, NULL, &pi);
-
- git_buf_free(&buf);
-
- return error;
-}
-
-static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta)
-{
- char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
-
- git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid);
- git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid);
-
- /* TODO: Match git diff more closely */
- if (delta->old_file.mode == delta->new_file.mode) {
- git_buf_printf(pi->buf, "index %s..%s %o\n",
- start_oid, end_oid, delta->old_file.mode);
- } else {
- if (delta->old_file.mode == 0) {
- git_buf_printf(pi->buf, "new file mode %o\n", delta->new_file.mode);
- } else if (delta->new_file.mode == 0) {
- git_buf_printf(pi->buf, "deleted file mode %o\n", delta->old_file.mode);
- } else {
- git_buf_printf(pi->buf, "old mode %o\n", delta->old_file.mode);
- git_buf_printf(pi->buf, "new mode %o\n", delta->new_file.mode);
- }
- git_buf_printf(pi->buf, "index %s..%s\n", start_oid, end_oid);
- }
-
- if (git_buf_oom(pi->buf))
- return -1;
-
- return 0;
-}
-
-static int print_patch_file(
- const git_diff_delta *delta, float progress, void *data)
-{
- diff_print_info *pi = data;
- const char *oldpfx = pi->diff->opts.old_prefix;
- const char *oldpath = delta->old_file.path;
- const char *newpfx = pi->diff->opts.new_prefix;
- const char *newpath = delta->new_file.path;
-
- GIT_UNUSED(progress);
-
- if (S_ISDIR(delta->new_file.mode) ||
- delta->status == GIT_DELTA_UNMODIFIED ||
- delta->status == GIT_DELTA_IGNORED ||
- (delta->status == GIT_DELTA_UNTRACKED &&
- (pi->diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0))
- return 0;
-
- if (!oldpfx)
- oldpfx = DIFF_OLD_PREFIX_DEFAULT;
-
- if (!newpfx)
- newpfx = DIFF_NEW_PREFIX_DEFAULT;
-
- git_buf_clear(pi->buf);
- git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
-
- if (print_oid_range(pi, delta) < 0)
- return -1;
-
- if (git_oid_iszero(&delta->old_file.oid)) {
- oldpfx = "";
- oldpath = "/dev/null";
- }
- if (git_oid_iszero(&delta->new_file.oid)) {
- newpfx = "";
- newpath = "/dev/null";
- }
-
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
- git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
- git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
- }
-
- if (git_buf_oom(pi->buf))
- return -1;
-
- if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
- return 0;
-
- git_buf_clear(pi->buf);
- git_buf_printf(
- pi->buf, "Binary files %s%s and %s%s differ\n",
- oldpfx, oldpath, newpfx, newpath);
- if (git_buf_oom(pi->buf))
- return -1;
-
- if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- return 0;
-}
-
-static int print_patch_hunk(
- const git_diff_delta *d,
- const git_diff_range *r,
- const char *header,
- size_t header_len,
- void *data)
-{
- diff_print_info *pi = data;
-
- if (S_ISDIR(d->new_file.mode))
- return 0;
-
- git_buf_clear(pi->buf);
- if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0)
- return -1;
-
- if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- return 0;
-}
-
-static int print_patch_line(
- const git_diff_delta *delta,
- const git_diff_range *range,
- char line_origin, /* GIT_DIFF_LINE value from above */
- const char *content,
- size_t content_len,
- void *data)
-{
- diff_print_info *pi = data;
-
- if (S_ISDIR(delta->new_file.mode))
- return 0;
-
- git_buf_clear(pi->buf);
-
- if (line_origin == GIT_DIFF_LINE_ADDITION ||
- line_origin == GIT_DIFF_LINE_DELETION ||
- line_origin == GIT_DIFF_LINE_CONTEXT)
- git_buf_printf(pi->buf, "%c%.*s", line_origin, (int)content_len, content);
- else if (content_len > 0)
- git_buf_printf(pi->buf, "%.*s", (int)content_len, content);
-
- if (git_buf_oom(pi->buf))
- return -1;
-
- if (pi->print_cb(delta, range, line_origin,
- git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
- return callback_error();
-
- return 0;
-}
-
-int git_diff_print_patch(
- git_diff_list *diff,
- git_diff_data_cb print_cb,
- void *payload)
-{
- int error;
- git_buf buf = GIT_BUF_INIT;
- diff_print_info pi;
-
- if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
- error = git_diff_foreach(
- diff, print_patch_file, print_patch_hunk, print_patch_line, &pi);
-
- git_buf_free(&buf);
-
- return error;
-}
-
-static void set_data_from_blob(
- const git_blob *blob, git_map *map, git_diff_file *file)
-{
- if (blob) {
- file->size = git_blob_rawsize(blob);
- git_oid_cpy(&file->oid, git_object_id((const git_object *)blob));
- file->mode = 0644;
-
- map->len = (size_t)file->size;
- map->data = (char *)git_blob_rawcontent(blob);
- } else {
- file->size = 0;
- file->flags |= GIT_DIFF_FLAG__NO_DATA;
-
- map->len = 0;
- map->data = "";
- }
-}
-
-static void set_data_from_buffer(
- const char *buffer, size_t buffer_len, git_map *map, git_diff_file *file)
-{
- file->size = (git_off_t)buffer_len;
- file->mode = 0644;
- map->len = buffer_len;
-
- if (!buffer) {
- file->flags |= GIT_DIFF_FLAG__NO_DATA;
- map->data = NULL;
- } else {
- map->data = (char *)buffer;
- git_odb_hash(&file->oid, buffer, buffer_len, GIT_OBJ_BLOB);
- }
-}
-
-typedef struct {
- diff_context ctxt;
- git_diff_delta delta;
- git_diff_patch patch;
-} diff_single_data;
-
-static int diff_single_init(
- diff_single_data *data,
- git_repository *repo,
- const git_diff_options *opts,
- git_diff_file_cb file_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_data_cb data_cb,
- void *payload)
-{
- GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
-
- memset(data, 0, sizeof(*data));
-
- if (diff_context_init(
- &data->ctxt, NULL, repo, opts,
- file_cb, hunk_cb, data_cb, payload) < 0)
- return -1;
-
- diff_patch_init(&data->ctxt, &data->patch);
-
- return 0;
-}
-
-static int diff_single_apply(diff_single_data *data)
-{
- int error;
- git_diff_delta *delta = &data->delta;
- bool has_old = ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
- bool has_new = ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
-
- /* finish setting up fake git_diff_delta record and loaded data */
-
- data->patch.delta = delta;
- delta->flags = delta->flags & ~KNOWN_BINARY_FLAGS;
-
- delta->status = has_new ?
- (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
- (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
-
- if (git_oid__cmp(&delta->new_file.oid, &delta->old_file.oid) == 0)
- delta->status = GIT_DELTA_UNMODIFIED;
-
- if ((error = diff_delta_is_binary_by_content(
- &data->ctxt, delta, &delta->old_file, &data->patch.old_data)) < 0 ||
- (error = diff_delta_is_binary_by_content(
- &data->ctxt, delta, &delta->new_file, &data->patch.new_data)) < 0)
- goto cleanup;
-
- data->patch.flags |= GIT_DIFF_PATCH_LOADED;
-
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
- delta->status != GIT_DELTA_UNMODIFIED)
- data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
-
- /* do diffs */
-
- if (!(error = diff_delta_file_callback(&data->ctxt, delta, 1)))
- error = diff_patch_generate(&data->ctxt, &data->patch);
-
-cleanup:
- if (error == GIT_EUSER)
- giterr_clear();
-
- diff_patch_unload(&data->patch);
-
- return error;
-}
-
-int git_diff_blobs(
- const git_blob *old_blob,
- const git_blob *new_blob,
- const git_diff_options *options,
- git_diff_file_cb file_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_data_cb data_cb,
- void *payload)
-{
- int error;
- diff_single_data d;
- git_repository *repo =
- new_blob ? git_object_owner((const git_object *)new_blob) :
- old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
-
- if (!repo) /* Hmm, given two NULL blobs, silently do no callbacks? */
- return 0;
-
- if ((error = diff_single_init(
- &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
- return error;
-
- if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
- const git_blob *swap = old_blob;
- old_blob = new_blob;
- new_blob = swap;
- }
-
- set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
- set_data_from_blob(new_blob, &d.patch.new_data, &d.delta.new_file);
-
- return diff_single_apply(&d);
-}
-
-int git_diff_blob_to_buffer(
- const git_blob *old_blob,
- const char *buf,
- size_t buflen,
- const git_diff_options *options,
- git_diff_file_cb file_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_data_cb data_cb,
- void *payload)
-{
- int error;
- diff_single_data d;
- git_repository *repo =
- old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
-
- if (!repo && !buf) /* Hmm, given NULLs, silently do no callbacks? */
- return 0;
-
- if ((error = diff_single_init(
- &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
- return error;
-
- if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
- set_data_from_buffer(buf, buflen, &d.patch.old_data, &d.delta.old_file);
- set_data_from_blob(old_blob, &d.patch.new_data, &d.delta.new_file);
- } else {
- set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
- set_data_from_buffer(buf, buflen, &d.patch.new_data, &d.delta.new_file);
- }
-
- return diff_single_apply(&d);
-}
-
-size_t git_diff_num_deltas(git_diff_list *diff)
-{
- assert(diff);
- return (size_t)diff->deltas.length;
-}
-
-size_t git_diff_num_deltas_of_type(git_diff_list *diff, git_delta_t type)
-{
- size_t i, count = 0;
- git_diff_delta *delta;
-
- assert(diff);
-
- git_vector_foreach(&diff->deltas, i, delta) {
- count += (delta->status == type);
- }
-
- return count;
-}
-
-int git_diff_get_patch(
- git_diff_patch **patch_ptr,
- const git_diff_delta **delta_ptr,
- git_diff_list *diff,
- size_t idx)
-{
- int error;
- diff_context ctxt;
- git_diff_delta *delta;
- git_diff_patch *patch;
-
- if (patch_ptr)
- *patch_ptr = NULL;
- if (delta_ptr)
- *delta_ptr = NULL;
-
- if (diff_required(diff, "git_diff_get_patch") < 0)
- return -1;
-
- if (diff_context_init(
- &ctxt, diff, NULL, NULL,
- NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL) < 0)
- return -1;
-
- delta = git_vector_get(&diff->deltas, idx);
- if (!delta) {
- giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
- return GIT_ENOTFOUND;
- }
-
- if (delta_ptr)
- *delta_ptr = delta;
-
- if (!patch_ptr &&
- ((delta->flags & KNOWN_BINARY_FLAGS) != 0 ||
- (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
- return 0;
-
- if (git_diff_delta__should_skip(ctxt.opts, delta))
- return 0;
-
- /* Don't load the patch if the user doesn't want it */
- if (!patch_ptr)
- return 0;
-
- patch = diff_patch_alloc(&ctxt, delta);
- if (!patch)
- return -1;
-
- if (!(error = diff_patch_load(&ctxt, patch))) {
- ctxt.payload = patch;
-
- error = diff_patch_generate(&ctxt, patch);
-
- if (error == GIT_EUSER)
- error = ctxt.error;
- }
-
- if (error)
- git_diff_patch_free(patch);
- else if (patch_ptr)
- *patch_ptr = patch;
-
- return error;
-}
-
-void git_diff_patch_free(git_diff_patch *patch)
-{
- if (patch)
- GIT_REFCOUNT_DEC(patch, diff_patch_free);
-}
-
-const git_diff_delta *git_diff_patch_delta(git_diff_patch *patch)
-{
- assert(patch);
- return patch->delta;
-}
-
-size_t git_diff_patch_num_hunks(git_diff_patch *patch)
-{
- assert(patch);
- return patch->hunks_size;
-}
-
-int git_diff_patch_line_stats(
- size_t *total_ctxt,
- size_t *total_adds,
- size_t *total_dels,
- const git_diff_patch *patch)
-{
- size_t totals[3], idx;
-
- memset(totals, 0, sizeof(totals));
-
- for (idx = 0; idx < patch->lines_size; ++idx) {
- switch (patch->lines[idx].origin) {
- case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
- case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
- case GIT_DIFF_LINE_DELETION: totals[2]++; break;
- default:
- /* diff --stat and --numstat don't count EOFNL marks because
- * they will always be paired with a ADDITION or DELETION line.
- */
- break;
- }
- }
-
- if (total_ctxt)
- *total_ctxt = totals[0];
- if (total_adds)
- *total_adds = totals[1];
- if (total_dels)
- *total_dels = totals[2];
-
- return 0;
-}
-
-static int diff_error_outofrange(const char *thing)
-{
- giterr_set(GITERR_INVALID, "Diff patch %s index out of range", thing);
- return GIT_ENOTFOUND;
-}
-
-int git_diff_patch_get_hunk(
- const git_diff_range **range,
- const char **header,
- size_t *header_len,
- size_t *lines_in_hunk,
- git_diff_patch *patch,
- size_t hunk_idx)
-{
- diff_patch_hunk *hunk;
-
- assert(patch);
-
- if (hunk_idx >= patch->hunks_size) {
- if (range) *range = NULL;
- if (header) *header = NULL;
- if (header_len) *header_len = 0;
- if (lines_in_hunk) *lines_in_hunk = 0;
-
- return diff_error_outofrange("hunk");
- }
-
- hunk = &patch->hunks[hunk_idx];
-
- if (range) *range = &hunk->range;
- if (header) *header = hunk->header;
- if (header_len) *header_len = hunk->header_len;
- if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
-
- return 0;
-}
-
-int git_diff_patch_num_lines_in_hunk(
- git_diff_patch *patch,
- size_t hunk_idx)
-{
- assert(patch);
-
- if (hunk_idx >= patch->hunks_size)
- return diff_error_outofrange("hunk");
- else
- return (int)patch->hunks[hunk_idx].line_count;
-}
-
-int git_diff_patch_get_line_in_hunk(
- char *line_origin,
- const char **content,
- size_t *content_len,
- int *old_lineno,
- int *new_lineno,
- git_diff_patch *patch,
- size_t hunk_idx,
- size_t line_of_hunk)
-{
- diff_patch_hunk *hunk;
- diff_patch_line *line;
- const char *thing;
-
- assert(patch);
-
- if (hunk_idx >= patch->hunks_size) {
- thing = "hunk";
- goto notfound;
- }
- hunk = &patch->hunks[hunk_idx];
-
- if (line_of_hunk >= hunk->line_count) {
- thing = "link";
- goto notfound;
- }
-
- line = &patch->lines[hunk->line_start + line_of_hunk];
-
- if (line_origin) *line_origin = line->origin;
- if (content) *content = line->ptr;
- if (content_len) *content_len = line->len;
- if (old_lineno) *old_lineno = (int)line->oldno;
- if (new_lineno) *new_lineno = (int)line->newno;
-
- return 0;
-
-notfound:
- if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT;
- if (content) *content = NULL;
- if (content_len) *content_len = 0;
- if (old_lineno) *old_lineno = -1;
- if (new_lineno) *new_lineno = -1;
-
- return diff_error_outofrange(thing);
-}
-
-static int print_to_buffer_cb(
- const git_diff_delta *delta,
- const git_diff_range *range,
- char line_origin,
- const char *content,
- size_t content_len,
- void *payload)
-{
- git_buf *output = payload;
- GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
- return git_buf_put(output, content, content_len);
-}
-
-int git_diff_patch_print(
- git_diff_patch *patch,
- git_diff_data_cb print_cb,
- void *payload)
-{
- int error;
- git_buf temp = GIT_BUF_INIT;
- diff_print_info pi;
- size_t h, l;
-
- assert(patch && print_cb);
-
- if (!(error = diff_print_info_init(
- &pi, &temp, patch->diff, print_cb, payload)))
- error = print_patch_file(patch->delta, 0, &pi);
-
- for (h = 0; h < patch->hunks_size && !error; ++h) {
- diff_patch_hunk *hunk = &patch->hunks[h];
-
- error = print_patch_hunk(
- patch->delta, &hunk->range, hunk->header, hunk->header_len, &pi);
-
- for (l = 0; l < hunk->line_count && !error; ++l) {
- diff_patch_line *line = &patch->lines[hunk->line_start + l];
-
- error = print_patch_line(
- patch->delta, &hunk->range,
- line->origin, line->ptr, line->len, &pi);
- }
- }
-
- git_buf_free(&temp);
-
- return error;
-}
-
-int git_diff_patch_to_str(
- char **string,
- git_diff_patch *patch)
-{
- int error;
- git_buf output = GIT_BUF_INIT;
-
- error = git_diff_patch_print(patch, print_to_buffer_cb, &output);
-
- /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
- * meaning a memory allocation failure, so just map to -1...
- */
- if (error == GIT_EUSER)
- error = -1;
-
- *string = git_buf_detach(&output);
-
- return error;
-}
-
-int git_diff__paired_foreach(
- git_diff_list *idx2head,
- git_diff_list *wd2idx,
- int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
- void *payload)
-{
- int cmp;
- git_diff_delta *i2h, *w2i;
- size_t i, j, i_max, j_max;
- int (*strcomp)(const char *, const char *);
-
- i_max = idx2head ? idx2head->deltas.length : 0;
- j_max = wd2idx ? wd2idx->deltas.length : 0;
-
- /* Get appropriate strcmp function */
- strcomp = idx2head ? idx2head->strcomp : wd2idx ? wd2idx->strcomp : NULL;
-
- /* Assert both iterators use matching ignore-case. If this function ever
- * supports merging diffs that are not sorted by the same function, then
- * it will need to spool and sort on one of the results before merging
- */
- if (idx2head && wd2idx) {
- assert(idx2head->strcomp == wd2idx->strcomp);
- }
-
- for (i = 0, j = 0; i < i_max || j < j_max; ) {
- i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL;
- w2i = wd2idx ? GIT_VECTOR_GET(&wd2idx->deltas,j) : NULL;
-
- cmp = !w2i ? -1 : !i2h ? 1 :
- strcomp(i2h->old_file.path, w2i->old_file.path);
-
- if (cmp < 0) {
- if (cb(i2h, NULL, payload))
- return GIT_EUSER;
- i++;
- } else if (cmp > 0) {
- if (cb(NULL, w2i, payload))
- return GIT_EUSER;
- j++;
- } else {
- if (cb(i2h, w2i, payload))
- return GIT_EUSER;
- i++; j++;
- }
- }
-
- return 0;
-}
diff --git a/src/diff_output.h b/src/diff_output.h
deleted file mode 100644
index 0833556..0000000
--- a/src/diff_output.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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_output_h__
-#define INCLUDE_diff_output_h__
-
-#include "git2/blob.h"
-#include "diff.h"
-#include "map.h"
-#include "xdiff/xdiff.h"
-
-#define MAX_DIFF_FILESIZE 0x20000000
-
-enum {
- GIT_DIFF_PATCH_ALLOCATED = (1 << 0),
- GIT_DIFF_PATCH_PREPPED = (1 << 1),
- GIT_DIFF_PATCH_LOADED = (1 << 2),
- GIT_DIFF_PATCH_DIFFABLE = (1 << 3),
- GIT_DIFF_PATCH_DIFFED = (1 << 4),
-};
-
-/* context for performing diffs */
-typedef struct {
- git_repository *repo;
- git_diff_list *diff;
- const git_diff_options *opts;
- git_diff_file_cb file_cb;
- git_diff_hunk_cb hunk_cb;
- git_diff_data_cb data_cb;
- void *payload;
- int error;
- git_diff_range range;
- xdemitconf_t xdiff_config;
- xpparam_t xdiff_params;
-} diff_context;
-
-/* cached information about a single span in a diff */
-typedef struct diff_patch_line diff_patch_line;
-struct diff_patch_line {
- const char *ptr;
- size_t len;
- size_t lines, oldno, newno;
- char origin;
-};
-
-/* cached information about a hunk in a diff */
-typedef struct diff_patch_hunk diff_patch_hunk;
-struct diff_patch_hunk {
- git_diff_range range;
- char header[128];
- size_t header_len;
- size_t line_start;
- size_t line_count;
-};
-
-struct git_diff_patch {
- git_refcount rc;
- git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
- git_diff_delta *delta;
- diff_context *ctxt; /* only valid while generating patch */
- git_iterator_type_t old_src;
- git_iterator_type_t new_src;
- git_blob *old_blob;
- git_blob *new_blob;
- git_map old_data;
- git_map new_data;
- uint32_t flags;
- diff_patch_hunk *hunks;
- size_t hunks_asize, hunks_size;
- diff_patch_line *lines;
- size_t lines_asize, lines_size;
- size_t oldno, newno;
-};
-
-/* context for performing diff on a single delta */
-typedef struct {
- git_diff_patch *patch;
- uint32_t prepped : 1;
- uint32_t loaded : 1;
- uint32_t diffable : 1;
- uint32_t diffed : 1;
-} diff_delta_context;
-
-extern int git_diff__paired_foreach(
- git_diff_list *idx2head,
- git_diff_list *wd2idx,
- int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
- void *payload);
-
-#endif
diff --git a/src/diff_patch.c b/src/diff_patch.c
new file mode 100644
index 0000000..a1e1fe8
--- /dev/null
+++ b/src/diff_patch.c
@@ -0,0 +1,922 @@
+/*
+ * 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_file.h"
+#include "diff_driver.h"
+#include "diff_patch.h"
+#include "diff_xdiff.h"
+
+/* cached information about a single span in a diff */
+typedef struct diff_patch_line diff_patch_line;
+struct diff_patch_line {
+ const char *ptr;
+ size_t len;
+ size_t lines, oldno, newno;
+ char origin;
+};
+
+/* cached information about a hunk in a diff */
+typedef struct diff_patch_hunk diff_patch_hunk;
+struct diff_patch_hunk {
+ git_diff_range range;
+ char header[128];
+ size_t header_len;
+ size_t line_start;
+ size_t line_count;
+};
+
+struct git_diff_patch {
+ git_refcount rc;
+ git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
+ git_diff_delta *delta;
+ size_t delta_index;
+ git_diff_file_content ofile;
+ git_diff_file_content nfile;
+ uint32_t flags;
+ git_array_t(diff_patch_hunk) hunks;
+ git_array_t(diff_patch_line) lines;
+ size_t oldno, newno;
+ size_t content_size;
+ git_pool flattened;
+};
+
+enum {
+ GIT_DIFF_PATCH_ALLOCATED = (1 << 0),
+ GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
+ GIT_DIFF_PATCH_LOADED = (1 << 2),
+ GIT_DIFF_PATCH_DIFFABLE = (1 << 3),
+ GIT_DIFF_PATCH_DIFFED = (1 << 4),
+ GIT_DIFF_PATCH_FLATTENED = (1 << 5),
+};
+
+static void diff_output_init(git_diff_output*, const git_diff_options*,
+ git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*);
+
+static void diff_output_to_patch(git_diff_output *, git_diff_patch *);
+
+static void diff_patch_update_binary(git_diff_patch *patch)
+{
+ if ((patch->delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
+ return;
+
+ if ((patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
+ (patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+ patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
+
+ else if ((patch->ofile.file.flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
+ (patch->nfile.file.flags & DIFF_FLAGS_NOT_BINARY) != 0)
+ patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
+}
+
+static void diff_patch_init_common(git_diff_patch *patch)
+{
+ diff_patch_update_binary(patch);
+
+ if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
+ patch->flags |= GIT_DIFF_PATCH_LOADED; /* set LOADED but not DIFFABLE */
+
+ patch->flags |= GIT_DIFF_PATCH_INITIALIZED;
+
+ if (patch->diff)
+ git_diff_list_addref(patch->diff);
+}
+
+static int diff_patch_init_from_diff(
+ git_diff_patch *patch, git_diff_list *diff, size_t delta_index)
+{
+ int error = 0;
+
+ memset(patch, 0, sizeof(*patch));
+ patch->diff = diff;
+ patch->delta = git_vector_get(&diff->deltas, delta_index);
+ patch->delta_index = delta_index;
+
+ if ((error = git_diff_file_content__init_from_diff(
+ &patch->ofile, diff, delta_index, true)) < 0 ||
+ (error = git_diff_file_content__init_from_diff(
+ &patch->nfile, diff, delta_index, false)) < 0)
+ return error;
+
+ diff_patch_init_common(patch);
+
+ return 0;
+}
+
+static int diff_patch_alloc_from_diff(
+ git_diff_patch **out,
+ git_diff_list *diff,
+ size_t delta_index)
+{
+ int error;
+ git_diff_patch *patch = git__calloc(1, sizeof(git_diff_patch));
+ GITERR_CHECK_ALLOC(patch);
+
+ if (!(error = diff_patch_init_from_diff(patch, diff, delta_index))) {
+ patch->flags |= GIT_DIFF_PATCH_ALLOCATED;
+ GIT_REFCOUNT_INC(patch);
+ } else {
+ git__free(patch);
+ patch = NULL;
+ }
+
+ *out = patch;
+ return error;
+}
+
+static int diff_patch_load(git_diff_patch *patch, git_diff_output *output)
+{
+ int error = 0;
+ bool incomplete_data;
+
+ if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0)
+ return 0;
+
+ /* if no hunk and data callbacks and user doesn't care if data looks
+ * binary, then there is no need to actually load the data
+ */
+ if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
+ output && !output->hunk_cb && !output->data_cb)
+ return 0;
+
+#define DIFF_FLAGS_KNOWN_DATA (GIT_DIFF_FLAG__NO_DATA|GIT_DIFF_FLAG_VALID_OID)
+
+ incomplete_data =
+ ((patch->ofile.file.flags & DIFF_FLAGS_KNOWN_DATA) != 0 &&
+ (patch->nfile.file.flags & DIFF_FLAGS_KNOWN_DATA) != 0);
+
+ /* always try to load workdir content first because filtering may
+ * need 2x data size and this minimizes peak memory footprint
+ */
+ if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
+ if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
+ (patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+ goto cleanup;
+ }
+ if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
+ if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
+ (patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+ goto cleanup;
+ }
+
+ /* once workdir has been tried, load other data as needed */
+ if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
+ if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
+ (patch->ofile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+ goto cleanup;
+ }
+ if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
+ if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
+ (patch->nfile.file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+ goto cleanup;
+ }
+
+ /* if we were previously missing an oid, reassess UNMODIFIED state */
+ if (incomplete_data &&
+ patch->ofile.file.mode == patch->nfile.file.mode &&
+ git_oid_equal(&patch->ofile.file.oid, &patch->nfile.file.oid))
+ patch->delta->status = GIT_DELTA_UNMODIFIED;
+
+cleanup:
+ diff_patch_update_binary(patch);
+
+ if (!error) {
+ /* patch is diffable only for non-binary, modified files where
+ * at least one side has data and the data actually changed
+ */
+ if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
+ patch->delta->status != GIT_DELTA_UNMODIFIED &&
+ (patch->ofile.map.len || patch->nfile.map.len) &&
+ (patch->ofile.map.len != patch->nfile.map.len ||
+ !git_oid_equal(&patch->ofile.file.oid, &patch->nfile.file.oid)))
+ patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
+
+ patch->flags |= GIT_DIFF_PATCH_LOADED;
+ }
+
+ return error;
+}
+
+static int diff_patch_file_callback(
+ git_diff_patch *patch, git_diff_output *output)
+{
+ float progress;
+
+ if (!output->file_cb)
+ return 0;
+
+ progress = patch->diff ?
+ ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
+
+ if (output->file_cb(patch->delta, progress, output->payload) != 0)
+ output->error = GIT_EUSER;
+
+ return output->error;
+}
+
+static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output)
+{
+ int error = 0;
+
+ if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
+ return 0;
+
+ if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 &&
+ (error = diff_patch_load(patch, output)) < 0)
+ return error;
+
+ if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0)
+ return 0;
+
+ if (output->diff_cb != NULL &&
+ !(error = output->diff_cb(output, patch)))
+ patch->flags |= GIT_DIFF_PATCH_DIFFED;
+
+ return error;
+}
+
+static void diff_patch_free(git_diff_patch *patch)
+{
+ git_diff_file_content__clear(&patch->ofile);
+ git_diff_file_content__clear(&patch->nfile);
+
+ git_array_clear(patch->lines);
+ git_array_clear(patch->hunks);
+
+ git_diff_list_free(patch->diff); /* decrements refcount */
+ patch->diff = NULL;
+
+ git_pool_clear(&patch->flattened);
+
+ if (patch->flags & GIT_DIFF_PATCH_ALLOCATED)
+ git__free(patch);
+}
+
+static int diff_required(git_diff_list *diff, const char *action)
+{
+ if (diff)
+ return 0;
+ giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
+ return -1;
+}
+
+int git_diff_foreach(
+ git_diff_list *diff,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb data_cb,
+ void *payload)
+{
+ int error = 0;
+ git_xdiff_output xo;
+ size_t idx;
+ git_diff_patch patch;
+
+ if (diff_required(diff, "git_diff_foreach") < 0)
+ return -1;
+
+ diff_output_init((git_diff_output *)&xo,
+ &diff->opts, file_cb, hunk_cb, data_cb, payload);
+ git_xdiff_init(&xo, &diff->opts);
+
+ git_vector_foreach(&diff->deltas, idx, patch.delta) {
+ /* check flags against patch status */
+ if (git_diff_delta__should_skip(&diff->opts, patch.delta))
+ continue;
+
+ if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) {
+
+ error = diff_patch_file_callback(&patch, (git_diff_output *)&xo);
+
+ if (!error)
+ error = diff_patch_generate(&patch, (git_diff_output *)&xo);
+
+ git_diff_patch_free(&patch);
+ }
+
+ if (error < 0)
+ break;
+ }
+
+ if (error == GIT_EUSER)
+ giterr_clear(); /* don't leave error message set invalidly */
+ return error;
+}
+
+typedef struct {
+ git_diff_patch patch;
+ git_diff_delta delta;
+} diff_patch_with_delta;
+
+static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
+{
+ int error = 0;
+ git_diff_patch *patch = &pd->patch;
+ bool has_old = ((patch->ofile.file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
+ bool has_new = ((patch->nfile.file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
+
+ pd->delta.status = has_new ?
+ (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
+ (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
+
+ if (git_oid_equal(&patch->nfile.file.oid, &patch->ofile.file.oid))
+ pd->delta.status = GIT_DELTA_UNMODIFIED;
+
+ patch->delta = &pd->delta;
+
+ diff_patch_init_common(patch);
+
+ error = diff_patch_file_callback(patch, (git_diff_output *)xo);
+
+ if (!error)
+ error = diff_patch_generate(patch, (git_diff_output *)xo);
+
+ if (error == GIT_EUSER)
+ giterr_clear(); /* don't leave error message set invalidly */
+
+ return error;
+}
+
+static int diff_patch_from_blobs(
+ diff_patch_with_delta *pd,
+ git_xdiff_output *xo,
+ const git_blob *old_blob,
+ const git_blob *new_blob,
+ const git_diff_options *opts)
+{
+ int error = 0;
+ git_repository *repo =
+ new_blob ? git_object_owner((const git_object *)new_blob) :
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
+
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
+
+ pd->patch.delta = &pd->delta;
+
+ if (!repo) /* return two NULL items as UNMODIFIED delta */
+ return 0;
+
+ if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
+ const git_blob *swap = old_blob;
+ old_blob = new_blob;
+ new_blob = swap;
+ }
+
+ if ((error = git_diff_file_content__init_from_blob(
+ &pd->patch.ofile, repo, opts, old_blob)) < 0 ||
+ (error = git_diff_file_content__init_from_blob(
+ &pd->patch.nfile, repo, opts, new_blob)) < 0)
+ return error;
+
+ return diff_single_generate(pd, xo);
+}
+
+int git_diff_blobs(
+ const git_blob *old_blob,
+ const git_blob *new_blob,
+ const git_diff_options *opts,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb data_cb,
+ void *payload)
+{
+ int error = 0;
+ diff_patch_with_delta pd;
+ git_xdiff_output xo;
+
+ memset(&pd, 0, sizeof(pd));
+ memset(&xo, 0, sizeof(xo));
+
+ diff_output_init(
+ (git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload);
+ git_xdiff_init(&xo, opts);
+
+ error = diff_patch_from_blobs(&pd, &xo, old_blob, new_blob, opts);
+
+ git_diff_patch_free((git_diff_patch *)&pd);
+
+ return error;
+}
+
+int git_diff_patch_from_blobs(
+ git_diff_patch **out,
+ const git_blob *old_blob,
+ const git_blob *new_blob,
+ const git_diff_options *opts)
+{
+ int error = 0;
+ diff_patch_with_delta *pd;
+ git_xdiff_output xo;
+
+ assert(out);
+ *out = NULL;
+
+ pd = git__calloc(1, sizeof(*pd));
+ GITERR_CHECK_ALLOC(pd);
+ pd->patch.flags = GIT_DIFF_PATCH_ALLOCATED;
+
+ memset(&xo, 0, sizeof(xo));
+
+ diff_output_to_patch((git_diff_output *)&xo, &pd->patch);
+ git_xdiff_init(&xo, opts);
+
+ if (!(error = diff_patch_from_blobs(pd, &xo, old_blob, new_blob, opts)))
+ *out = (git_diff_patch *)pd;
+ else
+ git_diff_patch_free((git_diff_patch *)pd);
+
+ return error;
+}
+
+static int diff_patch_from_blob_and_buffer(
+ diff_patch_with_delta *pd,
+ git_xdiff_output *xo,
+ const git_blob *old_blob,
+ const char *buf,
+ size_t buflen,
+ const git_diff_options *opts)
+{
+ int error = 0;
+ git_repository *repo =
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
+
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
+
+ pd->patch.delta = &pd->delta;
+
+ if (!repo && !buf) /* return two NULL items as UNMODIFIED delta */
+ return 0;
+
+ if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
+ if (!(error = git_diff_file_content__init_from_raw(
+ &pd->patch.ofile, repo, opts, buf, buflen)))
+ error = git_diff_file_content__init_from_blob(
+ &pd->patch.nfile, repo, opts, old_blob);
+ } else {
+ if (!(error = git_diff_file_content__init_from_blob(
+ &pd->patch.ofile, repo, opts, old_blob)))
+ error = git_diff_file_content__init_from_raw(
+ &pd->patch.nfile, repo, opts, buf, buflen);
+ }
+
+ return diff_single_generate(pd, xo);
+}
+
+int git_diff_blob_to_buffer(
+ const git_blob *old_blob,
+ const char *buf,
+ size_t buflen,
+ const git_diff_options *opts,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb data_cb,
+ void *payload)
+{
+ int error = 0;
+ diff_patch_with_delta pd;
+ git_xdiff_output xo;
+
+ memset(&pd, 0, sizeof(pd));
+ memset(&xo, 0, sizeof(xo));
+
+ diff_output_init(
+ (git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload);
+ git_xdiff_init(&xo, opts);
+
+ error = diff_patch_from_blob_and_buffer(
+ &pd, &xo, old_blob, buf, buflen, opts);
+
+ git_diff_patch_free((git_diff_patch *)&pd);
+
+ return error;
+}
+
+int git_diff_patch_from_blob_and_buffer(
+ git_diff_patch **out,
+ const git_blob *old_blob,
+ const char *buf,
+ size_t buflen,
+ const git_diff_options *opts)
+{
+ int error = 0;
+ diff_patch_with_delta *pd;
+ git_xdiff_output xo;
+
+ assert(out);
+ *out = NULL;
+
+ pd = git__calloc(1, sizeof(*pd));
+ GITERR_CHECK_ALLOC(pd);
+ pd->patch.flags = GIT_DIFF_PATCH_ALLOCATED;
+
+ memset(&xo, 0, sizeof(xo));
+
+ diff_output_to_patch((git_diff_output *)&xo, &pd->patch);
+ git_xdiff_init(&xo, opts);
+
+ if (!(error = diff_patch_from_blob_and_buffer(
+ pd, &xo, old_blob, buf, buflen, opts)))
+ *out = (git_diff_patch *)pd;
+ else
+ git_diff_patch_free((git_diff_patch *)pd);
+
+ return error;
+}
+
+int git_diff_get_patch(
+ git_diff_patch **patch_ptr,
+ const git_diff_delta **delta_ptr,
+ git_diff_list *diff,
+ size_t idx)
+{
+ int error = 0;
+ git_xdiff_output xo;
+ git_diff_delta *delta = NULL;
+ git_diff_patch *patch = NULL;
+
+ if (patch_ptr) *patch_ptr = NULL;
+ if (delta_ptr) *delta_ptr = NULL;
+
+ if (diff_required(diff, "git_diff_get_patch") < 0)
+ return -1;
+
+ delta = git_vector_get(&diff->deltas, idx);
+ if (!delta) {
+ giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
+ return GIT_ENOTFOUND;
+ }
+
+ if (delta_ptr)
+ *delta_ptr = delta;
+
+ if (git_diff_delta__should_skip(&diff->opts, delta))
+ return 0;
+
+ /* don't load the patch data unless we need it for binary check */
+ if (!patch_ptr &&
+ ((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
+ (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
+ return 0;
+
+ if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
+ return error;
+
+ diff_output_to_patch((git_diff_output *)&xo, patch);
+ git_xdiff_init(&xo, &diff->opts);
+
+ error = diff_patch_file_callback(patch, (git_diff_output *)&xo);
+
+ if (!error)
+ error = diff_patch_generate(patch, (git_diff_output *)&xo);
+
+ if (!error) {
+ /* if cumulative diff size is < 0.5 total size, flatten the patch */
+ /* unload the file content */
+ }
+
+ if (error || !patch_ptr)
+ git_diff_patch_free(patch);
+ else
+ *patch_ptr = patch;
+
+ if (error == GIT_EUSER)
+ giterr_clear(); /* don't leave error message set invalidly */
+ return error;
+}
+
+void git_diff_patch_free(git_diff_patch *patch)
+{
+ if (patch)
+ GIT_REFCOUNT_DEC(patch, diff_patch_free);
+}
+
+const git_diff_delta *git_diff_patch_delta(git_diff_patch *patch)
+{
+ assert(patch);
+ return patch->delta;
+}
+
+size_t git_diff_patch_num_hunks(git_diff_patch *patch)
+{
+ assert(patch);
+ return git_array_size(patch->hunks);
+}
+
+int git_diff_patch_line_stats(
+ size_t *total_ctxt,
+ size_t *total_adds,
+ size_t *total_dels,
+ const git_diff_patch *patch)
+{
+ size_t totals[3], idx;
+
+ memset(totals, 0, sizeof(totals));
+
+ for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
+ diff_patch_line *line = git_array_get(patch->lines, idx);
+ if (!line)
+ continue;
+
+ switch (line->origin) {
+ case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
+ case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
+ case GIT_DIFF_LINE_DELETION: totals[2]++; break;
+ default:
+ /* diff --stat and --numstat don't count EOFNL marks because
+ * they will always be paired with a ADDITION or DELETION line.
+ */
+ break;
+ }
+ }
+
+ if (total_ctxt)
+ *total_ctxt = totals[0];
+ if (total_adds)
+ *total_adds = totals[1];
+ if (total_dels)
+ *total_dels = totals[2];
+
+ return 0;
+}
+
+static int diff_error_outofrange(const char *thing)
+{
+ giterr_set(GITERR_INVALID, "Diff patch %s index out of range", thing);
+ return GIT_ENOTFOUND;
+}
+
+int git_diff_patch_get_hunk(
+ const git_diff_range **range,
+ const char **header,
+ size_t *header_len,
+ size_t *lines_in_hunk,
+ git_diff_patch *patch,
+ size_t hunk_idx)
+{
+ diff_patch_hunk *hunk;
+ assert(patch);
+
+ hunk = git_array_get(patch->hunks, hunk_idx);
+
+ if (!hunk) {
+ if (range) *range = NULL;
+ if (header) *header = NULL;
+ if (header_len) *header_len = 0;
+ if (lines_in_hunk) *lines_in_hunk = 0;
+ return diff_error_outofrange("hunk");
+ }
+
+ if (range) *range = &hunk->range;
+ if (header) *header = hunk->header;
+ if (header_len) *header_len = hunk->header_len;
+ if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
+ return 0;
+}
+
+int git_diff_patch_num_lines_in_hunk(git_diff_patch *patch, size_t hunk_idx)
+{
+ diff_patch_hunk *hunk;
+ assert(patch);
+
+ if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
+ return diff_error_outofrange("hunk");
+ return (int)hunk->line_count;
+}
+
+int git_diff_patch_get_line_in_hunk(
+ char *line_origin,
+ const char **content,
+ size_t *content_len,
+ int *old_lineno,
+ int *new_lineno,
+ git_diff_patch *patch,
+ size_t hunk_idx,
+ size_t line_of_hunk)
+{
+ diff_patch_hunk *hunk;
+ diff_patch_line *line;
+ const char *thing;
+
+ assert(patch);
+
+ if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
+ thing = "hunk";
+ goto notfound;
+ }
+
+ if (line_of_hunk >= hunk->line_count ||
+ !(line = git_array_get(
+ patch->lines, hunk->line_start + line_of_hunk))) {
+ thing = "line";
+ goto notfound;
+ }
+
+ if (line_origin) *line_origin = line->origin;
+ if (content) *content = line->ptr;
+ if (content_len) *content_len = line->len;
+ if (old_lineno) *old_lineno = (int)line->oldno;
+ if (new_lineno) *new_lineno = (int)line->newno;
+
+ return 0;
+
+notfound:
+ if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT;
+ if (content) *content = NULL;
+ if (content_len) *content_len = 0;
+ if (old_lineno) *old_lineno = -1;
+ if (new_lineno) *new_lineno = -1;
+
+ return diff_error_outofrange(thing);
+}
+
+git_diff_list *git_diff_patch__diff(git_diff_patch *patch)
+{
+ return patch->diff;
+}
+
+git_diff_driver *git_diff_patch__driver(git_diff_patch *patch)
+{
+ /* ofile driver is representative for whole patch */
+ return patch->ofile.driver;
+}
+
+void git_diff_patch__old_data(
+ char **ptr, size_t *len, git_diff_patch *patch)
+{
+ *ptr = patch->ofile.map.data;
+ *len = patch->ofile.map.len;
+}
+
+void git_diff_patch__new_data(
+ char **ptr, size_t *len, git_diff_patch *patch)
+{
+ *ptr = patch->nfile.map.data;
+ *len = patch->nfile.map.len;
+}
+
+int git_diff_patch__invoke_callbacks(
+ git_diff_patch *patch,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb line_cb,
+ void *payload)
+{
+ int error = 0;
+ uint32_t i, j;
+
+ if (file_cb)
+ error = file_cb(patch->delta, 0, payload);
+
+ if (!hunk_cb && !line_cb)
+ return error;
+
+ for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
+ diff_patch_hunk *h = git_array_get(patch->hunks, i);
+
+ error = hunk_cb(
+ patch->delta, &h->range, h->header, h->header_len, payload);
+
+ if (!line_cb)
+ continue;
+
+ for (j = 0; !error && j < h->line_count; ++j) {
+ diff_patch_line *l =
+ git_array_get(patch->lines, h->line_start + j);
+
+ error = line_cb(
+ patch->delta, &h->range, l->origin, l->ptr, l->len, payload);
+ }
+ }
+
+ return error;
+}
+
+
+static int diff_patch_file_cb(
+ const git_diff_delta *delta,
+ float progress,
+ void *payload)
+{
+ GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
+ return 0;
+}
+
+static int diff_patch_hunk_cb(
+ const git_diff_delta *delta,
+ const git_diff_range *range,
+ const char *header,
+ size_t header_len,
+ void *payload)
+{
+ git_diff_patch *patch = payload;
+ diff_patch_hunk *hunk;
+
+ GIT_UNUSED(delta);
+
+ hunk = git_array_alloc(patch->hunks);
+ GITERR_CHECK_ALLOC(hunk);
+
+ memcpy(&hunk->range, range, sizeof(hunk->range));
+
+ assert(header_len + 1 < sizeof(hunk->header));
+ memcpy(&hunk->header, header, header_len);
+ hunk->header[header_len] = '\0';
+ hunk->header_len = header_len;
+
+ hunk->line_start = git_array_size(patch->lines);
+ hunk->line_count = 0;
+
+ patch->oldno = range->old_start;
+ patch->newno = range->new_start;
+
+ return 0;
+}
+
+static int diff_patch_line_cb(
+ const git_diff_delta *delta,
+ const git_diff_range *range,
+ char line_origin,
+ const char *content,
+ size_t content_len,
+ void *payload)
+{
+ git_diff_patch *patch = payload;
+ diff_patch_hunk *hunk;
+ diff_patch_line *line;
+
+ GIT_UNUSED(delta);
+ GIT_UNUSED(range);
+
+ hunk = git_array_last(patch->hunks);
+ GITERR_CHECK_ALLOC(hunk);
+
+ line = git_array_alloc(patch->lines);
+ GITERR_CHECK_ALLOC(line);
+
+ line->ptr = content;
+ line->len = content_len;
+ line->origin = line_origin;
+
+ patch->content_size += content_len;
+
+ /* do some bookkeeping so we can provide old/new line numbers */
+
+ for (line->lines = 0; content_len > 0; --content_len) {
+ if (*content++ == '\n')
+ ++line->lines;
+ }
+
+ switch (line_origin) {
+ case GIT_DIFF_LINE_ADDITION:
+ case GIT_DIFF_LINE_DEL_EOFNL:
+ line->oldno = -1;
+ line->newno = patch->newno;
+ patch->newno += line->lines;
+ break;
+ case GIT_DIFF_LINE_DELETION:
+ case GIT_DIFF_LINE_ADD_EOFNL:
+ line->oldno = patch->oldno;
+ line->newno = -1;
+ patch->oldno += line->lines;
+ break;
+ default:
+ line->oldno = patch->oldno;
+ line->newno = patch->newno;
+ patch->oldno += line->lines;
+ patch->newno += line->lines;
+ break;
+ }
+
+ hunk->line_count++;
+
+ return 0;
+}
+
+static void diff_output_init(
+ git_diff_output *out,
+ const git_diff_options *opts,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb data_cb,
+ void *payload)
+{
+ GIT_UNUSED(opts);
+
+ memset(out, 0, sizeof(*out));
+
+ out->file_cb = file_cb;
+ out->hunk_cb = hunk_cb;
+ out->data_cb = data_cb;
+ out->payload = payload;
+}
+
+static void diff_output_to_patch(git_diff_output *out, git_diff_patch *patch)
+{
+ diff_output_init(
+ out, NULL,
+ diff_patch_file_cb, diff_patch_hunk_cb, diff_patch_line_cb, patch);
+}
diff --git a/src/diff_patch.h b/src/diff_patch.h
new file mode 100644
index 0000000..56af146
--- /dev/null
+++ b/src/diff_patch.h
@@ -0,0 +1,46 @@
+/*
+ * 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_patch_h__
+#define INCLUDE_diff_patch_h__
+
+#include "common.h"
+#include "diff.h"
+#include "diff_file.h"
+#include "array.h"
+
+extern git_diff_list *git_diff_patch__diff(git_diff_patch *);
+
+extern git_diff_driver *git_diff_patch__driver(git_diff_patch *);
+
+extern void git_diff_patch__old_data(char **, size_t *, git_diff_patch *);
+extern void git_diff_patch__new_data(char **, size_t *, git_diff_patch *);
+
+extern int git_diff_patch__invoke_callbacks(
+ git_diff_patch *patch,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb line_cb,
+ void *payload);
+
+typedef struct git_diff_output git_diff_output;
+struct git_diff_output {
+ /* these callbacks are issued with the diff data */
+ git_diff_file_cb file_cb;
+ git_diff_hunk_cb hunk_cb;
+ git_diff_data_cb data_cb;
+ void *payload;
+
+ /* this records the actual error in cases where it may be obscured */
+ int error;
+
+ /* this callback is used to do the diff and drive the other callbacks.
+ * see diff_xdiff.h for how to use this in practice for now.
+ */
+ int (*diff_cb)(git_diff_output *output, git_diff_patch *patch);
+};
+
+#endif
diff --git a/src/diff_print.c b/src/diff_print.c
new file mode 100644
index 0000000..244aa6e
--- /dev/null
+++ b/src/diff_print.c
@@ -0,0 +1,417 @@
+/*
+ * 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_patch.h"
+#include "buffer.h"
+
+typedef struct {
+ git_diff_list *diff;
+ git_diff_data_cb print_cb;
+ void *payload;
+ git_buf *buf;
+ int oid_strlen;
+} diff_print_info;
+
+static int diff_print_info_init(
+ diff_print_info *pi,
+ git_buf *out, git_diff_list *diff, git_diff_data_cb cb, void *payload)
+{
+ assert(diff && diff->repo);
+
+ pi->diff = diff;
+ pi->print_cb = cb;
+ pi->payload = payload;
+ pi->buf = out;
+
+ if (git_repository__cvar(&pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0)
+ return -1;
+
+ pi->oid_strlen += 1; /* for NUL byte */
+
+ if (pi->oid_strlen < 2)
+ pi->oid_strlen = 2;
+ else if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
+ pi->oid_strlen = GIT_OID_HEXSZ + 1;
+
+ return 0;
+}
+
+static char pick_suffix(int mode)
+{
+ if (S_ISDIR(mode))
+ return '/';
+ else if (mode & 0100) //-V536
+ /* in git, modes are very regular, so we must have 0100755 mode */
+ return '*';
+ else
+ return ' ';
+}
+
+char git_diff_status_char(git_delta_t status)
+{
+ char code;
+
+ switch (status) {
+ case GIT_DELTA_ADDED: code = 'A'; break;
+ case GIT_DELTA_DELETED: code = 'D'; break;
+ case GIT_DELTA_MODIFIED: code = 'M'; break;
+ case GIT_DELTA_RENAMED: code = 'R'; break;
+ case GIT_DELTA_COPIED: code = 'C'; break;
+ case GIT_DELTA_IGNORED: code = 'I'; break;
+ case GIT_DELTA_UNTRACKED: code = '?'; break;
+ default: code = ' '; break;
+ }
+
+ return code;
+}
+
+static int callback_error(void)
+{
+ giterr_clear();
+ return GIT_EUSER;
+}
+
+static int print_compact(
+ const git_diff_delta *delta, float progress, void *data)
+{
+ diff_print_info *pi = data;
+ char old_suffix, new_suffix, code = git_diff_status_char(delta->status);
+
+ GIT_UNUSED(progress);
+
+ if (code == ' ')
+ return 0;
+
+ old_suffix = pick_suffix(delta->old_file.mode);
+ new_suffix = pick_suffix(delta->new_file.mode);
+
+ git_buf_clear(pi->buf);
+
+ if (delta->old_file.path != delta->new_file.path &&
+ pi->diff->strcomp(delta->old_file.path,delta->new_file.path) != 0)
+ git_buf_printf(pi->buf, "%c\t%s%c -> %s%c\n", code,
+ delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
+ else if (delta->old_file.mode != delta->new_file.mode &&
+ delta->old_file.mode != 0 && delta->new_file.mode != 0)
+ git_buf_printf(pi->buf, "%c\t%s%c (%o -> %o)\n", code,
+ delta->old_file.path, new_suffix, delta->old_file.mode, delta->new_file.mode);
+ else if (old_suffix != ' ')
+ git_buf_printf(pi->buf, "%c\t%s%c\n", code, delta->old_file.path, old_suffix);
+ else
+ git_buf_printf(pi->buf, "%c\t%s\n", code, delta->old_file.path);
+
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ return 0;
+}
+
+int git_diff_print_compact(
+ git_diff_list *diff,
+ git_diff_data_cb print_cb,
+ void *payload)
+{
+ int error;
+ git_buf buf = GIT_BUF_INIT;
+ diff_print_info pi;
+
+ if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
+ error = git_diff_foreach(diff, print_compact, NULL, NULL, &pi);
+
+ git_buf_free(&buf);
+
+ return error;
+}
+
+static int print_raw(
+ const git_diff_delta *delta, float progress, void *data)
+{
+ diff_print_info *pi = data;
+ char code = git_diff_status_char(delta->status);
+ char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
+
+ GIT_UNUSED(progress);
+
+ if (code == ' ')
+ return 0;
+
+ git_buf_clear(pi->buf);
+
+ git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid);
+ git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid);
+
+ git_buf_printf(
+ pi->buf, ":%06o %06o %s... %s... %c",
+ delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code);
+
+ if (delta->similarity > 0)
+ git_buf_printf(pi->buf, "%03u", delta->similarity);
+
+ if (delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED)
+ git_buf_printf(
+ pi->buf, "\t%s %s\n", delta->old_file.path, delta->new_file.path);
+ else
+ git_buf_printf(
+ pi->buf, "\t%s\n", delta->old_file.path ?
+ delta->old_file.path : delta->new_file.path);
+
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ return 0;
+}
+
+int git_diff_print_raw(
+ git_diff_list *diff,
+ git_diff_data_cb print_cb,
+ void *payload)
+{
+ int error;
+ git_buf buf = GIT_BUF_INIT;
+ diff_print_info pi;
+
+ if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
+ error = git_diff_foreach(diff, print_raw, NULL, NULL, &pi);
+
+ git_buf_free(&buf);
+
+ return error;
+}
+
+static int print_oid_range(diff_print_info *pi, const git_diff_delta *delta)
+{
+ char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
+
+ git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.oid);
+ git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.oid);
+
+ /* TODO: Match git diff more closely */
+ if (delta->old_file.mode == delta->new_file.mode) {
+ git_buf_printf(pi->buf, "index %s..%s %o\n",
+ start_oid, end_oid, delta->old_file.mode);
+ } else {
+ if (delta->old_file.mode == 0) {
+ git_buf_printf(pi->buf, "new file mode %o\n", delta->new_file.mode);
+ } else if (delta->new_file.mode == 0) {
+ git_buf_printf(pi->buf, "deleted file mode %o\n", delta->old_file.mode);
+ } else {
+ git_buf_printf(pi->buf, "old mode %o\n", delta->old_file.mode);
+ git_buf_printf(pi->buf, "new mode %o\n", delta->new_file.mode);
+ }
+ git_buf_printf(pi->buf, "index %s..%s\n", start_oid, end_oid);
+ }
+
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ return 0;
+}
+
+static int print_patch_file(
+ const git_diff_delta *delta, float progress, void *data)
+{
+ diff_print_info *pi = data;
+ const char *oldpfx = pi->diff->opts.old_prefix;
+ const char *oldpath = delta->old_file.path;
+ const char *newpfx = pi->diff->opts.new_prefix;
+ const char *newpath = delta->new_file.path;
+
+ GIT_UNUSED(progress);
+
+ if (S_ISDIR(delta->new_file.mode) ||
+ delta->status == GIT_DELTA_UNMODIFIED ||
+ delta->status == GIT_DELTA_IGNORED ||
+ (delta->status == GIT_DELTA_UNTRACKED &&
+ (pi->diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0))
+ return 0;
+
+ if (!oldpfx)
+ oldpfx = DIFF_OLD_PREFIX_DEFAULT;
+
+ if (!newpfx)
+ newpfx = DIFF_NEW_PREFIX_DEFAULT;
+
+ git_buf_clear(pi->buf);
+ git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
+
+ if (print_oid_range(pi, delta) < 0)
+ return -1;
+
+ if (git_oid_iszero(&delta->old_file.oid)) {
+ oldpfx = "";
+ oldpath = "/dev/null";
+ }
+ if (git_oid_iszero(&delta->new_file.oid)) {
+ newpfx = "";
+ newpath = "/dev/null";
+ }
+
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
+ git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
+ git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
+ }
+
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
+ return 0;
+
+ git_buf_clear(pi->buf);
+ git_buf_printf(
+ pi->buf, "Binary files %s%s and %s%s differ\n",
+ oldpfx, oldpath, newpfx, newpath);
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ return 0;
+}
+
+static int print_patch_hunk(
+ const git_diff_delta *d,
+ const git_diff_range *r,
+ const char *header,
+ size_t header_len,
+ void *data)
+{
+ diff_print_info *pi = data;
+
+ if (S_ISDIR(d->new_file.mode))
+ return 0;
+
+ git_buf_clear(pi->buf);
+ if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0)
+ return -1;
+
+ if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ return 0;
+}
+
+static int print_patch_line(
+ const git_diff_delta *delta,
+ const git_diff_range *range,
+ char line_origin, /* GIT_DIFF_LINE value from above */
+ const char *content,
+ size_t content_len,
+ void *data)
+{
+ diff_print_info *pi = data;
+
+ if (S_ISDIR(delta->new_file.mode))
+ return 0;
+
+ git_buf_clear(pi->buf);
+
+ if (line_origin == GIT_DIFF_LINE_ADDITION ||
+ line_origin == GIT_DIFF_LINE_DELETION ||
+ line_origin == GIT_DIFF_LINE_CONTEXT)
+ git_buf_printf(pi->buf, "%c%.*s", line_origin, (int)content_len, content);
+ else if (content_len > 0)
+ git_buf_printf(pi->buf, "%.*s", (int)content_len, content);
+
+ if (git_buf_oom(pi->buf))
+ return -1;
+
+ if (pi->print_cb(delta, range, line_origin,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
+ return 0;
+}
+
+int git_diff_print_patch(
+ git_diff_list *diff,
+ git_diff_data_cb print_cb,
+ void *payload)
+{
+ int error;
+ git_buf buf = GIT_BUF_INIT;
+ diff_print_info pi;
+
+ if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload)))
+ error = git_diff_foreach(
+ diff, print_patch_file, print_patch_hunk, print_patch_line, &pi);
+
+ git_buf_free(&buf);
+
+ return error;
+}
+
+
+static int print_to_buffer_cb(
+ const git_diff_delta *delta,
+ const git_diff_range *range,
+ char line_origin,
+ const char *content,
+ size_t content_len,
+ void *payload)
+{
+ git_buf *output = payload;
+ GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
+ return git_buf_put(output, content, content_len);
+}
+
+int git_diff_patch_print(
+ git_diff_patch *patch,
+ git_diff_data_cb print_cb,
+ void *payload)
+{
+ int error;
+ git_buf temp = GIT_BUF_INIT;
+ diff_print_info pi;
+
+ assert(patch && print_cb);
+
+ if (!(error = diff_print_info_init(
+ &pi, &temp, git_diff_patch__diff(patch), print_cb, payload)))
+ error = git_diff_patch__invoke_callbacks(
+ patch, print_patch_file, print_patch_hunk, print_patch_line, &pi);
+
+ git_buf_free(&temp);
+
+ return error;
+}
+
+int git_diff_patch_to_str(
+ char **string,
+ git_diff_patch *patch)
+{
+ int error;
+ git_buf output = GIT_BUF_INIT;
+
+ error = git_diff_patch_print(patch, print_to_buffer_cb, &output);
+
+ /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
+ * meaning a memory allocation failure, so just map to -1...
+ */
+ if (error == GIT_EUSER)
+ error = -1;
+
+ *string = git_buf_detach(&output);
+
+ return error;
+}
diff --git a/src/diff_tform.c b/src/diff_tform.c
index bc3acae..597c240 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -5,10 +5,14 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
-#include "diff.h"
+
#include "git2/config.h"
#include "git2/blob.h"
+
+#include "diff.h"
#include "hashsig.h"
+#include "path.h"
+#include "fileops.h"
static git_diff_delta *diff_delta__dup(
const git_diff_delta *d, git_pool *pool)
diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c
new file mode 100644
index 0000000..7694fb9
--- /dev/null
+++ b/src/diff_xdiff.c
@@ -0,0 +1,166 @@
+/*
+ * 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_driver.h"
+#include "diff_patch.h"
+#include "diff_xdiff.h"
+
+static int git_xdiff_scan_int(const char **str, int *value)
+{
+ const char *scan = *str;
+ int v = 0, digits = 0;
+ /* find next digit */
+ for (scan = *str; *scan && !git__isdigit(*scan); scan++);
+ /* parse next number */
+ for (; git__isdigit(*scan); scan++, digits++)
+ v = (v * 10) + (*scan - '0');
+ *str = scan;
+ *value = v;
+ return (digits > 0) ? 0 : -1;
+}
+
+static int git_xdiff_parse_hunk(git_diff_range *range, const char *header)
+{
+ /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
+ if (*header != '@')
+ return -1;
+ if (git_xdiff_scan_int(&header, &range->old_start) < 0)
+ return -1;
+ if (*header == ',') {
+ if (git_xdiff_scan_int(&header, &range->old_lines) < 0)
+ return -1;
+ } else
+ range->old_lines = 1;
+ if (git_xdiff_scan_int(&header, &range->new_start) < 0)
+ return -1;
+ if (*header == ',') {
+ if (git_xdiff_scan_int(&header, &range->new_lines) < 0)
+ return -1;
+ } else
+ range->new_lines = 1;
+ if (range->old_start < 0 || range->new_start < 0)
+ return -1;
+
+ return 0;
+}
+
+typedef struct {
+ git_xdiff_output *xo;
+ git_diff_patch *patch;
+ git_diff_range range;
+} git_xdiff_info;
+
+static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
+{
+ git_xdiff_info *info = priv;
+ git_diff_patch *patch = info->patch;
+ const git_diff_delta *delta = git_diff_patch_delta(patch);
+ git_diff_output *output = &info->xo->output;
+
+ if (len == 1) {
+ output->error = git_xdiff_parse_hunk(&info->range, bufs[0].ptr);
+ if (output->error < 0)
+ return output->error;
+
+ if (output->hunk_cb != NULL &&
+ output->hunk_cb(delta, &info->range,
+ bufs[0].ptr, bufs[0].size, output->payload))
+ output->error = GIT_EUSER;
+ }
+
+ if (len == 2 || len == 3) {
+ /* expect " "/"-"/"+", then data */
+ char origin =
+ (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
+ (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
+ GIT_DIFF_LINE_CONTEXT;
+
+ if (output->data_cb != NULL &&
+ output->data_cb(delta, &info->range,
+ origin, bufs[1].ptr, bufs[1].size, output->payload))
+ output->error = GIT_EUSER;
+ }
+
+ if (len == 3 && !output->error) {
+ /* If we have a '+' and a third buf, then we have added a line
+ * without a newline and the old code had one, so DEL_EOFNL.
+ * If we have a '-' and a third buf, then we have removed a line
+ * with out a newline but added a blank line, so ADD_EOFNL.
+ */
+ char origin =
+ (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
+ (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
+ GIT_DIFF_LINE_CONTEXT_EOFNL;
+
+ if (output->data_cb != NULL &&
+ output->data_cb(delta, &info->range,
+ origin, bufs[2].ptr, bufs[2].size, output->payload))
+ output->error = GIT_EUSER;
+ }
+
+ return output->error;
+}
+
+static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
+{
+ git_xdiff_output *xo = (git_xdiff_output *)output;
+ git_xdiff_info info;
+ git_diff_find_context_payload findctxt;
+ mmfile_t xd_old_data, xd_new_data;
+
+ memset(&info, 0, sizeof(info));
+ info.patch = patch;
+ info.xo = xo;
+
+ xo->callback.priv = &info;
+
+ git_diff_find_context_init(
+ &xo->config.find_func, &findctxt, git_diff_patch__driver(patch));
+ xo->config.find_func_priv = &findctxt;
+
+ if (xo->config.find_func != NULL)
+ xo->config.flags |= XDL_EMIT_FUNCNAMES;
+ else
+ xo->config.flags &= ~XDL_EMIT_FUNCNAMES;
+
+ /* TODO: check ofile.opts_flags to see if driver-specific per-file
+ * updates are needed to xo->params.flags
+ */
+
+ git_diff_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch);
+ git_diff_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch);
+
+ xdl_diff(&xd_old_data, &xd_new_data,
+ &xo->params, &xo->config, &xo->callback);
+
+ git_diff_find_context_clear(&findctxt);
+
+ return xo->output.error;
+}
+
+void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
+{
+ uint32_t flags = opts ? opts->flags : GIT_DIFF_NORMAL;
+
+ xo->output.diff_cb = git_xdiff;
+
+ memset(&xo->config, 0, sizeof(xo->config));
+ xo->config.ctxlen = opts ? opts->context_lines : 3;
+ xo->config.interhunkctxlen = opts ? opts->interhunk_lines : 0;
+
+ memset(&xo->params, 0, sizeof(xo->params));
+ if (flags & GIT_DIFF_IGNORE_WHITESPACE)
+ xo->params.flags |= XDF_WHITESPACE_FLAGS;
+ if (flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
+ xo->params.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
+ if (flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
+ xo->params.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
+
+ memset(&xo->callback, 0, sizeof(xo->callback));
+ xo->callback.outf = git_xdiff_cb;
+}
diff --git a/src/diff_xdiff.h b/src/diff_xdiff.h
new file mode 100644
index 0000000..c547b00
--- /dev/null
+++ b/src/diff_xdiff.h
@@ -0,0 +1,28 @@
+/*
+ * 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_xdiff_h__
+#define INCLUDE_diff_xdiff_h__
+
+#include "diff.h"
+#include "diff_patch.h"
+#include "xdiff/xdiff.h"
+
+/* A git_xdiff_output is a git_diff_output with extra fields necessary
+ * to use libxdiff. Calling git_xdiff_init() will set the diff_cb field
+ * of the output to use xdiff to generate the diffs.
+ */
+typedef struct {
+ git_diff_output output;
+
+ xdemitconf_t config;
+ xpparam_t params;
+ xdemitcb_t callback;
+} git_xdiff_output;
+
+void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts);
+
+#endif
diff --git a/src/fetch.c b/src/fetch.c
index b5ec697..03fad5f 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -16,6 +16,8 @@
#include "pack.h"
#include "fetch.h"
#include "netops.h"
+#include "repository.h"
+#include "refs.h"
struct filter_payload {
git_remote *remote;
diff --git a/src/index.c b/src/index.c
index 25c38b0..fd55616 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2030,6 +2030,8 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data);
index_entries_free(&entries);
+ git_vector_free(&entries);
+
git_vector_sort(&index->entries);
return error;
diff --git a/src/iterator.c b/src/iterator.c
index 4360b99..76b0e41 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -7,6 +7,7 @@
#include "iterator.h"
#include "tree.h"
+#include "index.h"
#include "ignore.h"
#include "buffer.h"
#include "git2/submodule.h"
diff --git a/src/merge.c b/src/merge.c
index 047d960..82d2e6f 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -24,6 +24,8 @@
#include "blob.h"
#include "hashsig.h"
#include "oid.h"
+#include "index.h"
+#include "filebuf.h"
#include "git2/types.h"
#include "git2/repository.h"
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 4083ba9..b9e283a 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -9,6 +9,7 @@
#include "hash.h"
#include "repository.h"
#include "fileops.h"
+#include "filebuf.h"
#include "pack.h"
#include "reflog.h"
#include "refdb.h"
diff --git a/src/refs.c b/src/refs.c
index 2b54595..c0e460c 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -9,6 +9,7 @@
#include "hash.h"
#include "repository.h"
#include "fileops.h"
+#include "filebuf.h"
#include "pack.h"
#include "reflog.h"
#include "refdb.h"
diff --git a/src/remote.c b/src/remote.c
index 943b72b..0e8354a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1267,8 +1267,10 @@ static int rename_remote_references(
return -1;
while ((error = git_reference_next(&ref, iter)) == 0) {
- if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR))
+ if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) {
+ git_reference_free(ref);
continue;
+ }
if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) {
git_reference_iterator_free(iter);
diff --git a/src/remote.h b/src/remote.h
index c9c26b7..dce4803 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -11,7 +11,7 @@
#include "git2/transport.h"
#include "refspec.h"
-#include "repository.h"
+#include "vector.h"
#define GIT_REMOTE_ORIGIN "origin"
diff --git a/src/repository.c b/src/repository.c
index 2e7a334..e445149 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -17,12 +17,15 @@
#include "tag.h"
#include "blob.h"
#include "fileops.h"
+#include "filebuf.h"
+#include "index.h"
#include "config.h"
#include "refs.h"
#include "filter.h"
#include "odb.h"
#include "remote.h"
#include "merge.h"
+#include "diff_driver.h"
#define GIT_FILE_CONTENT_PREFIX "gitdir:"
@@ -109,6 +112,9 @@ void git_repository_free(git_repository *repo)
git_cache_free(&repo->objects);
git_submodule_config_free(repo);
+ git_diff_driver_registry_free(repo->diff_drivers);
+ repo->diff_drivers = NULL;
+
git__free(repo->path_repository);
git__free(repo->workdir);
git__free(repo->namespace);
diff --git a/src/repository.h b/src/repository.h
index bd5f63d..12dc50d 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -14,15 +14,13 @@
#include "git2/object.h"
#include "git2/config.h"
-#include "index.h"
#include "cache.h"
#include "refs.h"
#include "buffer.h"
-#include "odb.h"
#include "object.h"
#include "attrcache.h"
#include "strmap.h"
-#include "refdb.h"
+#include "diff_driver.h"
#define DOT_GIT ".git"
#define GIT_DIR DOT_GIT "/"
@@ -108,6 +106,7 @@ struct git_repository {
git_cache objects;
git_attr_cache attrcache;
git_strmap *submodules;
+ git_diff_driver_registry *diff_drivers;
char *path_repository;
char *workdir;
diff --git a/src/signature.c b/src/signature.c
index e338f08..0a34ccf 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -9,6 +9,7 @@
#include "signature.h"
#include "repository.h"
#include "git2/common.h"
+#include "posix.h"
void git_signature_free(git_signature *sig)
{
diff --git a/src/stash.c b/src/stash.c
index 19b29be..1222634 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -14,6 +14,7 @@
#include "git2/stash.h"
#include "git2/status.h"
#include "git2/checkout.h"
+#include "git2/index.h"
#include "signature.h"
static int create_error(int error, const char *msg)
diff --git a/src/status.c b/src/status.c
index 89f3eed..712e0d5 100644
--- a/src/status.c
+++ b/src/status.c
@@ -14,10 +14,10 @@
#include "git2/status.h"
#include "repository.h"
#include "ignore.h"
+#include "index.h"
#include "git2/diff.h"
#include "diff.h"
-#include "diff_output.h"
static unsigned int index_delta2status(git_delta_t index_status)
{
diff --git a/src/submodule.c b/src/submodule.c
index 16114d8..af488b7 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -22,6 +22,8 @@
#include "submodule.h"
#include "tree.h"
#include "iterator.h"
+#include "path.h"
+#include "index.h"
#define GIT_MODULES_FILE ".gitmodules"
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 49b5f3b..f56f61b 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -7,8 +7,6 @@
#ifndef INCLUDE_thread_utils_h__
#define INCLUDE_thread_utils_h__
-#include "common.h"
-
/* Common operations even if threading has been disabled */
typedef struct {
#if defined(GIT_WIN32)
diff --git a/src/tree.c b/src/tree.c
index 10d1314..65d01b4 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -10,6 +10,9 @@
#include "tree.h"
#include "git2/repository.h"
#include "git2/object.h"
+#include "path.h"
+#include "tree-cache.h"
+#include "index.h"
#define DEFAULT_TREE_SIZE 16
#define MAX_FILEMODE_BYTES 6
diff --git a/src/util.h b/src/util.h
index 5ae87ac..43ba792 100644
--- a/src/util.h
+++ b/src/util.h
@@ -194,6 +194,8 @@ extern int git__strcasecmp(const char *a, const char *b);
extern int git__strncmp(const char *a, const char *b, size_t sz);
extern int git__strncasecmp(const char *a, const char *b, size_t sz);
+#include "thread-utils.h"
+
typedef struct {
git_atomic refcount;
void *owner;
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index 78ff5ac..a3a0f8f 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -2,6 +2,7 @@
#include "checkout_helpers.h"
#include "git2/checkout.h"
+#include "fileops.h"
#include "repository.h"
static git_repository *g_repo;
diff --git a/tests-clar/clar.c b/tests-clar/clar.c
index fed87c3..0eae81b 100644
--- a/tests-clar/clar.c
+++ b/tests-clar/clar.c
@@ -183,10 +183,10 @@ clar_run_test(
}
static void
-clar_run_suite(const struct clar_suite *suite)
+clar_run_suite(const struct clar_suite *suite, const char *name)
{
const struct clar_func *test = suite->tests;
- size_t i;
+ size_t i, namelen;
if (!suite->enabled)
return;
@@ -200,7 +200,23 @@ clar_run_suite(const struct clar_suite *suite)
_clar.active_suite = suite->name;
_clar.suite_errors = 0;
+ if (name) {
+ size_t suitelen = strlen(suite->name);
+ namelen = strlen(name);
+ if (namelen <= suitelen) {
+ name = NULL;
+ } else {
+ name += suitelen;
+ while (*name == ':')
+ ++name;
+ namelen = strlen(name);
+ }
+ }
+
for (i = 0; i < suite->test_count; ++i) {
+ if (name && strncmp(test[i].name, name, namelen))
+ continue;
+
_clar.active_test = test[i].name;
clar_run_test(&test[i], &suite->initialize, &suite->cleanup);
@@ -240,7 +256,7 @@ clar_parse_args(int argc, char **argv)
case 'x': { /* given suite name */
int offset = (argument[2] == '=') ? 3 : 2, found = 0;
char action = argument[1];
- size_t j, len;
+ size_t j, len, cmplen;
argument += offset;
len = strlen(argument);
@@ -249,7 +265,11 @@ clar_parse_args(int argc, char **argv)
clar_usage(argv[0]);
for (j = 0; j < _clar_suite_count; ++j) {
- if (strncmp(argument, _clar_suites[j].name, len) == 0) {
+ cmplen = strlen(_clar_suites[j].name);
+ if (cmplen > len)
+ cmplen = len;
+
+ if (strncmp(argument, _clar_suites[j].name, cmplen) == 0) {
int exact = !strcmp(argument, _clar_suites[j].name);
++found;
@@ -258,9 +278,9 @@ clar_parse_args(int argc, char **argv)
_clar.report_suite_names = 1;
switch (action) {
- case 's': clar_run_suite(&_clar_suites[j]); break;
- case 'i': _clar_suites[j].enabled = 1; break;
- case 'x': _clar_suites[j].enabled = 0; break;
+ case 's': clar_run_suite(&_clar_suites[j], argument); break;
+ case 'i': _clar_suites[j].enabled = 1; break;
+ case 'x': _clar_suites[j].enabled = 0; break;
}
if (exact)
@@ -318,7 +338,7 @@ clar_test(int argc, char **argv)
if (!_clar.suites_ran) {
size_t i;
for (i = 0; i < _clar_suite_count; ++i)
- clar_run_suite(&_clar_suites[i]);
+ clar_run_suite(&_clar_suites[i], NULL);
}
clar_print_shutdown(
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 8aae1fb..339b1e7 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -1,8 +1,9 @@
#include "clar_libgit2.h"
#include "git2/clone.h"
-#include "repository.h"
#include "remote.h"
+#include "fileops.h"
+#include "repository.h"
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 2ac8dbc..b12186d 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -120,6 +120,93 @@ void test_diff_blob__can_compare_text_blobs(void)
git_blob_free(c);
}
+void test_diff_blob__can_compare_text_blobs_with_patch(void)
+{
+ git_blob *a, *b, *c;
+ git_oid a_oid, b_oid, c_oid;
+ git_diff_patch *p;
+ size_t tc, ta, td;
+
+ /* tests/resources/attr/root_test1 */
+ cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
+ cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
+
+ /* tests/resources/attr/root_test2 */
+ cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
+ cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
+
+ /* tests/resources/attr/root_test3 */
+ cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
+ cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8));
+
+ /* Doing the equivalent of a `git diff -U1` on these files */
+
+ /* diff on tests/resources/attr/root_test1 */
+ cl_git_pass(git_diff_patch_from_blobs(&p, a, b, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(1, (int)tc);
+ cl_assert_equal_i(5, (int)ta);
+ cl_assert_equal_i(0, (int)td);
+
+ git_diff_patch_free(p);
+
+ /* diff on tests/resources/attr/root_test2 */
+ cl_git_pass(git_diff_patch_from_blobs(&p, b, c, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(15, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(3, (int)tc);
+ cl_assert_equal_i(9, (int)ta);
+ cl_assert_equal_i(3, (int)td);
+
+ git_diff_patch_free(p);
+
+ /* diff on tests/resources/attr/root_test3 */
+ cl_git_pass(git_diff_patch_from_blobs(&p, a, c, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(13, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(0, (int)tc);
+ cl_assert_equal_i(12, (int)ta);
+ cl_assert_equal_i(1, (int)td);
+
+ git_diff_patch_free(p);
+
+ /* one more */
+ cl_git_pass(git_diff_patch_from_blobs(&p, c, d, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(2, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(5, git_diff_patch_num_lines_in_hunk(p, 0));
+ cl_assert_equal_i(9, git_diff_patch_num_lines_in_hunk(p, 1));
+
+ cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(4, (int)tc);
+ cl_assert_equal_i(6, (int)ta);
+ cl_assert_equal_i(4, (int)td);
+
+ git_diff_patch_free(p);
+
+ git_blob_free(a);
+ git_blob_free(b);
+ git_blob_free(c);
+}
+
void test_diff_blob__can_compare_against_null_blobs(void)
{
git_blob *e = NULL;
@@ -175,6 +262,66 @@ void test_diff_blob__can_compare_against_null_blobs(void)
cl_assert_equal_i(0, expected.lines);
}
+void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
+{
+ git_blob *e = NULL;
+ git_diff_patch *p;
+ int line;
+ char origin;
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, d, e, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) {
+ cl_git_pass(git_diff_patch_get_line_in_hunk(
+ &origin, NULL, NULL, NULL, NULL, p, 0, line));
+ cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
+ }
+
+ git_diff_patch_free(p);
+
+ opts.flags |= GIT_DIFF_REVERSE;
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, d, e, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) {
+ cl_git_pass(git_diff_patch_get_line_in_hunk(
+ &origin, NULL, NULL, NULL, NULL, p, 0, line));
+ cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
+ }
+
+ git_diff_patch_free(p);
+
+ opts.flags ^= GIT_DIFF_REVERSE;
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, alien, NULL, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status);
+ cl_assert((git_diff_patch_delta(p)->flags & GIT_DIFF_FLAG_BINARY) != 0);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+
+ git_diff_patch_free(p);
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, NULL, alien, &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status);
+ cl_assert((git_diff_patch_delta(p)->flags & GIT_DIFF_FLAG_BINARY) != 0);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+
+ git_diff_patch_free(p);
+}
+
static void assert_identical_blobs_comparison(diff_expects *expected)
{
cl_assert_equal_i(1, expected->files);
@@ -206,6 +353,29 @@ void test_diff_blob__can_compare_identical_blobs(void)
assert_identical_blobs_comparison(&expected);
}
+void test_diff_blob__can_compare_identical_blobs_with_patch(void)
+{
+ git_diff_patch *p;
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, d, d, &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+ git_diff_patch_free(p);
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, NULL, NULL, &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+ git_diff_patch_free(p);
+
+ cl_git_pass(git_diff_patch_from_blobs(&p, alien, alien, &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+ git_diff_patch_free(p);
+}
+
static void assert_binary_blobs_comparison(diff_expects *expected)
{
cl_assert(expected->files_binary > 0);
@@ -428,6 +598,74 @@ void test_diff_blob__can_compare_blob_to_buffer(void)
git_blob_free(a);
}
+void test_diff_blob__can_compare_blob_to_buffer_with_patch(void)
+{
+ git_diff_patch *p;
+ git_blob *a;
+ git_oid a_oid;
+ const char *a_content = "Hello from the root\n";
+ const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n";
+ size_t tc, ta, td;
+
+ /* tests/resources/attr/root_test1 */
+ cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
+ cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
+
+ /* diff from blob a to content of b */
+ cl_git_pass(git_diff_patch_from_blob_and_buffer(
+ &p, a, b_content, strlen(b_content), &opts));
+
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0));
+
+ cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(1, (int)tc);
+ cl_assert_equal_i(5, (int)ta);
+ cl_assert_equal_i(0, (int)td);
+
+ git_diff_patch_free(p);
+
+ /* diff from blob a to content of a */
+ cl_git_pass(git_diff_patch_from_blob_and_buffer(
+ &p, a, a_content, strlen(a_content), &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p));
+ git_diff_patch_free(p);
+
+ /* diff from NULL blob to content of a */
+ cl_git_pass(git_diff_patch_from_blob_and_buffer(
+ &p, NULL, a_content, strlen(a_content), &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0));
+ git_diff_patch_free(p);
+
+ /* diff from blob a to NULL buffer */
+ cl_git_pass(git_diff_patch_from_blob_and_buffer(
+ &p, a, NULL, 0, &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0));
+ git_diff_patch_free(p);
+
+ /* diff with reverse */
+ opts.flags ^= GIT_DIFF_REVERSE;
+
+ cl_git_pass(git_diff_patch_from_blob_and_buffer(
+ &p, a, NULL, 0, &opts));
+ cl_assert(p != NULL);
+ cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status);
+ cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p));
+ cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0));
+ git_diff_patch_free(p);
+
+ git_blob_free(a);
+}
static void assert_one_modified_with_lines(diff_expects *expected, int lines)
{
diff --git a/tests-clar/diff/drivers.c b/tests-clar/diff/drivers.c
new file mode 100644
index 0000000..06ab2ff
--- /dev/null
+++ b/tests-clar/diff/drivers.c
@@ -0,0 +1,125 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+#include "repository.h"
+#include "diff_driver.h"
+
+static git_repository *g_repo = NULL;
+
+void test_diff_drivers__initialize(void)
+{
+}
+
+void test_diff_drivers__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+ g_repo = NULL;
+}
+
+void test_diff_drivers__patterns(void)
+{
+ git_config *cfg;
+ const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13";
+ git_tree *one;
+ git_diff_list *diff;
+ git_diff_patch *patch;
+ char *text;
+ const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
+ const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n";
+ const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
+
+ g_repo = cl_git_sandbox_init("renames");
+
+ one = resolve_commit_oid_to_tree(g_repo, one_sha);
+
+ /* no diff */
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(0, (int)git_diff_num_deltas(diff));
+ git_diff_list_free(diff);
+
+ /* default diff */
+
+ cl_git_append2file("renames/untimely.txt", "\r\nSome new stuff\r\n");
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
+
+ cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
+ cl_git_pass(git_diff_patch_to_str(&text, patch));
+ cl_assert_equal_s(expected0, text);
+
+ git__free(text);
+ git_diff_patch_free(patch);
+ git_diff_list_free(diff);
+
+ /* attribute diff set to false */
+
+ cl_git_rewritefile("renames/.gitattributes", "untimely.txt -diff\n");
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
+
+ cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
+ cl_git_pass(git_diff_patch_to_str(&text, patch));
+ cl_assert_equal_s(expected1, text);
+
+ git__free(text);
+ git_diff_patch_free(patch);
+ git_diff_list_free(diff);
+
+ /* attribute diff set to unconfigured value (should use default) */
+
+ cl_git_rewritefile("renames/.gitattributes", "untimely.txt diff=kipling0\n");
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
+
+ cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
+ cl_git_pass(git_diff_patch_to_str(&text, patch));
+ cl_assert_equal_s(expected0, text);
+
+ git__free(text);
+ git_diff_patch_free(patch);
+ git_diff_list_free(diff);
+
+ /* let's define that driver */
+
+ cl_git_pass(git_repository_config(&cfg, g_repo));
+ cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 1));
+ git_config_free(cfg);
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
+
+ cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
+ cl_git_pass(git_diff_patch_to_str(&text, patch));
+ cl_assert_equal_s(expected1, text);
+
+ git__free(text);
+ git_diff_patch_free(patch);
+ git_diff_list_free(diff);
+
+ /* let's use a real driver with some regular expressions */
+
+ git_diff_driver_registry_free(g_repo->diff_drivers);
+ g_repo->diff_drivers = NULL;
+
+ cl_git_pass(git_repository_config(&cfg, g_repo));
+ cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0));
+ cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H"));
+ git_config_free(cfg);
+
+ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
+ cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
+
+ cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
+ cl_git_pass(git_diff_patch_to_str(&text, patch));
+ cl_assert_equal_s(expected2, text);
+
+ git__free(text);
+ git_diff_patch_free(patch);
+ git_diff_list_free(diff);
+
+ git_tree_free(one);
+}
+
diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c
index f9e913a..3f14a0d 100644
--- a/tests-clar/diff/patch.c
+++ b/tests-clar/diff/patch.c
@@ -543,7 +543,7 @@ void test_diff_patch__line_counts_with_eofnl(void)
"index 378a7d9..3d0154e 100644\n"
"--- a/songof7cities.txt\n"
"+++ b/songof7cities.txt\n"
- "@@ -42,7 +42,7 @@\n"
+ "@@ -42,7 +42,7 @@ With peoples undefeated of the dark, enduring blood.\n"
" \n"
" To the sound of trumpets shall their seed restore my Cities\n"
" Wealthy and well-weaponed, that once more may I behold\n"
diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c
index 8bff96c..ca3f506 100644
--- a/tests-clar/diff/rename.c
+++ b/tests-clar/diff/rename.c
@@ -558,7 +558,7 @@ void test_diff_rename__patch(void)
git_diff_patch *patch;
const git_diff_delta *delta;
char *text;
- const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n";
+ const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n";
old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
new_tree = resolve_commit_oid_to_tree(g_repo, sha1);
diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c
index f152af4..6e52a63 100644
--- a/tests-clar/diff/submodules.c
+++ b/tests-clar/diff/submodules.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "repository.h"
+#include "posix.h"
#include "../submodule/submodule_helpers.h"
static git_repository *g_repo = NULL;
diff --git a/tests-clar/fetchhead/nonetwork.c b/tests-clar/fetchhead/nonetwork.c
index ef30679..a68ebb0 100644
--- a/tests-clar/fetchhead/nonetwork.c
+++ b/tests-clar/fetchhead/nonetwork.c
@@ -1,6 +1,6 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "fetchhead.h"
#include "fetchhead_data.h"
diff --git a/tests-clar/merge/merge_helpers.c b/tests-clar/merge/merge_helpers.c
index bc31b1f..e409278 100644
--- a/tests-clar/merge/merge_helpers.c
+++ b/tests-clar/merge/merge_helpers.c
@@ -1,5 +1,5 @@
#include "clar_libgit2.h"
-#include "buffer.h"
+#include "fileops.h"
#include "refs.h"
#include "tree.h"
#include "merge_helpers.h"
diff --git a/tests-clar/odb/alternates.c b/tests-clar/odb/alternates.c
index be7bfa9..4e876c2 100644
--- a/tests-clar/odb/alternates.c
+++ b/tests-clar/odb/alternates.c
@@ -1,6 +1,6 @@
#include "clar_libgit2.h"
#include "odb.h"
-#include "repository.h"
+#include "filebuf.h"
static git_buf destpath, filepath;
static const char *paths[] = {
diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c
index aa12e47..bc4285a 100644
--- a/tests-clar/online/clone.c
+++ b/tests-clar/online/clone.c
@@ -2,8 +2,9 @@
#include "git2/clone.h"
#include "git2/cred_helpers.h"
-#include "repository.h"
#include "remote.h"
+#include "fileops.h"
+#include "refs.h"
#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
diff --git a/tests-clar/online/fetchhead.c b/tests-clar/online/fetchhead.c
index e14ae09..58717ee 100644
--- a/tests-clar/online/fetchhead.c
+++ b/tests-clar/online/fetchhead.c
@@ -1,6 +1,6 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "fetchhead.h"
#include "../fetchhead/fetchhead_data.h"
#include "git2/clone.h"
diff --git a/tests-clar/refs/delete.c b/tests-clar/refs/delete.c
index 053f412..973768a 100644
--- a/tests-clar/refs/delete.c
+++ b/tests-clar/refs/delete.c
@@ -1,7 +1,8 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "git2/reflog.h"
+#include "git2/refdb.h"
#include "reflog.h"
#include "ref_helpers.h"
@@ -31,7 +32,7 @@ void test_refs_delete__packed_loose(void)
git_buf temp_path = GIT_BUF_INIT;
/* Ensure the loose reference exists on the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
cl_assert(git_path_exists(temp_path.ptr));
/* Lookup the reference */
diff --git a/tests-clar/refs/pack.c b/tests-clar/refs/pack.c
index 412c4c5..d8d5cc6 100644
--- a/tests-clar/refs/pack.c
+++ b/tests-clar/refs/pack.c
@@ -1,8 +1,10 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "git2/reflog.h"
+#include "git2/refdb.h"
#include "reflog.h"
+#include "refs.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
@@ -33,7 +35,7 @@ void test_refs_pack__empty(void)
// create a packfile for an empty folder
git_buf temp_path = GIT_BUF_INIT;
- cl_git_pass(git_buf_join_n(&temp_path, '/', 3, g_repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir"));
+ cl_git_pass(git_buf_join_n(&temp_path, '/', 3, git_repository_path(g_repo), GIT_REFS_HEADS_DIR, "empty_dir"));
cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
git_buf_free(&temp_path);
@@ -60,7 +62,7 @@ void test_refs_pack__loose(void)
packall();
/* Ensure the packed-refs file exists */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE));
cl_assert(git_path_exists(temp_path.ptr));
/* Ensure the known ref can still be looked up but is now packed */
@@ -69,7 +71,7 @@ void test_refs_pack__loose(void)
cl_assert_equal_s(reference->name, loose_tag_ref_name);
/* Ensure the known ref has been removed from the loose folder structure */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, loose_tag_ref_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name));
cl_assert(!git_path_exists(temp_path.ptr));
git_reference_free(reference);
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
index 1cd0ddd..095cabf 100644
--- a/tests-clar/refs/reflog/reflog.c
+++ b/tests-clar/refs/reflog/reflog.c
@@ -1,6 +1,6 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "git2/reflog.h"
#include "reflog.h"
diff --git a/tests-clar/refs/rename.c b/tests-clar/refs/rename.c
index 5ab84c4..543bc4d 100644
--- a/tests-clar/refs/rename.c
+++ b/tests-clar/refs/rename.c
@@ -1,8 +1,9 @@
#include "clar_libgit2.h"
-#include "repository.h"
+#include "fileops.h"
#include "git2/reflog.h"
#include "reflog.h"
+#include "refs.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
@@ -38,7 +39,7 @@ void test_refs_rename__loose(void)
const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu";
/* Ensure the ref doesn't exist on the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, new_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name));
cl_assert(!git_path_exists(temp_path.ptr));
/* Retrieval of the reference to rename */
@@ -64,7 +65,7 @@ void test_refs_rename__loose(void)
cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref can be found in the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, new_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name));
cl_assert(git_path_exists(temp_path.ptr));
git_reference_free(new_ref);
@@ -80,7 +81,7 @@ void test_refs_rename__packed(void)
const char *brand_new_name = "refs/heads/brand_new_name";
/* Ensure the ref doesn't exist on the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_head_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_head_name));
cl_assert(!git_path_exists(temp_path.ptr));
/* The reference can however be looked-up... */
@@ -106,7 +107,7 @@ void test_refs_rename__packed(void)
cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref now happily lives in the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, brand_new_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), brand_new_name));
cl_assert(git_path_exists(temp_path.ptr));
git_reference_free(new_ref);
@@ -122,7 +123,7 @@ void test_refs_rename__packed_doesnt_pack_others(void)
const char *brand_new_name = "refs/heads/brand_new_name";
/* Ensure the other reference exists on the file system */
- cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name));
+ cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
cl_assert(git_path_exists(temp_path.ptr));
/* Lookup the other reference */
diff --git a/tests-clar/repo/discover.c b/tests-clar/repo/discover.c
index 3d9aeed..f93ff24 100644
--- a/tests-clar/repo/discover.c
+++ b/tests-clar/repo/discover.c
@@ -1,9 +1,9 @@
#include "clar_libgit2.h"
#include "odb.h"
+#include "fileops.h"
#include "repository.h"
-
#define TEMP_REPO_FOLDER "temprepo/"
#define DISCOVER_FOLDER TEMP_REPO_FOLDER "discover.git"
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 2d3898b..4f6879c 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -169,7 +169,7 @@ void test_status_ignore__ignore_pattern_ignorecase(void)
cl_git_mkfile("empty_standard_repo/A.txt", "Differs in case");
cl_git_pass(git_repository_index(&index, g_repo));
- ignore_case = index->ignore_case;
+ ignore_case = (git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0;
git_index_free(index);
cl_git_pass(git_status_file(&flags, g_repo, "A.txt"));
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 062a09a..1333584 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -105,7 +105,7 @@ void test_status_worktree__swap_subdir_and_file(void)
bool ignore_case;
cl_git_pass(git_repository_index(&index, repo));
- ignore_case = index->ignore_case;
+ ignore_case = (git_index_caps(index) & GIT_INDEXCAP_IGNORE_CASE) != 0;
git_index_free(index);
/* first alter the contents of the worktree */