Provided legacy URI support for the blob action.
Dan Brook [Sat, 6 Mar 2010 17:38:58 +0000 (17:38 +0000)]
* Also fixed the links under _refs, but haven't provided support for remote refs.

lib/Gitalist/Controller/LegacyURI.pm
lib/Gitalist/URIStructure/Ref.pm
root/_refs.tt2
t/03legacy_uri.t

index cd8b599..5259b14 100644 (file)
@@ -5,16 +5,43 @@ use namespace::autoclean;
 
 BEGIN { extends 'Gitalist::Controller' }
 
+my %LEGACY_DISPATCH = (
+    opml                    => sub { '/opml/opml' },
+    project_index           => sub { '/legacyuri/project_index' },
+    '(?:summary|heads|tags)' => sub {
+       my($c, $action, $repos) = @_;
+       return "/repository/$action", [$repos];
+    },
+    blob => sub {
+       my($c, $action, $repos) = @_;
+       my $ref = $c->req->param('hb') || $c->req->param('h');
+       return '/ref/blob', [$repos, $ref], $c->req->param('f');
+    },
+);
+
+sub _legacy_uri {
+    my($self, $c, $repos, $action) = @_;
+
+    return
+       unless $action;
+
+    my @result  = grep { $action =~ /^$_$/ } keys %LEGACY_DISPATCH;
+    die "Matched too many actions for '$a' - @result"
+       if @result > 1;
+
+    return $LEGACY_DISPATCH{$result[0]}->($c, $action, $repos)
+       if $result[0];
+}
+
 sub handler : Chained('/base') PathPart('legacy') Args() {
     my ( $self, $c, $repos ) = @_;
-    my ($action, @captures);
-    if (my $a = $c->req->param('a')) {
-        $a eq 'opml' && do { $action = '/opml/opml'; };
-        $a eq 'project_index' && do { $action = '/legacyuri/project_index'; };
-        $a =~ /^(summary|heads|tags)$/ && do { $action = "/repository/$1"; push(@captures, $repos); };
-    }
-    die("Not supported") unless $action;
-    $c->res->redirect($c->uri_for_action($action, \@captures));
+
+    my ($action, $captures, @args) = $self->_legacy_uri($c, $repos, $c->req->param('a'));
+
+    die("Not supported")
+       unless $action;
+
+    $c->res->redirect($c->uri_for_action($action, $captures || [], @args));
     $c->res->status(301);
 }
 
index 9516b7d..13da60d 100644 (file)
@@ -44,10 +44,16 @@ sub tree : Chained('find') Does('FilenameArgs') Args() {}
 
 sub find_blob : Action {
     my ($self, $c) = @_;
+    my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
     # 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);
+    my $h  = $object->isa('Gitalist::Git::Object::Commit')
+          ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
+          : $object->isa('Gitalist::Git::Object::Blob')
+             ? $object->sha1
+             : die "Unknown object type for '${\$object->sha1}'";
+    die "No file or sha1 provided."
+        unless $h;
+    $c->stash(blob => $repo->get_object($h)->content);
 }
 
 sub blob : Chained('find') Does('FilenameArgs') Args() {
index 71bb0ed..4c3893a 100644 (file)
@@ -1,7 +1,7 @@
 <span class='refs'>
  [% FOREACH ref IN refs.${object.sha1} %]
  <span class='[% ref.search('^remotes/') ? 'remote' : 'head' %]'>
-  <a href='[% c.uri_for("shortlog", {h='refs/' _ ref}) %]'>[% ref.replace('^(remote|head)s/', '') %]</a>
+  <a href='[% c.uri_for_action("/repository/shortlog", [ref.match('(\w+)$').0]) %]'>[% ref.replace('^(remote|head)s/', '') %]</a>
  </span>
  [% END %]
 </span>
index 4390baa..f9ed349 100644 (file)
@@ -32,9 +32,6 @@ test('/', 'a=opml');
 test('/', 'a=summary');
 test('/', 'a=heads');
 test('/', 'a=tags');
-{
-
-    local $TODO = 'FIXME';
 
 test('/', 'a=blob;f=dir1/file2;h=257cc5642cb1a054f08cc83f2d943e56fd3ebe99;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
 test('/', 'a=blob;f=dir1/file2;h=257cc5642cb1a054f08cc83f2d943e56fd3ebe99;hb=HEAD');
@@ -54,6 +51,10 @@ test('/', 'a=blob;f=file1;hb=3bc0634310b9c62222bb0e724c11ffdfb297b4ac');
 test('/', 'a=blob;f=file1;hb=3f7567c7bdf7e7ebf410926493b92d398333116e');
 
 
+{
+
+    local $TODO = 'FIXME';
+
 test('/', 'a=blob_plain;f=dir1/file2;hb=36c6c6708b8360d7023e8a1649c45bcf9b3bd818');
 test('/', 'a=blob_plain;f=dir1/file2;hb=HEAD');
 test('/', 'a=blob_plain;f=dir1/file2;hb=master');