Default ::Object attributes tree_sha1 and comment to '' when not present.
[catagits/Gitalist.git] / lib / Gitalist / Git / Project.pm
index 844dcf3..7f25e9c 100644 (file)
@@ -1,13 +1,12 @@
 use MooseX::Declare;
 
-class Gitalist::Git::Project {
+class Gitalist::Git::Project with Gitalist::Git::HasUtils {
     # FIXME, use Types::Path::Class and coerce
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
     use DateTime;
     use MooseX::Types::Path::Class qw/Dir/;
     use List::MoreUtils qw/any zip/;
-    use Gitalist::Git::Util;
     use aliased 'Gitalist::Git::Object';
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
@@ -29,11 +28,6 @@ class Gitalist::Git::Project {
                          is => 'ro',
                          lazy_build => 1,
                      );
-    has _util => ( isa => 'Gitalist::Git::Util',
-                   is => 'ro',
-                   lazy_build => 1,
-                   handles => [ 'run_cmd', 'get_gpp_object' ],
-               );
 
     has project_dir => ( isa => Dir,
         is => 'ro',
@@ -61,7 +55,7 @@ class Gitalist::Git::Project {
     );
 
     method BUILD {
-        $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
+        $self->$_() for qw/last_change owner description/; # Ensure to build early.
     }
 
     method _project_dir {
@@ -195,13 +189,16 @@ The keys for each item will be:
         return @ret;
     }
 
-    method get_object (Str $sha1) {
+    method get_object (NonEmptySimpleStr $sha1) {
+        unless ( $self->valid_rev($sha1) ) {
+            $sha1 = $self->head_hash($sha1);
+        }
         return Object->new(
             project => $self,
             sha1 => $sha1,
         );
     }
-    
+
     # Should be in ::Object
     method get_object_mode_string (Gitalist::Git::Object $object) {
         return unless $object && $object->{mode};
@@ -289,7 +286,7 @@ The keys for each item will be:
     # XXX Ideally this would return a wee object instead of ad hoc structures.
     method diff ( Gitalist::Git::Object :$commit,
                   Bool :$patch?,
-                  NonEmptySimpleStr :$parent?,
+                  Maybe[NonEmptySimpleStr] :$parent?,
                   NonEmptySimpleStr :$file? ) {
         # Use parent if specifed, else take the parent from the commit
         # if there is only one, otherwise it was a merge commit.
@@ -304,7 +301,8 @@ The keys for each item will be:
 
         my @out = $self->raw_diff(
             ( $patch ? '--patch-with-raw' : () ),
-            $parent, $commit->sha1, @etc
+            ( $parent ? $parent : () ),
+            $commit->sha1, @etc,
         );
 
         # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
@@ -382,6 +380,50 @@ The keys for each item will be:
         return @ret;
     }
 
+    method reflog (@logargs) {
+        my @entries
+            =  $self->run_cmd(qw(log -g), @logargs)
+                =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
+
+=pod
+  commit 02526fc15beddf2c64798a947fecdd8d11bf993d
+  Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
+  Reflog message: push
+  Author: Foo Barsby <fbarsby@example.com>
+  Date:   Thu Sep 17 12:26:05 2009 +0100
+
+      Merge branch 'abc123'
+
+=cut
+
+        return map {
+            # XXX Stuff like this makes me want to switch to Git::PurePerl
+            my($sha1, $type, $author, $date)
+                = m{
+                       ^ commit \s+ ($SHA1RE)$
+                       .*?
+                       Reflog[ ]message: \s+ (.+?)$ \s+
+                     Author: \s+ ([^<]+) <.*?$ \s+
+                   Date: \s+ (.+?)$
+               }xms;
+
+            pos($_) = index($_, $date) + length $date;
+
+            # Yeah, I just did that.
+            my($msg) = /\G\s+(\S.*)/sg;
+            {
+                hash    => $sha1,
+                type    => $type,
+                author  => $author,
+
+                # XXX Add DateTime goodness.
+                date    => $date,
+                message => $msg,
+            }
+            ;
+        } @entries;
+    }
+
     # Compatibility
 
 =head2 info