Make Object aware of its containing repository, and able to return its contents.
Zachary Stevens [Sun, 1 Nov 2009 17:41:32 +0000 (17:41 +0000)]
lib/Gitalist/Git/Object.pm
lib/Gitalist/Git/Project.pm
t/git/object.t
t/git/project.t

index 8eacbef..4dceffa 100644 (file)
@@ -2,25 +2,64 @@ use MooseX::Declare;
 
 class Gitalist::Git::Object {
     use File::Stat::ModeString qw/mode_to_string/;
-
+    has project => ( isa => 'Gitalist::Git::Project',
+                     required => 1,
+                     is => 'ro',
+                     handles => [ 'run_cmd' ],
+                 );
     has sha1 => ( isa => 'Str',
+                  required => 1,
                   is => 'ro' );
     has type => ( isa => 'Str',
-                  is => 'ro' );
+                  required => 1,
+                  is => 'ro',
+                  lazy_build => 1 );
     has file => ( isa => 'Str',
+                  required => 1,
                   is => 'ro' );
     has mode => ( isa => 'Int',
+                  required => 1,
                   is => 'ro' );
     has modestr => ( isa => 'Str',
                      is => 'ro',
                      lazy_build => 1,
                  );
+    has size => ( isa => 'Int',
+                  is => 'ro',
+                  lazy_build => 1);
 
+    method _build_type {
+        my $output = $self->run_cmd(qw/cat-file -t/, $self->{sha1});
+        chomp($output);
+        return $output;
+    }
+    
     method _build_modestr {
         my $modestr = mode_to_string($self->{mode});
         return $modestr;
     }
 
+    method _build_size {
+        my $output = $self->run_cmd(qw/cat-file -s/, $self->{sha1});
+        chomp($output);
+        return $output;
+    }
+
+=head2 contents
+
+Return the contents of a given file.
+
+=cut
 
+    method contents {
+        if ( $self->type ne 'blob' ) {
+            die "object $self->sha1 is not a file\n"
+        }
+
+        my $output = $self->run_cmd(qw/cat-file -p/, $self->sha1);
+        return unless $output;
+
+        return $output;
+    }
 
 } # end class
index ceea61b..eda7d6d 100644 (file)
@@ -106,6 +106,7 @@ The keys for each item will be:
                                     type => $type,
                                     sha1 => $object,
                                     file => $file,
+                                    project => $self,
                                   );
         }
         return @ret;
index 56a47f1..b95f868 100644 (file)
@@ -5,20 +5,40 @@ use Test::More qw/no_plan/;
 
 use Data::Dumper;
 
+use Path::Class;
+use Gitalist::Git::Project;
+my $project = Gitalist::Git::Project->new(
+    path => dir("$Bin/../lib/repositories/repo1"),
+);
+
 BEGIN { use_ok 'Gitalist::Git::Object' }
 
 my $object = Gitalist::Git::Object->new(
+    project => $project,
     sha1 => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
     type => 'tree',
     file => 'dir1',
     mode => 16384,
 );
 isa_ok($object, 'Gitalist::Git::Object');
-
-warn( Dumper($object) );
-is($object->{sha1},'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct');
-is($object->{type}, 'tree', 'type is correct');
-is($object->{file}, 'dir1', 'file is correct');
+is($object->sha1,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'sha1 is correct');
+is($object->type, 'tree', 'type is correct');
+is($object->file, 'dir1', 'file is correct');
 is($object->mode, 16384, 'mode is correct');
 is($object->modestr, 'd---------', "modestr is correct" );
 
+# Create object from hash.
+my $obj2 = Gitalist::Git::Object->new(
+    project => $project,
+    sha1 => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
+    file => 'file1',
+    mode => 33188,
+);
+isa_ok($obj2, 'Gitalist::Git::Object');
+is($obj2->sha1,'5716ca5987cbf97d6bb54920bea6adde242d87e6', 'sha1 is correct');
+is($obj2->type, 'blob', 'type is correct');
+is($obj2->file, 'file1', 'file is correct');
+is($obj2->mode, 33188, 'mode is correct');
+is($obj2->modestr, '-rw-r--r--', "modestr is correct" );
+is($obj2->contents, "bar\n", 'obj2 contents is correct');
+
index 6a8da94..69fbd89 100644 (file)
@@ -26,4 +26,4 @@ is($proj->head_hash, qw/36c6c6708b8360d7023e8a1649c45bcf9b3bd818/, 'head_hash fo
 
 is(scalar $proj->list_tree, 2, 'expected number of entries in tree');
 isa_ok(($proj->list_tree)[0], 'Gitalist::Git::Object');
-warn( Dumper($proj->list_tree) );
+