Unfuck DateTime type with newer MX::Types
[catagits/Gitalist.git] / lib / Gitalist / Git / Head.pm
CommitLineData
03bf0cab 1package Gitalist::Git::Head;
5c07fcf1 2
72fbbef7 3use Moose;
4use namespace::autoclean;
03bf0cab 5
5c07fcf1 6with 'Gitalist::Git::Serializable';
7
72fbbef7 8use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5c07fcf1 9use MooseX::Types::Moose qw/Maybe/;
86b3e7ce 10use Gitalist::Git::Types qw/SHA1/;
11use MooseX::Types::DateTime qw/DateTime/;
5c07fcf1 12
13use aliased 'DateTime' => 'DT';
03bf0cab 14
72fbbef7 15has sha1 => ( isa => SHA1,
16 is => 'ro',
17 required => 1,
18 );
19has name => ( isa => NonEmptySimpleStr,
20 is => 'ro',
21 required => 1,
22 );
23has committer => ( isa => NonEmptySimpleStr,
24 is => 'ro',
25 required => 1,
26 );
5c07fcf1 27has last_change => ( isa => Maybe[DateTime],
72fbbef7 28 is => 'ro',
29 required => 1,
72fbbef7 30);
31
32around BUILDARGS => sub {
33 my $orig = shift;
34 my $class = shift;
35
36 if ( @_ == 1 && ! ref $_[0] ) {
37 my $line = $_[0];
38 # expects $line to match the output from
39 # for-each-ref --format=%(objectname)%00%(refname)%00%(committer)
40 my ($sha1, $name, $commitinfo) = split /\0/, $line, 3;
41 $name =~ s!^refs/heads/!!;
42
43 my ($committer, $epoch, $tz) =
44 $commitinfo =~ /(.*)\s(\d+)\s+([+-]\d+)$/;
5c07fcf1 45 my $dt = DT->from_epoch(
72fbbef7 46 epoch => $epoch,
47 time_zone => $tz,
48 );
49
50 return $class->$orig(
51 sha1 => $sha1,
52 name => $name,
53 committer => $committer,
54 last_change => $dt,
55 );
56 } else {
57 return $class->$orig(@_);
58 }
59};
60
611;