Merge pull request #3897 from pks-t/pks/squelch-example-warnings Squelch example warnings, enable CI
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
diff --git a/examples/add.c b/examples/add.c
index 0101ab9..e584989 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -75,15 +75,14 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
{
struct print_payload p = *(struct print_payload*)(payload);
int ret;
- git_status_t status;
+ unsigned status;
(void)matched_pathspec;
- if (git_status_file((unsigned int*)(&status), p.repo, path)) {
- return -1; //abort
+ if (git_status_file(&status, p.repo, path)) {
+ return -1;
}
- if (status & GIT_STATUS_WT_MODIFIED ||
- status & GIT_STATUS_WT_NEW) {
+ if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) {
printf("add '%s'\n", path);
ret = 0;
} else {
diff --git a/examples/common.c b/examples/common.c
index 0f25f37..96f5eaa 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -146,6 +146,25 @@ int match_uint16_arg(
return 1;
}
+int match_uint32_arg(
+ uint32_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 = (uint32_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)
{
diff --git a/examples/common.h b/examples/common.h
index b9fa37c..adea0d3 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -73,6 +73,15 @@ extern int match_uint16_arg(
uint16_t *out, struct args_info *args, const char *opt);
/**
+ * Check current `args` entry against `opt` string parsing as uint32. 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
+ * uint32_t value; otherwise return 0.
+ */
+extern int match_uint32_arg(
+ uint32_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
diff --git a/examples/diff.c b/examples/diff.c
index b69cb22..9a4f7a5 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -293,11 +293,11 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
else if (is_prefixed(a, "-B") || is_prefixed(a, "--break-rewrites"))
/* TODO: parse thresholds */
o->findopts.flags |= GIT_DIFF_FIND_REWRITES;
- else if (!match_uint16_arg(
+ else if (!match_uint32_arg(
&o->diffopts.context_lines, &args, "-U") &&
- !match_uint16_arg(
+ !match_uint32_arg(
&o->diffopts.context_lines, &args, "--unified") &&
- !match_uint16_arg(
+ !match_uint32_arg(
&o->diffopts.interhunk_lines, &args, "--inter-hunk-context") &&
!match_uint16_arg(
&o->diffopts.id_abbrev, &args, "--abbrev") &&
diff --git a/examples/general.c b/examples/general.c
index 32fdaf4..7c05d87 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -12,39 +12,58 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
-// [**libgit2**][lg] is a portable, pure C implementation of the Git core
-// methods provided as a re-entrant linkable library with a solid API,
-// allowing you to write native speed custom Git applications in any
-// language which supports C bindings.
-//
-// This file is an example of using that API in a real, compilable C file.
-// As the API is updated, this file will be updated to demonstrate the new
-// functionality.
-//
-// If you're trying to write something in C using [libgit2][lg], you should
-// also check out the generated [API documentation][ap]. We try to link to
-// the relevant sections of the API docs in each section in this file.
-//
-// **libgit2** (for the most part) only implements the core plumbing
-// functions, not really the higher level porcelain stuff. For a primer on
-// Git Internals that you will need to know to work with Git at this level,
-// check out [Chapter 10][pg] of the Pro Git book.
-//
-// [lg]: http://libgit2.github.com
-// [ap]: http://libgit2.github.com/libgit2
-// [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
-
-// ### Includes
-
-// Including the `git2.h` header will include all the other libgit2 headers
-// that you need. It should be the only thing you need to include in order
-// to compile properly and get all the libgit2 API.
+/**
+ * [**libgit2**][lg] is a portable, pure C implementation of the Git core
+ * methods provided as a re-entrant linkable library with a solid API,
+ * allowing you to write native speed custom Git applications in any
+ * language which supports C bindings.
+ *
+ * This file is an example of using that API in a real, compilable C file.
+ * As the API is updated, this file will be updated to demonstrate the new
+ * functionality.
+ *
+ * If you're trying to write something in C using [libgit2][lg], you should
+ * also check out the generated [API documentation][ap]. We try to link to
+ * the relevant sections of the API docs in each section in this file.
+ *
+ * **libgit2** (for the most part) only implements the core plumbing
+ * functions, not really the higher level porcelain stuff. For a primer on
+ * Git Internals that you will need to know to work with Git at this level,
+ * check out [Chapter 10][pg] of the Pro Git book.
+ *
+ * [lg]: http://libgit2.github.com
+ * [ap]: http://libgit2.github.com/libgit2
+ * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
+ */
+
+/**
+ * ### Includes
+ *
+ * Including the `git2.h` header will include all the other libgit2 headers
+ * that you need. It should be the only thing you need to include in order
+ * to compile properly and get all the libgit2 API.
+ */
#include <git2.h>
#include <stdio.h>
-
-// Almost all libgit2 functions return 0 on success or negative on error.
-// This is not production quality error checking, but should be sufficient
-// as an example.
+#include <string.h>
+
+static void oid_parsing(git_oid *out);
+static void object_database(git_repository *repo, git_oid *oid);
+static void commit_writing(git_repository *repo);
+static void commit_parsing(git_repository *repo);
+static void tag_parsing(git_repository *repo);
+static void tree_parsing(git_repository *repo);
+static void blob_parsing(git_repository *repo);
+static void revwalking(git_repository *repo);
+static void index_walking(git_repository *repo);
+static void reference_listing(git_repository *repo);
+static void config_files(const char *repo_path);
+
+/**
+ * Almost all libgit2 functions return 0 on success or negative on error.
+ * This is not production quality error checking, but should be sufficient
+ * as an example.
+ */
static void check_error(int error_code, const char *action)
{
const git_error *error = giterr_last();
@@ -52,479 +71,645 @@ static void check_error(int error_code, const char *action)
return;
printf("Error %d %s - %s\n", error_code, action,
- (error && error->message) ? error->message : "???");
+ (error && error->message) ? error->message : "???");
exit(1);
}
int main (int argc, char** argv)
{
- // Initialize the library, this will set up any global state which libgit2 needs
- // including threading and crypto
- git_libgit2_init();
-
- // ### Opening the Repository
-
- // There are a couple of methods for opening a repository, this being the
- // simplest. There are also [methods][me] for specifying the index file
- // and work tree locations, here we assume they are in the normal places.
- //
- // (Try running this program against tests/resources/testrepo.git.)
- //
- // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
- int error;
- const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
- git_repository *repo;
-
- error = git_repository_open(&repo, repo_path);
- check_error(error, "opening repository");
-
- // ### SHA-1 Value Conversions
-
- // For our first example, we will convert a 40 character hex value to the
- // 20 byte raw SHA1 value.
- printf("*Hex to Raw*\n");
- char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
-
- // The `git_oid` is the structure that keeps the SHA value. We will use
- // this throughout the example for storing the value of the current SHA
- // key we're working with.
- git_oid oid;
- git_oid_fromstr(&oid, hex);
-
- // Once we've converted the string into the oid value, we can get the raw
- // value of the SHA by accessing `oid.id`
-
- // Next we will convert the 20 byte raw SHA1 value to a human readable 40
- // char hex value.
- printf("\n*Raw to Hex*\n");
- char out[GIT_OID_HEXSZ+1];
- out[GIT_OID_HEXSZ] = '\0';
-
- // If you have a oid, you can easily get the hex value of the SHA as well.
- git_oid_fmt(out, &oid);
- printf("SHA hex string: %s\n", out);
-
- // ### Working with the Object Database
-
- // **libgit2** provides [direct access][odb] to the object database. The
- // object database is where the actual objects are stored in Git. For
- // working with raw objects, we'll need to get this structure from the
- // repository.
- //
- // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
- git_odb *odb;
- git_repository_odb(&odb, repo);
-
- // #### Raw Object Reading
-
- printf("\n*Raw Object Read*\n");
- git_odb_object *obj;
- git_otype otype;
- const unsigned char *data;
- const char *str_type;
-
- // We can read raw objects directly from the object database if we have
- // the oid (SHA) of the object. This allows us to access objects without
- // knowing their type and inspect the raw bytes unparsed.
- error = git_odb_read(&obj, odb, &oid);
- check_error(error, "finding object in repository");
-
- // A raw object only has three properties - the type (commit, blob, tree
- // or tag), the size of the raw data and the raw, unparsed data itself.
- // For a commit or tag, that raw data is human readable plain ASCII
- // text. For a blob it is just file contents, so it could be text or
- // binary data. For a tree it is a special binary format, so it's unlikely
- // to be hugely helpful as a raw object.
- data = (const unsigned char *)git_odb_object_data(obj);
- otype = git_odb_object_type(obj);
-
- // We provide methods to convert from the object type which is an enum, to
- // a string representation of that value (and vice-versa).
- str_type = git_object_type2string(otype);
- printf("object length and type: %d, %s\n",
- (int)git_odb_object_size(obj),
- str_type);
-
- // For proper memory management, close the object when you are done with
- // it or it will leak memory.
- git_odb_object_free(obj);
-
- // #### Raw Object Writing
-
- printf("\n*Raw Object Write*\n");
-
- // You can also write raw object data to Git. This is pretty cool because
- // it gives you direct access to the key/value properties of Git. Here
- // we'll write a new blob object that just contains a simple string.
- // Notice that we have to specify the object type as the `git_otype` enum.
- git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
-
- // Now that we've written the object, we can check out what SHA1 was
- // generated when the object was written to our database.
- git_oid_fmt(out, &oid);
- printf("Written Object: %s\n", out);
-
- // ### Object Parsing
-
- // libgit2 has methods to parse every object type in Git so you don't have
- // to work directly with the raw data. This is much faster and simpler
- // than trying to deal with the raw data yourself.
-
- // #### Commit Parsing
-
- // [Parsing commit objects][pco] is simple and gives you access to all the
- // data in the commit - the author (name, email, datetime), committer
- // (same), tree, message, encoding and parent(s).
- //
- // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit
-
- printf("\n*Commit Parsing*\n");
-
- git_commit *commit;
- git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
-
- error = git_commit_lookup(&commit, repo, &oid);
- check_error(error, "looking up commit");
-
- const git_signature *author, *cmtter;
- const char *message;
- time_t ctime;
- unsigned int parents, p;
-
- // Each of the properties of the commit object are accessible via methods,
- // including commonly needed variations, such as `git_commit_time` which
- // returns the author time and `git_commit_message` which gives you the
- // commit message (as a NUL-terminated string).
- message = git_commit_message(commit);
- author = git_commit_author(commit);
- cmtter = git_commit_committer(commit);
- ctime = git_commit_time(commit);
-
- // The author and committer methods return [git_signature] structures,
- // which give you name, email and `when`, which is a `git_time` structure,
- // giving you a timestamp and timezone offset.
- printf("Author: %s (%s)\n", author->name, author->email);
-
- // Commits can have zero or more parents. The first (root) commit will
- // have no parents, most commits will have one (i.e. the commit it was
- // based on) and merge commits will have two or more. Commits can
- // technically have any number, though it's rare to have more than two.
- parents = git_commit_parentcount(commit);
- for (p = 0;p < parents;p++) {
- git_commit *parent;
- git_commit_parent(&parent, commit, p);
- git_oid_fmt(out, git_commit_id(parent));
- printf("Parent: %s\n", out);
- git_commit_free(parent);
- }
-
- // Don't forget to close the object to prevent memory leaks. You will have
- // to do this for all the objects you open and parse.
- git_commit_free(commit);
-
- // #### Writing Commits
-
- // libgit2 provides a couple of methods to create commit objects easily as
- // well. There are four different create signatures, we'll just show one
- // of them here. You can read about the other ones in the [commit API
- // docs][cd].
- //
- // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
-
- printf("\n*Commit Writing*\n");
- git_oid tree_id, parent_id, commit_id;
- git_tree *tree;
- git_commit *parent;
-
- // Creating signatures for an authoring identity and time is simple. You
- // will need to do this to specify who created a commit and when. Default
- // values for the name and email should be found in the `user.name` and
- // `user.email` configuration options. See the `config` section of this
- // example file to see how to access config values.
- git_signature_new((git_signature **)&author,
- "Scott Chacon", "schacon@gmail.com", 123456789, 60);
- git_signature_new((git_signature **)&cmtter,
- "Scott A Chacon", "scott@github.com", 987654321, 90);
-
- // Commit objects need a tree to point to and optionally one or more
- // parents. Here we're creating oid objects to create the commit with,
- // but you can also use
- git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
- git_tree_lookup(&tree, repo, &tree_id);
- git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
- git_commit_lookup(&parent, repo, &parent_id);
-
- // Here we actually create the commit object with a single call with all
- // the values we need to create the commit. The SHA key is written to the
- // `commit_id` variable here.
- git_commit_create_v(
- &commit_id, /* out id */
- repo,
- NULL, /* do not update the HEAD */
- author,
- cmtter,
- NULL, /* use default message encoding */
- "example commit",
- tree,
- 1, parent);
-
- // Now we can take a look at the commit SHA we've generated.
- git_oid_fmt(out, &commit_id);
- printf("New Commit: %s\n", out);
-
- // #### Tag Parsing
-
- // You can parse and create tags with the [tag management API][tm], which
- // functions very similarly to the commit lookup, parsing and creation
- // methods, since the objects themselves are very similar.
- //
- // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag
- printf("\n*Tag Parsing*\n");
- git_tag *tag;
- const char *tmessage, *tname;
- git_otype ttype;
-
- // We create an oid for the tag object if we know the SHA and look it up
- // the same way that we would a commit (or any other object).
- git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
-
- error = git_tag_lookup(&tag, repo, &oid);
- check_error(error, "looking up tag");
-
- // Now that we have the tag object, we can extract the information it
- // generally contains: the target (usually a commit object), the type of
- // the target object (usually 'commit'), the name ('v1.0'), the tagger (a
- // git_signature - name, email, timestamp), and the tag message.
- git_tag_target((git_object **)&commit, tag);
- tname = git_tag_name(tag); // "test"
- ttype = git_tag_target_type(tag); // GIT_OBJ_COMMIT (otype enum)
- tmessage = git_tag_message(tag); // "tag message\n"
- printf("Tag Message: %s\n", tmessage);
-
- git_commit_free(commit);
-
- // #### Tree Parsing
-
- // [Tree parsing][tp] is a bit different than the other objects, in that
- // we have a subtype which is the tree entry. This is not an actual
- // object type in Git, but a useful structure for parsing and traversing
- // tree entries.
- //
- // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree
- printf("\n*Tree Parsing*\n");
-
- const git_tree_entry *entry;
- git_object *objt;
-
- // Create the oid and lookup the tree object just like the other objects.
- git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5");
- git_tree_lookup(&tree, repo, &oid);
-
- // Getting the count of entries in the tree so you can iterate over them
- // if you want to.
- size_t cnt = git_tree_entrycount(tree); // 3
- printf("tree entries: %d\n", (int)cnt);
-
- entry = git_tree_entry_byindex(tree, 0);
- printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c"
-
- // You can also access tree entries by name if you know the name of the
- // entry you're looking for.
- entry = git_tree_entry_byname(tree, "README");
- git_tree_entry_name(entry); // "hello.c"
-
- // Once you have the entry object, you can access the content or subtree
- // (or commit, in the case of submodules) that it points to. You can also
- // get the mode if you want.
- git_tree_entry_to_object(&objt, repo, entry); // blob
-
- // Remember to close the looked-up object once you are done using it
- git_object_free(objt);
-
- // #### Blob Parsing
-
- // The last object type is the simplest and requires the least parsing
- // help. Blobs are just file contents and can contain anything, there is
- // no structure to it. The main advantage to using the [simple blob
- // api][ba] is that when you're creating blobs you don't have to calculate
- // the size of the content. There is also a helper for reading a file
- // from disk and writing it to the db and getting the oid back so you
- // don't have to do all those steps yourself.
- //
- // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob
-
- printf("\n*Blob Parsing*\n");
- git_blob *blob;
-
- git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
- git_blob_lookup(&blob, repo, &oid);
-
- // You can access a buffer with the raw contents of the blob directly.
- // Note that this buffer may not be contain ASCII data for certain blobs
- // (e.g. binary files): do not consider the buffer a NULL-terminated
- // string, and use the `git_blob_rawsize` attribute to find out its exact
- // size in bytes
- printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8
- git_blob_rawcontent(blob); // "content"
-
- // ### Revwalking
-
- // The libgit2 [revision walking api][rw] provides methods to traverse the
- // directed graph created by the parent pointers of the commit objects.
- // Since all commits point back to the commit that came directly before
- // them, you can walk this parentage as a graph and find all the commits
- // that were ancestors of (reachable from) a given starting point. This
- // can allow you to create `git log` type functionality.
- //
- // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk
-
- printf("\n*Revwalking*\n");
- git_revwalk *walk;
- git_commit *wcommit;
-
- git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
-
- // To use the revwalker, create a new walker, tell it how you want to sort
- // the output and then push one or more starting points onto the walker.
- // If you want to emulate the output of `git log` you would push the SHA
- // of the commit that HEAD points to into the walker and then start
- // traversing them. You can also 'hide' commits that you want to stop at
- // or not see any of their ancestors. So if you want to emulate `git log
- // branch1..branch2`, you would push the oid of `branch2` and hide the oid
- // of `branch1`.
- git_revwalk_new(&walk, repo);
- git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
- git_revwalk_push(walk, &oid);
-
- const git_signature *cauth;
- const char *cmsg;
-
- // Now that we have the starting point pushed onto the walker, we start
- // asking for ancestors. It will return them in the sorting order we asked
- // for as commit oids. We can then lookup and parse the committed pointed
- // at by the returned OID; note that this operation is specially fast
- // since the raw contents of the commit object will be cached in memory
- while ((git_revwalk_next(&oid, walk)) == 0) {
- error = git_commit_lookup(&wcommit, repo, &oid);
- check_error(error, "looking up commit during revwalk");
-
- cmsg = git_commit_message(wcommit);
- cauth = git_commit_author(wcommit);
- printf("%s (%s)\n", cmsg, cauth->email);
-
- git_commit_free(wcommit);
- }
-
- // Like the other objects, be sure to free the revwalker when you're done
- // to prevent memory leaks. Also, make sure that the repository being
- // walked it not deallocated while the walk is in progress, or it will
- // result in undefined behavior
- git_revwalk_free(walk);
-
- // ### Index File Manipulation
-
- // The [index file API][gi] allows you to read, traverse, update and write
- // the Git index file (sometimes thought of as the staging area).
- //
- // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index
-
- printf("\n*Index Walking*\n");
-
- git_index *index;
- unsigned int i, ecount;
-
- // You can either open the index from the standard location in an open
- // repository, as we're doing here, or you can open and manipulate any
- // index file with `git_index_open_bare()`. The index for the repository
- // will be located and loaded from disk.
- git_repository_index(&index, repo);
-
- // For each entry in the index, you can get a bunch of information
- // including the SHA (oid), path and mode which map to the tree objects
- // that are written out. It also has filesystem properties to help
- // determine what to inspect for changes (ctime, mtime, dev, ino, uid,
- // gid, file_size and flags) All these properties are exported publicly in
- // the `git_index_entry` struct
- ecount = git_index_entrycount(index);
- for (i = 0; i < ecount; ++i) {
- const git_index_entry *e = git_index_get_byindex(index, i);
-
- printf("path: %s\n", e->path);
- printf("mtime: %d\n", (int)e->mtime.seconds);
- printf("fs: %d\n", (int)e->file_size);
- }
-
- git_index_free(index);
-
- // ### References
-
- // The [reference API][ref] allows you to list, resolve, create and update
- // references such as branches, tags and remote references (everything in
- // the .git/refs directory).
- //
- // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
-
- printf("\n*Reference Listing*\n");
-
- // Here we will implement something like `git for-each-ref` simply listing
- // out all available references and the object SHA they resolve to.
- git_strarray ref_list;
- git_reference_list(&ref_list, repo);
-
- const char *refname;
- git_reference *ref;
-
- // Now that we have the list of reference names, we can lookup each ref
- // one at a time and resolve them to the SHA, then print both values out.
- for (i = 0; i < ref_list.count; ++i) {
- refname = ref_list.strings[i];
- git_reference_lookup(&ref, repo, refname);
-
- switch (git_reference_type(ref)) {
- case GIT_REF_OID:
- git_oid_fmt(out, git_reference_target(ref));
- printf("%s [%s]\n", refname, out);
- break;
-
- case GIT_REF_SYMBOLIC:
- printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
- break;
- default:
- fprintf(stderr, "Unexpected reference type\n");
- exit(1);
- }
- }
-
- git_strarray_free(&ref_list);
-
- // ### Config Files
-
- // The [config API][config] allows you to list and updatee config values
- // in any of the accessible config file locations (system, global, local).
- //
- // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config
+ int error;
+ git_oid oid;
+ char *repo_path;
+ git_repository *repo;
+
+ /**
+ * Initialize the library, this will set up any global state which libgit2 needs
+ * including threading and crypto
+ */
+ git_libgit2_init();
+
+ /**
+ * ### Opening the Repository
+ *
+ * There are a couple of methods for opening a repository, this being the
+ * simplest. There are also [methods][me] for specifying the index file
+ * and work tree locations, here we assume they are in the normal places.
+ *
+ * (Try running this program against tests/resources/testrepo.git.)
+ *
+ * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
+ */
+ repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
+
+ error = git_repository_open(&repo, repo_path);
+ check_error(error, "opening repository");
+
+ oid_parsing(&oid);
+ object_database(repo, &oid);
+ commit_writing(repo);
+ commit_parsing(repo);
+ tag_parsing(repo);
+ tree_parsing(repo);
+ blob_parsing(repo);
+ revwalking(repo);
+ index_walking(repo);
+ reference_listing(repo);
+ config_files(repo_path);
+
+ /**
+ * Finally, when you're done with the repository, you can free it as well.
+ */
+ git_repository_free(repo);
+
+ return 0;
+}
+
+/**
+ * ### SHA-1 Value Conversions
+ */
+static void oid_parsing(git_oid *oid)
+{
+ char out[GIT_OID_HEXSZ+1];
+ char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
+
+ printf("*Hex to Raw*\n");
+
+ /**
+ * For our first example, we will convert a 40 character hex value to the
+ * 20 byte raw SHA1 value.
+ *
+ * The `git_oid` is the structure that keeps the SHA value. We will use
+ * this throughout the example for storing the value of the current SHA
+ * key we're working with.
+ */
+ git_oid_fromstr(oid, hex);
+
+ // Once we've converted the string into the oid value, we can get the raw
+ // value of the SHA by accessing `oid.id`
+
+ // Next we will convert the 20 byte raw SHA1 value to a human readable 40
+ // char hex value.
+ printf("\n*Raw to Hex*\n");
+ out[GIT_OID_HEXSZ] = '\0';
+
+ /**
+ * If you have a oid, you can easily get the hex value of the SHA as well.
+ */
+ git_oid_fmt(out, oid);
+
+ /**
+ * If you have a oid, you can easily get the hex value of the SHA as well.
+ */
+ git_oid_fmt(out, oid);
+ printf("SHA hex string: %s\n", out);
+}
+
+/**
+ * ### Working with the Object Database
+ *
+ * **libgit2** provides [direct access][odb] to the object database. The
+ * object database is where the actual objects are stored in Git. For
+ * working with raw objects, we'll need to get this structure from the
+ * repository.
+ *
+ * [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
+ */
+static void object_database(git_repository *repo, git_oid *oid)
+{
+ char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
+ const unsigned char *data;
+ const char *str_type;
+ int error;
+ git_odb_object *obj;
+ git_odb *odb;
+ git_otype otype;
+
+ git_repository_odb(&odb, repo);
+
+ /**
+ * #### Raw Object Reading
+ */
+
+ printf("\n*Raw Object Read*\n");
+
+ /**
+ * We can read raw objects directly from the object database if we have
+ * the oid (SHA) of the object. This allows us to access objects without
+ * knowing their type and inspect the raw bytes unparsed.
+ */
+ error = git_odb_read(&obj, odb, oid);
+ check_error(error, "finding object in repository");
+
+ /**
+ * A raw object only has three properties - the type (commit, blob, tree
+ * or tag), the size of the raw data and the raw, unparsed data itself.
+ * For a commit or tag, that raw data is human readable plain ASCII
+ * text. For a blob it is just file contents, so it could be text or
+ * binary data. For a tree it is a special binary format, so it's unlikely
+ * to be hugely helpful as a raw object.
+ */
+ data = (const unsigned char *)git_odb_object_data(obj);
+ otype = git_odb_object_type(obj);
+
+ /**
+ * We provide methods to convert from the object type which is an enum, to
+ * a string representation of that value (and vice-versa).
+ */
+ str_type = git_object_type2string(otype);
+ printf("object length and type: %d, %s\nobject data: %s\n",
+ (int)git_odb_object_size(obj),
+ str_type, data);
+
+ /**
+ * For proper memory management, close the object when you are done with
+ * it or it will leak memory.
+ */
+ git_odb_object_free(obj);
+
+ /**
+ * #### Raw Object Writing
+ */
+
+ printf("\n*Raw Object Write*\n");
+
+ /**
+ * You can also write raw object data to Git. This is pretty cool because
+ * it gives you direct access to the key/value properties of Git. Here
+ * we'll write a new blob object that just contains a simple string.
+ * Notice that we have to specify the object type as the `git_otype` enum.
+ */
+ git_odb_write(oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
+
+ /**
+ * Now that we've written the object, we can check out what SHA1 was
+ * generated when the object was written to our database.
+ */
+ git_oid_fmt(oid_hex, oid);
+ printf("Written Object: %s\n", oid_hex);
+}
+
+/**
+ * #### Writing Commits
+ *
+ * libgit2 provides a couple of methods to create commit objects easily as
+ * well. There are four different create signatures, we'll just show one
+ * of them here. You can read about the other ones in the [commit API
+ * docs][cd].
+ *
+ * [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
+ */
+static void commit_writing(git_repository *repo)
+{
+ git_oid tree_id, parent_id, commit_id;
+ git_tree *tree;
+ git_commit *parent;
+ const git_signature *author, *cmtter;
+ char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
+
+ printf("\n*Commit Writing*\n");
+
+ /**
+ * Creating signatures for an authoring identity and time is simple. You
+ * will need to do this to specify who created a commit and when. Default
+ * values for the name and email should be found in the `user.name` and
+ * `user.email` configuration options. See the `config` section of this
+ * example file to see how to access config values.
+ */
+ git_signature_new((git_signature **)&author,
+ "Scott Chacon", "schacon@gmail.com", 123456789, 60);
+ git_signature_new((git_signature **)&cmtter,
+ "Scott A Chacon", "scott@github.com", 987654321, 90);
+
+ /**
+ * Commit objects need a tree to point to and optionally one or more
+ * parents. Here we're creating oid objects to create the commit with,
+ * but you can also use
+ */
+ git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
+ git_tree_lookup(&tree, repo, &tree_id);
+ git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
+ git_commit_lookup(&parent, repo, &parent_id);
+
+ /**
+ * Here we actually create the commit object with a single call with all
+ * the values we need to create the commit. The SHA key is written to the
+ * `commit_id` variable here.
+ */
+ git_commit_create_v(
+ &commit_id, /* out id */
+ repo,
+ NULL, /* do not update the HEAD */
+ author,
+ cmtter,
+ NULL, /* use default message encoding */
+ "example commit",
+ tree,
+ 1, parent);
+
+ /**
+ * Now we can take a look at the commit SHA we've generated.
+ */
+ git_oid_fmt(oid_hex, &commit_id);
+ printf("New Commit: %s\n", oid_hex);
+}
+
+/**
+ * ### Object Parsing
+ *
+ * libgit2 has methods to parse every object type in Git so you don't have
+ * to work directly with the raw data. This is much faster and simpler
+ * than trying to deal with the raw data yourself.
+ */
+
+/**
+ * #### Commit Parsing
+ *
+ * [Parsing commit objects][pco] is simple and gives you access to all the
+ * data in the commit - the author (name, email, datetime), committer
+ * (same), tree, message, encoding and parent(s).
+ *
+ * [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit
+ */
+static void commit_parsing(git_repository *repo)
+{
+ const git_signature *author, *cmtter;
+ git_commit *commit, *parent;
+ git_oid oid;
+ char oid_hex[GIT_OID_HEXSZ+1];
+ const char *message;
+ unsigned int parents, p;
+ int error;
+ time_t time;
+
+ printf("\n*Commit Parsing*\n");
+
+ git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
+
+ error = git_commit_lookup(&commit, repo, &oid);
+ check_error(error, "looking up commit");
+
+ /**
+ * Each of the properties of the commit object are accessible via methods,
+ * including commonly needed variations, such as `git_commit_time` which
+ * returns the author time and `git_commit_message` which gives you the
+ * commit message (as a NUL-terminated string).
+ */
+ message = git_commit_message(commit);
+ author = git_commit_author(commit);
+ cmtter = git_commit_committer(commit);
+ time = git_commit_time(commit);
+
+ /**
+ * The author and committer methods return [git_signature] structures,
+ * which give you name, email and `when`, which is a `git_time` structure,
+ * giving you a timestamp and timezone offset.
+ */
+ printf("Author: %s (%s)\nCommitter: %s (%s)\nDate: %s\nMessage: %s\n",
+ author->name, author->email,
+ cmtter->name, cmtter->email,
+ ctime(&time), message);
+
+ /**
+ * Commits can have zero or more parents. The first (root) commit will
+ * have no parents, most commits will have one (i.e. the commit it was
+ * based on) and merge commits will have two or more. Commits can
+ * technically have any number, though it's rare to have more than two.
+ */
+ parents = git_commit_parentcount(commit);
+ for (p = 0;p < parents;p++) {
+ memset(oid_hex, 0, sizeof(oid_hex));
+
+ git_commit_parent(&parent, commit, p);
+ git_oid_fmt(oid_hex, git_commit_id(parent));
+ printf("Parent: %s\n", oid_hex);
+ git_commit_free(parent);
+ }
+
+ git_commit_free(commit);
+}
+
+/**
+ * #### Tag Parsing
+ *
+ * You can parse and create tags with the [tag management API][tm], which
+ * functions very similarly to the commit lookup, parsing and creation
+ * methods, since the objects themselves are very similar.
+ *
+ * [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag
+ */
+static void tag_parsing(git_repository *repo)
+{
+ git_commit *commit;
+ git_otype type;
+ git_tag *tag;
+ git_oid oid;
+ const char *name, *message;
+ int error;
+
+ printf("\n*Tag Parsing*\n");
+
+ /**
+ * We create an oid for the tag object if we know the SHA and look it up
+ * the same way that we would a commit (or any other object).
+ */
+ git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
+
+ error = git_tag_lookup(&tag, repo, &oid);
+ check_error(error, "looking up tag");
+
+ /**
+ * Now that we have the tag object, we can extract the information it
+ * generally contains: the target (usually a commit object), the type of
+ * the target object (usually 'commit'), the name ('v1.0'), the tagger (a
+ * git_signature - name, email, timestamp), and the tag message.
+ */
+ git_tag_target((git_object **)&commit, tag);
+ name = git_tag_name(tag); /* "test" */
+ type = git_tag_target_type(tag); /* GIT_OBJ_COMMIT (otype enum) */
+ message = git_tag_message(tag); /* "tag message\n" */
+ printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n",
+ name, git_object_type2string(type), message);
+
+ git_commit_free(commit);
+}
+
+/**
+ * #### Tree Parsing
+ *
+ * [Tree parsing][tp] is a bit different than the other objects, in that
+ * we have a subtype which is the tree entry. This is not an actual
+ * object type in Git, but a useful structure for parsing and traversing
+ * tree entries.
+ *
+ * [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree
+ */
+static void tree_parsing(git_repository *repo)
+{
+ const git_tree_entry *entry;
+ size_t cnt;
+ git_object *obj;
+ git_tree *tree;
+ git_oid oid;
+
+ printf("\n*Tree Parsing*\n");
+
+ /**
+ * Create the oid and lookup the tree object just like the other objects.
+ */
+ git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
+ git_tree_lookup(&tree, repo, &oid);
+
+ /**
+ * Getting the count of entries in the tree so you can iterate over them
+ * if you want to.
+ */
+ cnt = git_tree_entrycount(tree); /* 2 */
+ printf("tree entries: %d\n", (int) cnt);
+
+ entry = git_tree_entry_byindex(tree, 0);
+ printf("Entry name: %s\n", git_tree_entry_name(entry)); /* "README" */
+
+ /**
+ * You can also access tree entries by name if you know the name of the
+ * entry you're looking for.
+ */
+ entry = git_tree_entry_byname(tree, "README");
+ git_tree_entry_name(entry); /* "README" */
+
+ /**
+ * Once you have the entry object, you can access the content or subtree
+ * (or commit, in the case of submodules) that it points to. You can also
+ * get the mode if you want.
+ */
+ git_tree_entry_to_object(&obj, repo, entry); /* blob */
+
+ /**
+ * Remember to close the looked-up object once you are done using it
+ */
+ git_object_free(obj);
+}
+
+/**
+ * #### Blob Parsing
+ *
+ * The last object type is the simplest and requires the least parsing
+ * help. Blobs are just file contents and can contain anything, there is
+ * no structure to it. The main advantage to using the [simple blob
+ * api][ba] is that when you're creating blobs you don't have to calculate
+ * the size of the content. There is also a helper for reading a file
+ * from disk and writing it to the db and getting the oid back so you
+ * don't have to do all those steps yourself.
+ *
+ * [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob
+ */
+static void blob_parsing(git_repository *repo)
+{
+ git_blob *blob;
+ git_oid oid;
+
+ printf("\n*Blob Parsing*\n");
+
+ git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
+ git_blob_lookup(&blob, repo, &oid);
+
+ /**
+ * You can access a buffer with the raw contents of the blob directly.
+ * Note that this buffer may not be contain ASCII data for certain blobs
+ * (e.g. binary files): do not consider the buffer a NULL-terminated
+ * string, and use the `git_blob_rawsize` attribute to find out its exact
+ * size in bytes
+ * */
+ printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */
+ git_blob_rawcontent(blob); /* "content" */
+}
- printf("\n*Config Listing*\n");
+/**
+ * ### Revwalking
+ *
+ * The libgit2 [revision walking api][rw] provides methods to traverse the
+ * directed graph created by the parent pointers of the commit objects.
+ * Since all commits point back to the commit that came directly before
+ * them, you can walk this parentage as a graph and find all the commits
+ * that were ancestors of (reachable from) a given starting point. This
+ * can allow you to create `git log` type functionality.
+ *
+ * [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk
+ */
+static void revwalking(git_repository *repo)
+{
+ const git_signature *cauth;
+ const char *cmsg;
+ int error;
+ git_revwalk *walk;
+ git_commit *wcommit;
+ git_oid oid;
+
+ printf("\n*Revwalking*\n");
+
+ git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
+
+ /**
+ * To use the revwalker, create a new walker, tell it how you want to sort
+ * the output and then push one or more starting points onto the walker.
+ * If you want to emulate the output of `git log` you would push the SHA
+ * of the commit that HEAD points to into the walker and then start
+ * traversing them. You can also 'hide' commits that you want to stop at
+ * or not see any of their ancestors. So if you want to emulate `git log
+ * branch1..branch2`, you would push the oid of `branch2` and hide the oid
+ * of `branch1`.
+ */
+ git_revwalk_new(&walk, repo);
+ git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
+ git_revwalk_push(walk, &oid);
+
+ /**
+ * Now that we have the starting point pushed onto the walker, we start
+ * asking for ancestors. It will return them in the sorting order we asked
+ * for as commit oids. We can then lookup and parse the committed pointed
+ * at by the returned OID; note that this operation is specially fast
+ * since the raw contents of the commit object will be cached in memory
+ */
+ while ((git_revwalk_next(&oid, walk)) == 0) {
+ error = git_commit_lookup(&wcommit, repo, &oid);
+ check_error(error, "looking up commit during revwalk");
+
+ cmsg = git_commit_message(wcommit);
+ cauth = git_commit_author(wcommit);
+ printf("%s (%s)\n", cmsg, cauth->email);
+
+ git_commit_free(wcommit);
+ }
+
+ /**
+ * Like the other objects, be sure to free the revwalker when you're done
+ * to prevent memory leaks. Also, make sure that the repository being
+ * walked it not deallocated while the walk is in progress, or it will
+ * result in undefined behavior
+ */
+ git_revwalk_free(walk);
+}
- const char *email;
- int32_t j;
+/**
+ * ### Index File Manipulation *
+ * The [index file API][gi] allows you to read, traverse, update and write
+ * the Git index file (sometimes thought of as the staging area).
+ *
+ * [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index
+ */
+static void index_walking(git_repository *repo)
+{
+ git_index *index;
+ unsigned int i, ecount;
+
+ printf("\n*Index Walking*\n");
+
+ /**
+ * You can either open the index from the standard location in an open
+ * repository, as we're doing here, or you can open and manipulate any
+ * index file with `git_index_open_bare()`. The index for the repository
+ * will be located and loaded from disk.
+ */
+ git_repository_index(&index, repo);
+
+ /**
+ * For each entry in the index, you can get a bunch of information
+ * including the SHA (oid), path and mode which map to the tree objects
+ * that are written out. It also has filesystem properties to help
+ * determine what to inspect for changes (ctime, mtime, dev, ino, uid,
+ * gid, file_size and flags) All these properties are exported publicly in
+ * the `git_index_entry` struct
+ */
+ ecount = git_index_entrycount(index);
+ for (i = 0; i < ecount; ++i) {
+ const git_index_entry *e = git_index_get_byindex(index, i);
+
+ printf("path: %s\n", e->path);
+ printf("mtime: %d\n", (int)e->mtime.seconds);
+ printf("fs: %d\n", (int)e->file_size);
+ }
+
+ git_index_free(index);
+}
- git_config *cfg;
+/**
+ * ### References
+ *
+ * The [reference API][ref] allows you to list, resolve, create and update
+ * references such as branches, tags and remote references (everything in
+ * the .git/refs directory).
+ *
+ * [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
+ */
+static void reference_listing(git_repository *repo)
+{
+ git_strarray ref_list;
+ const char *refname;
+ git_reference *ref;
+ unsigned i;
+ char oid_hex[GIT_OID_HEXSZ+1];
+
+ printf("\n*Reference Listing*\n");
+
+ /**
+ * Here we will implement something like `git for-each-ref` simply listing
+ * out all available references and the object SHA they resolve to.
+ *
+ * Now that we have the list of reference names, we can lookup each ref
+ * one at a time and resolve them to the SHA, then print both values out.
+ */
+
+ git_reference_list(&ref_list, repo);
+
+ for (i = 0; i < ref_list.count; ++i) {
+ memset(oid_hex, 0, sizeof(oid_hex));
+ refname = ref_list.strings[i];
+ git_reference_lookup(&ref, repo, refname);
+
+ switch (git_reference_type(ref)) {
+ case GIT_REF_OID:
+ git_oid_fmt(oid_hex, git_reference_target(ref));
+ printf("%s [%s]\n", refname, oid_hex);
+ break;
+
+ case GIT_REF_SYMBOLIC:
+ printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
+ break;
+ default:
+ fprintf(stderr, "Unexpected reference type\n");
+ exit(1);
+ }
+ }
+
+ git_strarray_free(&ref_list);
+}
- // Open a config object so we can read global values from it.
- char config_path[256];
- sprintf(config_path, "%s/config", repo_path);
- check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
+/**
+ * ### Config Files
+ *
+ * The [config API][config] allows you to list and updatee config values
+ * in any of the accessible config file locations (system, global, local).
+ *
+ * [config]: http://libgit2.github.com/libgit2/#HEAD/group/config
+ */
+static void config_files(const char *repo_path)
+{
+ const char *email;
+ char config_path[256];
+ int32_t j;
+ git_config *cfg;
- git_config_get_int32(&j, cfg, "help.autocorrect");
- printf("Autocorrect: %d\n", j);
+ printf("\n*Config Listing*\n");
- git_config_get_string(&email, cfg, "user.email");
- printf("Email: %s\n", email);
+ /**
+ * Open a config object so we can read global values from it.
+ */
+ sprintf(config_path, "%s/config", repo_path);
+ check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
- // Finally, when you're done with the repository, you can free it as well.
- git_repository_free(repo);
+ git_config_get_int32(&j, cfg, "help.autocorrect");
+ printf("Autocorrect: %d\n", j);
- return 0;
+ git_config_get_string(&email, cfg, "user.email");
+ printf("Email: %s\n", email);
}
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 177359b..10974a9 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -55,6 +55,8 @@ static int update_cb(const char *refname, const git_oid *a, const git_oid *b, vo
*/
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);
@@ -71,7 +73,6 @@ int fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
const git_transfer_progress *stats;
- struct dl_data data;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
if (argc < 2) {
@@ -79,14 +80,13 @@ int fetch(git_repository *repo, int argc, char **argv)
return EXIT_FAILURE;
}
- // Figure out whether it's a named remote or a URL
+ /* 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_lookup(&remote, repo, argv[1]) < 0)
if (git_remote_create_anonymous(&remote, repo, argv[1]) < 0)
- return -1;
- }
+ goto on_error;
- // Set up the callbacks (only update_tips for now)
+ /* 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;
@@ -98,7 +98,7 @@ int fetch(git_repository *repo, int argc, char **argv)
* "fetch".
*/
if (git_remote_fetch(remote, NULL, &fetch_opts, "fetch") < 0)
- return -1;
+ goto on_error;
/**
* If there are local objects (we got a thin pack), then tell
diff --git a/script/cibuild.sh b/script/cibuild.sh
index b13ad88..403df22 100755
--- a/script/cibuild.sh
+++ b/script/cibuild.sh
@@ -20,7 +20,7 @@ java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar &
mkdir _build
cd _build
# shellcheck disable=SC2086
-cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $?
+cmake .. -DBUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $?
make -j2 install || exit $?
# If this platform doesn't support test execution, bail out now