Minimally working snapshot action.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
index 9b1de8a..752b930 100644 (file)
@@ -6,7 +6,9 @@ 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/;
+        use Gitalist::Util qw(to_utf8);
         our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
         has '+type' => ( default => 'commit' );
@@ -22,7 +24,31 @@ 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_fh( @cmd );
+        }
+
+        method diff ( Maybe[Bool] :$patch?,
                        Maybe[NonEmptySimpleStr] :$parent?,
                        Maybe[NonEmptySimpleStr] :$file?
                    ) {
@@ -54,6 +80,18 @@ class Gitalist::Git::Object::Commit
             return \@difftree, [$self->_parse_diff(@out)];
         }
 
+method snapshot ( NonEmptySimpleStr $format ) {
+#    return unless (qw/tar zip/->any($format));
+    my $name = $self->project->name;
+    $name =~ s,([^/])/*\.git$,$1,;
+    my $filename = to_utf8($name);
+    $filename .= "-$self->sha1.$format";
+    $name =~ s/\047/\047\\\047\047/g;
+
+    my @cmd = ('archive', "--format=$format", "--prefix=$name", $self->sha1);
+    return $self->_run_cmd_fh(@cmd);
+}
+
         ## Private methods
         # gitweb uses the following sort of command for diffing merges:
         # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index --cc 316cf158df3f6207afbae7270bcc5ba0 --