| 1 | # ABSTRACT: A Git reference (branch, tag, HEAD) | ||
| 2 | |||
| 3 | package Git::Native::Reference; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | use Moo; | ||
| 6 | use Git::Libgit2::FFI (); | ||
| 7 | use Git::Native::Error qw( check_rc ); | ||
| 8 | use Git::Native::Oid (); | ||
| 9 | |||
| 10 | has _handle => ( is => 'ro', required => 1 ); | ||
| 11 | has _owner => ( is => 'ro', required => 1 ); # Repository | ||
| 12 | |||
| 13 | sub name { | ||
| 14 | Git::Libgit2::FFI::git_reference_name( $_[0]->_handle ); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub target { | ||
| 18 | my $self = shift; | ||
| 19 | my $p = Git::Libgit2::FFI::git_reference_target( $self->_handle ); | ||
| 20 | return undef unless $p; | ||
| 21 | return Git::Native::Oid->from_ptr($p); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub is_symbolic { | ||
| 25 | Git::Libgit2::FFI::git_reference_type( $_[0]->_handle ) == 2 ? 1 : 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Target refname of a symbolic ref (e.g. HEAD -> 'refs/heads/main'); | ||
| 29 | # undef for a direct ref. | ||
| 30 | sub symbolic_target { | ||
| 31 | Git::Libgit2::FFI::git_reference_symbolic_target( $_[0]->_handle ); | ||
| 32 | } | ||
| 33 | |||
| 34 | # Human-readable short name: 'refs/heads/main' -> 'main'. | ||
| 35 | sub shorthand { | ||
| 36 | Git::Libgit2::FFI::git_reference_shorthand( $_[0]->_handle ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub is_branch { | ||
| 40 | Git::Libgit2::FFI::git_reference_is_branch( $_[0]->_handle ) ? 1 : 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub is_remote { | ||
| 44 | Git::Libgit2::FFI::git_reference_is_remote( $_[0]->_handle ) ? 1 : 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub is_tag { | ||
| 48 | Git::Libgit2::FFI::git_reference_is_tag( $_[0]->_handle ) ? 1 : 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | # Follow symbolic refs until a direct one is reached. Returns a fresh | ||
| 52 | # Reference; the original handle stays valid. | ||
| 53 | sub resolve { | ||
| 54 | my $self = shift; | ||
| 55 | check_rc Git::Libgit2::FFI::git_reference_resolve( \my $ref, $self->_handle ); | ||
| 56 | return Git::Native::Reference->new( _handle => $ref, _owner => $self->_owner ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Point a direct ref at a new OID. Returns the new Reference; fails on a | ||
| 60 | # symbolic ref (use symbolic_set_target there). | ||
| 61 | sub set_target { | ||
| 62 | my ( $self, $oid, %opts ) = @_; | ||
| 63 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 64 | check_rc Git::Libgit2::FFI::git_reference_set_target( | ||
| 65 | \my $ref, $self->_handle, $oid->ptr, $opts{message} // '', | ||
| 66 | ); | ||
| 67 | return Git::Native::Reference->new( _handle => $ref, _owner => $self->_owner ); | ||
| 68 | } | ||
| 69 | |||
| 70 | # Repoint a symbolic ref at a new target refname. Returns the new Reference. | ||
| 71 | sub symbolic_set_target { | ||
| 72 | my ( $self, $target, %opts ) = @_; | ||
| 73 | check_rc Git::Libgit2::FFI::git_reference_symbolic_set_target( | ||
| 74 | \my $ref, $self->_handle, $target, $opts{message} // '', | ||
| 75 | ); | ||
| 76 | return Git::Native::Reference->new( _handle => $ref, _owner => $self->_owner ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub delete { | ||
| 80 | my $self = shift; | ||
| 81 | check_rc Git::Libgit2::FFI::git_reference_delete( $self->_handle ); | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub DEMOLISH { | ||
| 86 | my $self = shift; | ||
| 87 | Git::Libgit2::FFI::git_reference_free( $self->{_handle} ) | ||
| 88 | if $self->{_handle}; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |