File Coverage

blib/lib/Git/PurePerl/PackIndex.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 5 5 100.0
pod 0 1 0.0
total 32 36 88.8


line stmt bran cond sub pod time code
1             package Git::PurePerl::PackIndex;
2 4     4   16 use Moose;
  4         6  
  4         21  
3 4     4   10806 use MooseX::StrictConstructor;
  4         6  
  4         23  
4 4     4   7318 use IO::File;
  4         7  
  4         488  
5 4     4   18 use namespace::autoclean;
  4         6  
  4         25  
6              
7             has 'filename' =>
8             ( is => 'ro', isa => 'Path::Class::File', required => 1, coerce => 1 );
9              
10             has 'fh' => ( is => 'rw', isa => 'IO::File', required => 0 );
11              
12             has 'offsets' =>
13             ( is => 'rw', isa => 'ArrayRef[Int]', required => 0, auto_deref => 1, );
14             has 'size' => ( is => 'rw', isa => 'Int', required => 0 );
15              
16             my $FanOutCount = 256;
17             my $SHA1Size = 20;
18             my $IdxOffsetSize = 4;
19             my $OffsetSize = 4;
20             my $CrcSize = 4;
21             my $OffsetStart = $FanOutCount * $IdxOffsetSize;
22             my $SHA1Start = $OffsetStart + $OffsetSize;
23             my $EntrySize = $OffsetSize + $SHA1Size;
24             my $EntrySizeV2 = $SHA1Size + $CrcSize + $OffsetSize;
25              
26             sub BUILD {
27 3     3 0 7 my $self = shift;
28 3         96 my $filename = $self->filename;
29              
30 3   33     16 my $fh = IO::File->new($filename) || confess($!);
31 3         341 $self->fh($fh);
32              
33 3         7 my @offsets = (0);
34 3         17 $fh->seek( $self->global_offset, 0 );
35 3         28 foreach my $i ( 0 .. $FanOutCount - 1 ) {
36 768         861 $fh->read( my $data, $IdxOffsetSize );
37 768         1937 my $offset = unpack( 'N', $data );
38 768 50       851 confess("pack has discontinuous index") if $offset < $offsets[-1];
39 768         599 push @offsets, $offset;
40             }
41 3         114 $self->offsets( \@offsets );
42 3         100 $self->size( $offsets[-1] );
43             }
44              
45             __PACKAGE__->meta->make_immutable;
46