Commit | Line | Data |
03bf0cab |
1 | package Gitalist::Git::Head; |
5c07fcf1 |
2 | |
72fbbef7 |
3 | use Moose; |
4 | use namespace::autoclean; |
03bf0cab |
5 | |
5c07fcf1 |
6 | with 'Gitalist::Git::Serializable'; |
7 | |
72fbbef7 |
8 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
5c07fcf1 |
9 | use MooseX::Types::Moose qw/Maybe/; |
f9baba96 |
10 | use Gitalist::Git::Types qw/SHA1/; |
11 | use MooseX::Types::DateTime qw/DateTime/; |
5c07fcf1 |
12 | |
13 | use aliased 'DateTime' => 'DT'; |
03bf0cab |
14 | |
72fbbef7 |
15 | has sha1 => ( isa => SHA1, |
16 | is => 'ro', |
17 | required => 1, |
18 | ); |
19 | has name => ( isa => NonEmptySimpleStr, |
20 | is => 'ro', |
21 | required => 1, |
22 | ); |
23 | has committer => ( isa => NonEmptySimpleStr, |
24 | is => 'ro', |
25 | required => 1, |
26 | ); |
5c07fcf1 |
27 | has last_change => ( isa => Maybe[DateTime], |
72fbbef7 |
28 | is => 'ro', |
29 | required => 1, |
72fbbef7 |
30 | ); |
31 | |
32 | around 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 | |
61 | 1; |