Refactor blob, make blob_plain work, move rss/atom and snapshot.
Tomas Doran [Mon, 18 Jan 2010 02:42:03 +0000 (02:42 +0000)]
lib/Gitalist/Controller/Commit.pm
lib/Gitalist/Controller/Fragment/Commit.pm
lib/Gitalist/Controller/Repository.pm
lib/Gitalist/Controller/Root.pm
lib/Gitalist/URIStructure/Commit.pm

index ac2024a..2d3151c 100644 (file)
@@ -8,4 +8,30 @@ with 'Gitalist::URIStructure::Commit';
 
 sub base : Chained('/repository/find') PathPart('') CaptureArgs(0) {}
 
+sub blob_plain : Chained('find_blob') Does('FilenameArgs') Args() {
+    my ($self, $c) = @_;
+
+    $c->response->content_type('text/plain; charset=utf-8');
+    $c->response->body(delete $c->stash->{blob});
+}
+
+=head2 snapshot
+
+Provides a snapshot of a given commit.
+
+=cut
+
+sub snapshot : Chained('base') Args() {
+    my ($self, $c, $format) = @_;
+    $format ||= 'tgz';
+    my @snap = $c->stash->{Repository}->snapshot(
+        sha1 => $c->stash->{Commit}->sha1,
+        format => $format
+    );
+    $c->response->status(200);
+    $c->response->headers->header( 'Content-Disposition' =>
+                                       "attachment; filename=$snap[0]");
+    $c->response->body($snap[1]);
+}
+
 __PACKAGE__->meta->make_immutable;
index 2f84ab1..ae1d3b2 100644 (file)
@@ -74,14 +74,7 @@ The blob action i.e the contents of a file.
 
 after blob => sub {
     my ( $self, $c ) = @_;
-
-    my $repository = $c->stash->{Repository};
-    my $h  =
-          $repository->hash_by_path($c->stash->{Commit}->sha1, $c->stash->{filename})
-          || die "No file or sha1 provided.";
-     my $blob = $repository->get_object($h);
     $c->stash(
-        blob     => $blob->content,
         # XXX Hack hack hack, see View::SyntaxHighlight
         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
     );
index fbba9dd..c890d95 100644 (file)
@@ -1,5 +1,9 @@
 package Gitalist::Controller::Repository;
 use Moose;
+use XML::Atom::Feed;
+use XML::Atom::Entry;
+use XML::RSS;
+use Sys::Hostname qw/hostname/;
 use namespace::autoclean;
 
 BEGIN { extends 'Gitalist::Controller' }
@@ -7,4 +11,79 @@ with 'Gitalist::URIStructure::Repository';
 
 sub base : Chained('/base') PathPart('') CaptureArgs(0) {}
 
+=head2 atom
+
+Provides an atom feed for a given repository.
+
+=cut
+
+sub atom : Chained('find') Args(0) {
+  my($self, $c) = @_;
+
+  my $feed = XML::Atom::Feed->new;
+
+  my $host = lc hostname();
+  $feed->title($host . ' - ' . Gitalist->config->{name});
+  $feed->updated(~~DateTime->now);
+
+  my $repository = $c->stash->{Repository};
+  my %logargs = (
+      sha1   => $repository->head_hash,
+      count  => Gitalist->config->{paging}{log} || 25,
+  );
+
+  my $mk_title = $c->stash->{short_cmt};
+  for my $commit ($repository->list_revs(%logargs)) {
+    my $entry = XML::Atom::Entry->new;
+    $entry->title( $mk_title->($commit->comment) );
+    $entry->id($c->uri_for_action('/commit/commit', [$repository->name, $commit->sha1]));
+    # XXX FIXME Needs work ...
+    $entry->content($commit->comment);
+    $feed->add_entry($entry);
+  }
+
+  $c->response->body($feed->as_xml);
+  $c->response->content_type('application/atom+xml');
+}
+
+=head2 rss
+
+Provides an RSS feed for a given repository.
+
+=cut
+
+sub rss : Chained('find') Args(0) {
+  my ($self, $c) = @_;
+
+  my $repository = $c->stash->{Repository};
+
+  my $rss = XML::RSS->new(version => '2.0');
+  $rss->channel(
+    title          => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
+    link           => $c->uri_for('summary', {p=>$repository->name}),
+    language       => 'en',
+    description    => $repository->description,
+    pubDate        => DateTime->now,
+    lastBuildDate  => DateTime->now,
+  );
+
+  my %logargs = (
+      sha1   => $repository->head_hash,
+      count  => Gitalist->config->{paging}{log} || 25,
+#      ($c->req->param('f') ? (file => $c->req->param('f')) : ())
+  );
+  my $mk_title = $c->stash->{short_cmt};
+  for my $commit ($repository->list_revs(%logargs)) {
+    # XXX FIXME Needs work ....
+    $rss->add_item(
+        title       => $mk_title->($commit->comment),
+        permaLink   => $c->uri_for_action('/commit/commit', [$repository->name, $commit->sha1]),
+        description => $commit->comment,
+    );
+  }
+
+  $c->response->body($rss->as_string);
+  $c->response->content_type('application/rss+xml');
+}
+
 __PACKAGE__->meta->make_immutable;
index eb66233..b713839 100644 (file)
@@ -2,10 +2,7 @@ package Gitalist::Controller::Root;
 
 use Moose;
 use Moose::Autobox;
-use Sys::Hostname ();
-use XML::Atom::Feed;
-use XML::Atom::Entry;
-use XML::RSS;
+use Sys::Hostname qw/hostname/;
 use XML::OPML::SimpleGen;
 
 use Gitalist::Utils qw/ age_string /;
@@ -73,23 +70,6 @@ sub _blob_objs {
   return $blob, $repository->get_object($hb), $filename;
 }
 
-
-
-=head2 blob_plain
-
-The plain text version of blob, where file is rendered as is.
-
-=cut
-
-sub blob_plain : Chained('base') Args(0) {
-  my($self, $c) = @_;
-
-  my($blob) = $self->_blob_objs($c);
-  $c->response->content_type('text/plain; charset=utf-8');
-  $c->response->body($blob->content);
-  $c->response->status(200);
-}
-
 =head2 blobdiff_plain
 
 The plain text version of blobdiff.
@@ -203,95 +183,14 @@ Provides some help for the search form.
 
 =cut
 
-sub search_help : Chained('base') Args(0) {
-    my ($self, $c) = @_;
-    $c->stash(template => 'search_help.tt2');
-}
-
-=head2 atom
-
-Provides an atom feed for a given repository.
-
-=cut
-
-sub atom : Chained('base') Args(0) {
-  my($self, $c) = @_;
-
-  my $feed = XML::Atom::Feed->new;
-
-  my $host = lc Sys::Hostname::hostname();
-  $feed->title($host . ' - ' . Gitalist->config->{name});
-  $feed->updated(~~DateTime->now);
-
-  my $repository = $c->stash->{Repository};
-  my %logargs = (
-      sha1   => $repository->head_hash,
-      count  => Gitalist->config->{paging}{log} || 25,
-      ($c->req->param('f') ? (file => $c->req->param('f')) : ())
-  );
-
-  my $mk_title = $c->stash->{short_cmt};
-  for my $commit ($repository->list_revs(%logargs)) {
-    my $entry = XML::Atom::Entry->new;
-    $entry->title( $mk_title->($commit->comment) );
-    $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
-    # XXX Needs work ...
-    $entry->content($commit->comment);
-    $feed->add_entry($entry);
-  }
-
-  $c->response->body($feed->as_xml);
-  $c->response->content_type('application/atom+xml');
-  $c->response->status(200);
-}
-
-=head2 rss
-
-Provides an RSS feed for a given repository.
-
-=cut
-
-sub rss : Chained('base') Args(0) {
-  my ($self, $c) = @_;
-
-  my $repository = $c->stash->{Repository};
-
-  my $rss = XML::RSS->new(version => '2.0');
-  $rss->channel(
-    title          => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
-    link           => $c->uri_for('summary', {p=>$repository->name}),
-    language       => 'en',
-    description    => $repository->description,
-    pubDate        => DateTime->now,
-    lastBuildDate  => DateTime->now,
-  );
-
-  my %logargs = (
-      sha1   => $repository->head_hash,
-      count  => Gitalist->config->{paging}{log} || 25,
-      ($c->req->param('f') ? (file => $c->req->param('f')) : ())
-  );
-  my $mk_title = $c->stash->{short_cmt};
-  for my $commit ($repository->list_revs(%logargs)) {
-    # XXX Needs work ....
-    $rss->add_item(
-        title       => $mk_title->($commit->comment),
-        permaLink   => $c->uri_for(commit => {h=>$commit->sha1}),
-        description => $commit->comment,
-    );
-  }
-
-  $c->response->body($rss->as_string);
-  $c->response->content_type('application/rss+xml');
-  $c->response->status(200);
-}
+sub search_help : Chained('base') Args(0) {}
 
 sub opml : Chained('base') Args(0) {
   my($self, $c) = @_;
 
   my $opml = XML::OPML::SimpleGen->new();
 
-  $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name});
+  $opml->head(title => lc(hostname()) . ' - ' . Gitalist->config->{name});
 
   my @list = @{ $c->model()->repositories };
   die 'No repositories found in '. $c->model->repo_dir
@@ -337,28 +236,6 @@ sub patches : Chained('base') Args(0) {
     $c->response->status(200);
 }
 
-=head2 snapshot
-
-Provides a snapshot of a given commit.
-
-=cut
-
-sub snapshot : Chained('base') Args(0) {
-    my ($self, $c) = @_;
-    my $format = $c->req->param('sf') || 'tgz';
-    die unless $format;
-    my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
-    my @snap = $c->stash->{Repository}->snapshot(
-        sha1 => $sha1,
-        format => $format
-    );
-    $c->response->status(200);
-    $c->response->headers->header( 'Content-Disposition' =>
-                                       "attachment; filename=$snap[0]");
-    $c->response->body($snap[1]);
-}
-
-
 sub base : Chained('/root') PathPart('') CaptureArgs(0) {
   my($self, $c) = @_;
 
index c95139a..c711937 100644 (file)
@@ -28,9 +28,15 @@ sub commit : Chained('find') PathPart('') Args(0) {}
 
 sub tree : Chained('find') Does('FilenameArgs') Args() {}
 
-sub blob_plain : Chained('find') Does('FilenameArgs') Args() {}
+sub find_blob : Chained('find') PathPart('') CaptureArgs(0) {
+    my ($self, $c) = @_;
+    # FIXME - Eugh!
+    my $h  = $c->stash->{Repository}->hash_by_path($c->stash->{Commit}->sha1, $c->stash->{filename})
+           || die "No file or sha1 provided.";
+    $c->stash(blob => $c->stash->{Repository}->get_object($h)->content);
+}
 
-sub blob : Chained('find') Does('FilenameArgs') Args() {}
+sub blob : Chained('find_blob') Does('FilenameArgs') Args() {}
 
 sub blame : Chained('find') Does('FilenameArgs') Args() {}