Skip to content

Commit d9db713

Browse files
committed
More custom ODB backend content
1 parent 659ef40 commit d9db713

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ git_repository *repo;
128128
error = git_repository_wrap_odb(&repo, odb); // <2>
129129
130130
git_odb_backend *my_backend;
131-
error = git_odb_backend_mine(&my_backend, repo, …); // <3>
131+
error = git_odb_backend_mine(&my_backend, /*…*/); // <3>
132132
133133
error = git_odb_add_backendodb, my_backend, 1); // <4>
134134
----
@@ -140,7 +140,42 @@ _Note that error handling has been left out for this example. You should know be
140140
<3> Initialize a custom ODB backend.
141141
<4> Set the repository to use the custom backend for its ODB.
142142

143-
// TODO
143+
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+
Here's what it _could_ look like:
146+
147+
[source,c]
148+
----
149+
typedef struct {
150+
git_odb_backend parent;
151+
152+
// Some other stuff
153+
void *custom_context;
154+
} my_backend_struct;
155+
156+
int git_odb_backend_mine(git_odb_backend **backend_out, /*…*/)
157+
{
158+
my_backend_struct *backend;
159+
160+
backend = calloc(1, sizeof (my_backend_struct));
161+
162+
backend->custom_context = …;
163+
164+
backend->parent.read = &my_backend__read;
165+
backend->parent.read_prefix = &my_backend__read_prefix;
166+
backend->parent.read_header = &my_backend__read_header;
167+
// …
168+
169+
*backend_out = (git_odb_backend *) backend;
170+
171+
return GIT_SUCCESS;
172+
}
173+
----
174+
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 rest of it is arbitrary; this structure can be as large or small as you need it to be.
177+
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+
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.
144179

145180
==== Other Bindings
146181

0 commit comments

Comments
 (0)