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