Native/Index.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 102 CC 11 SCAR 18.7
CC
11
Cov
0%
is_tracked_under
12.0
_need_arg
12.0
find
6.0
CRAP 6.5
1 # ABSTRACT: A libgit2 index (the staging area), read-only
2  
3 package Git::Native::Index;
4 our $VERSION = '0.006';
5 use Moo;
6 use Carp ();
7 use Git::Libgit2 qw( GIT_ENOTFOUND );
8 use Git::Libgit2::FFI ();
9 use Git::Native::Error qw( check_rc );
10  
11 # The argument guards below croak rather than throwing a Git::Native::Error -
12 # they never reach libgit2, so there is no ->code to report (same rule as
13 # Git::Native::Oid->from_hex and Repository->object_by_prefix). @CARP_NOT keeps
14 # the blame on the caller's line if the argument arrived through another
15 # wrapper instead of directly.
16 our @CARP_NOT = qw( Git::Native::Repository );
17  
18 has _handle => ( is => 'ro', required => 1 );
19 has _owner => ( is => 'ro', required => 1 ); # Repository
20  
21 sub entrycount { Git::Libgit2::FFI::git_index_entrycount( $_[0]->_handle ) }
22  
23 # find($path): the 0-based position of the exact path, or undef when the index
24 # has no such entry. Only GIT_ENOTFOUND maps to undef; every other negative rc
25 # throws via check_rc, so a broken index is never reported as "untracked".
26 sub find {
27 my ( $self, $path ) = @_;
28 _need_arg( 'find', 'path', $path );
29 my $rc = Git::Libgit2::FFI::git_index_find( \my $pos, $self->_handle, $path );
30 return undef if $rc == GIT_ENOTFOUND;
31 check_rc $rc;
32 return $pos;
33 }
34  
35 # find_prefix($prefix): position of the first entry whose path STARTS WITH
36 # $prefix - a raw string prefix, no path semantics ('tasks' matches
37 # 'tasksfoo.txt'). undef when nothing matches. For the directory question use
38 # is_tracked_under.
39 sub find_prefix {
40 my ( $self, $prefix ) = @_;
41 _need_arg( 'find_prefix', 'prefix', $prefix );
42 my $rc = Git::Libgit2::FFI::git_index_find_prefix( \my $pos, $self->_handle, $prefix );
43 return undef if $rc == GIT_ENOTFOUND;
44 check_rc $rc;
45 return $pos;
46 }
47  
48 sub has_path {
49 my ( $self, $path ) = @_;
50 _need_arg( 'has_path', 'path', $path );
51 return defined $self->find($path) ? 1 : 0;
52 }
53  
54 sub has_prefix {
55 my ( $self, $prefix ) = @_;
56 _need_arg( 'has_prefix', 'prefix', $prefix );
57 return defined $self->find_prefix($prefix) ? 1 : 0;
58 }
59  
60 # is_tracked_under($path): the `git ls-files -- $path` question - is this exact
61 # path tracked, or is anything tracked below it as a directory?
62 #
63 # Not has_prefix: a string prefix answers yes for 'tasks' against
64 # 'tasksfoo.txt'. Appending the separator makes the directory case a path
65 # question again, and the exact-path check keeps a tracked file answering for
66 # itself.
67 sub is_tracked_under {
68 my ( $self, $path ) = @_;
69 _need_arg( 'is_tracked_under', 'path', $path );
70 ( my $dir = $path ) =~ s{/+\z}{};
71 Carp::croak
72 "Git::Native::Index->is_tracked_under requires a non-empty path, got '$path'"
73 unless length $dir;
74 return 1 if $self->has_path($dir);
75 return $self->has_prefix("$dir/");
76 }
77  
78 # reload(force => 0): re-read the index file from disk. force = 0 re-reads only
79 # when the file changed (libgit2 compares stat data), force = 1 unconditionally.
80 sub reload {
81 my ( $self, %args ) = @_;
82 check_rc Git::Libgit2::FFI::git_index_read( $self->_handle, $args{force} ? 1 : 0 );
83 return $self;
84 }
85  
86 # An empty path or prefix would match entry 0 rather than nothing, so it is
87 # rejected instead of silently answering yes.
88 sub _need_arg {
89 my ( $method, $name, $value ) = @_;
90 return if defined $value && length $value;
91 Carp::croak sprintf 'Git::Native::Index->%s requires a non-empty %s, got %s',
92 $method, $name, ( defined $value ? "''" : 'undef' );
93 }
94  
95 sub DEMOLISH {
96 my $self = shift;
97 Git::Libgit2::FFI::git_index_free( $self->{_handle} ) if $self->{_handle};
98 }
99  
100 1;
101  
102 __END__