Skip to content

Commit d3e1176

Browse files
committed
Libgit2 tweaks
1 parent d9db713 commit d3e1176

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

book/B-embedding-git/sections/libgit2.asc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ commit = repo.lookup(commit_id) # <8>
107107
<8> The return value is the SHA-1 hash of a new commit object, which you can then use to get a `Commit` object.
108108

109109
The Ruby code is nice and clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
110+
If you're not a rubyist, we touch on some other bindings in <<_libgit2_bindings>>.
110111

111112

112113
==== Advanced Functionality
@@ -117,7 +118,7 @@ Libgit2 allows custom backends for configuration, ref storage, and the object da
117118

118119
Let's take a look at how this works.
119120
The code below is borrowed from the set of backend examples provided by the Libgit2 team (which can be found at https://github.com/libgit2/libgit2-backends[]).
120-
Here's how a custom backend for the object database is set up.
121+
Here's how a custom backend for the object database is set up:
121122

122123
[source,c]
123124
----
@@ -133,15 +134,15 @@ error = git_odb_backend_mine(&my_backend, /*…*/); // <3>
133134
error = git_odb_add_backendodb, my_backend, 1); // <4>
134135
----
135136

136-
_Note that error handling has been left out for this example. You should know better._
137+
_(Note that errors are captured, but not handled. We hope your code is better than ours.)_
137138

138139
<1> Initialize an empty object database (ODB) ``frontend,'' which will act as a handle to the real ODB.
139140
<2> Construct a `git_repository` around the empty ODB.
140141
<3> Initialize a custom ODB backend.
141142
<4> Set the repository to use the custom backend for its ODB.
142143

143144
But what is this `git_odb_backend_mine` thing?
144-
Well, that's your own backend, and you can do whatever you want in there, so long as you fill in the `git_odb_backend` structure properly.
145+
Well, that's your own ODB implementation, and you can do whatever you want in there, so long as you fill in the `git_odb_backend` structure properly.
145146
Here's what it _could_ look like:
146147

147148
[source,c]
@@ -172,38 +173,41 @@ int git_odb_backend_mine(git_odb_backend **backend_out, /*…*/)
172173
}
173174
----
174175

175-
The `my_backend_struct` structure contains a `git_odb_backend` member first, which ensures that the memory layout is what the Libgit2 code expects it to be.
176+
The subtlest constraint here is that `my_backend_struct`'s first member must be a `git_odb_backend` structure; this ensures that the memory layout is what the Libgit2 code expects it to be.
176177
The rest of it is arbitrary; this structure can be as large or small as you need it to be.
178+
177179
The initialization function allocates some memory for the structure, sets up the custom context, and then fills in the members of the `parent` structure that it supports.
178180
Take a look at the `include/git2/sys/odb_backend.h` file in the Libgit2 source for a complete set of call signatures; your particular use case will help determine which of these you'll want to support.
179181

182+
[[_libgit2_bindings]]
180183
==== Other Bindings
181184

182185
Libgit2 has bindings for many languages.
183-
Here we show how to get the commit message from HEAD with a few of the more mature bindings pakages; there are many others, including C++, Go, Node.js, Erlang, and the JVM.
186+
Here we show a small example using a few of the more complete bindings pakages as of this writing; libraries exist for many other languages, including C++, Go, Node.js, Erlang, and the JVM, all in various stages of maturity.
184187
The official collection of bindings can be found by browsing the repositories at https://github.com/libgit2[].
188+
The code we'll write will return the commit message from the commit eventually pointed to by HEAD (sort of like `git log -1`).
185189

186190

187191
===== LibGit2Sharp
188192

189193
(((.NET)))(((C#)))(((Mono)))
190194
If you're writing a .NET or Mono application, LibGit2Sharp (https://github.com/libgit2/libgit2sharp[]) is what you're looking for.
191195
The bindings are written in C#, and great care has been taken to wrap the raw Libgit2 calls with native-feeling CLR APIs.
192-
Here's what it looks like to read HEAD's commit message:
196+
Here's what our example program looks like:
193197

194198
[source,csharp]
195199
-----
196200
new Repository(@"C:\path\to\repo").Head.Tip.Message;
197201
-----
198202

199-
For desktop Windows applications, there's a NuGet package that will help you get started quickly.
203+
For desktop Windows applications, there's even a NuGet package that will help you get started quickly.
200204

201205
===== objective-git
202206

203207
(((Apple)))(((Objective-C)))(((Cocoa)))
204208
If your application is running on an Apple platform, you're likely using Objective-C as your implementation language.
205209
Objective-Git (https://github.com/libgit2/objective-git[]) is the name of the Libgit2 bindings for that environment.
206-
Again, here's how to read HEAD's commit message:
210+
The example program looks like this:
207211

208212
[source,objc]
209213
-----
@@ -212,21 +216,25 @@ GTRepository *repo =
212216
NSString *msg = [[[repo headReferenceWithError:NULL] resolvedTarget] message];
213217
-----
214218

219+
Objective-git is fully interoperable with Swift, so don't fear if you've left Objective-C behind.
220+
215221

216222
===== pygit2
217223

218224
(((Python)))
219225
The bindings for Libgit2 in Python are called Pygit2, and can be found at http://www.pygit2.org/[].
220-
As always, HEAD's commit message:
226+
Our example program:
221227

222228
[source,python]
223229
----
224-
pygit2.Repository("/path/to/repo").head.resolve().get_object().message
230+
pygit2.Repository("/path/to/repo") # open repository
231+
.head.resolve() # get a direct ref
232+
.get_object().message # get commit, read message
225233
----
226234

227235

228236
==== Further Reading
229237

230238
Of course, a full treatment of Libgit2's capabilities is outside the scope of this book.
231239
If you want more information on Libgit2 itself, there's API documentation at https://libgit2.github.com/libgit2[], and a set of guides at https://libgit2.github.com/docs[].
232-
For the other bindings, check the README and tests; there are often small tutorials and pointers to further reading there.
240+
For the other bindings, check the bundled README and tests; there are often small tutorials and pointers to further reading there.

0 commit comments

Comments
 (0)