Add list_tree method to Project, returning an array of
Zachary Stevens [Sun, 1 Nov 2009 15:53:52 +0000 (15:53 +0000)]
Gitalist::Git::Object.

lib/Gitalist/Git/Object.pm [new file with mode: 0644]
lib/Gitalist/Git/Project.pm
t/git/object.t [new file with mode: 0644]
t/git/project.t

diff --git a/lib/Gitalist/Git/Object.pm b/lib/Gitalist/Git/Object.pm
new file mode 100644 (file)
index 0000000..8eacbef
--- /dev/null
@@ -0,0 +1,26 @@
+use MooseX::Declare;
+
+class Gitalist::Git::Object {
+    use File::Stat::ModeString qw/mode_to_string/;
+
+    has sha1 => ( isa => 'Str',
+                  is => 'ro' );
+    has type => ( isa => 'Str',
+                  is => 'ro' );
+    has file => ( isa => 'Str',
+                  is => 'ro' );
+    has mode => ( isa => 'Int',
+                  is => 'ro' );
+    has modestr => ( isa => 'Str',
+                     is => 'ro',
+                     lazy_build => 1,
+                 );
+
+    method _build_modestr {
+        my $modestr = mode_to_string($self->{mode});
+        return $modestr;
+    }
+
+
+
+} # end class
index 3fdce70..5209149 100644 (file)
@@ -6,6 +6,7 @@ class Gitalist::Git::Project {
     use DateTime;
     use Path::Class;
     use Gitalist::Git::Util;
+    use aliased 'Gitalist::Git::Object';
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
     
@@ -79,6 +80,38 @@ Find the hash of a given head (defaults to HEAD).
         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,
+                                  );
+        }
+        return @ret;
+    }
+
+
     method project_dir (Path::Class::Dir $project) {
         my $dir = $project->stringify;
         $dir .= '/.git'
diff --git a/t/git/object.t b/t/git/object.t
new file mode 100644 (file)
index 0000000..56a47f1
--- /dev/null
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use FindBin qw/$Bin/;
+use Test::More qw/no_plan/;
+
+use Data::Dumper;
+
+BEGIN { use_ok 'Gitalist::Git::Object' }
+
+my $object = Gitalist::Git::Object->new(
+    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->mode, 16384, 'mode is correct');
+is($object->modestr, 'd---------', "modestr is correct" );
+
index ed6ebcb..21f7731 100644 (file)
@@ -22,3 +22,7 @@ is($proj->description, qq/some test repository/, 'repository description loaded'
 isa_ok($proj->last_change, 'DateTime', 'last_change');
 
 is($proj->head_hash, qw/36c6c6708b8360d7023e8a1649c45bcf9b3bd818/, 'head_hash for HEAD is correct');
+
+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) );