Minimally working snapshot action.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object / Commit.pm
index efd51a9..752b930 100644 (file)
@@ -8,6 +8,7 @@ class Gitalist::Git::Object::Commit
         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' );
@@ -23,30 +24,28 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
-        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;
+        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 {
-                push @cmd_args, $self->parents->length > 1
+                #  if not, but a merge commit: --cc h
+                #  otherwise: --root h
+                push @cmd, $self->parents->length > 1
                     ? '--cc' : '--root';
-                push @cmd_args, $self->sha1;
+                push @cmd, $self->sha1;
             }
-            my $out = $self->_run_cmd( @cmd_args );
-            return $out;
+            return $self->_run_cmd_fh( @cmd );
         }
 
         method diff ( Maybe[Bool] :$patch?,
@@ -81,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 --