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         4  
  4         21  
3 4     4   15162 use MooseX::StrictConstructor;
  4         4  
  4         24  
4 4     4   7156 use Moose::Util::TypeConstraints;
  4         6  
  4         26  
5 4     4   6814 use Encode qw/decode/;
  4         26182  
  4         222  
6 4     4   20 use namespace::autoclean;
  4         5  
  4         28  
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 160 my $self = shift;
31 155 50       3778 return unless $self->content;
32 155         3346 my @lines = split "\n", $self->content;
33 155         188 my %header;
34 155         345 while ( my $line = shift @lines ) {
35 604 50       691 last unless $line;
36 604         828 my ( $key, $value ) = split ' ', $line, 2;
37 604         464 push @{$header{$key}}, $value;
  604         1861  
38             }
39             $header{encoding}
40 155   50     3719 ||= [ $self->git->config->get(key => "i18n.commitEncoding") || "utf-8" ];
      100        
41 155         57452 my $encoding = $header{encoding}->[-1];
42 155         398 for my $key (keys %header) {
43 756         670 for my $value (@{$header{$key}}) {
  756         1180  
44 758         1591 $value = decode($encoding, $value);
45 758 100 100     23554 if ( $key eq 'committer' or $key eq 'author' ) {
46 310         1000 my @data = split ' ', $value;
47 310         574 my ( $email, $epoch, $tz ) = splice( @data, -3 );
48 310         779 $email = substr( $email, 1, -1 );
49 310         614 my $name = join ' ', @data;
50 310         8238 my $actor
51             = Git::PurePerl::Actor->new( name => $name, email => $email );
52 310         8473 $self->$key($actor);
53 310         369 $key = $method_map{$key};
54 310         1021 my $dt
55             = DateTime->from_epoch( epoch => $epoch, time_zone => $tz );
56 310         141596 $self->$key($dt);
57             } else {
58 448   66     1077 $key = $method_map{$key} || $key;
59 448         8773 $self->$key($value);
60             }
61             }
62             }
63 155         562 $self->comment( decode($encoding, join "\n", @lines) );
64             }
65              
66              
67             sub tree {
68 13     13 0 26 my $self = shift;
69 13         367 return $self->git->get_object( $self->tree_sha1 );
70             }
71              
72              
73             sub _push_parent_sha1 {
74 138     138   172 my ($self, $sha1) = @_;
75            
76 138         103 push(@{$self->parent_sha1s}, $sha1);
  138         4324  
77             }
78              
79             sub parent_sha1 {
80 19     19 0 540 return shift->parent_sha1s->[0];
81             }
82            
83             sub parent {
84 10     10 0 20 my $self = shift;
85 10         268 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