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)
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. Libgit2 allows custom backends for configuration, ref storage, and the object database, among other things. Let’s take a look at how this works.
Libgit2 is developed with CMake, and this will be the easiest way to build a binary from the source. Once you have CMake installed, performing a libgit2 build is fairly simple. In a shell session with the libgit2 root directory as the cwd, simply do this:
Rugged (and Libgit2) can optionally update a reference when making a commit. The return value is the SHA-1 hash of a new commit object, which you can then use to get a Commit object. The Ruby code is nice and clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
Your Visual Studio project will need to know where to find the libgit2 headers and link libraries. Open up the project settings by clicking on the project in the Solution Explorer, and selecting Properties. Make sure you’re configuring the proper build configuration:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With