Implement the 'patch' action.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
index 7c6ebf0..efd51a9 100644 (file)
@@ -6,9 +6,11 @@ class Gitalist::Git::Object::Commit
     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',
@@ -21,7 +23,33 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
-         method diff ( Maybe[Bool] :$patch?,
+        method patch ( Maybe[NonEmptySimpleStr] $parent?,
+                       Int $count?) {
+            $count ||= 1;
+            # Assemble the git command.
+            # common args:
+            my @cmd_args = qw/format-patch --encoding=utf8 --stdout/;
+            # single patch, or patch set?
+            if ($count > 1) {
+                push @cmd_args, "-$count", "-n";
+            } else {
+                push @cmd_args, "-1";
+            }
+            # if a parent is specified: hp..h
+            # if not, but a merge commit: --cc h
+            # otherwise: --root h
+            if (defined $parent) {
+                push @cmd_args, "$parent.." . $self->sha1;
+            } else {
+                push @cmd_args, $self->parents->length > 1
+                    ? '--cc' : '--root';
+                push @cmd_args, $self->sha1;
+            }
+            my $out = $self->_run_cmd( @cmd_args );
+            return $out;
+        }
+
+        method diff ( Maybe[Bool] :$patch?,
                        Maybe[NonEmptySimpleStr] :$parent?,
                        Maybe[NonEmptySimpleStr] :$file?
                    ) {