Merge branch 'master' of git://github.com/zts/Gitalist into zts-patches
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
index dc3299c..d42e41b 100644 (file)
@@ -4,9 +4,13 @@ use MooseX::Declare;
 class Gitalist::Git::Object::Commit
     extends Gitalist::Git::Object
     with Gitalist::Git::Object::HasTree {
+        use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
+        use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+        use Moose::Autobox;
         use List::MoreUtils qw/any zip/;
         our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
+        has '+type' => ( default => 'commit' );
         has '+_gpp_obj' => ( handles => [ 'comment',
                                           'tree_sha1',
                                           'committer',
@@ -19,13 +23,34 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
-         method diff ( Maybe[Bool] :$patch?,
+        method get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
+                           Int $patch_count?) {
+            # assembling the git command to execute...
+            my @cmd = qw/format-patch --encoding=utf8 --stdout/;
+
+            # patch, or patch set?
+            push @cmd,
+                defined $patch_count
+                ? "-$patch_count -n" : "-1";
+
+            # refspec
+            if (defined $parent_hash) {
+                #  if a parent is specified: hp..h
+                push @cmd, "$parent_hash.." . $self->sha1;
+            } else {
+                #  if not, but a merge commit: --cc h
+                #  otherwise: --root h
+                push @cmd, $self->parents->length > 1
+                    ? '--cc' : '--root';
+                push @cmd, $self->sha1;
+            }
+            return $self->_run_cmd( @cmd );
+        }
+
+        method diff ( Maybe[Bool] :$patch?,
                        Maybe[NonEmptySimpleStr] :$parent?,
                        Maybe[NonEmptySimpleStr] :$file?
                    ) {
-#        method diff (:$patch?, :$parent?, :$file?) {
-            # Use parent if specifed, else take the parent from the commit
-            # if there is only one, otherwise it was a merge commit.
             $parent = $parent
                 ? $parent
                     : $self->parents <= 1