Make filename handling a generic actionrole. Fix more nav uris
Tomas Doran [Mon, 18 Jan 2010 01:49:55 +0000 (01:49 +0000)]
Makefile.PL
lib/Gitalist/ActionRole/FilenameArgs.pm [new file with mode: 0644]
lib/Gitalist/Controller.pm [new file with mode: 0644]
lib/Gitalist/Controller/Commit.pm
lib/Gitalist/Controller/Fragment.pm
lib/Gitalist/Controller/Fragment/Commit.pm
lib/Gitalist/Controller/Fragment/Repository.pm
lib/Gitalist/Controller/Repository.pm
lib/Gitalist/Controller/Root.pm
lib/Gitalist/URIStructure/Commit.pm
root/nav/actions.tt2

index df20b5d..9932c1c 100644 (file)
@@ -57,6 +57,7 @@ requires 'Catalyst::Plugin::Unicode::Encoding';
 requires 'Catalyst::Plugin::SubRequest';
 requires 'Catalyst::Action::RenderView';
 requires 'Catalyst::Component::InstancePerContext';
+requires 'Catalyst::Controller::ActionRole';
 requires 'Catalyst::View::Component::SubInclude' => '0.07';
 requires 'Catalyst::View::TT';
 requires 'Try::Tiny';
diff --git a/lib/Gitalist/ActionRole/FilenameArgs.pm b/lib/Gitalist/ActionRole/FilenameArgs.pm
new file mode 100644 (file)
index 0000000..3283aa2
--- /dev/null
@@ -0,0 +1,13 @@
+package Gitalist::ActionRole::FilenameArgs;
+use Moose::Role;
+use namespace::autoclean;
+
+requires 'execute';
+
+before 'execute' => sub {
+    my ($self, $controller, $c, @args) = @_;
+    $c->stash->{filename} = join('/', @args) || '';
+};
+
+1;
+
diff --git a/lib/Gitalist/Controller.pm b/lib/Gitalist/Controller.pm
new file mode 100644 (file)
index 0000000..9839ac3
--- /dev/null
@@ -0,0 +1,7 @@
+package Gitalist::Controller;
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'Catalyst::Controller::ActionRole' }
+
+__PACKAGE__->meta->make_immutable;
index 0290a51..ac2024a 100644 (file)
@@ -3,7 +3,7 @@ package Gitalist::Controller::Commit;
 use Moose;
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 with 'Gitalist::URIStructure::Commit';
 
 sub base : Chained('/repository/find') PathPart('') CaptureArgs(0) {}
index 6780891..5a4bfd2 100644 (file)
@@ -3,7 +3,7 @@ package Gitalist::Controller::Fragment;
 use Moose;
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 
 sub base : Chained('/base') PathPart('fragment') CaptureArgs(0) {
     my ($self, $c) = @_;
index b39b9ef..2f84ab1 100644 (file)
@@ -2,7 +2,7 @@ package Gitalist::Controller::Fragment::Commit;
 use Moose;
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 with 'Gitalist::URIStructure::Commit';
 
 sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {}
@@ -35,34 +35,30 @@ after diff_plain => sub {
 };
 
 after tree => sub {
-    my ( $self, $c, @fn ) = @_;
+    my ( $self, $c ) = @_;
     my $repository = $c->stash->{Repository};
     my $commit  = $c->stash->{Commit};
-    my $filename = join('/', @fn);
-    my $tree    = $filename
-      ? $repository->get_object($repository->hash_by_path($commit->sha1, $filename))
+    my $tree    = $c->stash->{filename}
+      ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
       : $repository->get_object($commit->tree_sha1)
     ;
     $c->stash(
         tree      => $tree,
         tree_list => [$repository->list_tree($tree->sha1)],
-        path      => $filename,
+        path      => $c->stash->{filename}, # FIXME?
     );
 };
 
 after blame => sub {
-    my($self, $c, @fn) = @_;
+    my($self, $c) = @_;
 
     my $repository = $c->stash->{Repository};
-    my $filename = join('/', @fn);
                                                       # WTF?
-    my $blame = $c->stash->{Commit}->blame($filename, $c->stash->{Commit}->sha1);
+    my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
     $c->stash(
         blame    => $blame,
-        filename => $filename,
-
         # XXX Hack hack hack, see View::SyntaxHighlight
-        language => ($filename =~ /\.p[lm]$/i ? 'Perl' : ''),
+        language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
         blob     => join("\n", map $_->{line}, @$blame),
     );
 
@@ -77,19 +73,17 @@ The blob action i.e the contents of a file.
 =cut
 
 after blob => sub {
-    my ( $self, $c, @fn ) = @_;
+    my ( $self, $c ) = @_;
 
-    my $filename = join('/', @fn);
     my $repository = $c->stash->{Repository};
     my $h  =
-          $repository->hash_by_path($c->stash->{Commit}->sha1, $filename)
+          $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,
-        filename => $filename,
         # XXX Hack hack hack, see View::SyntaxHighlight
-        language => ($filename =~ /\.p[lm]$/i ? 'Perl' : ''),
+        language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
     );
 
     $c->forward('View::SyntaxHighlight')
index ed0829d..ad011f6 100644 (file)
@@ -2,7 +2,7 @@ package Gitalist::Controller::Fragment::Repository;
 use Moose;
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 with 'Gitalist::URIStructure::Repository';
 
 sub base : Chained('/fragment/base') PathPart('') CaptureArgs(0) {}
index 96fe14f..fbba9dd 100644 (file)
@@ -2,7 +2,7 @@ package Gitalist::Controller::Repository;
 use Moose;
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 with 'Gitalist::URIStructure::Repository';
 
 sub base : Chained('/base') PathPart('') CaptureArgs(0) {}
index b20ebc1..eb66233 100644 (file)
@@ -12,7 +12,7 @@ use Gitalist::Utils qw/ age_string /;
 
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
 
 __PACKAGE__->config->{namespace} = '';
 
index 0140681..c95139a 100644 (file)
@@ -1,5 +1,6 @@
 package Gitalist::URIStructure::Commit;
 use MooseX::MethodAttributes::Role;
+use Moose::Autobox;
 use namespace::autoclean;
 
 requires 'base';
@@ -23,16 +24,20 @@ sub diff_fancy : Chained('diff') PathPart('') Args(0) {}
 
 sub diff_plain : Chained('diff') PathPart('plain') Args(0) {}
 
-sub tree : Chained('find') Args() {}
+sub commit : Chained('find') PathPart('') Args(0) {}
 
-sub commit : Chained('find') PathPart('') {}
+sub tree : Chained('find') Does('FilenameArgs') Args() {}
 
-sub blob : Chained('find') Args() {}
+sub blob_plain : Chained('find') Does('FilenameArgs') Args() {}
 
-sub blame : Chained('find') Args() {}
+sub blob : Chained('find') Does('FilenameArgs') Args() {}
 
-sub history : Chained('find') Args() {}
+sub blame : Chained('find') Does('FilenameArgs') Args() {}
 
-sub raw : Chained('find') Args() {}
+sub history : Chained('find') Does('FilenameArgs') Args() {}
+
+sub raw : Chained('find') Does('FilenameArgs') Args() {}
+
+sub shortlog : Chained('find') Does('FilenameArgs') Args() {}
 
 1;
index 9945e70..4ed51ee 100644 (file)
@@ -2,19 +2,19 @@
   <!-- This should probably be a real LIst -->
     <a href="[% c.uri_for_action('/repository/summary', [c.req.captures.0]) %]">summary</a> &bull;
     <a href="[% c.uri_for_action('/repository/shortlog', [c.req.captures.0]) %]">shortlog</a> &bull;
-    <a href="[% c.uri_for_action('/repository/log', [c.req.captures.0]) %]">log</a> &bull;
+    <a href="[% c.uri_for_action('/repository/log', [c.req.captures.0]) %]">log</a>
     [% IF Commit %]
+        &sect;
         <a href="[% c.uri_for_action('/commit/commit', [c.req.captures.0, Commit.sha1]) %]">commit</a> &bull;
         <a href="[% c.uri_for_action('/commit/diff_fancy', [c.req.captures.0, Commit.sha1]) %]">commitdiff</a> &bull;
         <a href="[% c.uri_for_action('/commit/tree', [c.req.captures.0, Commit.sha1]) %]">tree</a>
     [% END %]
-    [% IF filename && Commit %]&bull;[% END %]
     [% IF filename %]
     &sect;
-    <a href="[% c.uri_for('blob', {h=object.sha1,f=filename}) %]">blob</a> &bull;
-    <a href="[% c.uri_for('blob_plain', {h=object.sha1,f=filename}) %]">raw</a> &bull;
-    <a href="[% c.uri_for('blame', {h=object.sha1,f=filename}) %]">blame</a> &bull;
-    <a href="[% c.uri_for('shortlog', {h=object.sha1,f=filename}) %]">history</a> &bull;
+    <a href="[% c.uri_for_action('/commit/blob', [c.req.captures.0, Commit.sha1], filename) %]">blob</a> &bull;
+    <a href="[% c.uri_for_action('/commit/blob_plain', [c.req.captures.0, Commit.sha1], filename) %]">raw</a> &bull;
+    <a href="[% c.uri_for_action('/commit/blame', [c.req.captures.0, Commit.sha1], filename)  %]">blame</a> &bull;
+    <a href="[% c.uri_for_action('/commit/shortlog', [c.req.captures.0, Commit.sha1], filename)  %]">history</a> &bull;
     <a href="[% c.uri_for(c.controller.action_for('commit'), [c.req.captures.0, Repository.head_hash]) %]">HEAD</a>
     [% END %]
     <div class='chroma-hash'>[% INCLUDE 'inc/chroma_hash.tt2' sha1 = object.sha1 %]</div>