Merge branch 'action-patches' into patches-intg
Zachary Stevens [Thu, 19 Nov 2009 01:09:26 +0000 (01:09 +0000)]
gitalist.conf
lib/Gitalist/Controller/Root.pm
lib/Gitalist/Git/Object/Commit.pm
t/01app.t
t/02git_object.t
t/03legacy_uri.t
t/gitalist.conf

index 5eb974f..670b178 100644 (file)
@@ -44,3 +44,7 @@ project_maxdepth 2007
   log = 50
   summary = 17
 </paging>
+
+<patches>
+  max = 16
+</patches>
index dfb43e3..17c8750 100644 (file)
@@ -442,18 +442,18 @@ sub rss : Local {
 
 sub patch : Local {
     my ($self, $c) = @_;
+    $c->detach('patches', [1]);
+}
+
+sub patches : Local {
+    my ($self, $c, $count) = @_;
+    $count ||= Gitalist->config->{patches}{max};
     my $commit = $self->_get_object($c);
     my $parent = $c->req->param('hp') || undef;
-    my $patch = $commit->patch( $parent );
+    my $patch = $commit->get_patch( $parent, $count );
     $c->response->body($patch);
     $c->response->content_type('text/plain');
     $c->response->status(200);
-
-}
-
-sub patches : Local {
-    # FIXME - implement patches
-    Carp::croak "Not implemented.";
 }
 
 sub snapshot : Local {
index c521b5a..d42e41b 100644 (file)
@@ -6,6 +6,7 @@ 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}/;
 
@@ -22,16 +23,28 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
-        method patch ( Maybe[NonEmptySimpleStr] $parent? ) {
-            my @args = qw/format-patch --encoding=utf8 --stdout -1/;
-            my $refspec = $self->sha1;
-            if (defined $parent) {
-                push @args, '-n';
-                $refspec = $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 {
+                #  if not, but a merge commit: --cc h
+                #  otherwise: --root h
+                push @cmd, $self->parents->length > 1
+                    ? '--cc' : '--root';
+                push @cmd, $self->sha1;
             }
-            push @args, '--root', $refspec;
-            my $out = $self->_run_cmd( @args );
-            return $out;
+            return $self->_run_cmd( @cmd );
         }
 
         method diff ( Maybe[Bool] :$patch?,
index e4d270c..422e82a 100644 (file)
--- a/t/01app.t
+++ b/t/01app.t
@@ -34,6 +34,9 @@ is request('/summary?p=DoesNotExist')->code, 404,
   test('/patch');
   test('/patch', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e');
   test('/patch', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e;hp=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
+  test('/patches');
+  test('/patches', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e');
+  test('/patches', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e;hp=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
 }
 
 done_testing;
index d722bb0..a3396fc 100644 (file)
@@ -78,7 +78,7 @@ is($patch->{diff}, '--- a/file1
 ', 'patch->{diff} is correct');
 is($patch->{dst}, '5716ca5987cbf97d6bb54920bea6adde242d87e6', 'patch->{dst} is correct');
 
-is($commit_obj->patch, 'From 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001
+is($commit_obj->get_patch, 'From 3f7567c7bdf7e7ebf410926493b92d398333116e Mon Sep 17 00:00:00 2001
 From: Florian Ragwitz <rafl@debian.org>
 Date: Tue, 6 Mar 2007 20:39:45 +0100
 Subject: [PATCH] bar
@@ -97,4 +97,6 @@ index 257cc56..5716ca5 100644
 -- 
 1.6.4.2
 
-', 'commit_obj->patch is correct');
+', 'commit_obj->get_patch can return a patch');
+
+like($commit_obj->get_patch(undef, 3), qr!PATCH 2/2!, 'commit_obj->get_patch can return a patchset');
index 998656c..871dc08 100644 (file)
@@ -140,16 +140,13 @@ test('/', 'a=patch;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
 test('/', 'a=patch;hb=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
 test('/', 'a=patch;hb=3f7567c7bdf7e7ebf410926493b92d398333116e');
 
-TODO: {
-    local $TODO = "Action: patches is not yet implemented.";
-    test('/', 'a=patches');
-    test('/', 'a=patches;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
-    test('/', 'a=patches;h=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
-    test('/', 'a=patches;h=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patches;h=HEAD');
-    test('/', 'a=patches;h=master');
-    test('/', 'a=patches;h=refs/heads/master');
-}
+test('/', 'a=patches');
+test('/', 'a=patches;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
+test('/', 'a=patches;h=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
+test('/', 'a=patches;h=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patches;h=HEAD');
+test('/', 'a=patches;h=master');
+test('/', 'a=patches;h=refs/heads/master');
 
 test('/', 'a=search_help');
 
index 2f02d50..6617816 100644 (file)
@@ -43,3 +43,7 @@ project_maxdepth 2007
   log = 50
   summary = 16
 </paging>
+
+<patches>
+  max = 16
+</patches>