More tidying of ::Project.
Zachary Stevens [Thu, 12 Nov 2009 19:48:27 +0000 (19:48 +0000)]
Factor "run_cmd+split on newlines" idiom out into to Util.

lib/Gitalist/Git/HasUtils.pm
lib/Gitalist/Git/Project.pm
lib/Gitalist/Git/Util.pm

index 9560bf4..69addbc 100644 (file)
@@ -13,7 +13,7 @@ after BUILD => sub {
 has _util => ( isa => 'Gitalist::Git::Util',
                is => 'ro',
                lazy_build => 1,
-               handles => [ 'run_cmd', 'get_gpp_object' ],
+               handles => [ 'run_cmd', 'run_cmd_list', 'get_gpp_object' ],
            );
 
 sub _build__util { confess(shift() . " cannot build _util") }
index 80f2568..4862b46 100644 (file)
@@ -28,6 +28,17 @@ class Gitalist::Git::Project with Gitalist::Git::HasUtils {
     use DateTime;
     use aliased 'Gitalist::Git::Object';
 
+    our $SHA1RE = qr/[0-9a-fA-F]{40}/;
+
+    around BUILDARGS (ClassName $class: Dir $dir) {
+        my $name = $dir->dir_list(-1);
+        $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
+        confess("Can't find a git repository at " . $dir)
+            unless ( -f $dir->file('HEAD') );
+        return $class->$orig(name => $name,
+                             path => $dir);
+    }
+
 =head1 ATTRIBUTES
 
 =head2 name
@@ -99,63 +110,33 @@ Bool indicating whether this Project is bare.
         $self->$_() for qw/last_change owner description/; # Ensure to build early.
     }
 
-    around BUILDARGS (ClassName $class: Dir $dir) {
-        my $name = $dir->dir_list(-1);
-        $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
-        confess("Can't find a git repository at " . $dir)
-            unless ( -f $dir->file('HEAD') );
-        return $class->$orig(name => $name,
-                             path => $dir);
-    }
+=head1 METHODS
 
-    method _build__util {
-        Gitalist::Git::Util->new(
-            project => $self,
-        );
-    }
+=head2 head_hash
 
-    our $SHA1RE = qr/[0-9a-fA-F]{40}/;
+Return the sha1 for HEAD, or any specified head.
 
-    method _build_description {
-        my $description = "";
-        eval {
-            $description = $self->path->file('description')->slurp;
-            chomp $description;
-        };
-        return $description;
-    }
+=cut
 
-    method _build_owner {
-        my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
-        $gecos =~ s/,+$//;
-        return length($gecos) ? $gecos : $name;
-    }
+    method head_hash (Str $head?) {
+        my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
+        confess("No such head: " . $head) unless defined $output;
 
-    method _build_last_change {
-        my $last_change;
-        my $output = $self->run_cmd(
-            qw{ for-each-ref --format=%(committer)
-                --sort=-committerdate --count=1 refs/heads
-          });
-        if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
-            my $dt = DateTime->from_epoch(epoch => $epoch);
-            $dt->set_time_zone($tz);
-            $last_change = $dt;
-        }
-        return $last_change;
+        my($sha1) = $output =~ /^($SHA1RE)$/;
+        return $sha1;
     }
 
+
 =head2 heads
 
-Return an array containing the list of heads.
+Returns a list of hashes containing the name and sha1 of all heads.
 
 =cut
 
     method heads {
-        my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
-        my @output = $cmdout ? split(/\n/, $cmdout) : ();
+        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 (@output) {
+        for my $line (@revlines) {
             my ($rev, $head, $commiter) = split /\0/, $line, 3;
             $head =~ s!^refs/heads/!!;
 
@@ -174,20 +155,18 @@ Return an array containing the list of heads.
 
 =head2 references
 
-Return a hash of references.
+Returns a hash of references.
 
 =cut
 
     has references => ( isa => HashRef[ArrayRef[Str]], is => 'ro', lazy_build => 1 );
 
     method _build_references {
-
        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
        # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
-       my $cmdout = $self->run_cmd(qw(show-ref --dereference))
+       my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
                or return;
-            my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
-           my %refs;
+        my %refs;
            for(@reflist) {
                    push @{$refs{$1}}, $2
                            if m!^($SHA1RE)\srefs/(.*)$!;
@@ -196,20 +175,6 @@ Return a hash of references.
            return \%refs;
     }
 
-=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.
@@ -272,10 +237,10 @@ The keys for each item will be:
     method hash_by_path ($base, $path?, $type?) {
         $path ||= '';
         $path =~ s{/+$}();
-
-        my $output = $self->run_cmd('ls-tree', $base, '--', $path)
+        # FIXME should this really just take the first result?
+        my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
             or return;
-        my($line) = $output ? split(/\n/, $output) : ();
+        my $line = $paths[0];
 
         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
         $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
@@ -397,11 +362,10 @@ The keys for each item will be:
 # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 --
 
     method raw_diff (@args) {
-        my $cmdout = $self->run_cmd(
+        return $self->run_cmd_list(
             qw(diff-tree -r -M --no-commit-id --full-index),
             @args
         );
-        return $cmdout ? split(/\n/, $cmdout) : ();
     }
 
     method parse_diff_tree ($diff) {
@@ -490,6 +454,41 @@ be:
         };
     };
 
+    method _build__util {
+        Gitalist::Git::Util->new(
+            project => $self,
+        );
+    }
+
+    method _build_description {
+        my $description = "";
+        eval {
+            $description = $self->path->file('description')->slurp;
+            chomp $description;
+        };
+        return $description;
+    }
+
+    method _build_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(
+            qw{ for-each-ref --format=%(committer)
+                --sort=-committerdate --count=1 refs/heads
+          });
+        if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
+            my $dt = DateTime->from_epoch(epoch => $epoch);
+            $dt->set_time_zone($tz);
+            $last_change = $dt;
+        }
+        return $last_change;
+    }
+
 =head1 SEE ALSO
 
 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
index a7865b7..ece7b51 100644 (file)
@@ -40,12 +40,17 @@ EOR
     method run_cmd (@args) {
         unshift @args, ( '--git-dir' => $self->gitdir )
             if $self->has_project;
-
+#        print STDERR 'RUNNING: ', $self->_git, qq[ @args], $/;
         run [$self->_git, @args], \my($in, $out, $err);
 
         return $out;
     }
 
+    method run_cmd_list (@args) {
+        my $cmdout = $self->run_cmd(@args);
+        return $cmdout ? split(/\n/, $cmdout) : ();
+    }
+
     method get_gpp_object (NonEmptySimpleStr $sha1) {
         return $self->_gpp->get_object($sha1) || undef;
     }