libgit2pp
C++ convenience wrappers for libgit2. libgit2pp is a single header file, git2pp.h, to include in lieu of git2.h. Put it wherever suits your project.
To keep the code base small and future-proof, the library uses metaprogramming techniques to avoid replicating the entire API, while still allowing much more succinct code than raw libgit2.
Documentation
-
namespace
git2pp-
Error/check: check() throws Error when a libgit2 result indicates failure. -
Session: wrapsgit_libgit2_init()andgit_libgit2_shutdown()in RAII.-
operator[](): Wraps a libgit2 function. If the wrapped function has aT * *as its first parameter, the returned function removes this parameter and instead returns aUniquePtr<T>(see below).// libgit2 git_libgit2_init(); git_repository * repo = NULL; int err = git_repository_open(&repo, "."); if (err < 0) { … } … git_repository_free(repo); git_libgit2_shutdown();
// git2pp git2pp::Session git2; auto repo = git2[git_repository_open]("."); // Exception on failure.
-
-
UniquePtr<T>: Smart pointer wrapping a libgit2 pointer.-
ctor/operator=(UniquePtr &&): Transfers ownership. -
ctor/operator=(UniquePtr const &): Copies usinggit_…_dupfunction, where such a function exists. -
operator[](): Wraps libgit2 functions with sigs(T [const] *, …)or(U * *, T [const] *, …), whereUis some other output type. The resulting function removes theT [const] *parameter, instead passingthisbehind the scenes. It also removesU * *and instead returnsUniquePtr<U>.// libgit2 git_reference *ref = NULL; int err = git_reference_dwim(&ref, "master"); if (err < 0) { … } … git_reference_free(ref);
// git2pp. repo[…] returns a function that takes a char const *, calls // git_reference_dwim, and returns a UniquePtr<git_reference>. auto master = repo[git_reference_dwim]("master");
-
as<U>(): Casts one libgit2 object type to another. Ifthisis an rvalue reference, transfers ownership to a newUniquePtr<U>, otherwise returns a rawU *. WARNING: Will succeed for any pair of types, whether it's valid or not.
-
-
std::ostream & operator<<(std::ostream & os, git_oid const * oid): Outputs the hex representation ofoid.
-
If a libgit2 function takes additional pointer-to-T parameters and you have
a UniquePtr<T>, t, you can pass it in as &*t, e.g.:
auto me = repo[git_signature_now]("Me", "me@example.com");
commit[git_commit_amend](…, &*me, &*me, …);Example
Before
bool git2ok(int rc) {
if (rc >= 0) {
return true;
}
const git_error *e = giterr_last();
fprintf(stderr, "Error %d/%d: %s\n", rc, e->klass, e->message);
return false;
}
bool show_commit(char const * branch) {
git_libgit2_init();
git_repository * repo = NULL;
git_reference * master = NULL;
git_object * commit_obj = NULL;
git_commit * commit = NULL;
bool ok;
if (!(ok = git2ok(git_repository_open(&repo, ".")))) {
goto fail;
}
if (!(ok = git2ok(git_reference_dwim(&master, repo, branch)))) {
goto fail;
}
if (!(ok = git2ok(git_reference_peel(&commit_obj, master, GIT_OBJ_COMMIT)))) {
goto fail;
}
commit = (git_commit *)commit_obj;
std::cout << "master = " << git_oid_tostr_s(git_commit_id(commit)) << "\n";
std::cout << "author = " << git_commit_author(commit)->name << "\n";
std::cout << "message = " << git_commit_message(commit) << "\n";
fail:
git_object_free(commit_obj);
git_repository_free(repo);
git_libgit2_shutdown();
return ok;
}After
void show_commit(char const * branch) {
git2pp::Session git2;
auto repo = git2[git_repository_open](".");
auto master = repo[git_reference_dwim](branch);
auto commit = master[git_reference_peel](GIT_OBJ_COMMIT).as<git_commit>();
std::cout << "master = " << commit[git_commit_id]() << "\n";
std::cout << "author = " << commit[git_commit_author]()->name << "\n";
std::cout << "message = " << commit[git_commit_message]() << "\n";
}A more complete working demo may be found in demo.