Skip to content

Commit 87a3648

Browse files
committed
ODB backends part I
1 parent 25b2ad0 commit 87a3648

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,41 @@ commit = repo.lookup(commit_id) # <8>
106106
<7> Rugged (and Libgit2) can optionally update a reference when making a commit.
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

109+
The Ruby code is pretty clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
110+
109111

110112
==== Advanced Functionality
111113

112-
// TODO: backend, remote
114+
Libgit2 has a couple of capabilities that are outside the scope of core Git.
115+
One example is pluggability: Libgit2 allows you to provide custom ``backends'' for several types of operation, so you can store things in a different way than stock Git does.
116+
Libgit2 allows custom backends for configuration, ref storage, and the object database, among other things.
117+
118+
Let's take a look at how this works.
119+
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+
122+
[source,c]
123+
----
124+
git_odb *odb;
125+
int error = git_odb_new(&odb); // <1>
126+
127+
git_repository *repo;
128+
error = git_repository_wrap_odb(&repo, odb); // <2>
129+
130+
git_odb_backend *my_backend;
131+
error = git_odb_backend_mine(&my_backend, repo, …); // <3>
132+
133+
error = git_odb_add_backendodb, my_backend, 1); // <4>
134+
----
135+
136+
_Note that error handling has been elided for this example. You should know better._
137+
138+
<1> Initialize an empty object database (ODB) ``frontend,'' which will act as a handle to the real ODB.
139+
<2> Construct a `git_repository` around the empty ODB.
140+
<3> Initialize a custom ODB backend.
141+
<4> Set the repository to use the custom backend for its ODB.
142+
143+
// TODO
113144

114145
==== What Libgit2 Can't Do
115146

0 commit comments

Comments
 (0)