]> git.proxmox.com Git - libgit2.git/blob - examples/general.c
Merge pull request #3652 from libgit2/cmn/commit-to-memory
[libgit2.git] / examples / general.c
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
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.
19 //
20 // This file is an example of using that API in a real, compilable C file.
21 // As the API is updated, this file will be updated to demonstrate the new
22 // functionality.
23 //
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.
27 //
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,
31 // check out [Chapter 10][pg] of the Pro Git book.
32 //
33 // [lg]: http://libgit2.github.com
34 // [ap]: http://libgit2.github.com/libgit2
35 // [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
36
37 // ### Includes
38
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.
42 #include <git2.h>
43 #include <stdio.h>
44
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 {
50 const git_error *error = giterr_last();
51 if (!error_code)
52 return;
53
54 printf("Error %d %s - %s\n", error_code, action,
55 (error && error->message) ? error->message : "???");
56
57 exit(1);
58 }
59
60 int main (int argc, char** argv)
61 {
62 // Initialize the library, this will set up any global state which libgit2 needs
63 // including threading and crypto
64 git_libgit2_init();
65
66 // ### Opening the Repository
67
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.
71 //
72 // (Try running this program against tests/resources/testrepo.git.)
73 //
74 // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
75 int error;
76 const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
77 git_repository *repo;
78
79 error = git_repository_open(&repo, repo_path);
80 check_error(error, "opening repository");
81
82 // ### SHA-1 Value Conversions
83
84 // For our first example, we will convert a 40 character hex value to the
85 // 20 byte raw SHA1 value.
86 printf("*Hex to Raw*\n");
87 char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
88
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.
92 git_oid oid;
93 git_oid_fromstr(&oid, hex);
94
95 // Once we've converted the string into the oid value, we can get the raw
96 // value of the SHA by accessing `oid.id`
97
98 // Next we will convert the 20 byte raw SHA1 value to a human readable 40
99 // char hex value.
100 printf("\n*Raw to Hex*\n");
101 char out[GIT_OID_HEXSZ+1];
102 out[GIT_OID_HEXSZ] = '\0';
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
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
112 // working with raw objects, we'll need to get this structure from the
113 // repository.
114 //
115 // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
116 git_odb *odb;
117 git_repository_odb(&odb, repo);
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;
126
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
129 // knowing their type and inspect the raw bytes unparsed.
130 error = git_odb_read(&obj, odb, &oid);
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.
139 data = (const unsigned char *)git_odb_object_data(obj);
140 otype = git_odb_object_type(obj);
141
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).
144 str_type = git_object_type2string(otype);
145 printf("object length and type: %d, %s\n",
146 (int)git_odb_object_size(obj),
147 str_type);
148
149 // For proper memory management, close the object when you are done with
150 // it or it will leak memory.
151 git_odb_object_free(obj);
152
153 // #### Raw Object Writing
154
155 printf("\n*Raw Object Write*\n");
156
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.
161 git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
162
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.
165 git_oid_fmt(out, &oid);
166 printf("Written Object: %s\n", out);
167
168 // ### Object Parsing
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.
173
174 // #### Commit Parsing
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 //
180 // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit
181
182 printf("\n*Commit Parsing*\n");
183
184 git_commit *commit;
185 git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
186
187 error = git_commit_lookup(&commit, repo, &oid);
188 check_error(error, "looking up commit");
189
190 const git_signature *author, *cmtter;
191 const char *message;
192 time_t ctime;
193 unsigned int parents, p;
194
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).
199 message = git_commit_message(commit);
200 author = git_commit_author(commit);
201 cmtter = git_commit_committer(commit);
202 ctime = git_commit_time(commit);
203
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.
207 printf("Author: %s (%s)\n", author->name, author->email);
208
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.
213 parents = git_commit_parentcount(commit);
214 for (p = 0;p < parents;p++) {
215 git_commit *parent;
216 git_commit_parent(&parent, commit, p);
217 git_oid_fmt(out, git_commit_id(parent));
218 printf("Parent: %s\n", out);
219 git_commit_free(parent);
220 }
221
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.
224 git_commit_free(commit);
225
226 // #### Writing Commits
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].
232 //
233 // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
234
235 printf("\n*Commit Writing*\n");
236 git_oid tree_id, parent_id, commit_id;
237 git_tree *tree;
238 git_commit *parent;
239
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
253 git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
254 git_tree_lookup(&tree, repo, &tree_id);
255 git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
256 git_commit_lookup(&parent, repo, &parent_id);
257
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.
261 git_commit_create_v(
262 &commit_id, /* out id */
263 repo,
264 NULL, /* do not update the HEAD */
265 author,
266 cmtter,
267 NULL, /* use default message encoding */
268 "example commit",
269 tree,
270 1, parent);
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
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 //
282 // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag
283 printf("\n*Tag Parsing*\n");
284 git_tag *tag;
285 const char *tmessage, *tname;
286 git_otype ttype;
287
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).
290 git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
291
292 error = git_tag_lookup(&tag, repo, &oid);
293 check_error(error, "looking up tag");
294
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.
299 git_tag_target((git_object **)&commit, tag);
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"
303 printf("Tag Message: %s\n", tmessage);
304
305 git_commit_free(commit);
306
307 // #### Tree Parsing
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.
313 //
314 // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree
315 printf("\n*Tree Parsing*\n");
316
317 const git_tree_entry *entry;
318 git_object *objt;
319
320 // Create the oid and lookup the tree object just like the other objects.
321 git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5");
322 git_tree_lookup(&tree, repo, &oid);
323
324 // Getting the count of entries in the tree so you can iterate over them
325 // if you want to.
326 size_t cnt = git_tree_entrycount(tree); // 3
327 printf("tree entries: %d\n", (int)cnt);
328
329 entry = git_tree_entry_byindex(tree, 0);
330 printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c"
331
332 // You can also access tree entries by name if you know the name of the
333 // entry you're looking for.
334 entry = git_tree_entry_byname(tree, "README");
335 git_tree_entry_name(entry); // "hello.c"
336
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.
340 git_tree_entry_to_object(&objt, repo, entry); // blob
341
342 // Remember to close the looked-up object once you are done using it
343 git_object_free(objt);
344
345 // #### Blob Parsing
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.
354 //
355 // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob
356
357 printf("\n*Blob Parsing*\n");
358 git_blob *blob;
359
360 git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
361 git_blob_lookup(&blob, repo, &oid);
362
363 // You can access a buffer with the raw contents of the blob directly.
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
368 printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); // 8
369 git_blob_rawcontent(blob); // "content"
370
371 // ### Revwalking
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.
379 //
380 // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk
381
382 printf("\n*Revwalking*\n");
383 git_revwalk *walk;
384 git_commit *wcommit;
385
386 git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
387
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
395 // of `branch1`.
396 git_revwalk_new(&walk, repo);
397 git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
398 git_revwalk_push(walk, &oid);
399
400 const git_signature *cauth;
401 const char *cmsg;
402
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
405 // for as commit oids. We can then lookup and parse the committed pointed
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
408 while ((git_revwalk_next(&oid, walk)) == 0) {
409 error = git_commit_lookup(&wcommit, repo, &oid);
410 check_error(error, "looking up commit during revwalk");
411
412 cmsg = git_commit_message(wcommit);
413 cauth = git_commit_author(wcommit);
414 printf("%s (%s)\n", cmsg, cauth->email);
415
416 git_commit_free(wcommit);
417 }
418
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
423 git_revwalk_free(walk);
424
425 // ### Index File Manipulation
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).
429 //
430 // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index
431
432 printf("\n*Index Walking*\n");
433
434 git_index *index;
435 unsigned int i, ecount;
436
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.
441 git_repository_index(&index, repo);
442
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
449 ecount = git_index_entrycount(index);
450 for (i = 0; i < ecount; ++i) {
451 const git_index_entry *e = git_index_get_byindex(index, i);
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
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).
465 //
466 // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
467
468 printf("\n*Reference Listing*\n");
469
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.
472 git_strarray ref_list;
473 git_reference_list(&ref_list, repo);
474
475 const char *refname;
476 git_reference *ref;
477
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.
480 for (i = 0; i < ref_list.count; ++i) {
481 refname = ref_list.strings[i];
482 git_reference_lookup(&ref, repo, refname);
483
484 switch (git_reference_type(ref)) {
485 case GIT_REF_OID:
486 git_oid_fmt(out, git_reference_target(ref));
487 printf("%s [%s]\n", refname, out);
488 break;
489
490 case GIT_REF_SYMBOLIC:
491 printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
492 break;
493 default:
494 fprintf(stderr, "Unexpected reference type\n");
495 exit(1);
496 }
497 }
498
499 git_strarray_free(&ref_list);
500
501 // ### Config Files
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).
505 //
506 // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config
507
508 printf("\n*Config Listing*\n");
509
510 const char *email;
511 int32_t j;
512
513 git_config *cfg;
514
515 // Open a config object so we can read global values from it.
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");
519
520 git_config_get_int32(&j, cfg, "help.autocorrect");
521 printf("Autocorrect: %d\n", j);
522
523 git_config_get_string(&email, cfg, "user.email");
524 printf("Email: %s\n", email);
525
526 // Finally, when you're done with the repository, you can free it as well.
527 git_repository_free(repo);
528
529 return 0;
530 }