You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use Git::Libgit2 qw( init_lib shutdown_lib version check_rc );
init_lib();
printf"libgit2 %s\n", version();
# Work directly with the FFI layeruse Git::Libgit2::FFI;
check_rc Git::Libgit2::FFI::git_repository_open(\my$repo, '/path/to/repo/.git');
check_rc Git::Libgit2::FFI::git_repository_free($repo);
shutdown_lib();
Description
Git::Libgit2 provides Perl-level access to the libgit2 C library through
FFI — no XS, no compiler needed at install time. It is intentionally a thin
surface over the C API: opaque handles, return codes, and manual memory
management are all exposed as they would be in C.
For an idiomatic Perl wrapper with automatic resource cleanup, see
Git::Native — it wraps this module
with Moo objects.
This module is the bindings layer. The two main namespaces are:
Git::Libgit2 — top-level facade with init_lib, shutdown_lib, version,
check_rc, oid_from_hex, oid_to_hex, and a handful of constants.
Git::Libgit2::FFI — the internal singleton that holds all attached libgit2
functions. Call it after init_lib.
Error handling
check_rc (from Git::Libgit2) passes return codes through on success, and
throws a Git::Libgit2::Error on failure:
check_rc Git::Libgit2::FFI::git_repository_open(\my$repo, $path);
# throws Git::Libgit2::Error if rc < 0
Git::Libgit2::Error stringifies to a message from git_error_last, carries
.code and .message accessors, and overloads "".
OID buffers
libgit2 represents object IDs as 20-byte raw binary. Perl code must pre-allocate
this buffer and keep it alive for the duration of any call that reads or writes
the OID:
my$buf = "\0"x20; # 20-byte OID buffermy ($ptr) = scalar_to_buffer($buf);
check_rc Git::Libgit2::FFI::git_blob_create_from_buffer($ptr, $repo, $content_ptr, length($content));
# — $buf must stay alive here —
oid_from_hex and oid_to_hex (from Git::Libgit2) convert between the
binary form and the 40-character hex string representation.
Handle lifetime
Every *_new / *_lookup call must be matched with its corresponding
*_free call. The bindings expose the raw handles; there is no RAII here.
See Git::Native for a wrapper that
handles this automatically.
Installation
cpanm Git::Libgit2
Requires a libgit2 shared library. Alien::Libgit2 will download and build one
automatically if one is not already installed on your system.
Functions
Top-level helpers (Git::Libgit2)
Function
Description
init_lib
Initialize libgit2 (reference-counted)
shutdown_lib
Decrement init count, returns remaining count
version
libgit2 version string or ($maj, $min, $rev) list
check_rc RC
Pass through if RC >= 0, else throw Git::Libgit2::Error