Daniel Stephens
Daniel Stephens

Reputation: 3219

How to add a custom object database to libgit2?

I try to understand how to implement a custom object database in libgit2. As the main entry point I already checked out the example repository called libgit2-backends which implements examples for memcached, mysql, redis and sqlite3.

But I still don't understand how to plug these into libgit2? Am I plugging in a shared library that libgit2 can load? Or do I compile libgit2 from scratch with the corresponding backend sources? What is the scope of such backend? Any insights are highly appreciated!

Motivation: By default git and libgit packs/compresses objects. In my case I would like to implement a backend that does not do that. (Yes, there is LFS, but I try to find a libgit2-only solution)

Upvotes: 7

Views: 797

Answers (1)

J. M. Arnold
J. M. Arnold

Reputation: 6809

You're able to achieve that with git_odb_open. It's part of the official libgit2 API.

Basically, git_odb_open "[c]reate[s] a new object database and automatically add[s] the two default backends" (quote taken from here). If you take a look at the ligbit2 GitHub repository, you can find the GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir); within odb.h (in general, I suggest to check out all the odb functions). Anyhow, the comments below the function are extremely useful:

/**
 * Add a custom backend to an existing Object DB
 *
 * The backends are checked in relative ordering, based on the
 * value of the `priority` parameter.
 *
 * Read <odb_backends.h> for more information.
 *
 * @param odb database to add the backend to
 * @param backend pointer to a git_odb_backend instance
 * @param priority Value for ordering the backends queue
 * @return 0 on success; error code otherwise
 */

Kinda unfortunate that those notes aren't included on the standard documentation. Anyway, here you can see the important @param for a backend pointer to the instance itself. On a slight off-topic note, I'd suggest checking out the code of odb_backend.h as well (fortunately, ligbit2 is very well documented).

By the way (for the sake of completeness), I should mention you also can plug a custom backend into an already existing odb via git_odb_add_backend (obviously you got the pointer there as well), though it requires you to have an existing database to begin with . In case you're looking add an alternative to already-existing backends, i.e. in your case one one that doesn't compresses objects, you may go with git_odb_add_alternate.

Upvotes: 7

Related Questions