]> git.proxmox.com Git - libgit2.git/blame - examples/general.c
Fix autopkgtest regression with CMake 3.19+
[libgit2.git] / examples / general.c
CommitLineData
6cb831bd
BS
1/*
2 * libgit2 "general" example - shows basic libgit2 concepts
3 *
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14
662eee15
PS
15/**
16 * [**libgit2**][lg] is a portable, pure C implementation of the Git core
17 * methods provided as a re-entrant linkable library with a solid API,
18 * allowing you to write native speed custom Git applications in any
19 * language which supports C bindings.
20 *
21 * This file is an example of using that API in a real, compilable C file.
22 * As the API is updated, this file will be updated to demonstrate the new
23 * functionality.
24 *
25 * If you're trying to write something in C using [libgit2][lg], you should
26 * also check out the generated [API documentation][ap]. We try to link to
27 * the relevant sections of the API docs in each section in this file.
28 *
29 * **libgit2** (for the most part) only implements the core plumbing
30 * functions, not really the higher level porcelain stuff. For a primer on
31 * Git Internals that you will need to know to work with Git at this level,
32 * check out [Chapter 10][pg] of the Pro Git book.
33 *
34 * [lg]: http://libgit2.github.com
35 * [ap]: http://libgit2.github.com/libgit2
36 * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
37 */
38
22a2d3d5
UG
39#include "common.h"
40
662eee15
PS
41/**
42 * ### Includes
43 *
44 * Including the `git2.h` header will include all the other libgit2 headers
45 * that you need. It should be the only thing you need to include in order
46 * to compile properly and get all the libgit2 API.
47 */
22a2d3d5 48#include "git2.h"
388f37b3 49
c313e3d9 50static void oid_parsing(git_oid *out);
29d9afc0 51static void object_database(git_repository *repo, git_oid *oid);
b009adad 52static void commit_writing(git_repository *repo);
15960454
PS
53static void commit_parsing(git_repository *repo);
54static void tag_parsing(git_repository *repo);
55static void tree_parsing(git_repository *repo);
56static void blob_parsing(git_repository *repo);
8b93ccdf 57static void revwalking(git_repository *repo);
c079e3c8 58static void index_walking(git_repository *repo);
f9a7973d 59static void reference_listing(git_repository *repo);
832278bf 60static void config_files(const char *repo_path, git_repository *repo);
986913f4 61
662eee15
PS
62/**
63 * Almost all libgit2 functions return 0 on success or negative on error.
64 * This is not production quality error checking, but should be sufficient
65 * as an example.
66 */
a7ed7460
RB
67static void check_error(int error_code, const char *action)
68{
ac3d33df 69 const git_error *error = git_error_last();
a7ed7460
RB
70 if (!error_code)
71 return;
72
a7ed7460 73 printf("Error %d %s - %s\n", error_code, action,
176d58ba 74 (error && error->message) ? error->message : "???");
a7ed7460
RB
75
76 exit(1);
77}
78
22a2d3d5 79int lg2_general(git_repository *repo, int argc, char** argv)
388f37b3 80{
e2d1b7ec 81 int error;
c313e3d9 82 git_oid oid;
e2d1b7ec 83 char *repo_path;
c313e3d9 84
662eee15
PS
85 /**
86 * Initialize the library, this will set up any global state which libgit2 needs
87 * including threading and crypto
88 */
176d58ba 89 git_libgit2_init();
799e22ea 90
662eee15
PS
91 /**
92 * ### Opening the Repository
93 *
94 * There are a couple of methods for opening a repository, this being the
95 * simplest. There are also [methods][me] for specifying the index file
96 * and work tree locations, here we assume they are in the normal places.
97 *
98 * (Try running this program against tests/resources/testrepo.git.)
99 *
100 * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
101 */
e2d1b7ec 102 repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
176d58ba
PS
103
104 error = git_repository_open(&repo, repo_path);
105 check_error(error, "opening repository");
106
c313e3d9 107 oid_parsing(&oid);
29d9afc0
PS
108 object_database(repo, &oid);
109 commit_writing(repo);
110 commit_parsing(repo);
111 tag_parsing(repo);
112 tree_parsing(repo);
113 blob_parsing(repo);
114 revwalking(repo);
115 index_walking(repo);
116 reference_listing(repo);
832278bf 117 config_files(repo_path, repo);
176d58ba 118
662eee15
PS
119 /**
120 * Finally, when you're done with the repository, you can free it as well.
121 */
29d9afc0
PS
122 git_repository_free(repo);
123
124 return 0;
125}
126
c313e3d9
PS
127/**
128 * ### SHA-1 Value Conversions
129 */
130static void oid_parsing(git_oid *oid)
131{
132 char out[GIT_OID_HEXSZ+1];
133 char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
134
135 printf("*Hex to Raw*\n");
136
137 /**
138 * For our first example, we will convert a 40 character hex value to the
139 * 20 byte raw SHA1 value.
140 *
141 * The `git_oid` is the structure that keeps the SHA value. We will use
142 * this throughout the example for storing the value of the current SHA
143 * key we're working with.
144 */
145 git_oid_fromstr(oid, hex);
146
ac3d33df
JK
147 /*
148 * Once we've converted the string into the oid value, we can get the raw
149 * value of the SHA by accessing `oid.id`
150 *
151 * Next we will convert the 20 byte raw SHA1 value to a human readable 40
152 * char hex value.
153 */
c313e3d9
PS
154 printf("\n*Raw to Hex*\n");
155 out[GIT_OID_HEXSZ] = '\0';
156
157 /**
158 * If you have a oid, you can easily get the hex value of the SHA as well.
159 */
160 git_oid_fmt(out, oid);
161
162 /**
163 * If you have a oid, you can easily get the hex value of the SHA as well.
164 */
165 git_oid_fmt(out, oid);
166 printf("SHA hex string: %s\n", out);
167}
168
29d9afc0
PS
169/**
170 * ### Working with the Object Database
171 *
172 * **libgit2** provides [direct access][odb] to the object database. The
173 * object database is where the actual objects are stored in Git. For
174 * working with raw objects, we'll need to get this structure from the
175 * repository.
176 *
177 * [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
178 */
179static void object_database(git_repository *repo, git_oid *oid)
180{
181 char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
182 const unsigned char *data;
183 const char *str_type;
184 int error;
185 git_odb_object *obj;
176d58ba 186 git_odb *odb;
ac3d33df 187 git_object_t otype;
29d9afc0 188
176d58ba
PS
189 git_repository_odb(&odb, repo);
190
29d9afc0
PS
191 /**
192 * #### Raw Object Reading
193 */
176d58ba
PS
194
195 printf("\n*Raw Object Read*\n");
176d58ba 196
29d9afc0
PS
197 /**
198 * We can read raw objects directly from the object database if we have
199 * the oid (SHA) of the object. This allows us to access objects without
200 * knowing their type and inspect the raw bytes unparsed.
201 */
202 error = git_odb_read(&obj, odb, oid);
176d58ba
PS
203 check_error(error, "finding object in repository");
204
29d9afc0
PS
205 /**
206 * A raw object only has three properties - the type (commit, blob, tree
207 * or tag), the size of the raw data and the raw, unparsed data itself.
208 * For a commit or tag, that raw data is human readable plain ASCII
209 * text. For a blob it is just file contents, so it could be text or
210 * binary data. For a tree it is a special binary format, so it's unlikely
211 * to be hugely helpful as a raw object.
212 */
176d58ba
PS
213 data = (const unsigned char *)git_odb_object_data(obj);
214 otype = git_odb_object_type(obj);
215
29d9afc0
PS
216 /**
217 * We provide methods to convert from the object type which is an enum, to
218 * a string representation of that value (and vice-versa).
219 */
176d58ba 220 str_type = git_object_type2string(otype);
e2d1b7ec 221 printf("object length and type: %d, %s\nobject data: %s\n",
176d58ba 222 (int)git_odb_object_size(obj),
e2d1b7ec 223 str_type, data);
176d58ba 224
29d9afc0
PS
225 /**
226 * For proper memory management, close the object when you are done with
227 * it or it will leak memory.
228 */
176d58ba
PS
229 git_odb_object_free(obj);
230
29d9afc0
PS
231 /**
232 * #### Raw Object Writing
233 */
176d58ba
PS
234
235 printf("\n*Raw Object Write*\n");
236
29d9afc0
PS
237 /**
238 * You can also write raw object data to Git. This is pretty cool because
239 * it gives you direct access to the key/value properties of Git. Here
240 * we'll write a new blob object that just contains a simple string.
241 * Notice that we have to specify the object type as the `git_otype` enum.
242 */
ac3d33df 243 git_odb_write(oid, odb, "test data", sizeof("test data") - 1, GIT_OBJECT_BLOB);
29d9afc0
PS
244
245 /**
246 * Now that we've written the object, we can check out what SHA1 was
247 * generated when the object was written to our database.
248 */
249 git_oid_fmt(oid_hex, oid);
250 printf("Written Object: %s\n", oid_hex);
f9ea8c6a
PS
251
252 /**
253 * Free the object database after usage.
254 */
255 git_odb_free(odb);
b009adad 256}
176d58ba 257
b009adad
PS
258/**
259 * #### Writing Commits
260 *
261 * libgit2 provides a couple of methods to create commit objects easily as
262 * well. There are four different create signatures, we'll just show one
263 * of them here. You can read about the other ones in the [commit API
264 * docs][cd].
265 *
266 * [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
267 */
268static void commit_writing(git_repository *repo)
269{
176d58ba
PS
270 git_oid tree_id, parent_id, commit_id;
271 git_tree *tree;
272 git_commit *parent;
8572e225 273 git_signature *author, *committer;
b009adad
PS
274 char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
275
276 printf("\n*Commit Writing*\n");
176d58ba 277
b009adad
PS
278 /**
279 * Creating signatures for an authoring identity and time is simple. You
280 * will need to do this to specify who created a commit and when. Default
281 * values for the name and email should be found in the `user.name` and
282 * `user.email` configuration options. See the `config` section of this
283 * example file to see how to access config values.
284 */
8572e225 285 git_signature_new(&author,
176d58ba 286 "Scott Chacon", "schacon@gmail.com", 123456789, 60);
8572e225 287 git_signature_new(&committer,
176d58ba
PS
288 "Scott A Chacon", "scott@github.com", 987654321, 90);
289
b009adad
PS
290 /**
291 * Commit objects need a tree to point to and optionally one or more
292 * parents. Here we're creating oid objects to create the commit with,
293 * but you can also use
294 */
176d58ba
PS
295 git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
296 git_tree_lookup(&tree, repo, &tree_id);
297 git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
298 git_commit_lookup(&parent, repo, &parent_id);
299
b009adad
PS
300 /**
301 * Here we actually create the commit object with a single call with all
302 * the values we need to create the commit. The SHA key is written to the
303 * `commit_id` variable here.
304 */
176d58ba
PS
305 git_commit_create_v(
306 &commit_id, /* out id */
307 repo,
308 NULL, /* do not update the HEAD */
309 author,
8572e225 310 committer,
176d58ba
PS
311 NULL, /* use default message encoding */
312 "example commit",
313 tree,
314 1, parent);
315
b009adad
PS
316 /**
317 * Now we can take a look at the commit SHA we've generated.
318 */
319 git_oid_fmt(oid_hex, &commit_id);
320 printf("New Commit: %s\n", oid_hex);
f9ea8c6a
PS
321
322 /**
323 * Free all objects used in the meanwhile.
324 */
325 git_tree_free(tree);
326 git_commit_free(parent);
327 git_signature_free(author);
328 git_signature_free(committer);
15960454
PS
329}
330
331/**
332 * ### Object Parsing
333 *
334 * libgit2 has methods to parse every object type in Git so you don't have
335 * to work directly with the raw data. This is much faster and simpler
336 * than trying to deal with the raw data yourself.
337 */
338
339/**
340 * #### Commit Parsing
341 *
342 * [Parsing commit objects][pco] is simple and gives you access to all the
343 * data in the commit - the author (name, email, datetime), committer
344 * (same), tree, message, encoding and parent(s).
345 *
346 * [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit
347 */
348static void commit_parsing(git_repository *repo)
349{
350 const git_signature *author, *cmtter;
351 git_commit *commit, *parent;
352 git_oid oid;
353 char oid_hex[GIT_OID_HEXSZ+1];
354 const char *message;
355 unsigned int parents, p;
356 int error;
e2d1b7ec 357 time_t time;
15960454
PS
358
359 printf("\n*Commit Parsing*\n");
360
361 git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
362
363 error = git_commit_lookup(&commit, repo, &oid);
364 check_error(error, "looking up commit");
365
366 /**
367 * Each of the properties of the commit object are accessible via methods,
368 * including commonly needed variations, such as `git_commit_time` which
369 * returns the author time and `git_commit_message` which gives you the
370 * commit message (as a NUL-terminated string).
371 */
372 message = git_commit_message(commit);
373 author = git_commit_author(commit);
374 cmtter = git_commit_committer(commit);
e2d1b7ec 375 time = git_commit_time(commit);
15960454
PS
376
377 /**
378 * The author and committer methods return [git_signature] structures,
379 * which give you name, email and `when`, which is a `git_time` structure,
380 * giving you a timestamp and timezone offset.
381 */
e2d1b7ec
PS
382 printf("Author: %s (%s)\nCommitter: %s (%s)\nDate: %s\nMessage: %s\n",
383 author->name, author->email,
384 cmtter->name, cmtter->email,
385 ctime(&time), message);
15960454
PS
386
387 /**
388 * Commits can have zero or more parents. The first (root) commit will
389 * have no parents, most commits will have one (i.e. the commit it was
390 * based on) and merge commits will have two or more. Commits can
391 * technically have any number, though it's rare to have more than two.
392 */
393 parents = git_commit_parentcount(commit);
394 for (p = 0;p < parents;p++) {
395 memset(oid_hex, 0, sizeof(oid_hex));
396
397 git_commit_parent(&parent, commit, p);
398 git_oid_fmt(oid_hex, git_commit_id(parent));
399 printf("Parent: %s\n", oid_hex);
400 git_commit_free(parent);
401 }
402
403 git_commit_free(commit);
404}
405
406/**
407 * #### Tag Parsing
408 *
409 * You can parse and create tags with the [tag management API][tm], which
410 * functions very similarly to the commit lookup, parsing and creation
411 * methods, since the objects themselves are very similar.
412 *
413 * [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag
414 */
415static void tag_parsing(git_repository *repo)
416{
417 git_commit *commit;
ac3d33df 418 git_object_t type;
176d58ba 419 git_tag *tag;
15960454
PS
420 git_oid oid;
421 const char *name, *message;
422 int error;
176d58ba 423
15960454
PS
424 printf("\n*Tag Parsing*\n");
425
426 /**
427 * We create an oid for the tag object if we know the SHA and look it up
428 * the same way that we would a commit (or any other object).
429 */
176d58ba
PS
430 git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
431
432 error = git_tag_lookup(&tag, repo, &oid);
433 check_error(error, "looking up tag");
434
15960454
PS
435 /**
436 * Now that we have the tag object, we can extract the information it
437 * generally contains: the target (usually a commit object), the type of
438 * the target object (usually 'commit'), the name ('v1.0'), the tagger (a
439 * git_signature - name, email, timestamp), and the tag message.
440 */
176d58ba 441 git_tag_target((git_object **)&commit, tag);
15960454 442 name = git_tag_name(tag); /* "test" */
ac3d33df 443 type = git_tag_target_type(tag); /* GIT_OBJECT_COMMIT (object_t enum) */
15960454 444 message = git_tag_message(tag); /* "tag message\n" */
e2d1b7ec
PS
445 printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n",
446 name, git_object_type2string(type), message);
176d58ba 447
f9ea8c6a
PS
448 /**
449 * Free both the commit and tag after usage.
450 */
176d58ba 451 git_commit_free(commit);
f9ea8c6a 452 git_tag_free(tag);
15960454 453}
176d58ba 454
15960454
PS
455/**
456 * #### Tree Parsing
457 *
458 * [Tree parsing][tp] is a bit different than the other objects, in that
459 * we have a subtype which is the tree entry. This is not an actual
460 * object type in Git, but a useful structure for parsing and traversing
461 * tree entries.
462 *
463 * [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree
464 */
465static void tree_parsing(git_repository *repo)
466{
467 const git_tree_entry *entry;
468 size_t cnt;
469 git_object *obj;
470 git_tree *tree;
471 git_oid oid;
176d58ba 472
176d58ba
PS
473 printf("\n*Tree Parsing*\n");
474
15960454
PS
475 /**
476 * Create the oid and lookup the tree object just like the other objects.
477 */
478 git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
176d58ba
PS
479 git_tree_lookup(&tree, repo, &oid);
480
15960454
PS
481 /**
482 * Getting the count of entries in the tree so you can iterate over them
483 * if you want to.
484 */
485 cnt = git_tree_entrycount(tree); /* 2 */
486 printf("tree entries: %d\n", (int) cnt);
176d58ba
PS
487
488 entry = git_tree_entry_byindex(tree, 0);
15960454 489 printf("Entry name: %s\n", git_tree_entry_name(entry)); /* "README" */
176d58ba 490
15960454
PS
491 /**
492 * You can also access tree entries by name if you know the name of the
493 * entry you're looking for.
494 */
176d58ba 495 entry = git_tree_entry_byname(tree, "README");
15960454 496 git_tree_entry_name(entry); /* "README" */
176d58ba 497
15960454
PS
498 /**
499 * Once you have the entry object, you can access the content or subtree
500 * (or commit, in the case of submodules) that it points to. You can also
501 * get the mode if you want.
502 */
503 git_tree_entry_to_object(&obj, repo, entry); /* blob */
176d58ba 504
15960454 505 /**
f9ea8c6a 506 * Remember to close the looked-up object and tree once you are done using it
15960454
PS
507 */
508 git_object_free(obj);
f9ea8c6a 509 git_tree_free(tree);
15960454 510}
176d58ba 511
15960454
PS
512/**
513 * #### Blob Parsing
514 *
515 * The last object type is the simplest and requires the least parsing
516 * help. Blobs are just file contents and can contain anything, there is
517 * no structure to it. The main advantage to using the [simple blob
518 * api][ba] is that when you're creating blobs you don't have to calculate
519 * the size of the content. There is also a helper for reading a file
520 * from disk and writing it to the db and getting the oid back so you
521 * don't have to do all those steps yourself.
522 *
523 * [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob
524 */
525static void blob_parsing(git_repository *repo)
526{
527 git_blob *blob;
528 git_oid oid;
176d58ba
PS
529
530 printf("\n*Blob Parsing*\n");
176d58ba
PS
531
532 git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
533 git_blob_lookup(&blob, repo, &oid);
534
15960454
PS
535 /**
536 * You can access a buffer with the raw contents of the blob directly.
537 * Note that this buffer may not be contain ASCII data for certain blobs
538 * (e.g. binary files): do not consider the buffer a NULL-terminated
539 * string, and use the `git_blob_rawsize` attribute to find out its exact
540 * size in bytes
541 * */
542 printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */
543 git_blob_rawcontent(blob); /* "content" */
f9ea8c6a
PS
544
545 /**
546 * Free the blob after usage.
547 */
548 git_blob_free(blob);
8b93ccdf
PS
549}
550
551/**
552 * ### Revwalking
553 *
554 * The libgit2 [revision walking api][rw] provides methods to traverse the
555 * directed graph created by the parent pointers of the commit objects.
556 * Since all commits point back to the commit that came directly before
557 * them, you can walk this parentage as a graph and find all the commits
558 * that were ancestors of (reachable from) a given starting point. This
559 * can allow you to create `git log` type functionality.
560 *
561 * [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk
562 */
563static void revwalking(git_repository *repo)
564{
565 const git_signature *cauth;
566 const char *cmsg;
567 int error;
176d58ba
PS
568 git_revwalk *walk;
569 git_commit *wcommit;
8b93ccdf
PS
570 git_oid oid;
571
572 printf("\n*Revwalking*\n");
176d58ba
PS
573
574 git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
575
8b93ccdf
PS
576 /**
577 * To use the revwalker, create a new walker, tell it how you want to sort
578 * the output and then push one or more starting points onto the walker.
579 * If you want to emulate the output of `git log` you would push the SHA
580 * of the commit that HEAD points to into the walker and then start
581 * traversing them. You can also 'hide' commits that you want to stop at
582 * or not see any of their ancestors. So if you want to emulate `git log
583 * branch1..branch2`, you would push the oid of `branch2` and hide the oid
584 * of `branch1`.
585 */
176d58ba
PS
586 git_revwalk_new(&walk, repo);
587 git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
588 git_revwalk_push(walk, &oid);
589
8b93ccdf
PS
590 /**
591 * Now that we have the starting point pushed onto the walker, we start
592 * asking for ancestors. It will return them in the sorting order we asked
593 * for as commit oids. We can then lookup and parse the committed pointed
594 * at by the returned OID; note that this operation is specially fast
595 * since the raw contents of the commit object will be cached in memory
596 */
176d58ba
PS
597 while ((git_revwalk_next(&oid, walk)) == 0) {
598 error = git_commit_lookup(&wcommit, repo, &oid);
599 check_error(error, "looking up commit during revwalk");
600
601 cmsg = git_commit_message(wcommit);
602 cauth = git_commit_author(wcommit);
603 printf("%s (%s)\n", cmsg, cauth->email);
604
605 git_commit_free(wcommit);
606 }
607
8b93ccdf
PS
608 /**
609 * Like the other objects, be sure to free the revwalker when you're done
610 * to prevent memory leaks. Also, make sure that the repository being
611 * walked it not deallocated while the walk is in progress, or it will
612 * result in undefined behavior
613 */
176d58ba 614 git_revwalk_free(walk);
c079e3c8 615}
176d58ba 616
c079e3c8
PS
617/**
618 * ### Index File Manipulation *
619 * The [index file API][gi] allows you to read, traverse, update and write
620 * the Git index file (sometimes thought of as the staging area).
621 *
622 * [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index
623 */
624static void index_walking(git_repository *repo)
625{
176d58ba 626 git_index *index;
22a2d3d5 627 size_t i, ecount;
176d58ba 628
c079e3c8
PS
629 printf("\n*Index Walking*\n");
630
631 /**
632 * You can either open the index from the standard location in an open
633 * repository, as we're doing here, or you can open and manipulate any
634 * index file with `git_index_open_bare()`. The index for the repository
635 * will be located and loaded from disk.
636 */
176d58ba
PS
637 git_repository_index(&index, repo);
638
c079e3c8
PS
639 /**
640 * For each entry in the index, you can get a bunch of information
641 * including the SHA (oid), path and mode which map to the tree objects
642 * that are written out. It also has filesystem properties to help
643 * determine what to inspect for changes (ctime, mtime, dev, ino, uid,
644 * gid, file_size and flags) All these properties are exported publicly in
645 * the `git_index_entry` struct
646 */
176d58ba
PS
647 ecount = git_index_entrycount(index);
648 for (i = 0; i < ecount; ++i) {
649 const git_index_entry *e = git_index_get_byindex(index, i);
650
651 printf("path: %s\n", e->path);
652 printf("mtime: %d\n", (int)e->mtime.seconds);
653 printf("fs: %d\n", (int)e->file_size);
654 }
655
656 git_index_free(index);
f9a7973d 657}
176d58ba 658
f9a7973d
PS
659/**
660 * ### References
661 *
662 * The [reference API][ref] allows you to list, resolve, create and update
663 * references such as branches, tags and remote references (everything in
664 * the .git/refs directory).
665 *
666 * [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
667 */
668static void reference_listing(git_repository *repo)
669{
176d58ba 670 git_strarray ref_list;
f9a7973d 671 unsigned i;
f9a7973d
PS
672
673 printf("\n*Reference Listing*\n");
674
675 /**
676 * Here we will implement something like `git for-each-ref` simply listing
677 * out all available references and the object SHA they resolve to.
678 *
679 * Now that we have the list of reference names, we can lookup each ref
680 * one at a time and resolve them to the SHA, then print both values out.
681 */
682
683 git_reference_list(&ref_list, repo);
176d58ba 684
176d58ba 685 for (i = 0; i < ref_list.count; ++i) {
5aa10107
PS
686 git_reference *ref;
687 char oid_hex[GIT_OID_HEXSZ+1] = GIT_OID_HEX_ZERO;
688 const char *refname;
689
176d58ba
PS
690 refname = ref_list.strings[i];
691 git_reference_lookup(&ref, repo, refname);
692
693 switch (git_reference_type(ref)) {
ac3d33df 694 case GIT_REFERENCE_DIRECT:
f9a7973d
PS
695 git_oid_fmt(oid_hex, git_reference_target(ref));
696 printf("%s [%s]\n", refname, oid_hex);
176d58ba
PS
697 break;
698
ac3d33df 699 case GIT_REFERENCE_SYMBOLIC:
176d58ba
PS
700 printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
701 break;
702 default:
703 fprintf(stderr, "Unexpected reference type\n");
704 exit(1);
705 }
5aa10107
PS
706
707 git_reference_free(ref);
176d58ba
PS
708 }
709
22a2d3d5 710 git_strarray_dispose(&ref_list);
986913f4 711}
96da90ae 712
986913f4
PS
713/**
714 * ### Config Files
715 *
716 * The [config API][config] allows you to list and updatee config values
717 * in any of the accessible config file locations (system, global, local).
718 *
719 * [config]: http://libgit2.github.com/libgit2/#HEAD/group/config
720 */
832278bf 721static void config_files(const char *repo_path, git_repository* repo)
986913f4 722{
176d58ba 723 const char *email;
986913f4 724 char config_path[256];
ed2b1c7e 725 int32_t autocorrect;
176d58ba 726 git_config *cfg;
832278bf 727 git_config *snap_cfg;
eae0bfdc 728 int error_code;
96da90ae 729
986913f4
PS
730 printf("\n*Config Listing*\n");
731
732 /**
733 * Open a config object so we can read global values from it.
734 */
176d58ba
PS
735 sprintf(config_path, "%s/config", repo_path);
736 check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
96da90ae 737
ed2b1c7e
PS
738 if (git_config_get_int32(&autocorrect, cfg, "help.autocorrect") == 0)
739 printf("Autocorrect: %d\n", autocorrect);
96da90ae 740
832278bf
DS
741 check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot");
742 git_config_get_string(&email, snap_cfg, "user.email");
176d58ba 743 printf("Email: %s\n", email);
f9ea8c6a 744
eae0bfdc
PP
745 error_code = git_config_get_int32(&autocorrect, cfg, "help.autocorrect");
746 switch (error_code)
747 {
748 case 0:
749 printf("Autocorrect: %d\n", autocorrect);
750 break;
751 case GIT_ENOTFOUND:
752 printf("Autocorrect: Undefined\n");
753 break;
754 default:
755 check_error(error_code, "get_int32 failed");
756 }
f9ea8c6a 757 git_config_free(cfg);
eae0bfdc
PP
758
759 check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot");
760 error_code = git_config_get_string(&email, snap_cfg, "user.email");
761 switch (error_code)
762 {
763 case 0:
764 printf("Email: %s\n", email);
765 break;
766 case GIT_ENOTFOUND:
767 printf("Email: Undefined\n");
768 break;
769 default:
770 check_error(error_code, "get_string failed");
771 }
772
f9ea8c6a 773 git_config_free(snap_cfg);
388f37b3 774}