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