Better debugging
[catagits/Gitalist.git] / lib / Gitalist / Controller / LegacyURI.pm
1 package Gitalist::Controller::LegacyURI;
2 use Moose;
3 use Moose::Autobox;
4 use namespace::autoclean;
5
6 BEGIN { extends 'Gitalist::Controller' }
7
8 my %LEGACY_DISPATCH = (
9     opml                     => sub { '/opml/opml' },
10     project_index            => sub { '/legacyuri/project_index' },
11     '(?:summary|heads|tags)' => sub {
12         my($c, $action, $repos) = @_;
13         return "/repository/$action", [$repos];
14     },
15     blob => sub {
16         my($c, $action, $repos) = @_;
17         my $ref = $c->req->param('hb') || $c->req->param('h');
18         return '/ref/blob', [$repos, $ref], $c->req->param('f');
19     },
20     blob_plain               => sub {
21         my($c, $action, $repos) =  @_;
22         my $ref = $c->req->param('hb') || $c->req->param('h');
23         return '/ref/raw', [$repos, $ref], $c->req->param('f');
24     },
25     blobdiff                 => sub {
26         my($c, $action, $repos) =  @_;
27         my $ref     = $c->req->param('hb')  || $c->req->param('h');
28         my $compare = $c->req->param('hbp') || $c->req->param('hp');
29         return '/ref/diff_fancy', [$repos, $ref], $compare, $c->req->param('f');
30     },
31     blobdiff_plain           => sub {
32         my($c, $action, $repos) =  @_;
33         my $ref     = $c->req->param('hb')  || $c->req->param('h');
34         my $compare = $c->req->param('hbp') || $c->req->param('hp');
35         return '/ref/diff_plain', [$repos, $ref], $compare, $c->req->param('f');
36     },
37     commit                   => sub {
38         my($c, $action, $repos) =  @_;
39         my $ref = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
40         return '/ref/commit', [$repos, $ref];
41     },
42     # XXX These can be consolidated with the blob equivalents.
43     commitdiff               => sub {
44         my($c, $action, $repos) =  @_;
45         my $ref     = $c->req->param('hb')  || $c->req->param('h') || 'HEAD';
46         my $compare = $c->req->param('hbp') || $c->req->param('hp');
47         return '/ref/diff_fancy', [$repos, $ref], $compare, $c->req->param('f');
48     },
49     commitdiff_plain         => sub {
50         my($c, $action, $repos) =  @_;
51         my $ref     = $c->req->param('hb')  || $c->req->param('h');
52         my $compare = $c->req->param('hbp') || $c->req->param('hp');
53         return '/ref/diff_plain', [$repos, $ref || 'HEAD'], $compare, $c->req->param('f');
54     },
55     history                  => sub {
56         my($c, $action, $repos) =  @_;
57         my $ref     = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
58         return '/ref/history', [$repos, $ref], $c->req->param('f');
59     },
60     log                      => sub {
61         my($c, $action, $repos) =  @_;
62         my $ref = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
63         return '/ref/longlog', [$repos, $ref];
64     },
65     patch                    => sub {
66         my($c, $action, $repos) =  @_;
67         my $ref = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
68         return '/ref/patch', [$repos, $ref];
69     },
70     patches                  => sub {
71         my($c, $action, $repos) = @_;
72         # XXX Is the arg there wrong? It's just copying G::C::R::patch.
73         return '/ref/patches', [$repos, $c->req->param('h') || 'HEAD'], 1;
74     },
75     search_help              => sub {
76         return '/search_help';
77     },
78     shortlog                 => sub {
79         my($c, $action, $repos) =  @_;
80         my $ref = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
81         return '/ref/shortlog', [$repos, $ref];
82     },
83     snapshot                 => sub {
84         my($c, $action, $repos) =  @_;
85         my $ref = $c->req->param('h') || 'HEAD';
86         return '/ref/snapshot', [$repos, $ref], $c->req->param('sf');
87     },
88     tree                     => sub {
89         my($c, $action, $repos) = @_;
90         my $ref = $c->req->param('hb') || $c->req->param('h') || 'HEAD';
91         return '/ref/tree', [$repos, $ref], $c->req->param('f');
92     },
93     '(?:atom|rss)'           => sub {
94         my($c, $action, $repos) =  @_;
95         # XXX No support for arbitrary branches or merges/nomerges option :(
96         return "/repository/$action", [$repos], $c->req->param('f');
97     },
98     blame                    => sub {
99         my($c, $action, $repos) = @_;
100         my $ref = $c->req->param('hb') || $c->req->param('h');
101         return '/ref/blame', [$repos, $ref], $c->req->param('f');
102     },
103 );
104
105 sub _legacy_uri {
106     my($self, $c, $repos, $action) = @_;
107
108     return
109         unless $action;
110
111     my @result  = grep { $action =~ /^$_$/ } keys %LEGACY_DISPATCH;
112     die "Matched too many actions for '$a' - @result"
113         if @result > 1;
114
115     return
116         unless $result[0];
117
118     my($real_action, $captures, @args) = $LEGACY_DISPATCH{$result[0]}->($c, $action, $repos);
119
120     return $real_action, $captures || [], grep defined, @args;
121 }
122
123 sub handler : Chained('/base') PathPart('legacy') Args() {
124     my ( $self, $c, $repos ) = @_;
125
126     $repos ||= $c->req->param('p');
127
128     my ($action, $captures, @args) = $self->_legacy_uri($c, $repos, $c->req->param('a'));
129
130     die("Not supported")
131         unless $action;
132
133     $c->res->redirect($c->uri_for_action($action, $captures, @args));
134     $c->res->status(301);
135 }
136
137 sub project_index : Chained('/base') Args(0) {
138       my ( $self, $c ) = @_;
139
140       $c->response->content_type('text/plain');
141       $c->response->body(
142           join "\n", map $_->name, $c->model()->repositories->flatten
143       ) or die 'No repositories found in ' . ref($c->model) . ' ' . $c->model->debug_string;
144 }
145
146 __PACKAGE__->meta->make_immutable;