Implement 'patch' action.
Zachary Stevens [Wed, 18 Nov 2009 00:37:00 +0000 (00:37 +0000)]
lib/Gitalist/Controller/Root.pm
lib/Gitalist/Git/Object/Commit.pm
t/01app.t
t/02git_object.t
t/03legacy_uri.t

index 69237a1..c535f2b 100644 (file)
@@ -385,8 +385,14 @@ sub blob_plain : Local {
 }
 
 sub patch : Local {
-    # FIXME - implement patches
-    Carp::croak "Not implemented.";
+    my ($self, $c) = @_;
+    my $commit = $self->_get_object($c);
+    my $parent = $c->req->param('hp') || undef;
+    my $patch = $commit->patch( $parent );
+    $c->response->body($patch);
+    $c->response->content_type('text/plain');
+    $c->response->status(200);
+
 }
 
 sub patches : Local {
index 9b1de8a..c521b5a 100644 (file)
@@ -22,7 +22,19 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
-         method diff ( Maybe[Bool] :$patch?,
+        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;
+            }
+            push @args, '--root', $refspec;
+            my $out = $self->_run_cmd( @args );
+            return $out;
+        }
+
+        method diff ( Maybe[Bool] :$patch?,
                        Maybe[NonEmptySimpleStr] :$parent?,
                        Maybe[NonEmptySimpleStr] :$file?
                    ) {
index 9ebc7a4..e4d270c 100644 (file)
--- a/t/01app.t
+++ b/t/01app.t
@@ -31,6 +31,9 @@ is request('/summary?p=DoesNotExist')->code, 404,
   test('/search', 'h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818&f=&type=commit&text=added');
   test('/blobdiff', 'f=file1;h=5716ca5987cbf97d6bb54920bea6adde242d87e6;hp=257cc5642cb1a054f08cc83f2d943e56fd3ebe99;hb=refs/heads/master;hpb=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
   test('/blob', 'f=dir1/file2;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
+  test('/patch');
+  test('/patch', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e');
+  test('/patch', 'h=3f7567c7bdf7e7ebf410926493b92d398333116e;hp=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
 }
 
 done_testing;
index 479a890..55075f0 100644 (file)
@@ -77,3 +77,18 @@ is($patch->{diff}, '--- a/file1
 +bar
 ', '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
+From: Florian Ragwitz <rafl@debian.org>
+Date: Tue, 6 Mar 2007 20:39:45 +0100
+Subject: [PATCH] bar
+
+
+diff --git a/file1 b/file1
+index 257cc56..5716ca5 100644
+--- a/file1
++++ b/file1
+@@ -1 +1 @@
+-foo
++bar
+', 'commit_obj->patch is correct');
index c0a25cc..a369537 100644 (file)
@@ -134,24 +134,21 @@ test('/', 'a=log;h=HEAD');
 test('/', 'a=log;h=master');
 test('/', 'a=log;h=refs/heads/master');
 
-TODO: {
-    local $TODO = "Action: patch is not yet implemented.";
-    test('/', 'a=patch');
-    test('/', 'a=patch;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
-    test('/', 'a=patch;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patch;h=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
-    test('/', 'a=patch;h=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patch;h=3f7567c7bdf7e7ebf410926493b92d398333116e;hp=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
-    test('/', 'a=patch;h=HEAD');
-    test('/', 'a=patch;h=HEAD;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patch;h=master');
-    test('/', 'a=patch;h=master;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patch;h=refs/heads/master');
-    test('/', 'a=patch;h=refs/heads/master;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
-    test('/', 'a=patch;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
-    test('/', 'a=patch;hb=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
-    test('/', 'a=patch;hb=3f7567c7bdf7e7ebf410926493b92d398333116e');
-}
+test('/', 'a=patch');
+test('/', 'a=patch;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
+test('/', 'a=patch;h=36c6c6708b8360d7023e8a1649c45bcf9b3bd818;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patch;h=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
+test('/', 'a=patch;h=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patch;h=3f7567c7bdf7e7ebf410926493b92d398333116e;hp=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
+test('/', 'a=patch;h=HEAD');
+test('/', 'a=patch;h=HEAD;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patch;h=master');
+test('/', 'a=patch;h=master;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patch;h=refs/heads/master');
+test('/', 'a=patch;h=refs/heads/master;hp=3f7567c7bdf7e7ebf410926493b92d398333116e');
+test('/', 'a=patch;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
+test('/', 'a=patch;hb=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
+test('/', 'a=patch;hb=3f7567c7bdf7e7ebf410926493b92d398333116e');
 
 TODO: {
     local $TODO = "Action: patches is not yet implemented.";