Lose some code by not converting to/from path::class objects, and also by flattening...
[catagits/Gitalist.git] / lib / Gitalist / Git / Project.pm
index d5671b6..de3f05f 100644 (file)
@@ -3,16 +3,20 @@ use MooseX::Declare;
 class Gitalist::Git::Project {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+    use MooseX::Types::Moose qw/Str Maybe/;
     use DateTime;
     use Path::Class;
     use Gitalist::Git::Util;
+    use aliased 'Gitalist::Git::Object';
+
+    our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
     has name => ( isa => NonEmptySimpleStr,
-                  is => 'ro' );
+                  is => 'ro', required => 1 );
     has path => ( isa => "Path::Class::Dir",
-                  is => 'ro');
+                  is => 'ro', required => 1);
 
-    has description => ( isa => NonEmptySimpleStr,
+    has description => ( isa => Str,
                          is => 'ro',
                          lazy_build => 1,
                      );
@@ -20,7 +24,7 @@ class Gitalist::Git::Project {
                    is => 'ro',
                    lazy_build => 1,
                );
-    has last_change => ( isa => 'DateTime',
+    has last_change => ( isa => Maybe['DateTime'],
                          is => 'ro',
                          lazy_build => 1,
                      );
@@ -30,25 +34,31 @@ class Gitalist::Git::Project {
                    handles => [ 'run_cmd' ],
                );
 
+    method BUILD {
+        $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
+    }
+
     method _build__util {
-        my $util = Gitalist::Git::Util->new(
+        Gitalist::Git::Util->new(
             gitdir => $self->path,
         );
-        return $util;
     }
-    
+
     method _build_description {
-        my $description = $self->path->file('description')->slurp;
-        chomp $description;
+        my $description = "";
+        eval {
+            $description = $self->path->file('description')->slurp;
+            chomp $description;
+        };
         return $description;
     }
 
     method _build_owner {
-        my $owner = (getpwuid $self->path->stat->uid)[6];
-        $owner =~ s/,+$//;
-        return $owner;
+        my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
+        $gecos =~ s/,+$//;
+        return length($gecos) ? $gecos : $name;
     }
-    
+
     method _build_last_change {
         my $last_change;
         my $output = $self->run_cmd(
@@ -63,16 +73,55 @@ class Gitalist::Git::Project {
         return $last_change;
     }
 
-    method project_dir (Path::Class::Dir $project) {
-        my $dir = $project->stringify;
-        $dir .= '/.git'
-            if -f dir($dir)->file('.git/HEAD');
-        return $dir;
+=head2 head_hash
+
+Find the hash of a given head (defaults to HEAD).
+
+=cut
+
+    method head_hash (Str $head?) {
+        my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
+        return unless defined $output;
+
+        my($sha1) = $output =~ /^($SHA1RE)$/;
+        return $sha1;
+    }
+
+=head2 list_tree
+
+Return an array of contents for a given tree.
+The tree is specified by sha1, and defaults to HEAD.
+The keys for each item will be:
+
+       mode
+       type
+       object
+       file
+
+=cut
+
+    method list_tree (Str $sha1?) {
+        $sha1 ||= $self->head_hash;
+
+        my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
+        return unless defined $output;
+
+        my @ret;
+        for my $line (split /\0/, $output) {
+            my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
+            push @ret, Object->new( mode => oct $mode,
+                                    type => $type,
+                                    sha1 => $object,
+                                    file => $file,
+                                    project => $self,
+                                  );
+        }
+        return @ret;
     }
 
     # Compatibility
 
-=head2 project_info
+=head2 info
 
 Returns a hash containing properties of this project. The keys will
 be:
@@ -84,7 +133,7 @@ be:
 
 =cut
 
-    method project_info {
+    method info {
         return {
             name => $self->name,
             description => $self->description,
@@ -92,5 +141,5 @@ be:
             last_change => $self->last_change,
         };
     };
-    
+
 } # end class