Introduce Gitalist::Git::Head.
Zachary Stevens [Sun, 27 Jun 2010 14:42:17 +0000 (15:42 +0100)]
Encapsulates a git head.
Repository->heads is now an ArrayRef of these.

lib/Gitalist/Git/Head.pm [new file with mode: 0644]
lib/Gitalist/Git/Repository.pm
t/02git_Repository.t
t/02git_head.t [new file with mode: 0644]

diff --git a/lib/Gitalist/Git/Head.pm b/lib/Gitalist/Git/Head.pm
new file mode 100644 (file)
index 0000000..0661a69
--- /dev/null
@@ -0,0 +1,26 @@
+package Gitalist::Git::Head;
+use MooseX::Declare;
+
+class Gitalist::Git::Head {
+    use Gitalist::Git::Types qw/SHA1/;
+    use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::DateTime qw/DateTime/;
+
+    has sha1        => ( isa      => SHA1,
+                         is       => 'ro',
+                         required => 1,
+                     );
+    has name        => ( isa      => NonEmptySimpleStr,
+                         is       => 'ro',
+                         required => 1,
+                     );
+    has committer   => ( isa      => NonEmptySimpleStr,
+                         is       => 'ro',
+                         required => 1,
+                     );
+    has last_change => ( isa      => DateTime,
+                         is       => 'ro',
+                         required => 1,
+                         coerce   => 1,
+                     );
+}
index 513f607..c07605d 100644 (file)
@@ -15,6 +15,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
     use Gitalist::Git::Object::Tree;
     use Gitalist::Git::Object::Commit;
     use Gitalist::Git::Object::Tag;
+    use Gitalist::Git::Head;
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
@@ -59,7 +60,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                              ? 1 : 0
                          },
                      );
-    has heads => ( isa => ArrayRef[HashRef],
+    has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
                    is => 'ro',
                    lazy_build => 1);
     has tags => ( isa => ArrayRef[HashRef],
@@ -237,17 +238,22 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
         my @ret;
         for my $line (@revlines) {
-            my ($rev, $head, $commiter) = split /\0/, $line, 3;
-            $head =~ s!^refs/heads/!!;
-
-            push @ret, { sha1 => $rev, name => $head };
-
-            #FIXME: That isn't the time I'm looking for..
-            if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
-                my $dt = DateTime->from_epoch(epoch => $epoch);
-                $dt->set_time_zone($tz);
-                $ret[-1]->{last_change} = $dt;
-            }
+            my ($sha1, $name, $commitinfo) = split /\0/, $line, 3;
+            $name =~ s!^refs/heads/!!;
+
+            my ($committer, $epoch, $tz) =
+                $commitinfo =~ /(.*)\s(\d+)\s+([+-]\d+)$/;
+            my $dt = DateTime->from_epoch(
+                epoch => $epoch,
+                time_zone => $tz,
+            );
+            my $head = Gitalist::Git::Head->new(
+                sha1 => $sha1,
+                name => $name,
+                committer => $committer,
+                last_change => $dt,
+            );
+            push @ret, $head;
         }
 
         return \@ret;
index cc69734..0ac8294 100644 (file)
@@ -50,10 +50,8 @@ ok(keys %references >= 2, '->references hash has elements');
 is($references{'36c6c6708b8360d7023e8a1649c45bcf9b3bd818'}->[0], 'heads/master', 'reference looks ok');
 my @heads = @{$proj->heads};
 ok(scalar @heads > 1, '->heads list has more than one element');
-my %head = %{$heads[1]};
-ok(keys %head == 3, '->heads[1] has the right number of keys');
-ok(defined $head{sha1}, '->heads[1]-sha1 is defined');
-ok(defined $head{name}, '->heads[1]-name is defined');
+my $head = $heads[1];
+isa_ok($head, 'Gitalist::Git::Head');
 is($proj->head_hash, '36c6c6708b8360d7023e8a1649c45bcf9b3bd818', 'head_hash for HEAD is correct');
 is($proj->head_hash('refs/heads/master'), '36c6c6708b8360d7023e8a1649c45bcf9b3bd818', 'head_hash for refs/heads/master is correct');
 is($proj->head_hash('rafs/head/mister'), undef, 'head_hash for rafs/head/mister is undef');
diff --git a/t/02git_head.t b/t/02git_head.t
new file mode 100644 (file)
index 0000000..a9133fd
--- /dev/null
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use Test::More qw/no_plan/;
+use Test::Exception;
+use Data::Dumper;
+
+BEGIN { use_ok 'Gitalist::Git::Head' }
+
+
+# Create an instance, passing last_change as a DateTime
+use DateTime;
+my $timespec = [1277578462, '+0100'];
+my $dt = DateTime->from_epoch(
+    epoch => @$timespec[0],
+    time_zone => @$timespec[1],
+);
+my $head = Gitalist::Git::Head->new(
+    sha1 => 'bca1153c22e393a952b6715bf2212901e4e77215',
+    name => 'master',
+    committer => 'Zachary Stevens <zts@cryptocracy.com>',
+    last_change => $dt,
+);
+isa_ok($head, 'Gitalist::Git::Head');