File Coverage

blib/lib/Git/PurePerl/Object/Commit.pm
Criterion Covered Total %
statement 51 55 92.7
branch 4 6 66.6
condition 8 10 80.0
subroutine 10 11 90.9
pod 0 5 0.0
total 73 87 83.9


line stmt bran cond sub pod time code
1             package Git::PurePerl::Object::Commit;
2 4     4   16 use Moose;
  4         6  
  4         27  
3 4     4   17171 use MooseX::StrictConstructor;
  4         7  
  4         27  
4 4     4   7657 use Moose::Util::TypeConstraints;
  4         5  
  4         31  
5 4     4   7034 use Encode qw/decode/;
  4         26996  
  4         238  
6 4     4   22 use namespace::autoclean;
  4         8  
  4         29  
7              
8             extends 'Git::PurePerl::Object';
9              
10             has 'kind' =>
11             ( is => 'ro', isa => 'ObjectKind', required => 1, default => 'commit' );
12             has 'tree_sha1' => ( is => 'rw', isa => 'Str', required => 0 );
13             has 'parent_sha1s' => ( is => 'rw', isa => 'ArrayRef[Str]', required => 0, default => sub { [] });
14             has 'author' => ( is => 'rw', isa => 'Git::PurePerl::Actor', required => 0 );
15             has 'authored_time' => ( is => 'rw', isa => 'DateTime', required => 0 );
16             has 'committer' =>
17             ( is => 'rw', isa => 'Git::PurePerl::Actor', required => 0 );
18             has 'committed_time' => ( is => 'rw', isa => 'DateTime', required => 0 );
19             has 'comment' => ( is => 'rw', isa => 'Str', required => 0 );
20             has 'encoding' => ( is => 'rw', isa => 'Str', required => 0 );
21              
22             my %method_map = (
23             'tree' => 'tree_sha1',
24             'parent' => '_push_parent_sha1',
25             'author' => 'authored_time',
26             'committer' => 'committed_time'
27             );
28              
29             sub BUILD {
30 155     155 0 187 my $self = shift;
31 155 50       3971 return unless $self->content;
32 155         3432 my @lines = split "\n", $self->content;
33 155         223 my %header;
34 155         396 while ( my $line = shift @lines ) {
35 604 50       732 last unless $line;
36 604         859 my ( $key, $value ) = split ' ', $line, 2;
37 604         527 push @{$header{$key}}, $value;
  604         1648  
38             }
39             $header{encoding}
40 155   50     3884 ||= [ $self->git->config->get(key => "i18n.commitEncoding") || "utf-8" ];
      100        
41 155         60808 my $encoding = $header{encoding}->[-1];
42 155         496 for my $key (keys %header) {
43 756         641 for my $value (@{$header{$key}}) {
  756         1263  
44 758         1697 $value = decode($encoding, $value);
45 758 100 100     24973 if ( $key eq 'committer' or $key eq 'author' ) {
46 310         1096 my @data = split ' ', $value;
47 310         732 my ( $email, $epoch, $tz ) = splice( @data, -3 );
48 310         895 $email = substr( $email, 1, -1 );
49 310         684 my $name = join ' ', @data;
50 310         8161 my $actor
51             = Git::PurePerl::Actor->new( name => $name, email => $email );
52 310         8561 $self->$key($actor);
53 310         436 $key = $method_map{$key};
54 310         1316 my $dt
55             = DateTime->from_epoch( epoch => $epoch, time_zone => $tz );
56 310         128485 $self->$key($dt);
57             } else {
58 448   66     1154 $key = $method_map{$key} || $key;
59 448         9027 $self->$key($value);
60             }
61             }
62             }
63 155         551 $self->comment( decode($encoding, join "\n", @lines) );
64             }
65              
66              
67             sub tree {
68 13     13 0 33 my $self = shift;
69 13         389 return $self->git->get_object( $self->tree_sha1 );
70             }
71              
72              
73             sub _push_parent_sha1 {
74 138     138   233 my ($self, $sha1) = @_;
75            
76 138         177 push(@{$self->parent_sha1s}, $sha1);
  138         3940  
77             }
78              
79             sub parent_sha1 {
80 19     19 0 560 return shift->parent_sha1s->[0];
81             }
82            
83             sub parent {
84 10     10 0 15 my $self = shift;
85 10         302 return $self->git->get_object( $self->parent_sha1 );
86             }
87              
88             sub parents {
89 0     0 0   my $self = shift;
90            
91 0           return map { $self->git->get_object( $_ ) } @{$self->parent_sha1s};
  0            
  0            
92             }
93              
94             __PACKAGE__->meta->make_immutable;
95