Merge pull request #1916 from libgit2/simplify-examples Fix examples to make the important stuff more obvious
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
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index c20a6df..596be45 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -9,6 +9,8 @@ ENDIF()
FILE(GLOB SRC_EXAMPLE_APPS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
FOREACH(src_app ${SRC_EXAMPLE_APPS})
STRING(REPLACE ".c" "" app_name ${src_app})
- ADD_EXECUTABLE(${app_name} ${src_app})
- TARGET_LINK_LIBRARIES(${app_name} git2)
+ IF(NOT ${app_name} STREQUAL "common")
+ ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
+ TARGET_LINK_LIBRARIES(${app_name} git2)
+ ENDIF()
ENDFOREACH()
diff --git a/examples/Makefile b/examples/Makefile
index d53ed82..f4d5557 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -8,7 +8,7 @@ APPS = general showindex diff rev-list cat-file status log rev-parse init
all: $(APPS)
% : %.c
- $(CC) -o $@ $(CFLAGS) $< $(LFLAGS)
+ $(CC) -o $@ common.c $(CFLAGS) $< $(LFLAGS)
clean:
$(RM) $(APPS)
diff --git a/examples/README.md b/examples/README.md
index f2b6d7d..494d329 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -1,11 +1,19 @@
libgit2 examples
================
-These examples are meant as thin, easy-to-read snippets for Docurium
-(https://github.com/github/docurium) rather than full-blown
-implementations of Git commands. They are not vetted as carefully
-for bugs, error handling, or cross-platform compatibility as the
-rest of the code in libgit2, so copy with some caution.
+These examples are a mixture of basic emulation of core Git command line
+functions and simple snippets demonstrating libgit2 API usage (for use
+with Docurium). As a whole, they are not vetted carefully for bugs, error
+handling, and cross-platform compatibility in the same manner as the rest
+of the code in libgit2, so copy with caution.
-For HTML versions, check "Examples" at http://libgit2.github.com/libgit2
+That being said, you are welcome to copy code from these examples as
+desired when using libgit2.
+For annotated HTML versions, see the "Examples" section of:
+
+ http://libgit2.github.com/libgit2
+
+such as:
+
+ http://libgit2.github.com/libgit2/ex/HEAD/general.html
diff --git a/examples/add.c b/examples/add.c
index a0edf43..999a41e 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -1,6 +1,11 @@
-#include <git2.h>
-#include <stdio.h>
-#include <string.h>
+/*
+ * 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 <assert.h>
enum print_options {
@@ -14,19 +19,49 @@ struct print_payload {
git_repository *repo;
};
-void init_array(git_strarray *array, int argc, char **argv)
+/* Forward declarations for helpers */
+static void parse_opts(int *options, int *count, int argc, char *argv[]);
+void init_array(git_strarray *array, int argc, char **argv);
+int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
+
+int main (int argc, char** argv)
{
- unsigned int i;
+ git_index_matched_path_cb matched_cb = NULL;
+ git_repository *repo = NULL;
+ git_index *index;
+ git_strarray array = {0};
+ int options = 0, count = 0;
+ struct print_payload payload = {0};
- array->count = argc;
- array->strings = malloc(sizeof(char*) * array->count);
- assert(array->strings!=NULL);
+ git_threads_init();
- for(i=0; i<array->count; i++) {
- array->strings[i]=argv[i];
+ parse_opts(&options, &count, argc, argv);
+
+ init_array(&array, argc-count, argv+count);
+
+ check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
+ check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
+
+ if (options&VERBOSE || options&SKIP) {
+ matched_cb = &print_matched_cb;
}
- return;
+ payload.options = options;
+ payload.repo = repo;
+
+ if (options&UPDATE) {
+ git_index_update_all(index, &array, matched_cb, &payload);
+ } else {
+ git_index_add_all(index, &array, 0, matched_cb, &payload);
+ }
+
+ git_index_write(index);
+ git_index_free(index);
+ git_repository_free(repo);
+
+ git_threads_shutdown();
+
+ return 0;
}
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
@@ -55,36 +90,46 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
return ret;
}
+void init_array(git_strarray *array, int argc, char **argv)
+{
+ unsigned int i;
+
+ array->count = argc;
+ array->strings = malloc(sizeof(char*) * array->count);
+ assert(array->strings!=NULL);
+
+ for(i=0; i<array->count; i++) {
+ array->strings[i]=argv[i];
+ }
+
+ return;
+}
+
void print_usage(void)
{
fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
fprintf(stderr, "\t-n, --dry-run dry run\n");
fprintf(stderr, "\t-v, --verbose be verbose\n");
fprintf(stderr, "\t-u, --update update tracked files\n");
+ exit(1);
}
-
-int main (int argc, char** argv)
+static void parse_opts(int *options, int *count, int argc, char *argv[])
{
- git_index_matched_path_cb matched_cb = NULL;
- git_repository *repo = NULL;
- git_index *index;
- git_strarray array = {0};
- int i, options = 0;
- struct print_payload payload = {0};
+ int i;
for (i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
break;
}
else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
- options |= VERBOSE;
+ *options |= VERBOSE;
}
else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
- options |= SKIP;
+ *options |= SKIP;
}
else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
- options |= UPDATE;
+ *options |= UPDATE;
}
else if(!strcmp(argv[i], "-h")) {
print_usage();
@@ -97,47 +142,11 @@ int main (int argc, char** argv)
else {
fprintf(stderr, "Unsupported option %s.\n", argv[i]);
print_usage();
- return 1;
}
}
- if (argc<=i) {
+ if (argc<=i)
print_usage();
- return 1;
- }
- git_threads_init();
-
- init_array(&array, argc-i, argv+i);
-
- if (git_repository_open(&repo, ".") < 0) {
- fprintf(stderr, "No git repository\n");
- return 1;
- }
-
- if (git_repository_index(&index, repo) < 0) {
- fprintf(stderr, "Could not open repository index\n");
- return 1;
- }
-
- if (options&VERBOSE || options&SKIP) {
- matched_cb = &print_matched_cb;
- }
-
- payload.options = options;
- payload.repo = repo;
-
- if (options&UPDATE) {
- git_index_update_all(index, &array, matched_cb, &payload);
- } else {
- git_index_add_all(index, &array, 0, matched_cb, &payload);
- }
-
- git_index_write(index);
- git_index_free(index);
- git_repository_free(repo);
-
- git_threads_shutdown();
-
- return 0;
+ *count = i;
}
diff --git a/examples/cat-file.c b/examples/cat-file.c
index ebb6cb0..5e54762 100644
--- a/examples/cat-file.c
+++ b/examples/cat-file.c
@@ -1,37 +1,11 @@
-#include <stdio.h>
-#include <git2.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * 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.
+ */
-static git_repository *g_repo;
-
-static void check(int error, const char *message)
-{
- if (error) {
- fprintf(stderr, "%s (%d)\n", message, error);
- exit(1);
- }
-}
-
-static void usage(const char *message, const char *arg)
-{
- if (message && arg)
- fprintf(stderr, "%s: %s\n", message, arg);
- else if (message)
- fprintf(stderr, "%s\n", message);
- fprintf(stderr, "usage: cat-file (-t | -s | -e | -p) [<options>] <object>\n");
- exit(1);
-}
-
-static int check_str_param(
- const char *arg, const char *pattern, const char **val)
-{
- size_t len = strlen(pattern);
- if (strncmp(arg, pattern, len))
- return 0;
- *val = (const char *)(arg + len);
- return 1;
-}
+#include "common.h"
static void print_signature(const char *header, const git_signature *sig)
{
@@ -57,12 +31,14 @@ static void print_signature(const char *header, const git_signature *sig)
sign, hours, minutes);
}
+/** Printing out a blob is simple, get the contents and print */
static void show_blob(const git_blob *blob)
{
/* ? Does this need crlf filtering? */
fwrite(git_blob_rawcontent(blob), git_blob_rawsize(blob), 1, stdout);
}
+/** Show each entry with its type, id and attributes */
static void show_tree(const git_tree *tree)
{
size_t i, max_i = (int)git_tree_entrycount(tree);
@@ -81,6 +57,9 @@ static void show_tree(const git_tree *tree)
}
}
+/**
+ * Commits and tags have a few interesting fields in their header.
+ */
static void show_commit(const git_commit *commit)
{
unsigned int i, max_i;
@@ -123,53 +102,34 @@ enum {
SHOW_PRETTY = 4
};
+/* Forward declarations for option-parsing helper */
+struct opts {
+ const char *dir;
+ const char *rev;
+ int action;
+ int verbose;
+};
+static void parse_opts(struct opts *o, int argc, char *argv[]);
+
+
+/** Entry point for this command */
int main(int argc, char *argv[])
{
- const char *dir = ".", *rev = NULL;
- int i, action = 0, verbose = 0;
+ git_repository *repo;
+ struct opts o = { ".", NULL, 0, 0 };
git_object *obj = NULL;
char oidstr[GIT_OID_HEXSZ + 1];
git_threads_init();
- for (i = 1; i < argc; ++i) {
- char *a = argv[i];
-
- if (a[0] != '-') {
- if (rev != NULL)
- usage("Only one rev should be provided", NULL);
- else
- rev = a;
- }
- else if (!strcmp(a, "-t"))
- action = SHOW_TYPE;
- else if (!strcmp(a, "-s"))
- action = SHOW_SIZE;
- else if (!strcmp(a, "-e"))
- action = SHOW_NONE;
- else if (!strcmp(a, "-p"))
- action = SHOW_PRETTY;
- else if (!strcmp(a, "-q"))
- verbose = 0;
- else if (!strcmp(a, "-v"))
- verbose = 1;
- else if (!strcmp(a, "--help") || !strcmp(a, "-h"))
- usage(NULL, NULL);
- else if (!check_str_param(a, "--git-dir=", &dir))
- usage("Unknown option", a);
- }
-
- if (!action || !rev)
- usage(NULL, NULL);
+ parse_opts(&o, argc, argv);
- check(git_repository_open_ext(&g_repo, dir, 0, NULL),
- "Could not open repository");
+ check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
+ "Could not open repository", NULL);
+ check_lg2(git_revparse_single(&obj, repo, o.rev),
+ "Could not resolve", o.rev);
- if (git_revparse_single(&obj, g_repo, rev) < 0) {
- fprintf(stderr, "Could not resolve '%s'\n", rev);
- exit(1);
- }
- if (verbose) {
+ if (o.verbose) {
char oidstr[GIT_OID_HEXSZ + 1];
git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj));
@@ -177,7 +137,7 @@ int main(int argc, char *argv[])
git_object_type2string(git_object_type(obj)), oidstr);
}
- switch (action) {
+ switch (o.action) {
case SHOW_TYPE:
printf("%s\n", git_object_type2string(git_object_type(obj)));
break;
@@ -185,9 +145,9 @@ int main(int argc, char *argv[])
git_odb *odb;
git_odb_object *odbobj;
- check(git_repository_odb(&odb, g_repo), "Could not open ODB");
- check(git_odb_read(&odbobj, odb, git_object_id(obj)),
- "Could not find obj");
+ check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL);
+ check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)),
+ "Could not find obj", NULL);
printf("%ld\n", (long)git_odb_object_size(odbobj));
@@ -221,9 +181,59 @@ int main(int argc, char *argv[])
}
git_object_free(obj);
- git_repository_free(g_repo);
+ git_repository_free(repo);
git_threads_shutdown();
return 0;
}
+
+/** Print out usage information */
+static void usage(const char *message, const char *arg)
+{
+ if (message && arg)
+ fprintf(stderr, "%s: %s\n", message, arg);
+ else if (message)
+ fprintf(stderr, "%s\n", message);
+ fprintf(stderr,
+ "usage: cat-file (-t | -s | -e | -p) [-v] [-q] "
+ "[-h|--help] [--git-dir=<dir>] <object>\n");
+ exit(1);
+}
+
+/** Parse the command-line options taken from git */
+static void parse_opts(struct opts *o, int argc, char *argv[])
+{
+ struct args_info args = ARGS_INFO_INIT;
+
+ for (args.pos = 1; args.pos < argc; ++args.pos) {
+ char *a = argv[args.pos];
+
+ if (a[0] != '-') {
+ if (o->rev != NULL)
+ usage("Only one rev should be provided", NULL);
+ else
+ o->rev = a;
+ }
+ else if (!strcmp(a, "-t"))
+ o->action = SHOW_TYPE;
+ else if (!strcmp(a, "-s"))
+ o->action = SHOW_SIZE;
+ else if (!strcmp(a, "-e"))
+ o->action = SHOW_NONE;
+ else if (!strcmp(a, "-p"))
+ o->action = SHOW_PRETTY;
+ else if (!strcmp(a, "-q"))
+ o->verbose = 0;
+ else if (!strcmp(a, "-v"))
+ o->verbose = 1;
+ else if (!strcmp(a, "--help") || !strcmp(a, "-h"))
+ usage(NULL, NULL);
+ else if (!match_str_arg(&o->dir, &args, "--git-dir"))
+ usage("Unknown option", a);
+ }
+
+ if (!o->action || !o->rev)
+ usage(NULL, NULL);
+
+}
diff --git a/examples/common.c b/examples/common.c
new file mode 100644
index 0000000..5972bc5
--- /dev/null
+++ b/examples/common.c
@@ -0,0 +1,184 @@
+/*
+ * 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"
+
+void check_lg2(int error, const char *message, const char *extra)
+{
+ const git_error *lg2err;
+ const char *lg2msg = "", *lg2spacer = "";
+
+ if (!error)
+ return;
+
+ if ((lg2err = giterr_last()) != NULL && lg2err->message != NULL) {
+ lg2msg = lg2err->message;
+ lg2spacer = " - ";
+ }
+
+ if (extra)
+ fprintf(stderr, "%s '%s' [%d]%s%s\n",
+ message, extra, error, lg2spacer, lg2msg);
+ else
+ fprintf(stderr, "%s [%d]%s%s\n",
+ message, error, lg2spacer, lg2msg);
+
+ exit(1);
+}
+
+void fatal(const char *message, const char *extra)
+{
+ if (extra)
+ fprintf(stderr, "%s %s\n", message, extra);
+ else
+ fprintf(stderr, "%s\n", message);
+
+ exit(1);
+}
+
+size_t is_prefixed(const char *str, const char *pfx)
+{
+ size_t len = strlen(pfx);
+ return strncmp(str, pfx, len) ? 0 : len;
+}
+
+int match_str_arg(
+ const char **out, struct args_info *args, const char *opt)
+{
+ const char *found = args->argv[args->pos];
+ size_t len = is_prefixed(found, opt);
+
+ if (!len)
+ return 0;
+
+ if (!found[len]) {
+ if (args->pos + 1 == args->argc)
+ fatal("expected value following argument", opt);
+ args->pos += 1;
+ *out = args->argv[args->pos];
+ return 1;
+ }
+
+ if (found[len] == '=') {
+ *out = found + len + 1;
+ return 1;
+ }
+
+ return 0;
+}
+
+static const char *match_numeric_arg(struct args_info *args, const char *opt)
+{
+ const char *found = args->argv[args->pos];
+ size_t len = is_prefixed(found, opt);
+
+ if (!len)
+ return NULL;
+
+ if (!found[len]) {
+ if (args->pos + 1 == args->argc)
+ fatal("expected numeric value following argument", opt);
+ args->pos += 1;
+ found = args->argv[args->pos];
+ } else {
+ found = found + len;
+ if (*found == '=')
+ found++;
+ }
+
+ return found;
+}
+
+int match_uint16_arg(
+ uint16_t *out, struct args_info *args, const char *opt)
+{
+ const char *found = match_numeric_arg(args, opt);
+ uint16_t val;
+ char *endptr = NULL;
+
+ if (!found)
+ return 0;
+
+ val = (uint16_t)strtoul(found, &endptr, 0);
+ if (!endptr || *endptr != '\0')
+ fatal("expected number after argument", opt);
+
+ if (out)
+ *out = val;
+ return 1;
+}
+
+static int match_int_internal(
+ int *out, const char *str, int allow_negative, const char *opt)
+{
+ char *endptr = NULL;
+ int val = (int)strtol(str, &endptr, 10);
+
+ if (!endptr || *endptr != '\0')
+ fatal("expected number", opt);
+ else if (val < 0 && !allow_negative)
+ fatal("negative values are not allowed", opt);
+
+ if (out)
+ *out = val;
+
+ return 1;
+}
+
+int is_integer(int *out, const char *str, int allow_negative)
+{
+ return match_int_internal(out, str, allow_negative, NULL);
+}
+
+int match_int_arg(
+ int *out, struct args_info *args, const char *opt, int allow_negative)
+{
+ const char *found = match_numeric_arg(args, opt);
+ if (!found)
+ return 0;
+ return match_int_internal(out, found, allow_negative, opt);
+}
+
+int diff_output(
+ const git_diff_delta *d,
+ const git_diff_hunk *h,
+ const git_diff_line *l,
+ void *p)
+{
+ FILE *fp = p;
+
+ (void)d; (void)h;
+
+ if (!fp)
+ fp = stdout;
+
+ if (l->origin == GIT_DIFF_LINE_CONTEXT ||
+ l->origin == GIT_DIFF_LINE_ADDITION ||
+ l->origin == GIT_DIFF_LINE_DELETION)
+ fputc(l->origin, fp);
+
+ fwrite(l->content, 1, l->content_len, fp);
+
+ return 0;
+}
+
+void treeish_to_tree(
+ git_tree **out, git_repository *repo, const char *treeish)
+{
+ git_object *obj = NULL;
+
+ check_lg2(
+ git_revparse_single(&obj, repo, treeish),
+ "looking up object", treeish);
+
+ check_lg2(
+ git_object_peel((git_object **)out, obj, GIT_OBJ_TREE),
+ "resolving object to tree", treeish);
+
+ git_object_free(obj);
+}
+
diff --git a/examples/common.h b/examples/common.h
new file mode 100644
index 0000000..5ffc9c8
--- /dev/null
+++ b/examples/common.h
@@ -0,0 +1,80 @@
+/*
+ * 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <git2.h>
+
+/**
+ * Check libgit2 error code, printing error to stderr on failure and
+ * exiting the program.
+ */
+extern void check_lg2(int error, const char *message, const char *extra);
+
+/**
+ * Exit the program, printing error to stderr
+ */
+extern void fatal(const char *message, const char *extra);
+
+/**
+ * Check if a string has the given prefix. Returns 0 if not prefixed
+ * or the length of the prefix if it is.
+ */
+extern size_t is_prefixed(const char *str, const char *pfx);
+
+/**
+ * Match an integer string, returning 1 if matched, 0 if not.
+ */
+extern int is_integer(int *out, const char *str, int allow_negative);
+
+struct args_info {
+ int argc;
+ char **argv;
+ int pos;
+};
+#define ARGS_INFO_INIT { argc, argv, 0 }
+
+/**
+ * Check current `args` entry against `opt` string. If it matches
+ * exactly, take the next arg as a string; if it matches as a prefix with
+ * an equal sign, take the remainder as a string; otherwise return 0.
+ */
+extern int match_str_arg(
+ const char **out, struct args_info *args, const char *opt);
+
+/**
+ * Check current `args` entry against `opt` string parsing as uint16. If
+ * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
+ * is a prefix (equal sign optional), take the remainder of the arg as a
+ * uint16_t value; otherwise return 0.
+ */
+extern int match_uint16_arg(
+ uint16_t *out, struct args_info *args, const char *opt);
+
+/**
+ * Check current `args` entry against `opt` string parsing as int. If
+ * `opt` matches exactly, take the next arg as an int value; if it matches
+ * as a prefix (equal sign optional), take the remainder of the arg as a
+ * int value; otherwise return 0.
+ */
+extern int match_int_arg(
+ int *out, struct args_info *args, const char *opt, int allow_negative);
+
+/**
+ * Basic output function for plain text diff output
+ * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
+ */
+extern int diff_output(
+ const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
+
+/**
+ * Convert a treeish argument to an actual tree; this will call check_lg2
+ * and exit the program if `treeish` cannot be resolved to a tree
+ */
+extern void treeish_to_tree(
+ git_tree **out, git_repository *repo, const char *treeish);
diff --git a/examples/diff.c b/examples/diff.c
index 694621f..b141564 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -1,49 +1,149 @@
-#include <stdio.h>
-#include <git2.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * 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"
+
+/**
+ * This example demonstrates the use of the libgit2 diff APIs to
+ * create `git_diff` objects and display them, emulating a number of
+ * core Git `diff` command line options.
+ *
+ * This covers on a portion of the core Git diff options and doesn't
+ * have particularly good error handling, but it should show most of
+ * the core libgit2 diff APIs, including various types of diffs and
+ * how to do renaming detection and patch formatting.
+ */
+
+static const char *colors[] = {
+ "\033[m", /* reset */
+ "\033[1m", /* bold */
+ "\033[31m", /* red */
+ "\033[32m", /* green */
+ "\033[36m" /* cyan */
+};
-static void check(int error, const char *message)
-{
- if (error) {
- fprintf(stderr, "%s (%d)\n", message, error);
- exit(1);
- }
-}
+/** The 'opts' struct captures all the various parsed command line options. */
+struct opts {
+ git_diff_options diffopts;
+ git_diff_find_options findopts;
+ int color;
+ int cached;
+ git_diff_format_t format;
+ const char *treeish1;
+ const char *treeish2;
+ const char *dir;
+};
+
+/** These functions are implemented at the end */
+static void parse_opts(struct opts *o, int argc, char *argv[]);
+static int color_printer(
+ const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
-static int resolve_to_tree(
- git_repository *repo, const char *identifier, git_tree **tree)
+int main(int argc, char *argv[])
{
- int err = 0;
- git_object *obj = NULL;
-
- if ((err = git_revparse_single(&obj, repo, identifier)) < 0)
- return err;
-
- switch (git_object_type(obj)) {
- case GIT_OBJ_TREE:
- *tree = (git_tree *)obj;
- break;
- case GIT_OBJ_COMMIT:
- err = git_commit_tree(tree, (git_commit *)obj);
- git_object_free(obj);
- break;
- default:
- err = GIT_ENOTFOUND;
+ git_repository *repo = NULL;
+ git_tree *t1 = NULL, *t2 = NULL;
+ git_diff *diff;
+ struct opts o = {
+ GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT,
+ -1, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
+ };
+
+ git_threads_init();
+
+ parse_opts(&o, argc, argv);
+
+ check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
+ "Could not open repository", o.dir);
+
+ /**
+ * Possible argument patterns:
+ *
+ * * <sha1> <sha2>
+ * * <sha1> --cached
+ * * <sha1>
+ * * --cached
+ * * nothing
+ *
+ * Currently ranged arguments like <sha1>..<sha2> and <sha1>...<sha2>
+ * are not supported in this example
+ */
+
+ if (o.treeish1)
+ treeish_to_tree(&t1, repo, o.treeish1);
+ if (o.treeish2)
+ treeish_to_tree(&t2, repo, o.treeish2);
+
+ if (t1 && t2)
+ check_lg2(
+ git_diff_tree_to_tree(&diff, repo, t1, t2, &o.diffopts),
+ "diff trees", NULL);
+ else if (t1 && o.cached)
+ check_lg2(
+ git_diff_tree_to_index(&diff, repo, t1, NULL, &o.diffopts),
+ "diff tree to index", NULL);
+ else if (t1)
+ check_lg2(
+ git_diff_tree_to_workdir_with_index(&diff, repo, t1, &o.diffopts),
+ "diff tree to working directory", NULL);
+ else if (o.cached) {
+ treeish_to_tree(&t1, repo, "HEAD");
+ check_lg2(
+ git_diff_tree_to_index(&diff, repo, t1, NULL, &o.diffopts),
+ "diff tree to index", NULL);
}
+ else
+ check_lg2(
+ git_diff_index_to_workdir(&diff, repo, NULL, &o.diffopts),
+ "diff index to working directory", NULL);
+
+ /** Apply rename and copy detection if requested. */
+
+ if ((o.findopts.flags & GIT_DIFF_FIND_ALL) != 0)
+ check_lg2(
+ git_diff_find_similar(diff, &o.findopts),
+ "finding renames and copies", NULL);
+
+ /** Generate simple output using libgit2 display helper. */
+
+ if (o.color >= 0)
+ fputs(colors[0], stdout);
+
+ check_lg2(
+ git_diff_print(diff, o.format, color_printer, &o.color),
+ "displaying diff", NULL);
- return err;
+ if (o.color >= 0)
+ fputs(colors[0], stdout);
+
+ /** Cleanup before exiting. */
+
+ git_diff_free(diff);
+ git_tree_free(t1);
+ git_tree_free(t2);
+ git_repository_free(repo);
+
+ git_threads_shutdown();
+
+ return 0;
}
-char *colors[] = {
- "\033[m", /* reset */
- "\033[1m", /* bold */
- "\033[31m", /* red */
- "\033[32m", /* green */
- "\033[36m" /* cyan */
-};
+static void usage(const char *message, const char *arg)
+{
+ if (message && arg)
+ fprintf(stderr, "%s: %s\n", message, arg);
+ else if (message)
+ fprintf(stderr, "%s\n", message);
+ fprintf(stderr, "usage: diff [<tree-oid> [<tree-oid>]]\n");
+ exit(1);
+}
-static int printer(
+/** This implements very rudimentary colorized output. */
+static int color_printer(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
@@ -63,6 +163,7 @@ static int printer(
case GIT_DIFF_LINE_HUNK_HDR: color = 4; break;
default: break;
}
+
if (color != *last_color) {
if (*last_color == 1 || color == 1)
fputs(colors[0], stdout);
@@ -71,186 +172,79 @@ static int printer(
}
}
- if (line->origin == GIT_DIFF_LINE_CONTEXT ||
- line->origin == GIT_DIFF_LINE_ADDITION ||
- line->origin == GIT_DIFF_LINE_DELETION)
- fputc(line->origin, stdout);
-
- fwrite(line->content, 1, line->content_len, stdout);
-
- return 0;
-}
-
-static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
-{
- size_t len = strlen(pattern);
- uint16_t strval;
- char *endptr = NULL;
- if (strncmp(arg, pattern, len))
- return 0;
- if (arg[len] == '\0' && pattern[len - 1] != '=')
- return 1;
- if (arg[len] == '=')
- len++;
- strval = strtoul(arg + len, &endptr, 0);
- if (endptr == arg)
- return 0;
- *val = strval;
- return 1;
-}
-
-static int check_str_param(const char *arg, const char *pattern, const char **val)
-{
- size_t len = strlen(pattern);
- if (strncmp(arg, pattern, len))
- return 0;
- *val = (const char *)(arg + len);
- return 1;
-}
-
-static void usage(const char *message, const char *arg)
-{
- if (message && arg)
- fprintf(stderr, "%s: %s\n", message, arg);
- else if (message)
- fprintf(stderr, "%s\n", message);
- fprintf(stderr, "usage: diff [<tree-oid> [<tree-oid>]]\n");
- exit(1);
+ return diff_output(delta, hunk, line, stdout);
}
-int main(int argc, char *argv[])
+/** Parse arguments as copied from git-diff. */
+static void parse_opts(struct opts *o, int argc, char *argv[])
{
- git_repository *repo = NULL;
- git_tree *t1 = NULL, *t2 = NULL;
- git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
- git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
- git_diff *diff;
- int i, color = -1, cached = 0;
- git_diff_format_t format = GIT_DIFF_FORMAT_PATCH;
- char *a, *treeish1 = NULL, *treeish2 = NULL;
- const char *dir = ".";
-
- git_threads_init();
+ struct args_info args = ARGS_INFO_INIT;
- /* parse arguments as copied from git-diff */
- for (i = 1; i < argc; ++i) {
- a = argv[i];
+ for (args.pos = 1; args.pos < argc; ++args.pos) {
+ const char *a = argv[args.pos];
if (a[0] != '-') {
- if (treeish1 == NULL)
- treeish1 = a;
- else if (treeish2 == NULL)
- treeish2 = a;
+ if (o->treeish1 == NULL)
+ o->treeish1 = a;
+ else if (o->treeish2 == NULL)
+ o->treeish2 = a;
else
usage("Only one or two tree identifiers can be provided", NULL);
}
else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
!strcmp(a, "--patch"))
- format = GIT_DIFF_FORMAT_PATCH;
+ o->format = GIT_DIFF_FORMAT_PATCH;
else if (!strcmp(a, "--cached"))
- cached = 1;
+ o->cached = 1;
else if (!strcmp(a, "--name-only"))
- format = GIT_DIFF_FORMAT_NAME_ONLY;
+ o->format = GIT_DIFF_FORMAT_NAME_ONLY;
else if (!strcmp(a, "--name-status"))
- format = GIT_DIFF_FORMAT_NAME_STATUS;
+ o->format = GIT_DIFF_FORMAT_NAME_STATUS;
else if (!strcmp(a, "--raw"))
- format = GIT_DIFF_FORMAT_RAW;
+ o->format = GIT_DIFF_FORMAT_RAW;
else if (!strcmp(a, "--color"))
- color = 0;
+ o->color = 0;
else if (!strcmp(a, "--no-color"))
- color = -1;
+ o->color = -1;
else if (!strcmp(a, "-R"))
- opts.flags |= GIT_DIFF_REVERSE;
+ o->diffopts.flags |= GIT_DIFF_REVERSE;
else if (!strcmp(a, "-a") || !strcmp(a, "--text"))
- opts.flags |= GIT_DIFF_FORCE_TEXT;
+ o->diffopts.flags |= GIT_DIFF_FORCE_TEXT;
else if (!strcmp(a, "--ignore-space-at-eol"))
- opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL;
+ o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL;
else if (!strcmp(a, "-b") || !strcmp(a, "--ignore-space-change"))
- opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
+ o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
else if (!strcmp(a, "-w") || !strcmp(a, "--ignore-all-space"))
- opts.flags |= GIT_DIFF_IGNORE_WHITESPACE;
+ o->diffopts.flags |= GIT_DIFF_IGNORE_WHITESPACE;
else if (!strcmp(a, "--ignored"))
- opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
+ o->diffopts.flags |= GIT_DIFF_INCLUDE_IGNORED;
else if (!strcmp(a, "--untracked"))
- opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
- else if (check_uint16_param(a, "-M", &findopts.rename_threshold) ||
- check_uint16_param(a, "--find-renames",
- &findopts.rename_threshold))
- findopts.flags |= GIT_DIFF_FIND_RENAMES;
- else if (check_uint16_param(a, "-C", &findopts.copy_threshold) ||
- check_uint16_param(a, "--find-copies",
- &findopts.copy_threshold))
- findopts.flags |= GIT_DIFF_FIND_COPIES;
+ o->diffopts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
+ else if (match_uint16_arg(
+ &o->findopts.rename_threshold, &args, "-M") ||
+ match_uint16_arg(
+ &o->findopts.rename_threshold, &args, "--find-renames"))
+ o->findopts.flags |= GIT_DIFF_FIND_RENAMES;
+ else if (match_uint16_arg(
+ &o->findopts.copy_threshold, &args, "-C") ||
+ match_uint16_arg(
+ &o->findopts.copy_threshold, &args, "--find-copies"))
+ o->findopts.flags |= GIT_DIFF_FIND_COPIES;
else if (!strcmp(a, "--find-copies-harder"))
- findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
- else if (!strncmp(a, "-B", 2) || !strncmp(a, "--break-rewrites", 16)) {
+ o->findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
+ else if (is_prefixed(a, "-B") || is_prefixed(a, "--break-rewrites"))
/* TODO: parse thresholds */
- findopts.flags |= GIT_DIFF_FIND_REWRITES;
- }
- else if (!check_uint16_param(a, "-U", &opts.context_lines) &&
- !check_uint16_param(a, "--unified=", &opts.context_lines) &&
- !check_uint16_param(a, "--inter-hunk-context=",
- &opts.interhunk_lines) &&
- !check_str_param(a, "--src-prefix=", &opts.old_prefix) &&
- !check_str_param(a, "--dst-prefix=", &opts.new_prefix) &&
- !check_str_param(a, "--git-dir=", &dir))
- usage("Unknown arg", a);
+ o->findopts.flags |= GIT_DIFF_FIND_REWRITES;
+ else if (!match_uint16_arg(
+ &o->diffopts.context_lines, &args, "-U") &&
+ !match_uint16_arg(
+ &o->diffopts.context_lines, &args, "--unified") &&
+ !match_uint16_arg(
+ &o->diffopts.interhunk_lines, &args, "--inter-hunk-context") &&
+ !match_str_arg(&o->diffopts.old_prefix, &args, "--src-prefix") &&
+ !match_str_arg(&o->diffopts.new_prefix, &args, "--dst-prefix") &&
+ !match_str_arg(&o->dir, &args, "--git-dir"))
+ usage("Unknown command line argument", a);
}
-
- /* open repo */
-
- check(git_repository_open_ext(&repo, dir, 0, NULL),
- "Could not open repository");
-
- if (treeish1)
- check(resolve_to_tree(repo, treeish1, &t1), "Looking up first tree");
- if (treeish2)
- check(resolve_to_tree(repo, treeish2, &t2), "Looking up second tree");
-
- /* <sha1> <sha2> */
- /* <sha1> --cached */
- /* <sha1> */
- /* --cached */
- /* nothing */
-
- if (t1 && t2)
- check(git_diff_tree_to_tree(&diff, repo, t1, t2, &opts), "Diff");
- else if (t1 && cached)
- check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
- else if (t1) {
- git_diff *diff2;
- check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
- check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff");
- check(git_diff_merge(diff, diff2), "Merge diffs");
- git_diff_free(diff2);
- }
- else if (cached) {
- check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD");
- check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
- }
- else
- check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
-
- if ((findopts.flags & GIT_DIFF_FIND_ALL) != 0)
- check(git_diff_find_similar(diff, &findopts),
- "finding renames and copies ");
-
- if (color >= 0)
- fputs(colors[0], stdout);
-
- check(git_diff_print(diff, format, printer, &color), "Displaying diff");
-
- if (color >= 0)
- fputs(colors[0], stdout);
-
- git_diff_free(diff);
- git_tree_free(t1);
- git_tree_free(t2);
- git_repository_free(repo);
-
- git_threads_shutdown();
-
- return 0;
}
-
diff --git a/examples/init.c b/examples/init.c
index 4a379c6..1c37125 100644
--- a/examples/init.c
+++ b/examples/init.c
@@ -1,4 +1,13 @@
/*
+ * 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"
+
+/**
* This is a sample program that is similar to "git init". See the
* documentation for that (try "git help init") to understand what this
* program is emulating.
@@ -8,179 +17,93 @@
* This also contains a special additional option that regular "git init"
* does not support which is "--initial-commit" to make a first empty commit.
* That is demonstrated in the "create_initial_commit" helper function.
- *
- * 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 <stdio.h>
-#include <git2.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-/* not actually good error handling */
-static void fail(const char *msg, const char *arg)
-{
- if (arg)
- fprintf(stderr, "%s %s\n", msg, arg);
- else
- fprintf(stderr, "%s\n", msg);
- exit(1);
-}
-
-static void usage(const char *error, const char *arg)
-{
- fprintf(stderr, "error: %s '%s'\n", error, arg);
- fprintf(stderr, "usage: init [-q | --quiet] [--bare] "
- "[--template=<dir>] [--shared[=perms]] <directory>\n");
- exit(1);
-}
-
-/* simple string prefix test used in argument parsing */
-static size_t is_prefixed(const char *arg, const char *pfx)
-{
- size_t len = strlen(pfx);
- return !strncmp(arg, pfx, len) ? len : 0;
-}
-
-/* parse the tail of the --shared= argument */
-static uint32_t parse_shared(const char *shared)
-{
- if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
- return GIT_REPOSITORY_INIT_SHARED_UMASK;
-
- else if (!strcmp(shared, "true") || !strcmp(shared, "group"))
- return GIT_REPOSITORY_INIT_SHARED_GROUP;
-
- else if (!strcmp(shared, "all") || !strcmp(shared, "world") ||
- !strcmp(shared, "everybody"))
- return GIT_REPOSITORY_INIT_SHARED_ALL;
-
- else if (shared[0] == '0') {
- long val;
- char *end = NULL;
- val = strtol(shared + 1, &end, 8);
- if (end == shared + 1 || *end != 0)
- usage("invalid octal value for --shared", shared);
- return (uint32_t)val;
- }
-
- else
- usage("unknown value for --shared", shared);
-
- return 0;
-}
-
-/* forward declaration of helper to make an empty parent-less commit */
+/** Forward declarations of helpers */
+struct opts {
+ int no_options;
+ int quiet;
+ int bare;
+ int initial_commit;
+ uint32_t shared;
+ const char *template;
+ const char *gitdir;
+ const char *dir;
+};
static void create_initial_commit(git_repository *repo);
+static void parse_opts(struct opts *o, int argc, char *argv[]);
int main(int argc, char *argv[])
{
git_repository *repo = NULL;
- int no_options = 1, quiet = 0, bare = 0, initial_commit = 0, i;
- uint32_t shared = GIT_REPOSITORY_INIT_SHARED_UMASK;
- const char *template = NULL, *gitdir = NULL, *dir = NULL;
- size_t pfxlen;
+ struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
git_threads_init();
- /* Process arguments */
+ parse_opts(&o, argc, argv);
- for (i = 1; i < argc; ++i) {
- char *a = argv[i];
+ /* Initialize repository. */
- if (a[0] == '-')
- no_options = 0;
-
- if (a[0] != '-') {
- if (dir != NULL)
- usage("extra argument", a);
- dir = a;
- }
- else if (!strcmp(a, "-q") || !strcmp(a, "--quiet"))
- quiet = 1;
- else if (!strcmp(a, "--bare"))
- bare = 1;
- else if ((pfxlen = is_prefixed(a, "--template=")) > 0)
- template = a + pfxlen;
- else if (!strcmp(a, "--separate-git-dir"))
- gitdir = argv[++i];
- else if ((pfxlen = is_prefixed(a, "--separate-git-dir=")) > 0)
- gitdir = a + pfxlen;
- else if (!strcmp(a, "--shared"))
- shared = GIT_REPOSITORY_INIT_SHARED_GROUP;
- else if ((pfxlen = is_prefixed(a, "--shared=")) > 0)
- shared = parse_shared(a + pfxlen);
- else if (!strcmp(a, "--initial-commit"))
- initial_commit = 1;
- else
- usage("unknown option", a);
- }
-
- if (!dir)
- usage("must specify directory to init", NULL);
-
- /* Initialize repository */
-
- if (no_options) {
- /* No options were specified, so let's demonstrate the default
+ if (o.no_options) {
+ /**
+ * No options were specified, so let's demonstrate the default
* simple case of git_repository_init() API usage...
*/
-
- if (git_repository_init(&repo, dir, 0) < 0)
- fail("Could not initialize repository", dir);
+ check_lg2(git_repository_init(&repo, o.dir, 0),
+ "Could not initialize repository", NULL);
}
else {
- /* Some command line options were specified, so we'll use the
+ /**
+ * Some command line options were specified, so we'll use the
* extended init API to handle them
*/
- git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+ git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+ initopts.flags = GIT_REPOSITORY_INIT_MKPATH;
- if (bare)
- opts.flags |= GIT_REPOSITORY_INIT_BARE;
+ if (o.bare)
+ initopts.flags |= GIT_REPOSITORY_INIT_BARE;
- if (template) {
- opts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
- opts.template_path = template;
+ if (o.template) {
+ initopts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
+ initopts.template_path = o.template;
}
- if (gitdir) {
- /* if you specified a separate git directory, then initialize
+ if (o.gitdir) {
+ /**
+ * If you specified a separate git directory, then initialize
* the repository at that path and use the second path as the
* working directory of the repository (with a git-link file)
*/
- opts.workdir_path = dir;
- dir = gitdir;
+ initopts.workdir_path = o.dir;
+ o.dir = o.gitdir;
}
- if (shared != 0)
- opts.mode = shared;
+ if (o.shared != 0)
+ initopts.mode = o.shared;
- if (git_repository_init_ext(&repo, dir, &opts) < 0)
- fail("Could not initialize repository", dir);
+ check_lg2(git_repository_init_ext(&repo, o.dir, &initopts),
+ "Could not initialize repository", NULL);
}
- /* Print a message to stdout like "git init" does */
+ /** Print a message to stdout like "git init" does. */
- if (!quiet) {
- if (bare || gitdir)
- dir = git_repository_path(repo);
+ if (!o.quiet) {
+ if (o.bare || o.gitdir)
+ o.dir = git_repository_path(repo);
else
- dir = git_repository_workdir(repo);
+ o.dir = git_repository_workdir(repo);
- printf("Initialized empty Git repository in %s\n", dir);
+ printf("Initialized empty Git repository in %s\n", o.dir);
}
- /* As an extension to the basic "git init" command, this example
+ /**
+ * As an extension to the basic "git init" command, this example
* gives the option to create an empty initial commit. This is
* mostly to demonstrate what it takes to do that, but also some
* people like to have that empty base commit in their repo.
*/
- if (initial_commit) {
+ if (o.initial_commit) {
create_initial_commit(repo);
printf("Created empty initial commit\n");
}
@@ -191,7 +114,8 @@ int main(int argc, char *argv[])
return 0;
}
-/* Unlike regular "git init", this example shows how to create an initial
+/**
+ * Unlike regular "git init", this example shows how to create an initial
* empty commit in the repository. This is the helper function that does
* that.
*/
@@ -202,31 +126,33 @@ static void create_initial_commit(git_repository *repo)
git_oid tree_id, commit_id;
git_tree *tree;
- /* First use the config to initialize a commit signature for the user */
+ /** First use the config to initialize a commit signature for the user. */
if (git_signature_default(&sig, repo) < 0)
- fail("Unable to create a commit signature.",
- "Perhaps 'user.name' and 'user.email' are not set");
+ fatal("Unable to create a commit signature.",
+ "Perhaps 'user.name' and 'user.email' are not set");
/* Now let's create an empty tree for this commit */
if (git_repository_index(&index, repo) < 0)
- fail("Could not open repository index", NULL);
+ fatal("Could not open repository index", NULL);
- /* Outside of this example, you could call git_index_add_bypath()
+ /**
+ * Outside of this example, you could call git_index_add_bypath()
* here to put actual files into the index. For our purposes, we'll
* leave it empty for now.
*/
if (git_index_write_tree(&tree_id, index) < 0)
- fail("Unable to write initial tree from index", NULL);
+ fatal("Unable to write initial tree from index", NULL);
git_index_free(index);
if (git_tree_lookup(&tree, repo, &tree_id) < 0)
- fail("Could not look up initial tree", NULL);
+ fatal("Could not look up initial tree", NULL);
- /* Ready to create the initial commit
+ /**
+ * Ready to create the initial commit.
*
* Normally creating a commit would involve looking up the current
* HEAD commit and making that be the parent of the initial commit,
@@ -236,10 +162,85 @@ static void create_initial_commit(git_repository *repo)
if (git_commit_create_v(
&commit_id, repo, "HEAD", sig, sig,
NULL, "Initial commit", tree, 0) < 0)
- fail("Could not create the initial commit", NULL);
+ fatal("Could not create the initial commit", NULL);
- /* Clean up so we don't leak memory */
+ /** Clean up so we don't leak memory. */
git_tree_free(tree);
git_signature_free(sig);
}
+
+static void usage(const char *error, const char *arg)
+{
+ fprintf(stderr, "error: %s '%s'\n", error, arg);
+ fprintf(stderr,
+ "usage: init [-q | --quiet] [--bare] [--template=<dir>]\n"
+ " [--shared[=perms]] [--initial-commit]\n"
+ " [--separate-git-dir] <directory>\n");
+ exit(1);
+}
+
+/** Parse the tail of the --shared= argument. */
+static uint32_t parse_shared(const char *shared)
+{
+ if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
+ return GIT_REPOSITORY_INIT_SHARED_UMASK;
+
+ else if (!strcmp(shared, "true") || !strcmp(shared, "group"))
+ return GIT_REPOSITORY_INIT_SHARED_GROUP;
+
+ else if (!strcmp(shared, "all") || !strcmp(shared, "world") ||
+ !strcmp(shared, "everybody"))
+ return GIT_REPOSITORY_INIT_SHARED_ALL;
+
+ else if (shared[0] == '0') {
+ long val;
+ char *end = NULL;
+ val = strtol(shared + 1, &end, 8);
+ if (end == shared + 1 || *end != 0)
+ usage("invalid octal value for --shared", shared);
+ return (uint32_t)val;
+ }
+
+ else
+ usage("unknown value for --shared", shared);
+
+ return 0;
+}
+
+static void parse_opts(struct opts *o, int argc, char *argv[])
+{
+ struct args_info args = ARGS_INFO_INIT;
+ const char *sharedarg;
+
+ /** Process arguments. */
+
+ for (args.pos = 1; args.pos < argc; ++args.pos) {
+ char *a = argv[args.pos];
+
+ if (a[0] == '-')
+ o->no_options = 0;
+
+ if (a[0] != '-') {
+ if (o->dir != NULL)
+ usage("extra argument", a);
+ o->dir = a;
+ }
+ else if (!strcmp(a, "-q") || !strcmp(a, "--quiet"))
+ o->quiet = 1;
+ else if (!strcmp(a, "--bare"))
+ o->bare = 1;
+ else if (!strcmp(a, "--shared"))
+ o->shared = GIT_REPOSITORY_INIT_SHARED_GROUP;
+ else if (!strcmp(a, "--initial-commit"))
+ o->initial_commit = 1;
+ else if (match_str_arg(&sharedarg, &args, "--shared"))
+ o->shared = parse_shared(sharedarg);
+ else if (!match_str_arg(&o->template, &args, "--template") ||
+ !match_str_arg(&o->gitdir, &args, "--separate-git-dir"))
+ usage("unknown option", a);
+ }
+
+ if (!o->dir)
+ usage("must specify directory to init", NULL);
+}
diff --git a/examples/log.c b/examples/log.c
index 81b056c..a36d4c9 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -1,88 +1,205 @@
-#include <stdio.h>
-#include <git2.h>
-#include <stdlib.h>
-#include <string.h>
-
-static void check(int error, const char *message, const char *arg)
-{
- if (!error)
- return;
- if (arg)
- fprintf(stderr, "%s '%s' (%d)\n", message, arg, error);
- else
- fprintf(stderr, "%s (%d)\n", message, error);
- exit(1);
-}
-
-static void usage(const char *message, const char *arg)
-{
- if (message && arg)
- fprintf(stderr, "%s: %s\n", message, arg);
- else if (message)
- fprintf(stderr, "%s\n", message);
- fprintf(stderr, "usage: log [<options>]\n");
- exit(1);
-}
-
+/*
+ * 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"
+
+/**
+ * This example demonstrates the libgit2 rev walker APIs to roughly
+ * simulate the output of `git log` and a few of command line arguments.
+ * `git log` has many many options and this only shows a few of them.
+ *
+ * This does not have:
+ *
+ * - Robust error handling
+ * - Colorized or paginated output formatting
+ * - Most of the `git log` options
+ *
+ * This does have:
+ *
+ * - Examples of translating command line arguments to equivalent libgit2
+ * revwalker configuration calls
+ * - Simplified options to apply pathspec limits and to show basic diffs
+ */
+
+/** log_state represents walker being configured while handling options */
struct log_state {
git_repository *repo;
const char *repodir;
git_revwalk *walker;
int hide;
int sorting;
+ int revisions;
};
-static void set_sorting(struct log_state *s, unsigned int sort_mode)
+/** utility functions that are called to configure the walker */
+static void set_sorting(struct log_state *s, unsigned int sort_mode);
+static void push_rev(struct log_state *s, git_object *obj, int hide);
+static int add_revision(struct log_state *s, const char *revstr);
+
+/** log_options holds other command line options that affect log output */
+struct log_options {
+ int show_diff;
+ int skip, limit;
+ int min_parents, max_parents;
+ git_time_t before;
+ git_time_t after;
+ char *author;
+ char *committer;
+};
+
+/** utility functions that parse options and help with log output */
+static int parse_options(
+ struct log_state *s, struct log_options *opt, int argc, char **argv);
+static void print_time(const git_time *intime, const char *prefix);
+static void print_commit(git_commit *commit);
+static int match_with_parent(git_commit *commit, int i, git_diff_options *);
+
+
+int main(int argc, char *argv[])
{
- if (!s->repo) {
- if (!s->repodir) s->repodir = ".";
- check(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
- "Could not open repository", s->repodir);
- }
+ int i, count = 0, printed = 0, parents, last_arg;
+ struct log_state s;
+ struct log_options opt;
+ git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
+ git_oid oid;
+ git_commit *commit = NULL;
+ git_pathspec *ps = NULL;
- if (!s->walker)
- check(git_revwalk_new(&s->walker, s->repo),
- "Could not create revision walker", NULL);
+ git_threads_init();
- if (sort_mode == GIT_SORT_REVERSE)
- s->sorting = s->sorting ^ GIT_SORT_REVERSE;
- else
- s->sorting = sort_mode | (s->sorting & GIT_SORT_REVERSE);
+ /** Parse arguments and set up revwalker. */
- git_revwalk_sorting(s->walker, s->sorting);
+ last_arg = parse_options(&s, &opt, argc, argv);
+
+ diffopts.pathspec.strings = &argv[last_arg];
+ diffopts.pathspec.count = argc - last_arg;
+ if (diffopts.pathspec.count > 0)
+ check_lg2(git_pathspec_new(&ps, &diffopts.pathspec),
+ "Building pathspec", NULL);
+
+ if (!s.revisions)
+ add_revision(&s, NULL);
+
+ /** Use the revwalker to traverse the history. */
+
+ printed = count = 0;
+
+ for (; !git_revwalk_next(&oid, s.walker); git_commit_free(commit)) {
+ check_lg2(git_commit_lookup(&commit, s.repo, &oid),
+ "Failed to look up commit", NULL);
+
+ parents = (int)git_commit_parentcount(commit);
+ if (parents < opt.min_parents)
+ continue;
+ if (opt.max_parents > 0 && parents > opt.max_parents)
+ continue;
+
+ if (diffopts.pathspec.count > 0) {
+ int unmatched = parents;
+
+ if (parents == 0) {
+ git_tree *tree;
+ check_lg2(git_commit_tree(&tree, commit), "Get tree", NULL);
+ if (git_pathspec_match_tree(
+ NULL, tree, GIT_PATHSPEC_NO_MATCH_ERROR, ps) != 0)
+ unmatched = 1;
+ git_tree_free(tree);
+ } else if (parents == 1) {
+ unmatched = match_with_parent(commit, 0, &diffopts) ? 0 : 1;
+ } else {
+ for (i = 0; i < parents; ++i) {
+ if (match_with_parent(commit, i, &diffopts))
+ unmatched--;
+ }
+ }
+
+ if (unmatched > 0)
+ continue;
+ }
+
+ if (count++ < opt.skip)
+ continue;
+ if (opt.limit != -1 && printed++ >= opt.limit) {
+ git_commit_free(commit);
+ break;
+ }
+
+ print_commit(commit);
+
+ if (opt.show_diff) {
+ git_tree *a = NULL, *b = NULL;
+ git_diff *diff = NULL;
+
+ if (parents > 1)
+ continue;
+ check_lg2(git_commit_tree(&b, commit), "Get tree", NULL);
+ if (parents == 1) {
+ git_commit *parent;
+ check_lg2(git_commit_parent(&parent, commit, 0), "Get parent", NULL);
+ check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL);
+ git_commit_free(parent);
+ }
+
+ check_lg2(git_diff_tree_to_tree(
+ &diff, git_commit_owner(commit), a, b, &diffopts),
+ "Diff commit with parent", NULL);
+ check_lg2(
+ git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_output, NULL),
+ "Displaying diff", NULL);
+
+ git_diff_free(diff);
+ git_tree_free(a);
+ git_tree_free(b);
+ }
+ }
+
+ git_pathspec_free(ps);
+ git_revwalk_free(s.walker);
+ git_repository_free(s.repo);
+ git_threads_shutdown();
+
+ return 0;
}
+/** Push object (for hide or show) onto revwalker. */
static void push_rev(struct log_state *s, git_object *obj, int hide)
{
hide = s->hide ^ hide;
+ /** Create revwalker on demand if it doesn't already exist. */
if (!s->walker) {
- check(git_revwalk_new(&s->walker, s->repo),
+ check_lg2(git_revwalk_new(&s->walker, s->repo),
"Could not create revision walker", NULL);
git_revwalk_sorting(s->walker, s->sorting);
}
if (!obj)
- check(git_revwalk_push_head(s->walker),
+ check_lg2(git_revwalk_push_head(s->walker),
"Could not find repository HEAD", NULL);
else if (hide)
- check(git_revwalk_hide(s->walker, git_object_id(obj)),
+ check_lg2(git_revwalk_hide(s->walker, git_object_id(obj)),
"Reference does not refer to a commit", NULL);
else
- check(git_revwalk_push(s->walker, git_object_id(obj)),
+ check_lg2(git_revwalk_push(s->walker, git_object_id(obj)),
"Reference does not refer to a commit", NULL);
git_object_free(obj);
}
+/** Parse revision string and add revs to walker. */
static int add_revision(struct log_state *s, const char *revstr)
{
git_revspec revs;
int hide = 0;
+ /** Open repo on demand if it isn't already open. */
if (!s->repo) {
if (!s->repodir) s->repodir = ".";
- check(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
+ check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
"Could not open repository", s->repodir);
}
@@ -107,10 +224,11 @@ static int add_revision(struct log_state *s, const char *revstr)
if ((revs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base;
- check(git_merge_base(&base, s->repo,
+ check_lg2(git_merge_base(&base, s->repo,
git_object_id(revs.from), git_object_id(revs.to)),
"Could not find merge base", revstr);
- check(git_object_lookup(&revs.to, s->repo, &base, GIT_OBJ_COMMIT),
+ check_lg2(
+ git_object_lookup(&revs.to, s->repo, &base, GIT_OBJ_COMMIT),
"Could not find merge base commit", NULL);
push_rev(s, revs.to, hide);
@@ -122,6 +240,30 @@ static int add_revision(struct log_state *s, const char *revstr)
return 0;
}
+/** Update revwalker with sorting mode. */
+static void set_sorting(struct log_state *s, unsigned int sort_mode)
+{
+ /** Open repo on demand if it isn't already open. */
+ if (!s->repo) {
+ if (!s->repodir) s->repodir = ".";
+ check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
+ "Could not open repository", s->repodir);
+ }
+
+ /** Create revwalker on demand if it doesn't already exist. */
+ if (!s->walker)
+ check_lg2(git_revwalk_new(&s->walker, s->repo),
+ "Could not create revision walker", NULL);
+
+ if (sort_mode == GIT_SORT_REVERSE)
+ s->sorting = s->sorting ^ GIT_SORT_REVERSE;
+ else
+ s->sorting = sort_mode | (s->sorting & GIT_SORT_REVERSE);
+
+ git_revwalk_sorting(s->walker, s->sorting);
+}
+
+/** Helper to format a git_time value like Git. */
static void print_time(const git_time *intime, const char *prefix)
{
char sign, out[32];
@@ -148,6 +290,7 @@ static void print_time(const git_time *intime, const char *prefix)
printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
}
+/** Helper to print a commit object. */
static void print_commit(git_commit *commit)
{
char buf[GIT_OID_HEXSZ + 1];
@@ -182,54 +325,21 @@ static void print_commit(git_commit *commit)
printf("\n");
}
-static int print_diff(
- const git_diff_delta *delta,
- const git_diff_hunk *hunk,
- const git_diff_line *line,
- void *data)
-{
- (void)delta; (void)hunk; (void)data;
-
- if (line->origin == GIT_DIFF_LINE_CONTEXT ||
- line->origin == GIT_DIFF_LINE_ADDITION ||
- line->origin == GIT_DIFF_LINE_DELETION)
- fputc(line->origin, stdout);
-
- fwrite(line->content, 1, line->content_len, stdout);
- return 0;
-}
-
-static int match_int(int *value, const char *arg, int allow_negative)
-{
- char *found;
- *value = (int)strtol(arg, &found, 10);
- return (found && *found == '\0' && (allow_negative || *value >= 0));
-}
-
-static int match_int_arg(
- int *value, const char *arg, const char *pfx, int allow_negative)
-{
- size_t pfxlen = strlen(pfx);
- if (strncmp(arg, pfx, pfxlen) != 0)
- return 0;
- if (!match_int(value, arg + pfxlen, allow_negative))
- usage("Invalid value after argument", arg);
- return 1;
-}
-
-static int match_with_parent(
- git_commit *commit, int i, git_diff_options *opts)
+/** Helper to find how many files in a commit changed from its nth parent. */
+static int match_with_parent(git_commit *commit, int i, git_diff_options *opts)
{
git_commit *parent;
git_tree *a, *b;
git_diff *diff;
int ndeltas;
- check(git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL);
- check(git_commit_tree(&a, parent), "Tree for parent", NULL);
- check(git_commit_tree(&b, commit), "Tree for commit", NULL);
- check(git_diff_tree_to_tree(&diff, git_commit_owner(commit), a, b, opts),
- "Checking diff between parent and commit", NULL);
+ check_lg2(
+ git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL);
+ check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL);
+ check_lg2(git_commit_tree(&b, commit), "Tree for commit", NULL);
+ check_lg2(
+ git_diff_tree_to_tree(&diff, git_commit_owner(commit), a, b, opts),
+ "Checking diff between parent and commit", NULL);
ndeltas = (int)git_diff_num_deltas(diff);
@@ -241,170 +351,77 @@ static int match_with_parent(
return ndeltas > 0;
}
-struct log_options {
- int show_diff;
- int skip, limit;
- int min_parents, max_parents;
- git_time_t before;
- git_time_t after;
- char *author;
- char *committer;
-};
-
-int main(int argc, char *argv[])
+/** Print a usage message for the program. */
+static void usage(const char *message, const char *arg)
{
- int i, count = 0, printed = 0, parents;
- char *a;
- struct log_state s;
- struct log_options opt;
- git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
- git_oid oid;
- git_commit *commit = NULL;
- git_pathspec *ps = NULL;
+ if (message && arg)
+ fprintf(stderr, "%s: %s\n", message, arg);
+ else if (message)
+ fprintf(stderr, "%s\n", message);
+ fprintf(stderr, "usage: log [<options>]\n");
+ exit(1);
+}
- git_threads_init();
+/** Parse some log command line options. */
+static int parse_options(
+ struct log_state *s, struct log_options *opt, int argc, char **argv)
+{
+ struct args_info args = ARGS_INFO_INIT;
- memset(&s, 0, sizeof(s));
- s.sorting = GIT_SORT_TIME;
+ memset(s, 0, sizeof(*s));
+ s->sorting = GIT_SORT_TIME;
- memset(&opt, 0, sizeof(opt));
- opt.max_parents = -1;
- opt.limit = -1;
+ memset(opt, 0, sizeof(*opt));
+ opt->max_parents = -1;
+ opt->limit = -1;
- for (i = 1; i < argc; ++i) {
- a = argv[i];
+ for (args.pos = 1; args.pos < argc; ++args.pos) {
+ const char *a = argv[args.pos];
if (a[0] != '-') {
- if (!add_revision(&s, a))
- ++count;
- else /* try failed revision parse as filename */
+ if (!add_revision(s, a))
+ s->revisions++;
+ else
+ /** Try failed revision parse as filename. */
break;
} else if (!strcmp(a, "--")) {
- ++i;
+ ++args.pos;
break;
}
else if (!strcmp(a, "--date-order"))
- set_sorting(&s, GIT_SORT_TIME);
+ set_sorting(s, GIT_SORT_TIME);
else if (!strcmp(a, "--topo-order"))
- set_sorting(&s, GIT_SORT_TOPOLOGICAL);
+ set_sorting(s, GIT_SORT_TOPOLOGICAL);
else if (!strcmp(a, "--reverse"))
- set_sorting(&s, GIT_SORT_REVERSE);
- else if (!strncmp(a, "--git-dir=", strlen("--git-dir=")))
- s.repodir = a + strlen("--git-dir=");
- else if (match_int_arg(&opt.skip, a, "--skip=", 0))
- /* found valid --skip */;
- else if (match_int_arg(&opt.limit, a, "--max-count=", 0))
- /* found valid --max-count */;
- else if (a[1] >= '0' && a[1] <= '9') {
- if (!match_int(&opt.limit, a + 1, 0))
- usage("Invalid limit on number of commits", a);
- } else if (!strcmp(a, "-n")) {
- if (i + 1 == argc || !match_int(&opt.limit, argv[i + 1], 0))
- usage("Argument -n not followed by valid count", argv[i + 1]);
- else
- ++i;
- }
+ set_sorting(s, GIT_SORT_REVERSE);
+ else if (match_str_arg(&s->repodir, &args, "--git-dir"))
+ /** Found git-dir. */;
+ else if (match_int_arg(&opt->skip, &args, "--skip", 0))
+ /** Found valid --skip. */;
+ else if (match_int_arg(&opt->limit, &args, "--max-count", 0))
+ /** Found valid --max-count. */;
+ else if (a[1] >= '0' && a[1] <= '9')
+ is_integer(&opt->limit, a + 1, 0);
+ else if (match_int_arg(&opt->limit, &args, "-n", 0))
+ /** Found valid -n. */;
else if (!strcmp(a, "--merges"))
- opt.min_parents = 2;
+ opt->min_parents = 2;
else if (!strcmp(a, "--no-merges"))
- opt.max_parents = 1;
+ opt->max_parents = 1;
else if (!strcmp(a, "--no-min-parents"))
- opt.min_parents = 0;
+ opt->min_parents = 0;
else if (!strcmp(a, "--no-max-parents"))
- opt.max_parents = -1;
- else if (match_int_arg(&opt.max_parents, a, "--max-parents=", 1))
- /* found valid --max-parents */;
- else if (match_int_arg(&opt.min_parents, a, "--min-parents=", 0))
- /* found valid --min_parents */;
+ opt->max_parents = -1;
+ else if (match_int_arg(&opt->max_parents, &args, "--max-parents=", 1))
+ /** Found valid --max-parents. */;
+ else if (match_int_arg(&opt->min_parents, &args, "--min-parents=", 0))
+ /** Found valid --min_parents. */;
else if (!strcmp(a, "-p") || !strcmp(a, "-u") || !strcmp(a, "--patch"))
- opt.show_diff = 1;
+ opt->show_diff = 1;
else
usage("Unsupported argument", a);
}
- if (!count)
- add_revision(&s, NULL);
-
- diffopts.pathspec.strings = &argv[i];
- diffopts.pathspec.count = argc - i;
- if (diffopts.pathspec.count > 0)
- check(git_pathspec_new(&ps, &diffopts.pathspec),
- "Building pathspec", NULL);
-
- printed = count = 0;
-
- for (; !git_revwalk_next(&oid, s.walker); git_commit_free(commit)) {
- check(git_commit_lookup(&commit, s.repo, &oid),
- "Failed to look up commit", NULL);
-
- parents = (int)git_commit_parentcount(commit);
- if (parents < opt.min_parents)
- continue;
- if (opt.max_parents > 0 && parents > opt.max_parents)
- continue;
-
- if (diffopts.pathspec.count > 0) {
- int unmatched = parents;
-
- if (parents == 0) {
- git_tree *tree;
- check(git_commit_tree(&tree, commit), "Get tree", NULL);
- if (git_pathspec_match_tree(
- NULL, tree, GIT_PATHSPEC_NO_MATCH_ERROR, ps) != 0)
- unmatched = 1;
- git_tree_free(tree);
- } else if (parents == 1) {
- unmatched = match_with_parent(commit, 0, &diffopts) ? 0 : 1;
- } else {
- for (i = 0; i < parents; ++i) {
- if (match_with_parent(commit, i, &diffopts))
- unmatched--;
- }
- }
-
- if (unmatched > 0)
- continue;
- }
-
- if (count++ < opt.skip)
- continue;
- if (opt.limit != -1 && printed++ >= opt.limit) {
- git_commit_free(commit);
- break;
- }
-
- print_commit(commit);
-
- if (opt.show_diff) {
- git_tree *a = NULL, *b = NULL;
- git_diff *diff = NULL;
-
- if (parents > 1)
- continue;
- check(git_commit_tree(&b, commit), "Get tree", NULL);
- if (parents == 1) {
- git_commit *parent;
- check(git_commit_parent(&parent, commit, 0), "Get parent", NULL);
- check(git_commit_tree(&a, parent), "Tree for parent", NULL);
- git_commit_free(parent);
- }
-
- check(git_diff_tree_to_tree(
- &diff, git_commit_owner(commit), a, b, &diffopts),
- "Diff commit with parent", NULL);
- check(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, print_diff, NULL),
- "Displaying diff", NULL);
-
- git_diff_free(diff);
- git_tree_free(a);
- git_tree_free(b);
- }
- }
-
- git_pathspec_free(ps);
- git_revwalk_free(s.walker);
- git_repository_free(s.repo);
- git_threads_shutdown();
-
- return 0;
+ return args.pos;
}
+
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 4167ef3..ad01001 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -47,6 +47,11 @@ exit:
return &data->ret;
}
+/**
+ * This function gets called for each remote-tracking branch that gets
+ * updated. The message we output depends on whether it's a new one or
+ * an update.
+ */
static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
@@ -66,6 +71,7 @@ static int update_cb(const char *refname, const git_oid *a, const git_oid *b, vo
return 0;
}
+/** Entry point for this command */
int fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
@@ -130,6 +136,11 @@ int fetch(git_repository *repo, int argc, char **argv)
pthread_join(worker, NULL);
#endif
+ /**
+ * If there are local objects (we got a thin pack), then tell
+ * the user how many objects we saved from having to cross the
+ * network.
+ */
if (stats->local_objects > 0) {
printf("\rReceived %d/%d objects in %zu bytes (used %d local objects)\n",
stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
index b65759e..18cd023 100644
--- a/examples/network/ls-remote.c
+++ b/examples/network/ls-remote.c
@@ -4,6 +4,7 @@
#include <string.h>
#include "common.h"
+/** Callback to show each item */
static int show_ref__cb(git_remote_head *head, void *payload)
{
char oid[GIT_OID_HEXSZ + 1] = {0};
@@ -28,6 +29,10 @@ static int use_remote(git_repository *repo, char *name)
goto cleanup;
}
+ /**
+ * Connect to the remote and call the printing function for
+ * each of the remote references.
+ */
callbacks.credentials = cred_acquire_cb;
git_remote_set_callbacks(remote, &callbacks);
@@ -42,9 +47,7 @@ cleanup:
return error;
}
-// This gets called to do the work. The remote can be given either as
-// the name of a configured remote or an URL.
-
+/** Entry point for this command */
int ls_remote(git_repository *repo, int argc, char **argv)
{
int error;
diff --git a/examples/rev-list.c b/examples/rev-list.c
index 1fb7ebf..9330c64 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -1,17 +1,35 @@
-#include <stdio.h>
-#include <string.h>
+/*
+ * 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 <git2.h>
+#include "common.h"
-static void check_error(int error_code, const char *action)
+static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
+
+int main (int argc, char **argv)
{
- if (!error_code)
- return;
+ git_repository *repo;
+ git_revwalk *walk;
+ git_oid oid;
+ char buf[41];
+
+ git_threads_init();
- const git_error *error = giterr_last();
- fprintf(stderr, "Error %d %s: %s\n", -error_code, action,
- (error && error->message) ? error->message : "???");
- exit(1);
+ check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "opening repository", NULL);
+ check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
+ check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
+
+ while (!git_revwalk_next(&oid, walk)) {
+ git_oid_fmt(buf, &oid);
+ buf[40] = '\0';
+ printf("%s\n", buf);
+ }
+
+ git_threads_shutdown();
+ return 0;
}
static int push_commit(git_revwalk *walk, const git_oid *oid, int hide)
@@ -93,27 +111,3 @@ static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts,
return 0;
}
-int main (int argc, char **argv)
-{
- int error;
- git_repository *repo;
- git_revwalk *walk;
- git_oid oid;
- char buf[41];
-
- error = git_repository_open_ext(&repo, ".", 0, NULL);
- check_error(error, "opening repository");
-
- error = git_revwalk_new(&walk, repo);
- check_error(error, "allocating revwalk");
- error = revwalk_parseopts(repo, walk, argc-1, argv+1);
- check_error(error, "parsing options");
-
- while (!git_revwalk_next(&oid, walk)) {
- git_oid_fmt(buf, &oid);
- buf[40] = '\0';
- printf("%s\n", buf);
- }
-
- return 0;
-}
diff --git a/examples/rev-parse.c b/examples/rev-parse.c
index cdbb61e..64a02fe 100644
--- a/examples/rev-parse.c
+++ b/examples/rev-parse.c
@@ -1,17 +1,36 @@
-#include <stdio.h>
-#include <git2.h>
-#include <stdlib.h>
-#include <string.h>
+/*
+ * 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.
+ */
-static void check(int error, const char *message, const char *arg)
+#include "common.h"
+
+/** Forward declarations for helpers. */
+struct parse_state {
+ git_repository *repo;
+ const char *repodir;
+ const char *spec;
+ int not;
+};
+static void parse_opts(struct parse_state *ps, int argc, char *argv[]);
+static int parse_revision(struct parse_state *ps);
+
+
+int main(int argc, char *argv[])
{
- if (!error)
- return;
- if (arg)
- fprintf(stderr, "%s %s (%d)\n", message, arg, error);
- else
- fprintf(stderr, "%s(%d)\n", message, error);
- exit(1);
+ struct parse_state ps = {0};
+
+ git_threads_init();
+ parse_opts(&ps, argc, argv);
+
+ check_lg2(parse_revision(&ps), "Parsing", NULL);
+
+ git_repository_free(ps.repo);
+ git_threads_shutdown();
+
+ return 0;
}
static void usage(const char *message, const char *arg)
@@ -24,13 +43,25 @@ static void usage(const char *message, const char *arg)
exit(1);
}
-struct parse_state {
- git_repository *repo;
- const char *repodir;
- int not;
-};
+static void parse_opts(struct parse_state *ps, int argc, char *argv[])
+{
+ struct args_info args = ARGS_INFO_INIT;
-static int parse_revision(struct parse_state *ps, const char *revstr)
+ for (args.pos=1; args.pos < argc; ++args.pos) {
+ const char *a = argv[args.pos];
+
+ if (a[0] != '-') {
+ if (ps->spec)
+ usage("Too many specs", a);
+ ps->spec = a;
+ } else if (!strcmp(a, "--not"))
+ ps->not = !ps->not;
+ else if (!match_str_arg(&ps->repodir, &args, "--git-dir"))
+ usage("Cannot handle argument", a);
+ }
+}
+
+static int parse_revision(struct parse_state *ps)
{
git_revspec rs;
char str[GIT_OID_HEXSZ + 1];
@@ -38,11 +69,11 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
if (!ps->repo) {
if (!ps->repodir)
ps->repodir = ".";
- check(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
+ check_lg2(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
"Could not open repository from", ps->repodir);
}
- check(git_revparse(&rs, ps->repo, revstr), "Could not parse", revstr);
+ check_lg2(git_revparse(&rs, ps->repo, ps->spec), "Could not parse", ps->spec);
if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) {
git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
@@ -56,9 +87,9 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base;
- check(git_merge_base(&base, ps->repo,
- git_object_id(rs.from), git_object_id(rs.to)),
- "Could not find merge base", revstr);
+ check_lg2(git_merge_base(&base, ps->repo,
+ git_object_id(rs.from), git_object_id(rs.to)),
+ "Could not find merge base", ps->spec);
git_oid_tostr(str, sizeof(str), &base);
printf("%s\n", str);
@@ -69,38 +100,9 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
git_object_free(rs.from);
}
else {
- check(0, "Invalid results from git_revparse", revstr);
+ fatal("Invalid results from git_revparse", ps->spec);
}
return 0;
}
-int main(int argc, char *argv[])
-{
- int i;
- char *a;
- struct parse_state ps;
-
- git_threads_init();
-
- memset(&ps, 0, sizeof(ps));
-
- for (i = 1; i < argc; ++i) {
- a = argv[i];
-
- if (a[0] != '-') {
- if (parse_revision(&ps, a) != 0)
- break;
- } else if (!strcmp(a, "--not"))
- ps.not = !ps.not;
- else if (!strncmp(a, "--git-dir=", strlen("--git-dir=")))
- ps.repodir = a + strlen("--git-dir=");
- else
- usage("Cannot handle argument", a);
- }
-
- git_repository_free(ps.repo);
- git_threads_shutdown();
-
- return 0;
-}
diff --git a/examples/showindex.c b/examples/showindex.c
index 93718c8..bf85238 100644
--- a/examples/showindex.c
+++ b/examples/showindex.c
@@ -1,10 +1,14 @@
-#include <git2.h>
-#include <stdio.h>
-#include <string.h>
+/*
+ * 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"
int main (int argc, char** argv)
{
- git_repository *repo = NULL;
git_index *index;
unsigned int i, ecount;
char *dir = ".";
@@ -14,28 +18,19 @@ int main (int argc, char** argv)
git_threads_init();
+ if (argc > 2)
+ fatal("usage: showindex [<repo-dir>]", NULL);
if (argc > 1)
dir = argv[1];
- if (!dir || argc > 2) {
- fprintf(stderr, "usage: showindex [<repo-dir>]\n");
- return 1;
- }
dirlen = strlen(dir);
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
- if (git_index_open(&index, dir) < 0) {
- fprintf(stderr, "could not open index: %s\n", dir);
- return 1;
- }
+ check_lg2(git_index_open(&index, dir), "could not open index", dir);
} else {
- if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
- fprintf(stderr, "could not open repository: %s\n", dir);
- return 1;
- }
- if (git_repository_index(&index, repo) < 0) {
- fprintf(stderr, "could not open repository index\n");
- return 1;
- }
+ git_repository *repo;
+ check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
+ check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
+ git_repository_free(repo);
}
git_index_read(index);
@@ -62,10 +57,7 @@ int main (int argc, char** argv)
}
git_index_free(index);
- git_repository_free(repo);
-
git_threads_shutdown();
return 0;
}
-
diff --git a/examples/status.c b/examples/status.c
index 0d9f55f..459e6fa 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -4,30 +4,22 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
-#include <git2.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-enum {
- FORMAT_DEFAULT = 0,
- FORMAT_LONG = 1,
- FORMAT_SHORT = 2,
- FORMAT_PORCELAIN = 3,
-};
-#define MAX_PATHSPEC 8
+#include "common.h"
-/*
+/**
* This example demonstrates the use of the libgit2 status APIs,
* particularly the `git_status_list` object, to roughly simulate the
* output of running `git status`. It serves as a simple example of
* using those APIs to get basic status information.
*
* This does not have:
+ *
* - Robust error handling
* - Colorized or paginated output formatting
*
* This does have:
+ *
* - Examples of translating command line arguments to the status
* options settings to mimic `git status` results.
* - A sample status formatter that matches the default "long" format
@@ -35,32 +27,83 @@ enum {
* - A sample status formatter that matches the "short" format
*/
-static void check(int error, const char *message, const char *extra)
+enum {
+ FORMAT_DEFAULT = 0,
+ FORMAT_LONG = 1,
+ FORMAT_SHORT = 2,
+ FORMAT_PORCELAIN = 3,
+};
+
+#define MAX_PATHSPEC 8
+
+struct opts {
+ git_status_options statusopt;
+ char *repodir;
+ char *pathspec[MAX_PATHSPEC];
+ int npaths;
+ int format;
+ int zterm;
+ int showbranch;
+};
+
+static void parse_opts(struct opts *o, int argc, char *argv[]);
+static void show_branch(git_repository *repo, int format);
+static void print_long(git_repository *repo, git_status_list *status);
+static void print_short(git_repository *repo, git_status_list *status);
+
+int main(int argc, char *argv[])
{
- const git_error *lg2err;
- const char *lg2msg = "", *lg2spacer = "";
+ git_repository *repo = NULL;
+ git_status_list *status;
+ struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
- if (!error)
- return;
+ git_threads_init();
- if ((lg2err = giterr_last()) != NULL && lg2err->message != NULL) {
- lg2msg = lg2err->message;
- lg2spacer = " - ";
- }
+ o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+ o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
+ GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
+
+ parse_opts(&o, argc, argv);
+
+ /**
+ * Try to open the repository at the given path (or at the current
+ * directory if none was given).
+ */
+ check_lg2(git_repository_open_ext(&repo, o.repodir, 0, NULL),
+ "Could not open repository", o.repodir);
+
+ if (git_repository_is_bare(repo))
+ fatal("Cannot report status on bare repository",
+ git_repository_path(repo));
- if (extra)
- fprintf(stderr, "%s '%s' [%d]%s%s\n",
- message, extra, error, lg2spacer, lg2msg);
+ /**
+ * Run status on the repository
+ *
+ * Because we want to simluate a full "git status" run and want to
+ * support some command line options, we use `git_status_foreach_ext()`
+ * instead of just the plain status call. This allows (a) iterating
+ * over the index and then the workdir and (b) extra flags that control
+ * which files are included. If you just want simple status (e.g. to
+ * enumerate files that are modified) then you probably don't need the
+ * extended API.
+ */
+ check_lg2(git_status_list_new(&status, repo, &o.statusopt),
+ "Could not get status", NULL);
+
+ if (o.showbranch)
+ show_branch(repo, o.format);
+
+ if (o.format == FORMAT_LONG)
+ print_long(repo, status);
else
- fprintf(stderr, "%s [%d]%s%s\n",
- message, error, lg2spacer, lg2msg);
+ print_short(repo, status);
- exit(1);
-}
+ git_status_list_free(status);
+ git_repository_free(repo);
+ git_threads_shutdown();
-static void fail(const char *message)
-{
- check(-1, message, NULL);
+ return 0;
}
static void show_branch(git_repository *repo, int format)
@@ -78,7 +121,7 @@ static void show_branch(git_repository *repo, int format)
if (!strncmp(branch, "refs/heads/", strlen("refs/heads/")))
branch += strlen("refs/heads/");
} else
- check(error, "failed to get current branch", NULL);
+ check_lg2(error, "failed to get current branch", NULL);
if (format == FORMAT_LONG)
printf("# On branch %s\n",
@@ -99,7 +142,7 @@ static void print_long(git_repository *repo, git_status_list *status)
(void)repo;
- /* print index changes */
+ /** Print index changes. */
for (i = 0; i < maxi; ++i) {
char *istatus = NULL;
@@ -148,7 +191,7 @@ static void print_long(git_repository *repo, git_status_list *status)
}
header = 0;
- /* print workdir changes to tracked files */
+ /** Print workdir changes to tracked files. */
for (i = 0; i < maxi; ++i) {
char *wstatus = NULL;
@@ -193,7 +236,7 @@ static void print_long(git_repository *repo, git_status_list *status)
}
header = 0;
- /* print untracked files */
+ /** Print untracked files. */
header = 0;
@@ -215,7 +258,7 @@ static void print_long(git_repository *repo, git_status_list *status)
header = 0;
- /* print ignored files */
+ /** Print ignored files. */
for (i = 0; i < maxi; ++i) {
s = git_status_byindex(status, i);
@@ -341,103 +384,58 @@ static void print_short(git_repository *repo, git_status_list *status)
}
}
-int main(int argc, char *argv[])
+static void parse_opts(struct opts *o, int argc, char *argv[])
{
- git_repository *repo = NULL;
- int i, npaths = 0, format = FORMAT_DEFAULT, zterm = 0, showbranch = 0;
- git_status_options opt = GIT_STATUS_OPTIONS_INIT;
- git_status_list *status;
- char *repodir = ".", *pathspec[MAX_PATHSPEC];
+ struct args_info args = ARGS_INFO_INIT;
- opt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
- opt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
- GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
- GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
+ for (args.pos = 1; args.pos < argc; ++args.pos) {
+ char *a = argv[args.pos];
- for (i = 1; i < argc; ++i) {
- if (argv[i][0] != '-') {
- if (npaths < MAX_PATHSPEC)
- pathspec[npaths++] = argv[i];
+ if (a[0] != '-') {
+ if (o->npaths < MAX_PATHSPEC)
+ o->pathspec[o->npaths++] = a;
else
- fail("Example only supports a limited pathspec");
+ fatal("Example only supports a limited pathspec", NULL);
}
- else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--short"))
- format = FORMAT_SHORT;
- else if (!strcmp(argv[i], "--long"))
- format = FORMAT_LONG;
- else if (!strcmp(argv[i], "--porcelain"))
- format = FORMAT_PORCELAIN;
- else if (!strcmp(argv[i], "-b") || !strcmp(argv[i], "--branch"))
- showbranch = 1;
- else if (!strcmp(argv[i], "-z")) {
- zterm = 1;
- if (format == FORMAT_DEFAULT)
- format = FORMAT_PORCELAIN;
+ else if (!strcmp(a, "-s") || !strcmp(a, "--short"))
+ o->format = FORMAT_SHORT;
+ else if (!strcmp(a, "--long"))
+ o->format = FORMAT_LONG;
+ else if (!strcmp(a, "--porcelain"))
+ o->format = FORMAT_PORCELAIN;
+ else if (!strcmp(a, "-b") || !strcmp(a, "--branch"))
+ o->showbranch = 1;
+ else if (!strcmp(a, "-z")) {
+ o->zterm = 1;
+ if (o->format == FORMAT_DEFAULT)
+ o->format = FORMAT_PORCELAIN;
}
- else if (!strcmp(argv[i], "--ignored"))
- opt.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED;
- else if (!strcmp(argv[i], "-uno") ||
- !strcmp(argv[i], "--untracked-files=no"))
- opt.flags &= ~GIT_STATUS_OPT_INCLUDE_UNTRACKED;
- else if (!strcmp(argv[i], "-unormal") ||
- !strcmp(argv[i], "--untracked-files=normal"))
- opt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
- else if (!strcmp(argv[i], "-uall") ||
- !strcmp(argv[i], "--untracked-files=all"))
- opt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ else if (!strcmp(a, "--ignored"))
+ o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED;
+ else if (!strcmp(a, "-uno") ||
+ !strcmp(a, "--untracked-files=no"))
+ o->statusopt.flags &= ~GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+ else if (!strcmp(a, "-unormal") ||
+ !strcmp(a, "--untracked-files=normal"))
+ o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+ else if (!strcmp(a, "-uall") ||
+ !strcmp(a, "--untracked-files=all"))
+ o->statusopt.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
- else if (!strcmp(argv[i], "--ignore-submodules=all"))
- opt.flags |= GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
- else if (!strncmp(argv[i], "--git-dir=", strlen("--git-dir=")))
- repodir = argv[i] + strlen("--git-dir=");
+ else if (!strcmp(a, "--ignore-submodules=all"))
+ o->statusopt.flags |= GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
+ else if (!strncmp(a, "--git-dir=", strlen("--git-dir=")))
+ o->repodir = a + strlen("--git-dir=");
else
- check(-1, "Unsupported option", argv[i]);
+ check_lg2(-1, "Unsupported option", a);
}
- if (format == FORMAT_DEFAULT)
- format = FORMAT_LONG;
- if (format == FORMAT_LONG)
- showbranch = 1;
- if (npaths > 0) {
- opt.pathspec.strings = pathspec;
- opt.pathspec.count = npaths;
+ if (o->format == FORMAT_DEFAULT)
+ o->format = FORMAT_LONG;
+ if (o->format == FORMAT_LONG)
+ o->showbranch = 1;
+ if (o->npaths > 0) {
+ o->statusopt.pathspec.strings = o->pathspec;
+ o->statusopt.pathspec.count = o->npaths;
}
-
- /*
- * Try to open the repository at the given path (or at the current
- * directory if none was given).
- */
- check(git_repository_open_ext(&repo, repodir, 0, NULL),
- "Could not open repository", repodir);
-
- if (git_repository_is_bare(repo))
- fail("Cannot report status on bare repository");
-
- /*
- * Run status on the repository
- *
- * Because we want to simluate a full "git status" run and want to
- * support some command line options, we use `git_status_foreach_ext()`
- * instead of just the plain status call. This allows (a) iterating
- * over the index and then the workdir and (b) extra flags that control
- * which files are included. If you just want simple status (e.g. to
- * enumerate files that are modified) then you probably don't need the
- * extended API.
- */
- check(git_status_list_new(&status, repo, &opt),
- "Could not get status", NULL);
-
- if (showbranch)
- show_branch(repo, format);
-
- if (format == FORMAT_LONG)
- print_long(repo, status);
- else
- print_short(repo, status);
-
- git_status_list_free(status);
- git_repository_free(repo);
-
- return 0;
}
-