| 1 | # ABSTRACT: A libgit2 repository handle | ||
| 2 | |||
| 3 | package Git::Native::Repository; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | use Moo; | ||
| 6 | use Carp (); | ||
| 7 | use Git::Libgit2 qw( | ||
| 8 | GIT_EMODIFIED GIT_ENOTFOUND GIT_EUNBORNBRANCH GIT_ITEROVER | ||
| 9 | GIT_OBJECT_ANY GIT_OBJECT_BLOB GIT_OBJECT_TREE | ||
| 10 | GIT_OBJECT_COMMIT GIT_OBJECT_TAG | ||
| 11 | GIT_OID_MINPREFIXLEN | ||
| 12 | ); | ||
| 13 | use Git::Libgit2::FFI (); | ||
| 14 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 15 | use Git::Native::Error qw( check_rc ); | ||
| 16 | use Git::Native::Reference (); | ||
| 17 | use Git::Native::Blob (); | ||
| 18 | use Git::Native::Tree (); | ||
| 19 | use Git::Native::TreeBuilder (); | ||
| 20 | use Git::Native::Commit (); | ||
| 21 | use Git::Native::Config (); | ||
| 22 | use Git::Native::Index (); | ||
| 23 | use Git::Native::Signature (); | ||
| 24 | use Git::Native::Oid (); | ||
| 25 | use Git::Native::Remote (); | ||
| 26 | use Git::Native::Revwalker (); | ||
| 27 | use Git::Native::Branch (); | ||
| 28 | use Git::Native::Tag (); | ||
| 29 | use FFI::Platypus 2.00 (); | ||
| 30 | |||
| 31 | has _handle => ( is => 'ro', required => 1 ); | ||
| 32 | |||
| 33 | sub workdir { Git::Libgit2::FFI::git_repository_workdir( $_[0]->_handle ) } | ||
| 34 | sub gitdir { Git::Libgit2::FFI::git_repository_path( $_[0]->_handle ) } | ||
| 35 | sub is_bare { Git::Libgit2::FFI::git_repository_is_bare( $_[0]->_handle ) ? 1 : 0 } | ||
| 36 | |||
| 37 | # ---------- references ---------- | ||
| 38 | |||
| 39 | sub reference { | ||
| 40 | my ( $self, $name ) = @_; | ||
| 41 | check_rc Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $name ); | ||
| 42 | return Git::Native::Reference->new( _handle => $ref, _owner => $self ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub reference_create { | ||
| 46 | my ( $self, $name, $oid, %opts ) = @_; | ||
| 47 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 48 | |||
| 49 | my $ref; | ||
| 50 | if ( exists $opts{expected_old} ) { | ||
| 51 | my $expected = defined $opts{expected_old} | ||
| 52 | ? $opts{expected_old} | ||
| 53 | : Git::Native::Oid->from_raw( "\0" x 20 ); | ||
| 54 | $expected = Git::Native::Oid->from_hex($expected) if !ref $expected; | ||
| 55 | # expected_old is the overwrite guard, so force must be true: force false | ||
| 56 | # returns GIT_EEXISTS before libgit2 can compare the expected OID. | ||
| 57 | check_rc Git::Libgit2::FFI::git_reference_create_matching( | ||
| 58 | \$ref, $self->_handle, $name, $oid->ptr, | ||
| 59 | 1, | ||
| 60 | $expected->ptr, | ||
| 61 | $opts{message} // '', | ||
| 62 | ); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | check_rc Git::Libgit2::FFI::git_reference_create( | ||
| 66 | \$ref, $self->_handle, $name, $oid->ptr, | ||
| 67 | $opts{force} ? 1 : 0, | ||
| 68 | $opts{message} // '', | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | return Git::Native::Reference->new( _handle => $ref, _owner => $self ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub reference_set_target { | ||
| 75 | my ( $self, $name, $oid, %opts ) = @_; | ||
| 76 | Carp::croak 'reference_set_target requires expected_old' | ||
| 77 | unless exists $opts{expected_old} && defined $opts{expected_old}; | ||
| 78 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 79 | my $expected = $opts{expected_old}; | ||
| 80 | $expected = Git::Native::Oid->from_hex($expected) if !ref $expected; | ||
| 81 | |||
| 82 | my $current = $self->reference($name); | ||
| 83 | my $actual = $current->target; | ||
| 84 | Carp::croak "reference_set_target requires a direct reference: $name" | ||
| 85 | unless $actual; | ||
| 86 | if ( $actual->hex ne $expected->hex ) { | ||
| 87 | Git::Native::Error->throw( | ||
| 88 | code => GIT_EMODIFIED, | ||
| 89 | message => "reference '$name' does not match expected old OID", | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | return $current->set_target( $oid, message => $opts{message} // '' ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub reference_delete { | ||
| 97 | my ( $self, $name ) = @_; | ||
| 98 | check_rc Git::Libgit2::FFI::git_reference_remove( $self->_handle, $name ); | ||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub reference_exists { | ||
| 103 | my ( $self, $name ) = @_; | ||
| 104 | my $rc = Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $name ); | ||
| 105 | if ( $rc == 0 ) { | ||
| 106 | Git::Libgit2::FFI::git_reference_free($ref); | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | return 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | # Returns list of full ref names. Optional `glob` filters libgit2-side. | ||
| 113 | sub reference_names { | ||
| 114 | my ( $self, %opts ) = @_; | ||
| 115 | my $iter; | ||
| 116 | if ( $opts{glob} ) { | ||
| 117 | check_rc Git::Libgit2::FFI::git_reference_iterator_glob_new( | ||
| 118 | \$iter, $self->_handle, $opts{glob}, | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | check_rc Git::Libgit2::FFI::git_reference_iterator_new( \$iter, $self->_handle ); | ||
| 123 | } | ||
| 124 | my @names; | ||
| 125 | while (1) { | ||
| 126 | my $rc = Git::Libgit2::FFI::git_reference_next_name( \my $name, $iter ); | ||
| 127 | last if $rc == GIT_ITEROVER; | ||
| 128 | check_rc $rc; | ||
| 129 | push @names, $name; | ||
| 130 | } | ||
| 131 | Git::Libgit2::FFI::git_reference_iterator_free($iter); | ||
| 132 | return \@names; | ||
| 133 | } | ||
| 134 | |||
| 135 | # Resolved HEAD reference, or undef when HEAD is unborn / missing | ||
| 136 | # (fresh repo with no commits yet). | ||
| 137 | sub head { | ||
| 138 | my $self = shift; | ||
| 139 | my $rc = Git::Libgit2::FFI::git_repository_head( \my $ref, $self->_handle ); | ||
| 140 | return undef if $rc == GIT_EUNBORNBRANCH || $rc == GIT_ENOTFOUND; | ||
| 141 | check_rc $rc; | ||
| 142 | return Git::Native::Reference->new( _handle => $ref, _owner => $self ); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub head_unborn { | ||
| 146 | my $rc = Git::Libgit2::FFI::git_repository_head_unborn( $_[0]->_handle ); | ||
| 147 | check_rc $rc if $rc < 0; | ||
| 148 | return $rc ? 1 : 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub head_detached { | ||
| 152 | my $rc = Git::Libgit2::FFI::git_repository_head_detached( $_[0]->_handle ); | ||
| 153 | check_rc $rc if $rc < 0; | ||
| 154 | return $rc ? 1 : 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | # Point HEAD at a branch refname (e.g. 'refs/heads/main'). The branch may | ||
| 158 | # be unborn - this is how you pin 'main' on a freshly init'd repo. | ||
| 159 | sub set_head { | ||
| 160 | my ( $self, $refname ) = @_; | ||
| 161 | check_rc Git::Libgit2::FFI::git_repository_set_head( $self->_handle, $refname ); | ||
| 162 | return $self; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub reference_symbolic_create { | ||
| 166 | my ( $self, $name, $target, %opts ) = @_; | ||
| 167 | check_rc Git::Libgit2::FFI::git_reference_symbolic_create( | ||
| 168 | \my $ref, $self->_handle, $name, $target, | ||
| 169 | $opts{force} ? 1 : 0, | ||
| 170 | $opts{message} // '', | ||
| 171 | ); | ||
| 172 | return Git::Native::Reference->new( _handle => $ref, _owner => $self ); | ||
| 173 | } | ||
| 174 | |||
| 175 | # ---------- blobs / trees / commits ---------- | ||
| 176 | |||
| 177 | sub blob_create_frombuffer { | ||
| 178 | my ( $self, $content ) = @_; | ||
| 179 | my $raw = "\0" x 20; | ||
| 180 | my ($oid_p) = scalar_to_buffer($raw); | ||
| 181 | my ($content_p) = scalar_to_buffer($content); | ||
| 182 | check_rc Git::Libgit2::FFI::git_blob_create_from_buffer( | ||
| 183 | $oid_p, $self->_handle, $content_p, length($content), | ||
| 184 | ); | ||
| 185 | return Git::Native::Oid->from_raw($raw); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub blob { | ||
| 189 | my ( $self, $oid ) = @_; | ||
| 190 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 191 | check_rc Git::Libgit2::FFI::git_blob_lookup( \my $b, $self->_handle, $oid->ptr ); | ||
| 192 | return Git::Native::Blob->new( _handle => $b, _owner => $self ); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub tree { | ||
| 196 | my ( $self, $oid ) = @_; | ||
| 197 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 198 | check_rc Git::Libgit2::FFI::git_tree_lookup( \my $t, $self->_handle, $oid->ptr ); | ||
| 199 | return Git::Native::Tree->new( _handle => $t, _owner => $self ); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub tree_builder { | ||
| 203 | my $self = shift; | ||
| 204 | check_rc Git::Libgit2::FFI::git_treebuilder_new( \my $tb, $self->_handle, undef ); | ||
| 205 | return Git::Native::TreeBuilder->new( _handle => $tb, _owner => $self ); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub commit { | ||
| 209 | my ( $self, $oid ) = @_; | ||
| 210 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 211 | check_rc Git::Libgit2::FFI::git_commit_lookup( \my $c, $self->_handle, $oid->ptr ); | ||
| 212 | return Git::Native::Commit->new( _handle => $c, _owner => $self ); | ||
| 213 | } | ||
| 214 | |||
| 215 | # object($oid): look up an object of unknown kind and return the matching | ||
| 216 | # typed wrapper (Blob / Tree / Commit / Tag). git_blob_free/git_commit_free/ | ||
| 217 | # git_tree_free/git_tag_free are all thin wrappers that call git_object_free | ||
| 218 | # (stable across libgit2 1.x), so it's safe to hold the git_object* handle in | ||
| 219 | # a typed wrapper whose DEMOLISH calls the type-specific free. | ||
| 220 | my %_OBJECT_WRAPPER = ( | ||
| 221 | GIT_OBJECT_BLOB() => 'Git::Native::Blob', | ||
| 222 | GIT_OBJECT_TREE() => 'Git::Native::Tree', | ||
| 223 | GIT_OBJECT_COMMIT() => 'Git::Native::Commit', | ||
| 224 | GIT_OBJECT_TAG() => 'Git::Native::Tag', | ||
| 225 | ); | ||
| 226 | |||
| 227 | sub object { | ||
| 228 | my ( $self, $oid ) = @_; | ||
| 229 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 230 | check_rc Git::Libgit2::FFI::git_object_lookup( | ||
| 231 | \my $obj, $self->_handle, $oid->ptr, GIT_OBJECT_ANY, | ||
| 232 | ); | ||
| 233 | return $self->_wrap_object( $obj, $oid ); | ||
| 234 | } | ||
| 235 | |||
| 236 | # object_by_prefix($hex): the `git rev-parse abc1234` lookup - resolve an | ||
| 237 | # abbreviated OID against this repository's object database. | ||
| 238 | # | ||
| 239 | # libgit2 takes the prefix in a full-width git_oid buffer plus a length in hex | ||
| 240 | # characters (nibbles, NOT bytes), so the prefix is zero-padded out to 40 and | ||
| 241 | # its original length passed alongside; libgit2 reads only that many nibbles. | ||
| 242 | # | ||
| 243 | # The minimum-length check is ours on purpose. libgit2 answers a prefix below | ||
| 244 | # GIT_OID_MINPREFIXLEN with GIT_EAMBIGUOUS ("ambiguous lookup - OID prefix is | ||
| 245 | # too short") - the same code a genuinely ambiguous prefix returns, so | ||
| 246 | # is_ambiguous could not tell "your code is wrong" from "ask the user for more | ||
| 247 | # characters". Croaking here keeps the two apart, same reasoning as | ||
| 248 | # commit_create's argument checks. | ||
| 249 | sub object_by_prefix { | ||
| 250 | my ( $self, $prefix ) = @_; | ||
| 251 | # An Oid is accepted as well; it stringifies to its full 40-char hex. | ||
| 252 | $prefix = "$prefix" if ref $prefix; | ||
| 253 | Carp::croak 'object_by_prefix requires a hex OID prefix of at most 40 characters' | ||
| 254 | unless defined $prefix && $prefix =~ /\A[0-9a-fA-F]{1,40}\z/; | ||
| 255 | Carp::croak sprintf | ||
| 256 | 'object_by_prefix: prefix %s is shorter than the %d characters libgit2 requires', | ||
| 257 | $prefix, GIT_OID_MINPREFIXLEN | ||
| 258 | if length($prefix) < GIT_OID_MINPREFIXLEN; | ||
| 259 | |||
| 260 | my $oid = Git::Native::Oid->from_hex( | ||
| 261 | $prefix . ( '0' x ( 40 - length $prefix ) ) ); | ||
| 262 | check_rc Git::Libgit2::FFI::git_object_lookup_prefix( | ||
| 263 | \my $obj, $self->_handle, $oid->ptr, length($prefix), GIT_OBJECT_ANY, | ||
| 264 | ); | ||
| 265 | return $self->_wrap_object( $obj, $prefix ); | ||
| 266 | } | ||
| 267 | |||
| 268 | # Shared tail of object() and object_by_prefix(): git_object* -> typed wrapper, | ||
| 269 | # freeing the handle if we have no wrapper for the type ($what is only for the | ||
| 270 | # message). | ||
| 271 | sub _wrap_object { | ||
| 272 | my ( $self, $obj, $what ) = @_; | ||
| 273 | my $type = Git::Libgit2::FFI::git_object_type($obj); | ||
| 274 | my $class = $_OBJECT_WRAPPER{$type}; | ||
| 275 | unless ($class) { | ||
| 276 | Git::Libgit2::FFI::git_object_free($obj); | ||
| 277 | Carp::croak "object: unexpected git object type $type for $what"; | ||
| 278 | } | ||
| 279 | return $class->new( _handle => $obj, _owner => $self ); | ||
| 280 | } | ||
| 281 | |||
| 282 | # commit_create(%args): tree => Oid|hex, parents => [Oid|hex, ...], | ||
| 283 | # message => str, update_ref => 'HEAD', author => Signature, committer => Signature | ||
| 284 | sub commit_create { | ||
| 285 | my ( $self, %args ) = @_; | ||
| 286 | |||
| 287 | # Check the required arguments before any FFI call: libgit2 reports a | ||
| 288 | # missing message as "invalid argument: 'string'" and a missing tree only | ||
| 289 | # surfaces inside Oid->from_hex, neither of which names the caller. | ||
| 290 | Carp::croak 'commit_create requires tree' unless defined $args{tree}; | ||
| 291 | Carp::croak 'commit_create requires message' unless defined $args{message}; | ||
| 292 | Carp::croak 'commit_create requires parents to be an arrayref' | ||
| 293 | if defined $args{parents} && ref $args{parents} ne 'ARRAY'; | ||
| 294 | |||
| 295 | my $tree_oid = $args{tree}; | ||
| 296 | $tree_oid = Git::Native::Oid->from_hex($tree_oid) if !ref $tree_oid; | ||
| 297 | |||
| 298 | # commit_create takes git_tree*, so we need to look it up. | ||
| 299 | check_rc Git::Libgit2::FFI::git_tree_lookup( \my $tree_h, $self->_handle, $tree_oid->ptr ); | ||
| 300 | |||
| 301 | my $sig_author = $args{author} // $self->signature_default; | ||
| 302 | my $sig_committer = $args{committer} // $sig_author; | ||
| 303 | |||
| 304 | # Parents: libgit2 wants an array of git_commit*. We pass undef for 0, | ||
| 305 | # otherwise look up each parent into commits and pass an opaque[] array. | ||
| 306 | # FFI::Platypus passes Perl arrays of opaque via 'opaque[]'. | ||
| 307 | my @parent_oids = @{ $args{parents} // [] }; | ||
| 308 | my @parent_handles; | ||
| 309 | for my $p (@parent_oids) { | ||
| 310 | $p = Git::Native::Oid->from_hex($p) if !ref $p; | ||
| 311 | check_rc Git::Libgit2::FFI::git_commit_lookup( \my $c, $self->_handle, $p->ptr ); | ||
| 312 | push @parent_handles, $c; | ||
| 313 | } | ||
| 314 | |||
| 315 | my $raw = "\0" x 20; | ||
| 316 | my ($oid_p) = scalar_to_buffer($raw); | ||
| 317 | |||
| 318 | # Build a parents-array pointer if non-empty. | ||
| 319 | # FFI::Platypus 2: we declared parents as 'opaque' — accepting NULL or a pointer. | ||
| 320 | # The non-empty branch packs ALL parent handles into a pointer array, so | ||
| 321 | # any parent count works (0 roots, 1 normal, 2+ merge commits — see t/48). | ||
| 322 | if ( @parent_handles == 0 ) { | ||
| 323 | check_rc Git::Libgit2::FFI::git_commit_create( | ||
| 324 | $oid_p, $self->_handle, $args{update_ref}, | ||
| 325 | $sig_author->_handle, $sig_committer->_handle, | ||
| 326 | $args{message_encoding} // 'UTF-8', | ||
| 327 | $args{message}, | ||
| 328 | $tree_h, | ||
| 329 | 0, undef, | ||
| 330 | ); | ||
| 331 | } | ||
| 332 | else { | ||
| 333 | # Pack pointer array. Each pointer is a 64-bit value on x86_64. | ||
| 334 | my $parents_buf = pack 'J*', @parent_handles; | ||
| 335 | my ($parents_p) = scalar_to_buffer($parents_buf); | ||
| 336 | check_rc Git::Libgit2::FFI::git_commit_create( | ||
| 337 | $oid_p, $self->_handle, $args{update_ref}, | ||
| 338 | $sig_author->_handle, $sig_committer->_handle, | ||
| 339 | $args{message_encoding} // 'UTF-8', | ||
| 340 | $args{message}, | ||
| 341 | $tree_h, | ||
| 342 | scalar(@parent_handles), $parents_p, | ||
| 343 | ); | ||
| 344 | } | ||
| 345 | |||
| 346 | Git::Libgit2::FFI::git_commit_free($_) for @parent_handles; | ||
| 347 | Git::Libgit2::FFI::git_tree_free($tree_h); | ||
| 348 | |||
| 349 | return Git::Native::Oid->from_raw($raw); | ||
| 350 | } | ||
| 351 | |||
| 352 | # ---------- remotes ---------- | ||
| 353 | |||
| 354 | sub remote { | ||
| 355 | my ( $self, $name ) = @_; | ||
| 356 | check_rc Git::Libgit2::FFI::git_remote_lookup( \my $r, $self->_handle, $name ); | ||
| 357 | return Git::Native::Remote->new( _handle => $r, _owner => $self ); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub remote_create { | ||
| 361 | my ( $self, $name, $url ) = @_; | ||
| 362 | check_rc Git::Libgit2::FFI::git_remote_create( \my $r, $self->_handle, $name, $url ); | ||
| 363 | return Git::Native::Remote->new( _handle => $r, _owner => $self ); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub remote_anonymous { | ||
| 367 | my ( $self, $url ) = @_; | ||
| 368 | check_rc Git::Libgit2::FFI::git_remote_create_anonymous( \my $r, $self->_handle, $url ); | ||
| 369 | return Git::Native::Remote->new( _handle => $r, _owner => $self ); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub has_remote { | ||
| 373 | my ( $self, $name ) = @_; | ||
| 374 | my $rc = Git::Libgit2::FFI::git_remote_lookup( \my $r, $self->_handle, $name ); | ||
| 375 | if ( $rc == 0 ) { | ||
| 376 | Git::Libgit2::FFI::git_remote_free($r); | ||
| 377 | return 1; | ||
| 378 | } | ||
| 379 | return 0; | ||
| 380 | } | ||
| 381 | |||
| 382 | # ---------- config ---------- | ||
| 383 | |||
| 384 | # Live, writable config (use set_string here). | ||
| 385 | sub config { | ||
| 386 | my $self = shift; | ||
| 387 | check_rc Git::Libgit2::FFI::git_repository_config( \my $cfg, $self->_handle ); | ||
| 388 | return Git::Native::Config->new( _handle => $cfg, _owner => $self ); | ||
| 389 | } | ||
| 390 | |||
| 391 | # Read-only snapshot - required for reliable git_config_get_string. | ||
| 392 | sub config_snapshot { | ||
| 393 | my $self = shift; | ||
| 394 | check_rc Git::Libgit2::FFI::git_repository_config_snapshot( \my $cfg, $self->_handle ); | ||
| 395 | return Git::Native::Config->new( _handle => $cfg, _owner => $self ); | ||
| 396 | } | ||
| 397 | |||
| 398 | # Convenience: read one key off a fresh snapshot. undef when unset. | ||
| 399 | sub config_string { | ||
| 400 | my ( $self, $key ) = @_; | ||
| 401 | return $self->config_snapshot->get_string($key); | ||
| 402 | } | ||
| 403 | |||
| 404 | # Convenience: read one key as a git-style boolean off a fresh snapshot. | ||
| 405 | # undef when unset; Git::Native::Error on a non-boolean value. | ||
| 406 | sub config_bool { | ||
| 407 | my ( $self, $key ) = @_; | ||
| 408 | return $self->config_snapshot->get_bool($key); | ||
| 409 | } | ||
| 410 | |||
| 411 | # ---------- revwalk ---------- | ||
| 412 | |||
| 413 | sub revwalker { | ||
| 414 | my $self = shift; | ||
| 415 | check_rc Git::Libgit2::FFI::git_revwalk_new( \my $w, $self->_handle ); | ||
| 416 | return Git::Native::Revwalker->new( _handle => $w, _owner => $self ); | ||
| 417 | } | ||
| 418 | |||
| 419 | # ---------- branches ---------- | ||
| 420 | |||
| 421 | sub branch { | ||
| 422 | my ( $self, $name, %opts ) = @_; | ||
| 423 | my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_LOCAL; | ||
| 424 | check_rc Git::Libgit2::FFI::git_branch_lookup( \my $ref, $self->_handle, $name, $type ); | ||
| 425 | return Git::Native::Branch->new( _handle => $ref, _owner => $self, type => $type ); | ||
| 426 | } | ||
| 427 | |||
| 428 | sub has_branch { | ||
| 429 | my ( $self, $name, %opts ) = @_; | ||
| 430 | my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_LOCAL; | ||
| 431 | my $rc = Git::Libgit2::FFI::git_branch_lookup( \my $ref, $self->_handle, $name, $type ); | ||
| 432 | if ( $rc == 0 ) { | ||
| 433 | Git::Libgit2::FFI::git_reference_free($ref); | ||
| 434 | return 1; | ||
| 435 | } | ||
| 436 | return 0; | ||
| 437 | } | ||
| 438 | |||
| 439 | sub branch_create { | ||
| 440 | my ( $self, $name, $target, %opts ) = @_; | ||
| 441 | my $oid = ref($target) && $target->isa('Git::Native::Oid') | ||
| 442 | ? $target : Git::Native::Oid->from_hex($target); | ||
| 443 | check_rc Git::Libgit2::FFI::git_commit_lookup( \my $commit_h, $self->_handle, $oid->ptr ); | ||
| 444 | my $rc = Git::Libgit2::FFI::git_branch_create( | ||
| 445 | \my $ref, $self->_handle, $name, $commit_h, $opts{force} ? 1 : 0, | ||
| 446 | ); | ||
| 447 | Git::Libgit2::FFI::git_commit_free($commit_h); | ||
| 448 | check_rc $rc; | ||
| 449 | return Git::Native::Branch->new( | ||
| 450 | _handle => $ref, _owner => $self, | ||
| 451 | type => Git::Native::Branch::GIT_BRANCH_LOCAL, | ||
| 452 | ); | ||
| 453 | } | ||
| 454 | |||
| 455 | sub branches { | ||
| 456 | my ( $self, %opts ) = @_; | ||
| 457 | my $type = $opts{type} // Git::Native::Branch::GIT_BRANCH_ALL; | ||
| 458 | check_rc Git::Libgit2::FFI::git_branch_iterator_new( \my $iter, $self->_handle, $type ); | ||
| 459 | my @out; | ||
| 460 | while (1) { | ||
| 461 | my $rc = Git::Libgit2::FFI::git_branch_next( \my $ref, \my $branch_type, $iter ); | ||
| 462 | last if $rc == GIT_ITEROVER; | ||
| 463 | if ( $rc != 0 ) { | ||
| 464 | Git::Libgit2::FFI::git_branch_iterator_free($iter); | ||
| 465 | check_rc $rc; | ||
| 466 | } | ||
| 467 | push @out, Git::Native::Branch->new( | ||
| 468 | _handle => $ref, _owner => $self, type => $branch_type, | ||
| 469 | ); | ||
| 470 | } | ||
| 471 | Git::Libgit2::FFI::git_branch_iterator_free($iter); | ||
| 472 | return \@out; | ||
| 473 | } | ||
| 474 | |||
| 475 | # ---------- tags ---------- | ||
| 476 | |||
| 477 | sub tag { | ||
| 478 | my ( $self, $name ) = @_; | ||
| 479 | # Resolve refs/tags/$name -> object id -> git_tag_lookup. | ||
| 480 | my $refname = $name =~ m{^refs/tags/} ? $name : "refs/tags/$name"; | ||
| 481 | check_rc Git::Libgit2::FFI::git_reference_lookup( \my $ref, $self->_handle, $refname ); | ||
| 482 | my $oidp = Git::Libgit2::FFI::git_reference_target($ref); | ||
| 483 | my $oid = Git::Native::Oid->from_ptr($oidp); | ||
| 484 | Git::Libgit2::FFI::git_reference_free($ref); | ||
| 485 | my $rc = Git::Libgit2::FFI::git_tag_lookup( \my $tag, $self->_handle, $oid->ptr ); | ||
| 486 | if ( $rc != 0 ) { | ||
| 487 | # Not an annotated tag - lightweight. Return undef; caller should use ->reference. | ||
| 488 | return undef; | ||
| 489 | } | ||
| 490 | return Git::Native::Tag->new( _handle => $tag, _owner => $self ); | ||
| 491 | } | ||
| 492 | |||
| 493 | sub tag_create { | ||
| 494 | my ( $self, $name, $target, %args ) = @_; | ||
| 495 | my $oid = ref($target) && $target->isa('Git::Native::Oid') | ||
| 496 | ? $target : Git::Native::Oid->from_hex($target); | ||
| 497 | # Look up target object generically (commit / tree / blob). | ||
| 498 | check_rc Git::Libgit2::FFI::git_object_lookup( | ||
| 499 | \my $obj, $self->_handle, $oid->ptr, GIT_OBJECT_ANY, | ||
| 500 | ); | ||
| 501 | |||
| 502 | my $raw = "\0" x 20; | ||
| 503 | my ($oid_p) = FFI::Platypus::Buffer::scalar_to_buffer($raw); | ||
| 504 | |||
| 505 | if ( defined $args{message} ) { | ||
| 506 | my $tagger = $args{tagger} // $self->signature_default; | ||
| 507 | my $rc = Git::Libgit2::FFI::git_tag_create( | ||
| 508 | $oid_p, $self->_handle, $name, $obj, | ||
| 509 | $tagger->_handle, $args{message}, | ||
| 510 | $args{force} ? 1 : 0, | ||
| 511 | ); | ||
| 512 | Git::Libgit2::FFI::git_object_free($obj); | ||
| 513 | check_rc $rc; | ||
| 514 | } | ||
| 515 | else { | ||
| 516 | my $rc = Git::Libgit2::FFI::git_tag_create_lightweight( | ||
| 517 | $oid_p, $self->_handle, $name, $obj, $args{force} ? 1 : 0, | ||
| 518 | ); | ||
| 519 | Git::Libgit2::FFI::git_object_free($obj); | ||
| 520 | check_rc $rc; | ||
| 521 | } | ||
| 522 | return Git::Native::Oid->from_raw($raw); | ||
| 523 | } | ||
| 524 | |||
| 525 | sub tag_delete { | ||
| 526 | my ( $self, $name ) = @_; | ||
| 527 | check_rc Git::Libgit2::FFI::git_tag_delete( $self->_handle, $name ); | ||
| 528 | return $self; | ||
| 529 | } | ||
| 530 | |||
| 531 | sub tag_names { | ||
| 532 | my ( $self, %opts ) = @_; | ||
| 533 | # git_strarray on stack: {char **strings; size_t count} = 16 bytes. | ||
| 534 | my $buf = "\0" x 16; | ||
| 535 | my ($p) = FFI::Platypus::Buffer::scalar_to_buffer($buf); | ||
| 536 | if ( $opts{pattern} ) { | ||
| 537 | check_rc Git::Libgit2::FFI::git_tag_list_match( $p, $opts{pattern}, $self->_handle ); | ||
| 538 | } | ||
| 539 | else { | ||
| 540 | check_rc Git::Libgit2::FFI::git_tag_list( $p, $self->_handle ); | ||
| 541 | } | ||
| 542 | # Unpack strings ptr (at offset 0) and count (at offset 8). | ||
| 543 | my ( $strings_ptr, $count ) = unpack 'Q Q', $buf; | ||
| 544 | my @names; | ||
| 545 | if ( $count > 0 && $strings_ptr ) { | ||
| 546 | my $ffi = Git::Libgit2::FFI::ffi(); | ||
| 547 | for my $i ( 0 .. $count - 1 ) { | ||
| 548 | my $sp = $ffi->cast( 'opaque', 'opaque[' . ( $i + 1 ) . ']', $strings_ptr )->[$i]; | ||
| 549 | my $s = $ffi->cast( 'opaque', 'string', $sp ); | ||
| 550 | push @names, $s; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | Git::Libgit2::FFI::git_strarray_free($p); | ||
| 554 | return \@names; | ||
| 555 | } | ||
| 556 | |||
| 557 | # ---------- status ---------- | ||
| 558 | |||
| 559 | # status() returns hashref { path => status_flags, ... }. | ||
| 560 | # status flags are the GIT_STATUS_* bitfield from libgit2. | ||
| 561 | sub status { | ||
| 562 | my $self = shift; | ||
| 563 | my %out; | ||
| 564 | my $ffi = Git::Libgit2::FFI::ffi(); | ||
| 565 | my $cb = $ffi->closure( sub { | ||
| 566 | my ( $path, $flags, $payload ) = @_; | ||
| 567 | $out{$path} = $flags; | ||
| 568 | return 0; | ||
| 569 | }); | ||
| 570 | check_rc Git::Libgit2::FFI::git_status_foreach( $self->_handle, $cb, undef ); | ||
| 571 | return \%out; | ||
| 572 | } | ||
| 573 | |||
| 574 | sub status_for_path { | ||
| 575 | my ( $self, $path ) = @_; | ||
| 576 | check_rc Git::Libgit2::FFI::git_status_file( \my $flags, $self->_handle, $path ); | ||
| 577 | return $flags; | ||
| 578 | } | ||
| 579 | |||
| 580 | # ---------- index ---------- | ||
| 581 | |||
| 582 | # The index, freshly read from disk. | ||
| 583 | # | ||
| 584 | # libgit2 caches the git_index* inside the git_repository: every | ||
| 585 | # git_repository_index call hands back the SAME object with its refcount | ||
| 586 | # bumped, so a new Perl wrapper on its own still sees whatever that cached | ||
| 587 | # object last read - a `git add` by another process would be invisible. The | ||
| 588 | # git_index_read here is what makes the accessor answer for the current disk | ||
| 589 | # state; force = 0 leaves it to libgit2's stat check, so repeated calls on an | ||
| 590 | # unchanged index cost nothing. Deliberately not cached Perl-side either, for | ||
| 591 | # the same reason. | ||
| 592 | # | ||
| 593 | # The wrapper is built before the read so a failing read frees the handle | ||
| 594 | # through DEMOLISH instead of leaking it. | ||
| 595 | sub index { | ||
| 596 | my $self = shift; | ||
| 597 | check_rc Git::Libgit2::FFI::git_repository_index( \my $idx, $self->_handle ); | ||
| 598 | my $index = Git::Native::Index->new( _handle => $idx, _owner => $self ); | ||
| 599 | check_rc Git::Libgit2::FFI::git_index_read( $idx, 0 ); | ||
| 600 | return $index; | ||
| 601 | } | ||
| 602 | |||
| 603 | sub signature_default { | ||
| 604 | my $self = shift; | ||
| 605 | my $rc = Git::Libgit2::FFI::git_signature_default( \my $sig, $self->_handle ); | ||
| 606 | # We got an allocated git_signature*; from_handle adopts it and copies | ||
| 607 | # name/email/when out of the struct, so the attributes report what the | ||
| 608 | # config actually says instead of a placeholder. | ||
| 609 | return Git::Native::Signature->from_handle($sig) if $rc == 0; | ||
| 610 | # Fallback if no user.name/email configured. | ||
| 611 | return Git::Native::Signature->new( | ||
| 612 | name => 'Git::Native', | ||
| 613 | email => 'unconfigured@example.invalid', | ||
| 614 | ); | ||
| 615 | } | ||
| 616 | |||
| 617 | sub DEMOLISH { | ||
| 618 | my $self = shift; | ||
| 619 | Git::Libgit2::FFI::git_repository_free( $self->{_handle} ) | ||
| 620 | if $self->{_handle}; | ||
| 621 | } | ||
| 622 | |||
| 623 | 1; | ||
| 624 | |||
| 625 | __END__ |