You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<8> The return value is the SHA-1 hash of a new commit object, which you can then use to get a `Commit` object.
108
108
109
109
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>>.
110
111
111
112
112
113
==== Advanced Functionality
@@ -117,7 +118,7 @@ Libgit2 allows custom backends for configuration, ref storage, and the object da
117
118
118
119
Let's take a look at how this works.
119
120
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:
_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.)_
137
138
138
139
<1> Initialize an empty object database (ODB) ``frontend,'' which will act as a handle to the real ODB.
139
140
<2> Construct a `git_repository` around the empty ODB.
140
141
<3> Initialize a custom ODB backend.
141
142
<4> Set the repository to use the custom backend for its ODB.
142
143
143
144
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.
145
146
Here's what it _could_ look like:
146
147
147
148
[source,c]
@@ -172,38 +173,41 @@ int git_odb_backend_mine(git_odb_backend **backend_out, /*…*/)
172
173
}
173
174
----
174
175
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.
176
177
The rest of it is arbitrary; this structure can be as large or small as you need it to be.
178
+
177
179
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.
178
180
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.
179
181
182
+
[[_libgit2_bindings]]
180
183
==== Other Bindings
181
184
182
185
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.
184
187
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`).
185
189
186
190
187
191
===== LibGit2Sharp
188
192
189
193
(((.NET)))(((C#)))(((Mono)))
190
194
If you're writing a .NET or Mono application, LibGit2Sharp (https://github.com/libgit2/libgit2sharp[]) is what you're looking for.
191
195
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:
193
197
194
198
[source,csharp]
195
199
-----
196
200
new Repository(@"C:\path\to\repo").Head.Tip.Message;
197
201
-----
198
202
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.
200
204
201
205
===== objective-git
202
206
203
207
(((Apple)))(((Objective-C)))(((Cocoa)))
204
208
If your application is running on an Apple platform, you're likely using Objective-C as your implementation language.
205
209
Objective-Git (https://github.com/libgit2/objective-git[]) is the name of the Libgit2 bindings for that environment.
pygit2.Repository("/path/to/repo") # open repository
231
+
.head.resolve() # get a direct ref
232
+
.get_object().message # get commit, read message
225
233
----
226
234
227
235
228
236
==== Further Reading
229
237
230
238
Of course, a full treatment of Libgit2's capabilities is outside the scope of this book.
231
239
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