Introduce Gitalist::Git::Head.
[catagits/Gitalist.git] / lib / Gitalist / Git / Repository.pm
index e60d803..c07605d 100644 (file)
@@ -3,19 +3,19 @@ use MooseX::Declare;
 class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Path::Class qw/Dir/;
     use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
-    use Gitalist::Git::Types qw/SHA1 DateTime Dir/;
+    use Gitalist::Git::Types qw/SHA1/;
     use Moose::Autobox;
     use List::MoreUtils qw/any zip/;
-    use aliased 'DateTime' => 'DT';
+    use DateTime;
     use Encode qw/decode/;
     use I18N::Langinfo qw/langinfo CODESET/;
     use Gitalist::Git::Object::Blob;
     use Gitalist::Git::Object::Tree;
     use Gitalist::Git::Object::Commit;
     use Gitalist::Git::Object::Tag;
-    
-    with 'Gitalist::Serializeable';
+    use Gitalist::Git::Head;
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
@@ -35,8 +35,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                   is => 'ro', required => 1 );
 
     has path => ( isa => Dir,
-                  is => 'ro', required => 1,
-                  traits => [qw/ DoNotSerialize /] );
+                  is => 'ro', required => 1);
 
     has description => ( isa => Str,
                          is => 'ro',
@@ -48,7 +47,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                    lazy_build => 1,
                );
 
-    has last_change => ( isa => Maybe[DateTime],
+    has last_change => ( isa => Maybe['DateTime'],
                          is => 'ro',
                          lazy_build => 1,
                      );
@@ -61,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],
@@ -72,16 +71,11 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                         lazy_build => 1 );
 
     method BUILD {
-        $self->$_() for qw/last_change owner description/; # Ensure to build early.
+        $self->$_() for qw/last_change owner description /; # Ensure to build early.
     }
 
     ## Public methods
 
-    method get_object_or_head (NonEmptySimpleStr $ref) {
-        my $sha1 = is_SHA1($ref) ? $ref : $self->head_hash($ref);
-        $self->get_object($sha1);
-    }
-
     method head_hash (Str $head?) {
         my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
         confess("No such head: " . $head) unless defined $output;
@@ -90,12 +84,6 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         return $sha1;
     }
 
-    method list_tree (SHA1 $sha1?) {
-        $sha1 ||= $self->head_hash;
-        my $object = $self->get_object($sha1);
-        return @{$object->tree};
-    }
-
     method get_object (NonEmptySimpleStr $sha1) {
         unless (is_SHA1($sha1)) {
             $sha1 = $self->head_hash($sha1);
@@ -110,20 +98,6 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         );
     }
 
-    method hash_by_path ($base, $path = '', $type?) {
-        $path =~ s{/+$}();
-        # FIXME should this really just take the first result?
-        my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
-            or return;
-        my $line = $paths[0];
-
-        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
-        $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
-        return defined $type && $type ne $2
-            ? ()
-                : $3;
-    }
-
     method list_revs ( NonEmptySimpleStr :$sha1!,
                        Int :$count?,
                        Int :$skip?,
@@ -253,7 +227,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                 --sort=-committerdate --count=1 refs/heads
           });
         if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
-            my $dt = DT->from_epoch(epoch => $epoch);
+            my $dt = DateTime->from_epoch(epoch => $epoch);
             $dt->set_time_zone($tz);
             $last_change = $dt;
         }
@@ -264,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 = DT->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;
@@ -297,7 +276,7 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
 
             #FIXME: That isn't the time I'm looking for..
             if($epoch and $tz) {
-                my $dt = DT->from_epoch(epoch => $epoch);
+                my $dt = DateTime->from_epoch(epoch => $epoch);
                 $dt->set_time_zone($tz);
                 $ret[-1]->{last_change} = $dt;
             }
@@ -393,20 +372,10 @@ Hashref of ArrayRefs for each reference.
 
 Return the sha1 for HEAD, or any specified head.
 
-=head2 list_tree ($sha1?)
-
-Return an array of contents for a given tree.
-The tree is specified by sha1, and defaults to HEAD.
-Each item is a L<Gitalist::Git::Object>.
-
 =head2 get_object ($sha1)
 
 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
 
-=head2 hash_by_path ($sha1, $path, $type?)
-
-Returns the sha1 for a given path, optionally limited by type.
-
 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
 
 Returns a list of revs for the given head ($sha1).