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