| 1 | # ABSTRACT: Low-level FFI bindings to libgit2 | ||
| 2 | |||
| 3 | package Git::Libgit2; | ||
| 4 | our $VERSION = '0.008'; | ||
| 5 | 31 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 31 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 31 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 31 | ▶ | use FFI::Platypus::Buffer qw( scalar_to_buffer ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Git::Libgit2::FFI (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Git::Libgit2::Error (); | ||
| 11 | use Exporter 'import'; | ||
| 12 | |||
| 13 | our @EXPORT_OK = qw( | ||
| 14 | init_lib | ||
| 15 | shutdown_lib | ||
| 16 | version | ||
| 17 | check_rc | ||
| 18 | oid_from_hex | ||
| 19 | oid_to_hex | ||
| 20 | fetch_options_prune_offset | ||
| 21 | cert_hostkey_offsets | ||
| 22 | |||
| 23 | GIT_OBJECT_ANY | ||
| 24 | GIT_OBJECT_INVALID | ||
| 25 | GIT_OBJECT_COMMIT | ||
| 26 | GIT_OBJECT_TREE | ||
| 27 | GIT_OBJECT_BLOB | ||
| 28 | GIT_OBJECT_TAG | ||
| 29 | |||
| 30 | GIT_REPOSITORY_INIT_BARE | ||
| 31 | |||
| 32 | GIT_OID_RAWSZ | ||
| 33 | GIT_OID_HEXSZ | ||
| 34 | GIT_OID_MINPREFIXLEN | ||
| 35 | |||
| 36 | GIT_OPT_SET_SEARCH_PATH | ||
| 37 | GIT_OPT_SET_SERVER_CONNECT_TIMEOUT | ||
| 38 | GIT_OPT_SET_SERVER_TIMEOUT | ||
| 39 | GIT_CONFIG_LEVEL_PROGRAMDATA | ||
| 40 | GIT_CONFIG_LEVEL_SYSTEM | ||
| 41 | GIT_CONFIG_LEVEL_XDG | ||
| 42 | GIT_CONFIG_LEVEL_GLOBAL | ||
| 43 | GIT_CONFIG_LEVEL_LOCAL | ||
| 44 | GIT_CONFIG_LEVEL_APP | ||
| 45 | GIT_CONFIG_HIGHEST_LEVEL | ||
| 46 | |||
| 47 | GIT_SORT_NONE | ||
| 48 | GIT_SORT_TOPOLOGICAL | ||
| 49 | GIT_SORT_TIME | ||
| 50 | GIT_SORT_REVERSE | ||
| 51 | GIT_DIRECTION_FETCH | ||
| 52 | GIT_DIRECTION_PUSH | ||
| 53 | GIT_BRANCH_LOCAL | ||
| 54 | GIT_BRANCH_REMOTE | ||
| 55 | GIT_BRANCH_ALL | ||
| 56 | GIT_FILEMODE_UNREADABLE | ||
| 57 | GIT_FILEMODE_TREE | ||
| 58 | GIT_FILEMODE_BLOB | ||
| 59 | GIT_FILEMODE_BLOB_EXECUTABLE | ||
| 60 | GIT_FILEMODE_LINK | ||
| 61 | GIT_FILEMODE_COMMIT | ||
| 62 | GIT_STATUS_CURRENT | ||
| 63 | GIT_STATUS_INDEX_NEW | ||
| 64 | GIT_STATUS_INDEX_MODIFIED | ||
| 65 | GIT_STATUS_INDEX_DELETED | ||
| 66 | GIT_STATUS_INDEX_RENAMED | ||
| 67 | GIT_STATUS_INDEX_TYPECHANGE | ||
| 68 | GIT_STATUS_WT_NEW | ||
| 69 | GIT_STATUS_WT_MODIFIED | ||
| 70 | GIT_STATUS_WT_DELETED | ||
| 71 | GIT_STATUS_WT_TYPECHANGE | ||
| 72 | GIT_STATUS_WT_RENAMED | ||
| 73 | GIT_STATUS_WT_UNREADABLE | ||
| 74 | GIT_STATUS_IGNORED | ||
| 75 | GIT_STATUS_CONFLICTED | ||
| 76 | |||
| 77 | GIT_OK | ||
| 78 | GIT_ERROR | ||
| 79 | GIT_ENOTFOUND | ||
| 80 | GIT_EEXISTS | ||
| 81 | GIT_EAMBIGUOUS | ||
| 82 | GIT_EBUFS | ||
| 83 | GIT_EUSER | ||
| 84 | GIT_EBAREREPO | ||
| 85 | GIT_EUNBORNBRANCH | ||
| 86 | GIT_EUNMERGED | ||
| 87 | GIT_ENONFASTFORWARD | ||
| 88 | GIT_EINVALIDSPEC | ||
| 89 | GIT_ECONFLICT | ||
| 90 | GIT_ELOCKED | ||
| 91 | GIT_EMODIFIED | ||
| 92 | GIT_EAUTH | ||
| 93 | GIT_ECERTIFICATE | ||
| 94 | GIT_EAPPLIED | ||
| 95 | GIT_EPEEL | ||
| 96 | GIT_EEOF | ||
| 97 | GIT_EINVALID | ||
| 98 | GIT_EUNCOMMITTED | ||
| 99 | GIT_EDIRECTORY | ||
| 100 | GIT_EMERGECONFLICT | ||
| 101 | GIT_PASSTHROUGH | ||
| 102 | GIT_ITEROVER | ||
| 103 | GIT_RETRY | ||
| 104 | GIT_EMISMATCH | ||
| 105 | GIT_EINDEXDIRTY | ||
| 106 | GIT_EAPPLYFAIL | ||
| 107 | GIT_EOWNER | ||
| 108 | GIT_TIMEOUT | ||
| 109 | GIT_EUNCHANGED | ||
| 110 | GIT_ENOTSUPPORTED | ||
| 111 | GIT_EREADONLY | ||
| 112 | ); | ||
| 113 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | ||
| 114 | |||
| 115 | use constant { | ||
| 116 | GIT_OBJECT_ANY => -2, | ||
| 117 | GIT_OBJECT_INVALID => -1, | ||
| 118 | GIT_OBJECT_COMMIT => 1, | ||
| 119 | GIT_OBJECT_TREE => 2, | ||
| 120 | GIT_OBJECT_BLOB => 3, | ||
| 121 | GIT_OBJECT_TAG => 4, | ||
| 122 | |||
| 123 | GIT_REPOSITORY_INIT_BARE => 1 << 0, | ||
| 124 | |||
| 125 | GIT_OID_RAWSZ => 20, | ||
| 126 | GIT_OID_HEXSZ => 40, | ||
| 127 | GIT_OID_MINPREFIXLEN => 4, | ||
| 128 | |||
| 129 | # git_libgit2_opt_t (include/git2/common.h). Unnumbered enum, so every value | ||
| 130 | # is positional — only options whose vararg shape one of the two | ||
| 131 | # git_libgit2_opts bindings can express are exported. Add siblings on demand. | ||
| 132 | GIT_OPT_SET_SEARCH_PATH => 5, | ||
| 133 | |||
| 134 | # opts(GIT_OPT_SET_SERVER_CONNECT_TIMEOUT, int timeout_in_milliseconds) and | ||
| 135 | # opts(GIT_OPT_SET_SERVER_TIMEOUT, int timeout_in_milliseconds): how long | ||
| 136 | # libgit2 waits to establish a connection to a remote, and how long it waits | ||
| 137 | # on any single read from or write to one. Both default to 0, which means no | ||
| 138 | # limit at all — a peer that accepts the connection and then says nothing | ||
| 139 | # hangs the process forever, and no callback fires in the meantime. | ||
| 140 | # | ||
| 141 | # Both were appended to the enum in libgit2 1.8, and the ssh transport | ||
| 142 | # ignored the server timeout until 1.9.3; against an older library | ||
| 143 | # these are simply out of range and git_libgit2_opts returns -1 ("invalid | ||
| 144 | # option") without acting. | ||
| 145 | GIT_OPT_SET_SERVER_CONNECT_TIMEOUT => 39, | ||
| 146 | GIT_OPT_SET_SERVER_TIMEOUT => 41, | ||
| 147 | |||
| 148 | # git_config_level_t (include/git2/config.h). | ||
| 149 | GIT_CONFIG_LEVEL_PROGRAMDATA => 1, | ||
| 150 | GIT_CONFIG_LEVEL_SYSTEM => 2, | ||
| 151 | GIT_CONFIG_LEVEL_XDG => 3, | ||
| 152 | GIT_CONFIG_LEVEL_GLOBAL => 4, | ||
| 153 | GIT_CONFIG_LEVEL_LOCAL => 5, | ||
| 154 | GIT_CONFIG_LEVEL_APP => 6, | ||
| 155 | GIT_CONFIG_HIGHEST_LEVEL => -1, | ||
| 156 | |||
| 157 | # git_sort_t (include/git2/revwalk.h). | ||
| 158 | GIT_SORT_NONE => 0, | ||
| 159 | GIT_SORT_TOPOLOGICAL => 1 << 0, | ||
| 160 | GIT_SORT_TIME => 1 << 1, | ||
| 161 | GIT_SORT_REVERSE => 1 << 2, | ||
| 162 | |||
| 163 | # git_direction (include/git2/net.h). | ||
| 164 | GIT_DIRECTION_FETCH => 0, | ||
| 165 | GIT_DIRECTION_PUSH => 1, | ||
| 166 | |||
| 167 | # git_branch_t (include/git2/types.h). ALL = LOCAL | REMOTE. | ||
| 168 | GIT_BRANCH_LOCAL => 1, | ||
| 169 | GIT_BRANCH_REMOTE => 2, | ||
| 170 | GIT_BRANCH_ALL => 3, | ||
| 171 | |||
| 172 | # git_filemode_t (include/git2/types.h). Octal literals — Perl octal | ||
| 173 | # matches the C enum values (e.g. 0100644 == 33188 decimal). | ||
| 174 | GIT_FILEMODE_UNREADABLE => 0, | ||
| 175 | GIT_FILEMODE_TREE => 0040000, | ||
| 176 | GIT_FILEMODE_BLOB => 0100644, | ||
| 177 | GIT_FILEMODE_BLOB_EXECUTABLE => 0100755, | ||
| 178 | GIT_FILEMODE_LINK => 0120000, | ||
| 179 | GIT_FILEMODE_COMMIT => 0160000, | ||
| 180 | |||
| 181 | # git_status_t (include/git2/status.h). | ||
| 182 | GIT_STATUS_CURRENT => 0, | ||
| 183 | GIT_STATUS_INDEX_NEW => 1 << 0, | ||
| 184 | GIT_STATUS_INDEX_MODIFIED => 1 << 1, | ||
| 185 | GIT_STATUS_INDEX_DELETED => 1 << 2, | ||
| 186 | GIT_STATUS_INDEX_RENAMED => 1 << 3, | ||
| 187 | GIT_STATUS_INDEX_TYPECHANGE => 1 << 4, | ||
| 188 | GIT_STATUS_WT_NEW => 1 << 7, | ||
| 189 | GIT_STATUS_WT_MODIFIED => 1 << 8, | ||
| 190 | GIT_STATUS_WT_DELETED => 1 << 9, | ||
| 191 | GIT_STATUS_WT_TYPECHANGE => 1 << 10, | ||
| 192 | GIT_STATUS_WT_RENAMED => 1 << 11, | ||
| 193 | GIT_STATUS_WT_UNREADABLE => 1 << 12, | ||
| 194 | GIT_STATUS_IGNORED => 1 << 14, | ||
| 195 | GIT_STATUS_CONFLICTED => 1 << 15, | ||
| 196 | |||
| 197 | # git_error_code enum (include/git2/errors.h). Stable values across | ||
| 198 | # libgit2 1.x. GIT_PASSTHROUGH / GIT_ITEROVER are control codes, not | ||
| 199 | # failures, but live in the same enum. | ||
| 200 | GIT_OK => 0, | ||
| 201 | GIT_ERROR => -1, | ||
| 202 | GIT_ENOTFOUND => -3, | ||
| 203 | GIT_EEXISTS => -4, | ||
| 204 | GIT_EAMBIGUOUS => -5, | ||
| 205 | GIT_EBUFS => -6, | ||
| 206 | GIT_EUSER => -7, | ||
| 207 | GIT_EBAREREPO => -8, | ||
| 208 | GIT_EUNBORNBRANCH => -9, | ||
| 209 | GIT_EUNMERGED => -10, | ||
| 210 | GIT_ENONFASTFORWARD => -11, | ||
| 211 | GIT_EINVALIDSPEC => -12, | ||
| 212 | GIT_ECONFLICT => -13, | ||
| 213 | GIT_ELOCKED => -14, | ||
| 214 | GIT_EMODIFIED => -15, | ||
| 215 | GIT_EAUTH => -16, | ||
| 216 | GIT_ECERTIFICATE => -17, | ||
| 217 | GIT_EAPPLIED => -18, | ||
| 218 | GIT_EPEEL => -19, | ||
| 219 | GIT_EEOF => -20, | ||
| 220 | GIT_EINVALID => -21, | ||
| 221 | GIT_EUNCOMMITTED => -22, | ||
| 222 | GIT_EDIRECTORY => -23, | ||
| 223 | GIT_EMERGECONFLICT => -24, | ||
| 224 | GIT_PASSTHROUGH => -30, | ||
| 225 | GIT_ITEROVER => -31, | ||
| 226 | GIT_RETRY => -32, | ||
| 227 | GIT_EMISMATCH => -33, | ||
| 228 | GIT_EINDEXDIRTY => -34, | ||
| 229 | GIT_EAPPLYFAIL => -35, | ||
| 230 | GIT_EOWNER => -36, | ||
| 231 | GIT_TIMEOUT => -37, | ||
| 232 | GIT_EUNCHANGED => -38, | ||
| 233 | GIT_ENOTSUPPORTED => -39, | ||
| 234 | GIT_EREADONLY => -40, | ||
| 235 | }; | ||
| 236 | |||
| 237 | my $initialised = 0; | ||
| 238 | |||
| 239 | sub init_lib { | ||
| 240 | Git::Libgit2::FFI::ffi(); | ||
| 241 | my $rc = Git::Libgit2::FFI::git_libgit2_init(); | ||
| 242 | Carp::croak "git_libgit2_init failed (rc=$rc)" if $rc < 1; | ||
| 243 | $initialised = $rc; | ||
| 244 | return $rc; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | sub shutdown_lib { | ||
| 249 | return 0 unless $initialised; | ||
| 250 | my $rc = Git::Libgit2::FFI::git_libgit2_shutdown(); | ||
| 251 | $initialised = $rc; | ||
| 252 | return $rc; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | sub version { | ||
| 257 | Git::Libgit2::FFI::ffi(); | ||
| 258 | my ( $maj, $min, $rev ); | ||
| 259 | Git::Libgit2::FFI::git_libgit2_version( \$maj, \$min, \$rev ); | ||
| 260 | return wantarray ? ( $maj, $min, $rev ) : "$maj.$min.$rev"; | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | sub check_rc { | ||
| 265 | my ($rc) = @_; | ||
| 266 | return $rc if $rc >= 0; | ||
| 267 | die Git::Libgit2::Error->last($rc); | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | sub oid_from_hex { | ||
| 272 | my ($hex) = @_; | ||
| 273 | Carp::croak "oid_from_hex: expected 40-char hex, got '$hex'" | ||
| 274 | unless defined $hex && length($hex) == GIT_OID_HEXSZ && $hex =~ /\A[0-9a-fA-F]{40}\z/; | ||
| 275 | my $raw = "\0" x GIT_OID_RAWSZ; | ||
| 276 | my ($ptr) = scalar_to_buffer($raw); | ||
| 277 | check_rc Git::Libgit2::FFI::git_oid_fromstr( $ptr, $hex ); | ||
| 278 | return $raw; | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub oid_to_hex { | ||
| 283 | my ($oid_ptr) = @_; | ||
| 284 | my $buf = "\0" x ( GIT_OID_HEXSZ + 1 ); | ||
| 285 | my ($bufp) = scalar_to_buffer($buf); | ||
| 286 | Git::Libgit2::FFI::git_oid_tostr( $bufp, GIT_OID_HEXSZ + 1, $oid_ptr ); | ||
| 287 | $buf =~ s/\0.*//s; | ||
| 288 | return $buf; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | sub cert_hostkey_offsets { | ||
| 293 | # git_cert_hostkey is { git_cert parent; git_cert_ssh_t type; unsigned | ||
| 294 | # char hash_md5[16]; hash_sha1[20]; hash_sha256[32]; ... }. git_cert is | ||
| 295 | # a lone enum, git_cert_ssh_t is an enum, and enums are int-sized on | ||
| 296 | # every ABI libgit2 supports; the hash fields are char arrays with no | ||
| 297 | # alignment demands of their own. The layout up to the end of | ||
| 298 | # hash_sha256 is therefore padding-free and follows from sizeof(int) | ||
| 299 | # alone -- unlike git_fetch_options.prune there is nothing here that | ||
| 300 | # moves between libgit2 releases, and libgit2 has no | ||
| 301 | # git_cert_hostkey_init that would give a marker to probe for. | ||
| 302 | # Cross-checked against the real offsetof() by Git::Native's | ||
| 303 | # t/75-cert-hostkey-layout.t wherever a C compiler is available. | ||
| 304 | my $int = Git::Libgit2::FFI::ffi()->sizeof('int'); | ||
| 305 | return ( | ||
| 306 | type => $int, | ||
| 307 | sha1 => $int + $int + 16, | ||
| 308 | sha256 => $int + $int + 16 + 20, | ||
| 309 | ); | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 313 | my $FETCH_OPTIONS_PRUNE_OFFSET; | ||
| 314 | |||
| 315 | sub fetch_options_prune_offset { | ||
| 316 | return $FETCH_OPTIONS_PRUNE_OFFSET if defined $FETCH_OPTIONS_PRUNE_OFFSET; | ||
| 317 | |||
| 318 | # git_fetch_options is { int version; git_remote_callbacks callbacks; | ||
| 319 | # git_fetch_prune_t prune; unsigned int update_fetchhead; ... }. The embedded | ||
| 320 | # callbacks struct gained fields between releases -- 120 bytes in 1.5, 128 in | ||
| 321 | # 1.9 -- so prune moves with it, and a hardcoded offset does not fail loudly: | ||
| 322 | # it writes the prune value into whichever callback pointer took its place. | ||
| 323 | # | ||
| 324 | # git_fetch_options_init fills the struct with libgit2's own defaults, which | ||
| 325 | # give us a marker to find: everything in callbacks past its version field is | ||
| 326 | # a NULL pointer, prune is GIT_FETCH_PRUNE_UNSPECIFIED (0), and the field | ||
| 327 | # directly behind it, update_fetchhead, is GIT_REMOTE_UPDATE_FETCHHEAD (1). | ||
| 328 | # So the first non-zero int past the callbacks version field is | ||
| 329 | # update_fetchhead, and prune is the int in front of it. | ||
| 330 | my $size = 512; | ||
| 331 | my $buf = "\0" x $size; | ||
| 332 | my $ptr = _scalar_ptr($buf); | ||
| 333 | check_rc( Git::Libgit2::FFI::git_fetch_options_init( $ptr, 1 ) ); # 1 = GIT_FETCH_OPTIONS_VERSION | ||
| 334 | |||
| 335 | for ( my $off = 16 ; $off + 4 <= $size ; $off += 4 ) { | ||
| 336 | next unless unpack( 'l', substr( $buf, $off, 4 ) ) == 1; | ||
| 337 | return $FETCH_OPTIONS_PRUNE_OFFSET = $off - 4; | ||
| 338 | } | ||
| 339 | |||
| 340 | Carp::croak 'cannot locate git_fetch_options.prune: libgit2 ' | ||
| 341 | . version() | ||
| 342 | . ' left update_fetchhead unset after git_fetch_options_init'; | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | # Get a raw pointer to a Perl scalar's bytes (for passing as 'opaque'). | ||
| 348 | sub _scalar_ptr { | ||
| 349 | my ($p) = scalar_to_buffer($_[0]); | ||
| 350 | return $p; | ||
| 351 | } | ||
| 352 | |||
| 353 | 1; | ||
| 354 | |||
| 355 | __END__ | ||