@@ -15,34 +15,34 @@ This article is about the portions of the code that deal with libgit2, so we'll
1515Let's skip the frontmatter, and jump straight to the meat of this thing.
1616The ` main ` function starts off with some boilerplate and a bit of command-line parsing noise, which we'll ignore for the purposes of this article.
1717
18- ``` c
18+ ~~~ c
1919int main (int argc, char * argv[ ] )
2020{
2121 git_repository * repo = NULL;
2222 struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
2323
2424 git_threads_init();
2525 parse_opts(&o, argc, argv);
26- ```
26+ ~~~
2727
2828Next we have a bit of a shortcut; if we were called with no options, we can use the simplest API for doing this, since its defaults match those of the command line.
2929That `check_lg2` utility checks the value passed in the first parameter, and if it's not zero, prints the other two parameters and exits the program.
3030Not the greatest error handling, but it'll do for these examples.
3131
32- ``` c
32+ ~~~ c
3333 if (o.no_options) {
3434 check_lg2(git_repository_init(&repo, o.dir, 0),
3535 "Could not initialize repository", NULL);
3636 }
37- ```
37+ ~~~
3838
3939If the situation is more complex, you can use the extended API to handle it.
4040The fields in [ the options structure] [ initopts ] are designed to provide much of what ` git init ` does.
4141Note that it's important to use the ` _INIT ` structure initializers; these structures have version numbers so future libgit2's can maintain backwards compatibility.
4242
4343[ initopts ] : http://libgit2.github.com/libgit2/#HEAD/type/git_repository_init_options
4444
45- ``` c
45+ ~~~ c
4646 else {
4747 git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
4848 initopts.flags = GIT_REPOSITORY_INIT_MKPATH;
@@ -54,26 +54,26 @@ Note that it's important to use the `_INIT` structure initializers; these struct
5454 initopts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
5555 initopts.template_path = o.template;
5656 }
57- ```
57+ ~~~
5858
5959Libgit2's repository is always oriented at the `.git` directory, so specifying an external git directory turns things a bit upside-down:
6060
61- ``` c
61+ ~~~ c
6262 if (o.gitdir) {
6363 initopts.workdir_path = o.dir;
6464 o.dir = o.gitdir;
6565 }
6666
6767 if (o.shared != 0)
6868 initopts.mode = o.shared;
69- ```
69+ ~~~
7070
7171Now the call that does all the work: [ ` git_repository_init_ext ` ] [ grie ] .
7272The output (if the call succeeds) lands in ` repo ` , which is a ` git_repository* ` , which we can then go on and use.
7373
7474[ grie ] : http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_init_ext
7575
76- ``` c
76+ ~~~ c
7777 check_lg2 (git_repository_init_ext(&repo, o.dir, &initopts),
7878 "Could not initialize repository", NULL);
7979 }
@@ -86,64 +86,64 @@ The output (if the call succeeds) lands in `repo`, which is a `git_repository*`,
8686
8787 printf("Initialized empty Git repository in %s\n", o.dir);
8888 }
89- ```
89+ ~~~
9090
9191If we get this far, the initialization is done.
9292This example goes one step farther than git, by providing an option to create an empty initial commit.
9393The body of this function is [below](#toc_2).
9494
95- ``` c
95+ ~~~ c
9696 if (o.initial_commit) {
9797 create_initial_commit(repo);
9898 printf("Created empty initial commit\n");
9999 }
100-
101- ```
100+
101+ ~~~
102102
103103Now to clean up our mess; C isn't your mother.
104104Unless the docs specifically say otherwise, any non-` const ` pointer that's filled in by libgit2 needs to be freed by the caller.
105105
106- ``` c
106+ ~~~ c
107107 git_repository_free (repo);
108108 git_threads_shutdown();
109109
110110 return 0;
111111}
112- ```
112+ ~~~
113113
114114## Creating the Initial Commit
115115
116116First, we declare all of our variables, which might give you a clue as to what's coming.
117117
118- ``` c
118+ ~~~ c
119119static void create_initial_commit(git_repository *repo)
120120{
121121 git_signature *sig;
122122 git_index *index;
123123 git_oid tree_id, commit_id;
124124 git_tree *tree;
125- ```
125+ ~~~
126126
127127Next, we generate a commit signature using the values in the user's config, and timestamp of right now.
128128
129- ``` c
129+ ~~~ c
130130 if (git_signature_default(&sig, repo) < 0 )
131131 fatal ("Unable to create a commit signature.",
132132 "Perhaps 'user.name' and 'user.email' are not set");
133- ```
133+ ~~~
134134
135135Now we store the index's tree into the ODB to use for the commit.
136136Since the repo was *just* initialized, the index has an empty tree.
137137
138- ``` c
138+ ~~~ c
139139 if (git_repository_index(&index, repo) < 0)
140140 fatal("Could not open repository index", NULL);
141141
142142 if (git_index_write_tree(&tree_id, index) < 0)
143143 fatal("Unable to write initial tree from index", NULL);
144144
145145 git_index_free(index);
146- ```
146+ ~~~
147147
148148It's worth noting that this doesn't actually write the index to disk.
149149There's a separate call for that: [ ` git_index_write ` ] [ write ] .
@@ -153,34 +153,34 @@ All this code does is use the empty index to get the SHA-1 hash of the empty tre
153153
154154Okay, now we have the empty tree's SHA-1 hash, but we need an actual ` git_tree ` object to create a commit.
155155
156- ``` c
156+ ~~~ c
157157 if (git_tree_lookup(&tree, repo, &tree_id) < 0 )
158158 fatal ("Could not look up initial tree", NULL);
159- ```
159+ ~~~
160160
161161**Now** we're ready to write the initial commit.
162162Normally you'd look up `HEAD` to use as the parent, but this commit will have no parents.
163163
164- ``` c
164+ ~~~ c
165165 if (git_commit_create_v(
166166 &commit_id, repo, "HEAD", sig, sig,
167167 NULL, "Initial commit", tree, 0) < 0)
168168 fatal("Could not create the initial commit", NULL);
169- ```
169+ ~~~
170170
171171And (of course) clean up our mess.
172172
173- ``` c
173+ ~~~ c
174174 git_tree_free (tree);
175175 git_signature_free(sig);
176176}
177- ```
177+ ~~~
178178
179179## Fin
180180
181181If you compile and run this program, you'll get output something like this:
182182
183- ``` bash
183+ ~~~ bash
184184$ ./init --initial-commit ./foo
185185Initialized empty Git repository in /tmp/foo/
186186Created empty initial commit
@@ -191,7 +191,7 @@ Author: Ben Straub <bs@github.com>
191191Date: Sat Oct 5 20:59:50 2013 -0700
192192
193193 Initial commit
194- ```
194+ ~~~
195195
196196## What's next?
197197Go [ back to the Learning center] ( /docs ) for more, or check out [ the API documentation] ( http://libgit2.github.com/libgit2/ ) .
0 commit comments