| 1 | # ABSTRACT: Walk commits in topological / time order | ||
| 2 | |||
| 3 | package Git::Native::Revwalker; | ||
| 4 | use Moo; | ||
| 5 | use Carp (); | ||
| 6 | use Git::Libgit2 qw( check_rc ); | ||
| 7 | use Git::Libgit2::FFI (); | ||
| 8 | use FFI::Platypus::Buffer qw( scalar_to_buffer ); | ||
| 9 | use Git::Native::Oid (); | ||
| 10 | |||
| 11 | use constant { | ||
| 12 | GIT_SORT_NONE => 0, | ||
| 13 | GIT_SORT_TOPOLOGICAL => 1, | ||
| 14 | GIT_SORT_TIME => 2, | ||
| 15 | GIT_SORT_REVERSE => 4, | ||
| 16 | GIT_ITEROVER => -31, | ||
| 17 | }; | ||
| 18 | |||
| 19 | has _handle => ( is => 'ro', required => 1 ); | ||
| 20 | has _owner => ( is => 'ro', required => 1 ); | ||
| 21 | |||
| 22 | sub push_oid { | ||
| 23 | my ( $self, $oid ) = @_; | ||
| 24 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 25 | check_rc Git::Libgit2::FFI::git_revwalk_push( $self->_handle, $oid->ptr ); | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub push_head { check_rc Git::Libgit2::FFI::git_revwalk_push_head( $_[0]->_handle ); $_[0] } | ||
| 30 | sub push_ref { check_rc Git::Libgit2::FFI::git_revwalk_push_ref( $_[0]->_handle, $_[1] ); $_[0] } | ||
| 31 | sub push_glob { check_rc Git::Libgit2::FFI::git_revwalk_push_glob( $_[0]->_handle, $_[1] ); $_[0] } | ||
| 32 | sub push_range { check_rc Git::Libgit2::FFI::git_revwalk_push_range( $_[0]->_handle, $_[1] ); $_[0] } | ||
| 33 | |||
| 34 | sub hide_oid { | ||
| 35 | my ( $self, $oid ) = @_; | ||
| 36 | $oid = Git::Native::Oid->from_hex($oid) if !ref $oid; | ||
| 37 | check_rc Git::Libgit2::FFI::git_revwalk_hide( $self->_handle, $oid->ptr ); | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub hide_head { check_rc Git::Libgit2::FFI::git_revwalk_hide_head( $_[0]->_handle ); $_[0] } | ||
| 42 | sub hide_ref { check_rc Git::Libgit2::FFI::git_revwalk_hide_ref( $_[0]->_handle, $_[1] ); $_[0] } | ||
| 43 | sub hide_glob { check_rc Git::Libgit2::FFI::git_revwalk_hide_glob( $_[0]->_handle, $_[1] ); $_[0] } | ||
| 44 | |||
| 45 | sub sorting { | ||
| 46 | my ( $self, $mode ) = @_; | ||
| 47 | check_rc Git::Libgit2::FFI::git_revwalk_sorting( $self->_handle, $mode ); | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub reset { check_rc Git::Libgit2::FFI::git_revwalk_reset( $_[0]->_handle ); $_[0] } | ||
| 52 | sub simplify_first_parent { | ||
| 53 | check_rc Git::Libgit2::FFI::git_revwalk_simplify_first_parent( $_[0]->_handle ); | ||
| 54 | $_[0]; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub next { | ||
| 58 | my $self = shift; | ||
| 59 | my $raw = "\0" x 20; | ||
| 60 | my ($p) = scalar_to_buffer($raw); | ||
| 61 | my $rc = Git::Libgit2::FFI::git_revwalk_next( $p, $self->_handle ); | ||
| 62 | return undef if $rc == GIT_ITEROVER; | ||
| 63 | check_rc $rc; | ||
| 64 | return Git::Native::Oid->from_raw($raw); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub all { | ||
| 68 | my $self = shift; | ||
| 69 | my @out; | ||
| 70 | while ( defined( my $o = $self->next ) ) { push @out, $o } | ||
| 71 | return \@out; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub DEMOLISH { | ||
| 75 | my $self = shift; | ||
| 76 | Git::Libgit2::FFI::git_revwalk_free( $self->{_handle} ) if $self->{_handle}; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ |