examples: create common lg2 executable Inside of our networking example code, we have a git2 executable that acts as an entry point to all the different network examples. As such, it is kind of the same like the normal git(1) executable in that it simply arbitrates to the respective subcommands. Let's extend this approach and merge all examples into a single standalone lg2 executable. Instead of building an executable for all the existing examples we have, we now bundle them all inside of the lg2 one and let them be callable via subcommands. In the process, we can get rid of duplicated library initialization, deinitialization and repository discovery code. Instead of having each subcommand handle these on its own, we simply do it inside of the single main function now.
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
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index c02af06..05e344d 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,25 +1,15 @@
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
-FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c common.?)
-ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
-SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
+FILE(GLOB LG2_SOURCES *.c)
+ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
+SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
# Ensure that we do not use deprecated functions internally
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
IF(WIN32 OR ANDROID)
- TARGET_LINK_LIBRARIES(cgit2 git2)
+ TARGET_LINK_LIBRARIES(lg2 git2)
ELSE()
- TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
+ TARGET_LINK_LIBRARIES(lg2 git2 pthread)
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})
- IF(NOT ${app_name} STREQUAL "common")
- ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
- TARGET_LINK_LIBRARIES(${app_name} git2)
- SET_TARGET_PROPERTIES(${app_name} PROPERTIES C_STANDARD 90)
- ENDIF()
-ENDFOREACH()
diff --git a/examples/add.c b/examples/add.c
index e584989..276ba35 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -31,22 +31,18 @@ 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)
+int lg2_add(git_repository *repo, int argc, char** argv)
{
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};
- git_libgit2_init();
-
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) {
@@ -64,9 +60,6 @@ int main (int argc, char** argv)
git_index_write(index);
git_index_free(index);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/blame.c b/examples/blame.c
index 9288352..0ddbfc6 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -35,30 +35,24 @@ struct opts {
};
static void parse_opts(struct opts *o, int argc, char *argv[]);
-int main(int argc, char *argv[])
+int lg2_blame(git_repository *repo, int argc, char *argv[])
{
int line, break_on_null_hunk;
size_t i, rawsize;
char spec[1024] = {0};
struct opts o = {0};
const char *rawdata;
- git_repository *repo = NULL;
git_revspec revspec = {0};
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
git_blame *blame = NULL;
git_blob *blob;
git_object *obj;
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
- /** Open the repository. */
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
-
/**
* The commit range comes in "commitish" form. Use the rev-parse API to
* nail down the end points.
@@ -131,9 +125,6 @@ int main(int argc, char *argv[])
/** Cleanup. */
git_blob_free(blob);
git_blame_free(blame);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/cat-file.c b/examples/cat-file.c
index 0fb7d45..9b14a70 100644
--- a/examples/cat-file.c
+++ b/examples/cat-file.c
@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
/** Entry point for this command */
-int main(int argc, char *argv[])
+int lg2_cat_file(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo;
struct opts o = { ".", NULL, 0, 0 };
git_object *obj = NULL;
char oidstr[GIT_OID_HEXSZ + 1];
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
- 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);
@@ -188,9 +183,6 @@ int main(int argc, char *argv[])
}
git_object_free(obj);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/checkout.c b/examples/checkout.c
index 9119d65..577a6f1 100644
--- a/examples/checkout.c
+++ b/examples/checkout.c
@@ -172,9 +172,8 @@ cleanup:
}
/** That example's entry point */
-int main(int argc, char **argv)
+int lg2_checkout(git_repository *repo, int argc, char **argv)
{
- git_repository *repo = NULL;
struct args_info args = ARGS_INFO_INIT;
checkout_options opts;
git_repository_state_t state;
@@ -185,15 +184,6 @@ int main(int argc, char **argv)
/** Parse our command line options */
parse_options(&path, &opts, &args);
- /** Initialize the library */
- err = git_libgit2_init();
- if (!err)
- check_lg2(err, "Failed to initialize libgit2", NULL);
-
- /** Open the repository corresponding to the options */
- check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
- "Could not open repository", NULL);
-
/** Make sure we're not about to checkout while something else is going on */
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
@@ -228,8 +218,5 @@ int main(int argc, char **argv)
cleanup:
git_annotated_commit_free(checkout_target);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return err;
}
diff --git a/examples/clone.c b/examples/clone.c
new file mode 100644
index 0000000..fc121bc
--- /dev/null
+++ b/examples/clone.c
@@ -0,0 +1,104 @@
+#include "common.h"
+
+typedef struct progress_data {
+ git_transfer_progress fetch_progress;
+ size_t completed_steps;
+ size_t total_steps;
+ const char *path;
+} progress_data;
+
+static void print_progress(const progress_data *pd)
+{
+ int network_percent = pd->fetch_progress.total_objects > 0 ?
+ (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects :
+ 0;
+ int index_percent = pd->fetch_progress.total_objects > 0 ?
+ (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects :
+ 0;
+
+ int checkout_percent = pd->total_steps > 0
+ ? (100 * pd->completed_steps) / pd->total_steps
+ : 0;
+ int kbytes = pd->fetch_progress.received_bytes / 1024;
+
+ if (pd->fetch_progress.total_objects &&
+ pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) {
+ printf("Resolving deltas %d/%d\r",
+ pd->fetch_progress.indexed_deltas,
+ pd->fetch_progress.total_deltas);
+ } else {
+ printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
+ network_percent, kbytes,
+ pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
+ index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
+ checkout_percent,
+ pd->completed_steps, pd->total_steps,
+ pd->path);
+ }
+}
+
+static int sideband_progress(const char *str, int len, void *payload)
+{
+ (void)payload; /* unused */
+
+ printf("remote: %.*s", len, str);
+ fflush(stdout);
+ return 0;
+}
+
+static int fetch_progress(const git_transfer_progress *stats, void *payload)
+{
+ progress_data *pd = (progress_data*)payload;
+ pd->fetch_progress = *stats;
+ print_progress(pd);
+ return 0;
+}
+static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
+{
+ progress_data *pd = (progress_data*)payload;
+ pd->completed_steps = cur;
+ pd->total_steps = tot;
+ pd->path = path;
+ print_progress(pd);
+}
+
+
+int lg2_clone(git_repository *repo, int argc, char **argv)
+{
+ progress_data pd = {{0}};
+ git_repository *cloned_repo = NULL;
+ git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
+ git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
+ const char *url = argv[1];
+ const char *path = argv[2];
+ int error;
+
+ (void)repo; /* unused */
+
+ /* Validate args */
+ if (argc < 3) {
+ printf ("USAGE: %s <url> <path>\n", argv[0]);
+ return -1;
+ }
+
+ /* Set up options */
+ checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+ checkout_opts.progress_cb = checkout_progress;
+ checkout_opts.progress_payload = &pd;
+ clone_opts.checkout_opts = checkout_opts;
+ clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
+ clone_opts.fetch_opts.callbacks.transfer_progress = &fetch_progress;
+ clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb;
+ clone_opts.fetch_opts.callbacks.payload = &pd;
+
+ /* Do the clone */
+ error = git_clone(&cloned_repo, url, path, &clone_opts);
+ printf("\n");
+ if (error != 0) {
+ const git_error *err = git_error_last();
+ if (err) printf("ERROR %d: %s\n", err->klass, err->message);
+ else printf("ERROR %d: no detailed info\n", error);
+ }
+ else if (cloned_repo) git_repository_free(cloned_repo);
+ return error;
+}
diff --git a/examples/common.h b/examples/common.h
index 63f25c7..ac1c067 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -26,8 +26,32 @@
#endif
#endif
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(x)
+extern int lg2_add(git_repository *repo, int argc, char **argv);
+extern int lg2_blame(git_repository *repo, int argc, char **argv);
+extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
+extern int lg2_checkout(git_repository *repo, int argc, char **argv);
+extern int lg2_clone(git_repository *repo, int argc, char **argv);
+extern int lg2_describe(git_repository *repo, int argc, char **argv);
+extern int lg2_diff(git_repository *repo, int argc, char **argv);
+extern int lg2_fetch(git_repository *repo, int argc, char **argv);
+extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv);
+extern int lg2_general(git_repository *repo, int argc, char **argv);
+extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
+extern int lg2_init(git_repository *repo, int argc, char **argv);
+extern int lg2_log(git_repository *repo, int argc, char **argv);
+extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
+extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
+extern int lg2_merge(git_repository *repo, int argc, char **argv);
+extern int lg2_remote(git_repository *repo, int argc, char **argv);
+extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
+extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
+extern int lg2_show_index(git_repository *repo, int argc, char **argv);
+extern int lg2_status(git_repository *repo, int argc, char **argv);
+extern int lg2_tag(git_repository *repo, int argc, char **argv);
+
/**
* Check libgit2 error code, printing error to stderr on failure and
* exiting the program.
@@ -142,8 +166,3 @@ extern int cred_acquire_cb(git_cred **out,
const char *username_from_url,
unsigned int allowed_types,
void *payload);
-
-extern int ls_remote(git_repository *repo, int argc, char **argv);
-extern int fetch(git_repository *repo, int argc, char **argv);
-extern int index_pack(git_repository *repo, int argc, char **argv);
-extern int do_clone(git_repository *repo, int argc, char **argv);
diff --git a/examples/describe.c b/examples/describe.c
index 2005de4..966da15 100644
--- a/examples/describe.c
+++ b/examples/describe.c
@@ -152,23 +152,14 @@ static void describe_options_init(describe_options *opts)
git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
}
-int main(int argc, char **argv)
+int lg2_describe(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
describe_options opts;
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
- "Could not open repository", NULL);
-
describe_options_init(&opts);
parse_options(&opts, argc, argv);
do_describe(repo, &opts);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}
diff --git a/examples/diff.c b/examples/diff.c
index 1de0483..e8ba918 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -67,9 +67,8 @@ static int color_printer(
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
static void diff_print_stats(git_diff *diff, struct opts *o);
-int main(int argc, char *argv[])
+int lg2_diff(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL;
git_diff *diff;
struct opts o = {
@@ -77,13 +76,8 @@ int main(int argc, char *argv[])
-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
};
- git_libgit2_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:
*
@@ -157,13 +151,9 @@ int main(int argc, char *argv[])
}
/** Cleanup before exiting. */
-
git_diff_free(diff);
git_tree_free(t1);
git_tree_free(t2);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/fetch.c b/examples/fetch.c
new file mode 100644
index 0000000..1df373e
--- /dev/null
+++ b/examples/fetch.c
@@ -0,0 +1,109 @@
+#include "common.h"
+
+static int progress_cb(const char *str, int len, void *data)
+{
+ (void)data;
+ printf("remote: %.*s", len, str);
+ fflush(stdout); /* We don't have the \n to force the flush */
+ return 0;
+}
+
+/**
+ * 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];
+ (void)data;
+
+ git_oid_fmt(b_str, b);
+ b_str[GIT_OID_HEXSZ] = '\0';
+
+ if (git_oid_iszero(a)) {
+ printf("[new] %.20s %s\n", b_str, refname);
+ } else {
+ git_oid_fmt(a_str, a);
+ a_str[GIT_OID_HEXSZ] = '\0';
+ printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname);
+ }
+
+ return 0;
+}
+
+/**
+ * This gets called during the download and indexing. Here we show
+ * processed and total objects in the pack and the amount of received
+ * data. Most frontends will probably want to show a percentage and
+ * the download rate.
+ */
+static int transfer_progress_cb(const git_transfer_progress *stats, void *payload)
+{
+ (void)payload;
+
+ if (stats->received_objects == stats->total_objects) {
+ printf("Resolving deltas %d/%d\r",
+ stats->indexed_deltas, stats->total_deltas);
+ } else if (stats->total_objects > 0) {
+ printf("Received %d/%d objects (%d) in %" PRIuZ " bytes\r",
+ stats->received_objects, stats->total_objects,
+ stats->indexed_objects, stats->received_bytes);
+ }
+ return 0;
+}
+
+/** Entry point for this command */
+int lg2_fetch(git_repository *repo, int argc, char **argv)
+{
+ git_remote *remote = NULL;
+ const git_transfer_progress *stats;
+ git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
+ return EXIT_FAILURE;
+ }
+
+ /* Figure out whether it's a named remote or a URL */
+ printf("Fetching %s for repo %p\n", argv[1], repo);
+ if (git_remote_lookup(&remote, repo, argv[1]) < 0)
+ if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
+ goto on_error;
+
+ /* Set up the callbacks (only update_tips for now) */
+ fetch_opts.callbacks.update_tips = &update_cb;
+ fetch_opts.callbacks.sideband_progress = &progress_cb;
+ fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
+ fetch_opts.callbacks.credentials = cred_acquire_cb;
+
+ /**
+ * Perform the fetch with the configured refspecs from the
+ * config. Update the reflog for the updated references with
+ * "fetch".
+ */
+ if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0)
+ goto on_error;
+
+ /**
+ * 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.
+ */
+ stats = git_remote_stats(remote);
+ if (stats->local_objects > 0) {
+ printf("\rReceived %d/%d objects in %" PRIuZ " bytes (used %d local objects)\n",
+ stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
+ } else{
+ printf("\rReceived %d/%d objects in %" PRIuZ "bytes\n",
+ stats->indexed_objects, stats->total_objects, stats->received_bytes);
+ }
+
+ git_remote_free(remote);
+
+ return 0;
+
+ on_error:
+ git_remote_free(remote);
+ return -1;
+}
diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c
index 3bc25fc..eea73d2 100644
--- a/examples/for-each-ref.c
+++ b/examples/for-each-ref.c
@@ -31,19 +31,15 @@ static int show_ref(git_reference *ref, void *data)
return 0;
}
-int main(int argc, char **argv)
+int lg2_for_each_ref(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
- git_libgit2_init();
+ UNUSED(argv);
- if (argc != 1 || argv[1] /* silence -Wunused-parameter */)
+ if (argc != 1)
fatal("Sorry, no for-each-ref options supported yet", NULL);
- check_lg2(git_repository_open(&repo, "."),
- "Could not open repository", NULL);
check_lg2(git_reference_foreach(repo, show_ref, repo),
"Could not iterate over references", NULL);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/general.c b/examples/general.c
index e0f5b5e..2e9a321 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -76,12 +76,11 @@ static void check_error(int error_code, const char *action)
exit(1);
}
-int main (int argc, char** argv)
+int lg2_general(git_repository *repo, int argc, char** argv)
{
int error;
git_oid oid;
char *repo_path;
- git_repository *repo;
/**
* Initialize the library, this will set up any global state which libgit2 needs
diff --git a/examples/index-pack.c b/examples/index-pack.c
new file mode 100644
index 0000000..df11177
--- /dev/null
+++ b/examples/index-pack.c
@@ -0,0 +1,87 @@
+#include "common.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#ifdef _WIN32
+# include <io.h>
+# include <Windows.h>
+
+# define open _open
+# define read _read
+# define close _close
+
+#define ssize_t unsigned int
+#else
+# include <unistd.h>
+#endif
+
+/*
+ * This could be run in the main loop whilst the application waits for
+ * the indexing to finish in a worker thread
+ */
+static int index_cb(const git_transfer_progress *stats, void *data)
+{
+ (void)data;
+ printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects);
+
+ return 0;
+}
+
+int lg2_index_pack(git_repository *repo, int argc, char **argv)
+{
+ git_indexer *idx;
+ git_transfer_progress stats = {0, 0};
+ int error;
+ char hash[GIT_OID_HEXSZ + 1] = {0};
+ int fd;
+ ssize_t read_bytes;
+ char buf[512];
+
+ (void)repo;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
+ return EXIT_FAILURE;
+ }
+
+ if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
+ puts("bad idx");
+ return -1;
+ }
+
+ if ((fd = open(argv[1], 0)) < 0) {
+ perror("open");
+ return -1;
+ }
+
+ do {
+ read_bytes = read(fd, buf, sizeof(buf));
+ if (read_bytes < 0)
+ break;
+
+ if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
+ goto cleanup;
+
+ index_cb(&stats, NULL);
+ } while (read_bytes > 0);
+
+ if (read_bytes < 0) {
+ error = -1;
+ perror("failed reading");
+ goto cleanup;
+ }
+
+ if ((error = git_indexer_commit(idx, &stats)) < 0)
+ goto cleanup;
+
+ printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects);
+
+ git_oid_fmt(hash, git_indexer_hash(idx));
+ puts(hash);
+
+ cleanup:
+ close(fd);
+ git_indexer_free(idx);
+ return error;
+}
diff --git a/examples/init.c b/examples/init.c
index fe7a672..23044d1 100644
--- a/examples/init.c
+++ b/examples/init.c
@@ -40,14 +40,10 @@ struct opts {
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[])
+int lg2_init(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
/* Initialize repository. */
@@ -116,7 +112,6 @@ int main(int argc, char *argv[])
}
git_repository_free(repo);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/lg2.c b/examples/lg2.c
new file mode 100644
index 0000000..1f6fd85
--- /dev/null
+++ b/examples/lg2.c
@@ -0,0 +1,109 @@
+#include "common.h"
+
+/* This part is not strictly libgit2-dependent, but you can use this
+ * as a starting point for a git-like tool */
+
+typedef int (*git_command_fn)(git_repository *, int , char **);
+
+struct {
+ char *name;
+ git_command_fn fn;
+ char requires_repo;
+} commands[] = {
+ { "add", lg2_add, 1 },
+ { "blame", lg2_blame, 1 },
+ { "cat-file", lg2_cat_file, 1 },
+ { "checkout", lg2_checkout, 1 },
+ { "clone", lg2_clone, 0 },
+ { "describe", lg2_describe, 1 },
+ { "diff", lg2_diff, 1 },
+ { "fetch", lg2_fetch, 1 },
+ { "for-each-ref", lg2_for_each_ref, 1 },
+ { "general", lg2_general, 0 },
+ { "index-pack", lg2_index_pack, 1 },
+ { "init", lg2_init, 0 },
+ { "log", lg2_log, 1 },
+ { "ls-files", lg2_ls_files, 1 },
+ { "ls-remote", lg2_ls_remote, 1 },
+ { "merge", lg2_merge, 1 },
+ { "remote", lg2_remote, 1 },
+ { "rev-list", lg2_rev_list, 1 },
+ { "rev-parse", lg2_rev_parse, 1 },
+ { "show-index", lg2_show_index, 0 },
+ { "status", lg2_status, 1 },
+ { "tag", lg2_tag, 1 },
+};
+
+static int run_command(git_command_fn fn, git_repository *repo, struct args_info args)
+{
+ int error;
+
+ /* Run the command. If something goes wrong, print the error message to stderr */
+ error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
+ if (error < 0) {
+ if (git_error_last() == NULL)
+ fprintf(stderr, "Error without message");
+ else
+ fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
+ }
+
+ return !!error;
+}
+
+int main(int argc, char **argv)
+{
+ struct args_info args = ARGS_INFO_INIT;
+ git_repository *repo = NULL;
+ const char *git_dir = NULL;
+ int return_code = 1;
+ size_t i;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ git_libgit2_init();
+
+ for (args.pos = 1; args.pos < args.argc; ++args.pos) {
+ char *a = args.argv[args.pos];
+
+ if (a[0] != '-') {
+ /* non-arg */
+ break;
+ } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
+ continue;
+ } else if (!strcmp(a, "--")) {
+ /* arg separator */
+ break;
+ }
+ }
+
+ if (!git_dir)
+ git_dir = ".";
+
+ for (i = 0; i < ARRAY_SIZE(commands); ++i) {
+ if (strcmp(args.argv[args.pos], commands[i].name))
+ continue;
+
+ /*
+ * Before running the actual command, create an instance
+ * of the local repository and pass it to the function.
+ * */
+ if (commands[i].requires_repo) {
+ check_lg2(git_repository_open_ext(&repo, git_dir, 0, NULL),
+ "Unable to open repository '%s'", git_dir);
+ }
+
+ return_code = run_command(commands[i].fn, repo, args);
+ goto shutdown;
+ }
+
+ fprintf(stderr, "Command not found: %s\n", argv[1]);
+
+shutdown:
+ git_repository_free(repo);
+ git_libgit2_shutdown();
+
+ return return_code;
+}
diff --git a/examples/log.c b/examples/log.c
index 3e891e4..a6bd957 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -71,7 +71,7 @@ static int match_with_parent(git_commit *commit, int i, git_diff_options *);
static int signature_matches(const git_signature *sig, const char *filter);
static int log_message_matches(const git_commit *commit, const char *filter);
-int main(int argc, char *argv[])
+int lg2_log(git_repository *repo, int argc, char *argv[])
{
int i, count = 0, printed = 0, parents, last_arg;
struct log_state s;
@@ -81,11 +81,9 @@ int main(int argc, char *argv[])
git_commit *commit = NULL;
git_pathspec *ps = NULL;
- git_libgit2_init();
-
/** Parse arguments and set up revwalker. */
-
last_arg = parse_options(&s, &opt, argc, argv);
+ s.repo = repo;
diffopts.pathspec.strings = &argv[last_arg];
diffopts.pathspec.count = argc - last_arg;
@@ -180,8 +178,6 @@ int main(int argc, char *argv[])
git_pathspec_free(ps);
git_revwalk_free(s.walker);
- git_repository_free(s.repo);
- git_libgit2_shutdown();
return 0;
}
@@ -243,13 +239,6 @@ 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_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
- "Could not open repository", s->repodir);
- }
-
if (!revstr) {
push_rev(s, NULL, hide);
return 0;
diff --git a/examples/ls-files.c b/examples/ls-files.c
index 98c89c9..b82af01 100644
--- a/examples/ls-files.c
+++ b/examples/ls-files.c
@@ -13,7 +13,6 @@
*/
#include "common.h"
-#include "array.h"
/**
* This example demonstrates the libgit2 index APIs to roughly
@@ -111,21 +110,15 @@ static int print_paths(ls_options *opts, git_index *index)
return 0;
}
-int main(int argc, char *argv[])
+int lg2_ls_files(git_repository *repo, int argc, char *argv[])
{
- ls_options opts;
- git_repository *repo = NULL;
git_index *index = NULL;
+ ls_options opts;
int error;
if ((error = parse_options(&opts, argc, argv)) < 0)
return error;
- git_libgit2_init();
-
- if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) < 0)
- goto cleanup;
-
if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup;
@@ -133,8 +126,6 @@ int main(int argc, char *argv[])
cleanup:
git_index_free(index);
- git_repository_free(repo);
- git_libgit2_shutdown();
return error;
}
diff --git a/examples/ls-remote.c b/examples/ls-remote.c
new file mode 100644
index 0000000..03ed887
--- /dev/null
+++ b/examples/ls-remote.c
@@ -0,0 +1,60 @@
+#include "common.h"
+
+static int use_remote(git_repository *repo, char *name)
+{
+ git_remote *remote = NULL;
+ int error;
+ const git_remote_head **refs;
+ size_t refs_len, i;
+ git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
+
+ /* Find the remote by name */
+ error = git_remote_lookup(&remote, repo, name);
+ if (error < 0) {
+ error = git_remote_create_anonymous(&remote, repo, name);
+ if (error < 0)
+ goto cleanup;
+ }
+
+ /**
+ * Connect to the remote and call the printing function for
+ * each of the remote references.
+ */
+ callbacks.credentials = cred_acquire_cb;
+
+ error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL);
+ if (error < 0)
+ goto cleanup;
+
+ /**
+ * Get the list of references on the remote and print out
+ * their name next to what they point to.
+ */
+ if (git_remote_ls(&refs, &refs_len, remote) < 0)
+ goto cleanup;
+
+ for (i = 0; i < refs_len; i++) {
+ char oid[GIT_OID_HEXSZ + 1] = {0};
+ git_oid_fmt(oid, &refs[i]->oid);
+ printf("%s\t%s\n", oid, refs[i]->name);
+ }
+
+cleanup:
+ git_remote_free(remote);
+ return error;
+}
+
+/** Entry point for this command */
+int lg2_ls_remote(git_repository *repo, int argc, char **argv)
+{
+ int error;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
+ return EXIT_FAILURE;
+ }
+
+ error = use_remote(repo, argv[1]);
+
+ return error;
+}
diff --git a/examples/merge.c b/examples/merge.c
index 13e3f96..a275ecb 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -278,9 +278,8 @@ cleanup:
return err;
}
-int main(int argc, char **argv)
+int lg2_merge(git_repository *repo, int argc, char **argv)
{
- git_repository *repo = NULL;
merge_options opts;
git_index *index;
git_repository_state_t state;
@@ -292,11 +291,6 @@ int main(int argc, char **argv)
merge_options_init(&opts);
parse_options(&path, &opts, argc, argv);
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
- "Could not open repository", NULL);
-
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
@@ -366,8 +360,6 @@ int main(int argc, char **argv)
cleanup:
free(opts.heads);
free(opts.annotated);
- git_repository_free(repo);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/network/.gitignore b/examples/network/.gitignore
deleted file mode 100644
index 1b48e66..0000000
--- a/examples/network/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/git2
diff --git a/examples/network/clone.c b/examples/network/clone.c
deleted file mode 100644
index 970e84f..0000000
--- a/examples/network/clone.c
+++ /dev/null
@@ -1,104 +0,0 @@
-#include "../common.h"
-
-typedef struct progress_data {
- git_transfer_progress fetch_progress;
- size_t completed_steps;
- size_t total_steps;
- const char *path;
-} progress_data;
-
-static void print_progress(const progress_data *pd)
-{
- int network_percent = pd->fetch_progress.total_objects > 0 ?
- (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects :
- 0;
- int index_percent = pd->fetch_progress.total_objects > 0 ?
- (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects :
- 0;
-
- int checkout_percent = pd->total_steps > 0
- ? (100 * pd->completed_steps) / pd->total_steps
- : 0;
- int kbytes = pd->fetch_progress.received_bytes / 1024;
-
- if (pd->fetch_progress.total_objects &&
- pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) {
- printf("Resolving deltas %d/%d\r",
- pd->fetch_progress.indexed_deltas,
- pd->fetch_progress.total_deltas);
- } else {
- printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
- network_percent, kbytes,
- pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
- index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
- checkout_percent,
- pd->completed_steps, pd->total_steps,
- pd->path);
- }
-}
-
-static int sideband_progress(const char *str, int len, void *payload)
-{
- (void)payload; /* unused */
-
- printf("remote: %.*s", len, str);
- fflush(stdout);
- return 0;
-}
-
-static int fetch_progress(const git_transfer_progress *stats, void *payload)
-{
- progress_data *pd = (progress_data*)payload;
- pd->fetch_progress = *stats;
- print_progress(pd);
- return 0;
-}
-static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
-{
- progress_data *pd = (progress_data*)payload;
- pd->completed_steps = cur;
- pd->total_steps = tot;
- pd->path = path;
- print_progress(pd);
-}
-
-
-int do_clone(git_repository *repo, int argc, char **argv)
-{
- progress_data pd = {{0}};
- git_repository *cloned_repo = NULL;
- git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
- git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
- const char *url = argv[1];
- const char *path = argv[2];
- int error;
-
- (void)repo; /* unused */
-
- /* Validate args */
- if (argc < 3) {
- printf ("USAGE: %s <url> <path>\n", argv[0]);
- return -1;
- }
-
- /* Set up options */
- checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
- checkout_opts.progress_cb = checkout_progress;
- checkout_opts.progress_payload = &pd;
- clone_opts.checkout_opts = checkout_opts;
- clone_opts.fetch_opts.callbacks.sideband_progress = sideband_progress;
- clone_opts.fetch_opts.callbacks.transfer_progress = &fetch_progress;
- clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb;
- clone_opts.fetch_opts.callbacks.payload = &pd;
-
- /* Do the clone */
- error = git_clone(&cloned_repo, url, path, &clone_opts);
- printf("\n");
- if (error != 0) {
- const git_error *err = git_error_last();
- if (err) printf("ERROR %d: %s\n", err->klass, err->message);
- else printf("ERROR %d: no detailed info\n", error);
- }
- else if (cloned_repo) git_repository_free(cloned_repo);
- return error;
-}
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
deleted file mode 100644
index bb5f1b1..0000000
--- a/examples/network/fetch.c
+++ /dev/null
@@ -1,109 +0,0 @@
-#include "../common.h"
-
-static int progress_cb(const char *str, int len, void *data)
-{
- (void)data;
- printf("remote: %.*s", len, str);
- fflush(stdout); /* We don't have the \n to force the flush */
- return 0;
-}
-
-/**
- * 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];
- (void)data;
-
- git_oid_fmt(b_str, b);
- b_str[GIT_OID_HEXSZ] = '\0';
-
- if (git_oid_iszero(a)) {
- printf("[new] %.20s %s\n", b_str, refname);
- } else {
- git_oid_fmt(a_str, a);
- a_str[GIT_OID_HEXSZ] = '\0';
- printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname);
- }
-
- return 0;
-}
-
-/**
- * This gets called during the download and indexing. Here we show
- * processed and total objects in the pack and the amount of received
- * data. Most frontends will probably want to show a percentage and
- * the download rate.
- */
-static int transfer_progress_cb(const git_transfer_progress *stats, void *payload)
-{
- (void)payload;
-
- if (stats->received_objects == stats->total_objects) {
- printf("Resolving deltas %d/%d\r",
- stats->indexed_deltas, stats->total_deltas);
- } else if (stats->total_objects > 0) {
- printf("Received %d/%d objects (%d) in %" PRIuZ " bytes\r",
- stats->received_objects, stats->total_objects,
- stats->indexed_objects, stats->received_bytes);
- }
- return 0;
-}
-
-/** Entry point for this command */
-int fetch(git_repository *repo, int argc, char **argv)
-{
- git_remote *remote = NULL;
- const git_transfer_progress *stats;
- git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
-
- if (argc < 2) {
- fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
- return EXIT_FAILURE;
- }
-
- /* Figure out whether it's a named remote or a URL */
- printf("Fetching %s for repo %p\n", argv[1], repo);
- if (git_remote_lookup(&remote, repo, argv[1]) < 0)
- if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
- goto on_error;
-
- /* Set up the callbacks (only update_tips for now) */
- fetch_opts.callbacks.update_tips = &update_cb;
- fetch_opts.callbacks.sideband_progress = &progress_cb;
- fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
- fetch_opts.callbacks.credentials = cred_acquire_cb;
-
- /**
- * Perform the fetch with the configured refspecs from the
- * config. Update the reflog for the updated references with
- * "fetch".
- */
- if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0)
- goto on_error;
-
- /**
- * 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.
- */
- stats = git_remote_stats(remote);
- if (stats->local_objects > 0) {
- printf("\rReceived %d/%d objects in %" PRIuZ " bytes (used %d local objects)\n",
- stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
- } else{
- printf("\rReceived %d/%d objects in %" PRIuZ "bytes\n",
- stats->indexed_objects, stats->total_objects, stats->received_bytes);
- }
-
- git_remote_free(remote);
-
- return 0;
-
- on_error:
- git_remote_free(remote);
- return -1;
-}
diff --git a/examples/network/git2.c b/examples/network/git2.c
deleted file mode 100644
index 4fa2839..0000000
--- a/examples/network/git2.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include "../common.h"
-
-/* This part is not strictly libgit2-dependent, but you can use this
- * as a starting point for a git-like tool */
-
-typedef int (*git_cb)(git_repository *, int , char **);
-
-struct {
- char *name;
- git_cb fn;
-} commands[] = {
- {"ls-remote", ls_remote},
- {"fetch", fetch},
- {"clone", do_clone},
- {"index-pack", index_pack},
- { NULL, NULL}
-};
-
-static int run_command(git_cb fn, git_repository *repo, struct args_info args)
-{
- int error;
-
- /* Run the command. If something goes wrong, print the error message to stderr */
- error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
- if (error < 0) {
- if (git_error_last() == NULL)
- fprintf(stderr, "Error without message");
- else
- fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
- }
-
- return !!error;
-}
-
-int main(int argc, char **argv)
-{
- int i;
- int return_code = 1;
- int error;
- git_repository *repo;
- struct args_info args = ARGS_INFO_INIT;
- const char *git_dir = NULL;
-
- if (argc < 2) {
- fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
- exit(EXIT_FAILURE);
- }
-
- git_libgit2_init();
-
- for (args.pos = 1; args.pos < args.argc; ++args.pos) {
- char *a = args.argv[args.pos];
-
- if (a[0] != '-') {
- /* non-arg */
- break;
- } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
- continue;
- } else if (!strcmp(a, "--")) {
- /* arg separator */
- break;
- }
- }
-
- /* Before running the actual command, create an instance of the local
- * repository and pass it to the function. */
-
- error = git_repository_open(&repo, git_dir);
- if (error < 0)
- repo = NULL;
-
- for (i = 0; commands[i].name != NULL; ++i) {
- if (!strcmp(args.argv[args.pos], commands[i].name)) {
- return_code = run_command(commands[i].fn, repo, args);
- goto shutdown;
- }
- }
-
- fprintf(stderr, "Command not found: %s\n", argv[1]);
-
-shutdown:
- git_repository_free(repo);
-
- git_libgit2_shutdown();
-
- return return_code;
-}
diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c
deleted file mode 100644
index 87445e4..0000000
--- a/examples/network/index-pack.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include "../common.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#ifdef _WIN32
-# include <io.h>
-# include <Windows.h>
-
-# define open _open
-# define read _read
-# define close _close
-
-#define ssize_t unsigned int
-#else
-# include <unistd.h>
-#endif
-
-/*
- * This could be run in the main loop whilst the application waits for
- * the indexing to finish in a worker thread
- */
-static int index_cb(const git_transfer_progress *stats, void *data)
-{
- (void)data;
- printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects);
-
- return 0;
-}
-
-int index_pack(git_repository *repo, int argc, char **argv)
-{
- git_indexer *idx;
- git_transfer_progress stats = {0, 0};
- int error;
- char hash[GIT_OID_HEXSZ + 1] = {0};
- int fd;
- ssize_t read_bytes;
- char buf[512];
-
- (void)repo;
-
- if (argc < 2) {
- fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
- return EXIT_FAILURE;
- }
-
- if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
- puts("bad idx");
- return -1;
- }
-
- if ((fd = open(argv[1], 0)) < 0) {
- perror("open");
- return -1;
- }
-
- do {
- read_bytes = read(fd, buf, sizeof(buf));
- if (read_bytes < 0)
- break;
-
- if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
- goto cleanup;
-
- index_cb(&stats, NULL);
- } while (read_bytes > 0);
-
- if (read_bytes < 0) {
- error = -1;
- perror("failed reading");
- goto cleanup;
- }
-
- if ((error = git_indexer_commit(idx, &stats)) < 0)
- goto cleanup;
-
- printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects);
-
- git_oid_fmt(hash, git_indexer_hash(idx));
- puts(hash);
-
- cleanup:
- close(fd);
- git_indexer_free(idx);
- return error;
-}
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
deleted file mode 100644
index 8181629..0000000
--- a/examples/network/ls-remote.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "../common.h"
-
-static int use_remote(git_repository *repo, char *name)
-{
- git_remote *remote = NULL;
- int error;
- const git_remote_head **refs;
- size_t refs_len, i;
- git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
-
- /* Find the remote by name */
- error = git_remote_lookup(&remote, repo, name);
- if (error < 0) {
- error = git_remote_create_anonymous(&remote, repo, name);
- if (error < 0)
- goto cleanup;
- }
-
- /**
- * Connect to the remote and call the printing function for
- * each of the remote references.
- */
- callbacks.credentials = cred_acquire_cb;
-
- error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL, NULL);
- if (error < 0)
- goto cleanup;
-
- /**
- * Get the list of references on the remote and print out
- * their name next to what they point to.
- */
- if (git_remote_ls(&refs, &refs_len, remote) < 0)
- goto cleanup;
-
- for (i = 0; i < refs_len; i++) {
- char oid[GIT_OID_HEXSZ + 1] = {0};
- git_oid_fmt(oid, &refs[i]->oid);
- printf("%s\t%s\n", oid, refs[i]->name);
- }
-
-cleanup:
- git_remote_free(remote);
- return error;
-}
-
-/** Entry point for this command */
-int ls_remote(git_repository *repo, int argc, char **argv)
-{
- int error;
-
- if (argc < 2) {
- fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
- return EXIT_FAILURE;
- }
-
- error = use_remote(repo, argv[1]);
-
- return error;
-}
diff --git a/examples/remote.c b/examples/remote.c
index 1cb61cc..7345f82 100644
--- a/examples/remote.c
+++ b/examples/remote.c
@@ -48,24 +48,13 @@ static void parse_subcmd(
struct opts *opt, int argc, char **argv);
static void usage(const char *msg, const char *arg);
-int main(int argc, char *argv[])
+int lg2_remote(git_repository *repo, int argc, char *argv[])
{
int retval = 0;
struct opts opt = {0};
- git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
- git_repository *repo = NULL;
parse_subcmd(&opt, argc, argv);
- git_libgit2_init();
-
- check_lg2(git_repository_discover(&buf, ".", 0, NULL),
- "Could not find repository", NULL);
-
- check_lg2(git_repository_open(&repo, buf.ptr),
- "Could not open repository", NULL);
- git_buf_dispose(&buf);
-
switch (opt.cmd)
{
case subcmd_add:
@@ -85,8 +74,6 @@ int main(int argc, char *argv[])
break;
}
- git_libgit2_shutdown();
-
return retval;
}
diff --git a/examples/rev-list.c b/examples/rev-list.c
index ee9afc4..c366ece 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -17,16 +17,12 @@
static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
-int main (int argc, char **argv)
+int lg2_rev_list(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
git_revwalk *walk;
git_oid oid;
char buf[GIT_OID_HEXSZ+1];
- git_libgit2_init();
-
- 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);
@@ -36,7 +32,6 @@ int main (int argc, char **argv)
printf("%s\n", buf);
}
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/rev-parse.c b/examples/rev-parse.c
index 483d6e0..7d6e998 100644
--- a/examples/rev-parse.c
+++ b/examples/rev-parse.c
@@ -16,26 +16,20 @@
/** 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);
+static int parse_revision(git_repository *repo, struct parse_state *ps);
-
-int main(int argc, char *argv[])
+int lg2_rev_parse(git_repository *repo, int argc, char *argv[])
{
struct parse_state ps = {0};
- git_libgit2_init();
parse_opts(&ps, argc, argv);
- check_lg2(parse_revision(&ps), "Parsing", NULL);
-
- git_repository_free(ps.repo);
- git_libgit2_shutdown();
+ check_lg2(parse_revision(repo, &ps), "Parsing", NULL);
return 0;
}
@@ -68,19 +62,12 @@ static void parse_opts(struct parse_state *ps, int argc, char *argv[])
}
}
-static int parse_revision(struct parse_state *ps)
+static int parse_revision(git_repository *repo, struct parse_state *ps)
{
git_revspec rs;
char str[GIT_OID_HEXSZ + 1];
- if (!ps->repo) {
- if (!ps->repodir)
- ps->repodir = ".";
- check_lg2(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
- "Could not open repository from", ps->repodir);
- }
-
- check_lg2(git_revparse(&rs, ps->repo, ps->spec), "Could not parse", ps->spec);
+ check_lg2(git_revparse(&rs, 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));
@@ -94,7 +81,7 @@ static int parse_revision(struct parse_state *ps)
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base;
- check_lg2(git_merge_base(&base, ps->repo,
+ check_lg2(git_merge_base(&base, repo,
git_object_id(rs.from), git_object_id(rs.to)),
"Could not find merge base", ps->spec);
diff --git a/examples/show-index.c b/examples/show-index.c
new file mode 100644
index 0000000..6acadbe
--- /dev/null
+++ b/examples/show-index.c
@@ -0,0 +1,66 @@
+/*
+ * libgit2 "showindex" example - shows how to extract data from the index
+ *
+ * Written by the libgit2 contributors
+ *
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * You should have received a copy of the CC0 Public Domain Dedication along
+ * with this software. If not, see
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
+ */
+
+#include "common.h"
+
+int lg2_show_index(git_repository *repo, int argc, char** argv)
+{
+ git_index *index;
+ unsigned int i, ecount;
+ char *dir = ".";
+ size_t dirlen;
+ char out[GIT_OID_HEXSZ+1];
+ out[GIT_OID_HEXSZ] = '\0';
+
+ if (argc > 2)
+ fatal("usage: showindex [<repo-dir>]", NULL);
+ if (argc > 1)
+ dir = argv[1];
+
+ dirlen = strlen(dir);
+ if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
+ check_lg2(git_index_open(&index, dir), "could not open index", dir);
+ } else {
+ 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, 0);
+
+ ecount = git_index_entrycount(index);
+ if (!ecount)
+ printf("Empty index\n");
+
+ for (i = 0; i < ecount; ++i) {
+ const git_index_entry *e = git_index_get_byindex(index, i);
+
+ git_oid_fmt(out, &e->id);
+
+ printf("File Path: %s\n", e->path);
+ printf(" Stage: %d\n", git_index_entry_stage(e));
+ printf(" Blob SHA: %s\n", out);
+ printf("File Mode: %07o\n", e->mode);
+ printf("File Size: %d bytes\n", (int)e->file_size);
+ printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
+ printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
+ printf(" ctime: %d\n", (int)e->ctime.seconds);
+ printf(" mtime: %d\n", (int)e->mtime.seconds);
+ printf("\n");
+ }
+
+ git_index_free(index);
+
+ return 0;
+}
diff --git a/examples/showindex.c b/examples/showindex.c
deleted file mode 100644
index 43be5e2..0000000
--- a/examples/showindex.c
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * libgit2 "showindex" example - shows how to extract data from the index
- *
- * Written by the libgit2 contributors
- *
- * To the extent possible under law, the author(s) have dedicated all copyright
- * and related and neighboring rights to this software to the public domain
- * worldwide. This software is distributed without any warranty.
- *
- * You should have received a copy of the CC0 Public Domain Dedication along
- * with this software. If not, see
- * <http://creativecommons.org/publicdomain/zero/1.0/>.
- */
-
-#include "common.h"
-
-int main (int argc, char** argv)
-{
- git_index *index;
- unsigned int i, ecount;
- char *dir = ".";
- size_t dirlen;
- char out[GIT_OID_HEXSZ+1];
- out[GIT_OID_HEXSZ] = '\0';
-
- git_libgit2_init();
-
- if (argc > 2)
- fatal("usage: showindex [<repo-dir>]", NULL);
- if (argc > 1)
- dir = argv[1];
-
- dirlen = strlen(dir);
- if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
- check_lg2(git_index_open(&index, dir), "could not open index", dir);
- } else {
- 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, 0);
-
- ecount = git_index_entrycount(index);
- if (!ecount)
- printf("Empty index\n");
-
- for (i = 0; i < ecount; ++i) {
- const git_index_entry *e = git_index_get_byindex(index, i);
-
- git_oid_fmt(out, &e->id);
-
- printf("File Path: %s\n", e->path);
- printf(" Stage: %d\n", git_index_entry_stage(e));
- printf(" Blob SHA: %s\n", out);
- printf("File Mode: %07o\n", e->mode);
- printf("File Size: %d bytes\n", (int)e->file_size);
- printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
- printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
- printf(" ctime: %d\n", (int)e->ctime.seconds);
- printf(" mtime: %d\n", (int)e->mtime.seconds);
- printf("\n");
- }
-
- git_index_free(index);
- git_libgit2_shutdown();
-
- return 0;
-}
diff --git a/examples/status.c b/examples/status.c
index 49f006d..38e5120 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -67,14 +67,11 @@ static void print_long(git_status_list *status);
static void print_short(git_repository *repo, git_status_list *status);
static int print_submod(git_submodule *sm, const char *name, void *payload);
-int main(int argc, char *argv[])
+int lg2_status(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
git_status_list *status;
struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
- git_libgit2_init();
-
o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
@@ -82,13 +79,6 @@ int main(int argc, char *argv[])
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));
@@ -134,9 +124,6 @@ show_status:
goto show_status;
}
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}
diff --git a/examples/tag.c b/examples/tag.c
index fa405b8..7bfb92a 100644
--- a/examples/tag.c
+++ b/examples/tag.c
@@ -293,18 +293,12 @@ static void tag_options_init(tag_options *opts)
opts->force = 0;
}
-int main(int argc, char **argv)
+int lg2_tag(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
tag_options opts;
tag_action action;
tag_state state;
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
- "Could not open repository", NULL);
-
tag_options_init(&opts);
parse_options(&action, &opts, argc, argv);
@@ -312,8 +306,5 @@ int main(int argc, char **argv)
state.opts = &opts;
action(&state);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}