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