From: Zachary Stevens Date: Sun, 1 Nov 2009 15:53:52 +0000 (+0000) Subject: Add list_tree method to Project, returning an array of X-Git-Tag: 0.000000_01~100^2~15^2~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8c0e3a9c12276c93ec795ef8d31fdd055a409a14;p=catagits%2FGitalist.git Add list_tree method to Project, returning an array of Gitalist::Git::Object. --- diff --git a/lib/Gitalist/Git/Object.pm b/lib/Gitalist/Git/Object.pm new file mode 100644 index 0000000..8eacbef --- /dev/null +++ b/lib/Gitalist/Git/Object.pm @@ -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 diff --git a/lib/Gitalist/Git/Project.pm b/lib/Gitalist/Git/Project.pm index 3fdce70..5209149 100644 --- a/lib/Gitalist/Git/Project.pm +++ b/lib/Gitalist/Git/Project.pm @@ -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 index 0000000..56a47f1 --- /dev/null +++ b/t/git/object.t @@ -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" ); + diff --git a/t/git/project.t b/t/git/project.t index ed6ebcb..21f7731 100644 --- a/t/git/project.t +++ b/t/git/project.t @@ -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) );