7

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)

4
  • "I would like to implement a backend that does not do that." . . . what's wrong with git config core.compression 0?
    – jthill
    Jan 9, 2021 at 22:48
  • Good idea but it doesn't seem to be supported by libgit2 Jan 10, 2021 at 2:33
  • You could try reaching on the libgit2 slack?
    – vanntile
    Jan 12, 2021 at 6:40
  • Also , in tge libgit2 API docs, the odb section might be helpful, especially the ` git_odb_backend_loose` function
    – vanntile
    Jan 12, 2021 at 7:03

1 Answer 1

7
+400

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.

1
  • Either @HelloWorld or Daniel Stephens, please let me know whether I've covered all of your questions and/or if there are any questions left! Hope I could help you out. Jan 15, 2021 at 22:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.