| 1 | # ABSTRACT: A libgit2 branch (thin wrapper over git_reference) | ||
| 2 | |||
| 3 | package Git::Native::Branch; | ||
| 4 | our $VERSION = '0.006'; | ||
| 5 | use Moo; | ||
| 6 | use Git::Libgit2 qw( GIT_BRANCH_LOCAL GIT_BRANCH_REMOTE GIT_BRANCH_ALL ); | ||
| 7 | use Git::Libgit2::FFI (); | ||
| 8 | use Git::Native::Error qw( check_rc ); | ||
| 9 | use Git::Native::Oid (); | ||
| 10 | |||
| 11 | has _handle => ( is => 'ro', required => 1 ); # git_reference* | ||
| 12 | has _owner => ( is => 'ro', required => 1 ); | ||
| 13 | has type => ( is => 'ro', default => sub { GIT_BRANCH_LOCAL } ); | ||
| 14 | |||
| 15 | sub name { | ||
| 16 | my $self = shift; | ||
| 17 | check_rc Git::Libgit2::FFI::git_branch_name( \my $n, $self->_handle ); | ||
| 18 | return $n; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub refname { Git::Libgit2::FFI::git_reference_name( $_[0]->_handle ) } | ||
| 22 | |||
| 23 | sub target { | ||
| 24 | my $self = shift; | ||
| 25 | my $oidp = Git::Libgit2::FFI::git_reference_target( $self->_handle ); | ||
| 26 | return undef unless $oidp; | ||
| 27 | return Git::Native::Oid->from_ptr($oidp); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub is_head { Git::Libgit2::FFI::git_branch_is_head( $_[0]->_handle ) ? 1 : 0 } | ||
| 31 | sub is_local { $_[0]->type == GIT_BRANCH_LOCAL ? 1 : 0 } | ||
| 32 | sub is_remote { $_[0]->type == GIT_BRANCH_REMOTE ? 1 : 0 } | ||
| 33 | |||
| 34 | sub delete { | ||
| 35 | my $self = shift; | ||
| 36 | check_rc Git::Libgit2::FFI::git_branch_delete( $self->_handle ); | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub rename { | ||
| 41 | my ( $self, $new_name, %opts ) = @_; | ||
| 42 | check_rc Git::Libgit2::FFI::git_branch_move( | ||
| 43 | \my $new_ref, $self->_handle, $new_name, $opts{force} ? 1 : 0, | ||
| 44 | ); | ||
| 45 | return ref($self)->new( _handle => $new_ref, _owner => $self->_owner, type => $self->type ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub DEMOLISH { | ||
| 49 | my $self = shift; | ||
| 50 | Git::Libgit2::FFI::git_reference_free( $self->{_handle} ) if $self->{_handle}; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |