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