Move search and reflog and patch actions to the right places
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
CommitLineData
89de6a33 1package Gitalist::Controller::Root;
89de6a33 2
c113db92 3use Moose;
cce8c9b6 4use Moose::Autobox;
b132dce6 5use Sys::Hostname qw/hostname/;
286cea09 6use XML::OPML::SimpleGen;
d17ce39c 7
c113db92 8use Gitalist::Utils qw/ age_string /;
89de6a33 9
c113db92 10use namespace::autoclean;
89de6a33 11
b6ec181b 12BEGIN { extends 'Gitalist::Controller' }
89de6a33 13
c113db92 14__PACKAGE__->config->{namespace} = '';
89de6a33 15
c113db92 16sub root : Chained('/') PathPart('') CaptureArgs(0) {}
89de6a33 17
832cbc81 18sub _get_object {
b4b4d0fd 19 my($self, $c, $haveh) = @_;
9dc3b9a5 20
c1f608c8 21 my $h = $haveh || $c->req->param('h') || '';
0ee97fec 22 my $f = $c->req->param('f');
8dbe8024 23
87581f05 24 my $m = $c->stash->{Repository};
1aad4e81 25 my $pd = $m->path;
0ee97fec 26
9dc3b9a5 27 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
a7cc1ede 28 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
29 || ($f && $m->hash_by_path($f))
30 || $m->head_hash
9dc3b9a5 31 # XXX This could definitely use more context.
32 || Carp::croak("Couldn't find a hash for the commit object!");
33
c046a52f 34 my $obj = $m->get_object($hash)
35 or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
9dc3b9a5 36
c046a52f 37 return $obj;
9dc3b9a5 38}
39
5bb401c6 40sub index : Chained('base') PathPart('') Args(0) {
7bc165b3 41 my ( $self, $c ) = @_;
42
43 $c->detach($c->req->param('a'))
44 if $c->req->param('a');
45
7bc165b3 46 my $search = $c->req->param('s') || '';
7bc165b3 47
48 $c->stash(
49 search_text => $search,
7bc165b3 50 );
04d1d917 51}
52
b5f3d3e7 53sub _blob_objs {
295c9703 54 my ( $self, $c ) = @_;
82bc0f05 55 my $repository = $c->stash->{Repository};
c8870bd3 56 my $h = $c->req->param('h')
82bc0f05 57 || $repository->hash_by_path($c->req->param('hb'), $c->req->param('f'))
c8870bd3 58 || die "No file or sha1 provided.";
59 my $hb = $c->req->param('hb')
82bc0f05 60 || $repository->head_hash
c8870bd3 61 || die "Couldn't discern the corresponding head.";
62
f5da8e7a 63 my $filename = $c->req->param('f') || '';
64
82bc0f05 65 my $blob = $repository->get_object($h);
66 $blob = $repository->get_object(
67 $repository->hash_by_path($h || $hb, $filename)
b5f3d3e7 68 ) if $blob->type ne 'blob';
69
82bc0f05 70 return $blob, $repository->get_object($hb), $filename;
b5f3d3e7 71}
72
b5f3d3e7 73=head2 blobdiff_plain
74
75The plain text version of blobdiff.
76
77=cut
78
5bb401c6 79sub blobdiff_plain : Chained('base') Args(0) {
c8a42dd5 80 my($self, $c) = @_;
81
82 $c->stash(no_wrapper => 1);
83 $c->response->content_type('text/plain; charset=utf-8');
84
85 $c->forward('blobdiff');
295c9703 86}
87
6cf4366a 88=head2 blobdiff
89
90Exposes a given diff of a blob.
91
92=cut
93
5bb401c6 94sub blobdiff : Chained('base') Args(0) {
6cf4366a 95 my ( $self, $c ) = @_;
832cbc81 96 my $commit = $self->_get_object($c, $c->req->param('hb'));
6cf4366a 97 my $filename = $c->req->param('f')
98 || croak("No file specified!");
87581f05 99 my($tree, $patch) = $c->stash->{Repository}->diff(
ad8884fc 100 commit => $commit,
ad8884fc 101 patch => 1,
c8a42dd5 102 parent => $c->req->param('hpb') || undef,
103 file => $filename,
6cf4366a 104 );
105 $c->stash(
106 commit => $commit,
ad8884fc 107 diff => $patch,
592b68ef 108 filename => $filename,
6cf4366a 109 # XXX Hack hack hack, see View::SyntaxHighlight
ad8884fc 110 blobs => [$patch->[0]->{diff}],
6cf4366a 111 language => 'Diff',
6cf4366a 112 );
113
c8a42dd5 114 $c->forward('View::SyntaxHighlight')
115 unless $c->stash->{no_wrapper};
6cf4366a 116}
117
c8a42dd5 118# For legacy support.
5bb401c6 119sub history : Chained('base') Args(0) {
cce8c9b6 120 my ( $self, $c ) = @_;
121 $self->shortlog($c);
82bc0f05 122 my $repository = $c->stash->{Repository};
123 my $file = $repository->get_object(
124 $repository->hash_by_path(
125 $repository->head_hash,
cce8c9b6 126 $c->stash->{filename}
127 )
128 );
066e9aa4 129 $c->stash(
cce8c9b6 130 filetype => $file->type,
131 );
c8a42dd5 132}
133
ea19a20c 134=head2 search_help
135
136Provides some help for the search form.
137
138=cut
139
b132dce6 140sub search_help : Chained('base') Args(0) {}
14664e1c 141
5bb401c6 142sub opml : Chained('base') Args(0) {
286cea09 143 my($self, $c) = @_;
144
145 my $opml = XML::OPML::SimpleGen->new();
146
b132dce6 147 $opml->head(title => lc(hostname()) . ' - ' . Gitalist->config->{name});
286cea09 148
82bc0f05 149 my @list = @{ $c->model()->repositories };
150 die 'No repositories found in '. $c->model->repo_dir
286cea09 151 unless @list;
152
153 for my $proj ( @list ) {
154 $opml->insert_outline(
155 text => $proj->name. ' - '. $proj->description,
156 xmlUrl => $c->uri_for(rss => {p => $proj->name}),
157 );
158 }
159
160 $c->response->body($opml->as_string);
161 $c->response->content_type('application/rss');
162 $c->response->status(200);
163}
164
5bb401c6 165sub base : Chained('/root') PathPart('') CaptureArgs(0) {
4621ecf0 166 my($self, $c) = @_;
04d1d917 167
066e9aa4 168 my $git_version = `git --version`;
169 chomp($git_version);
4621ecf0 170 $c->stash(
066e9aa4 171 git_version => $git_version,
da8f4f82 172 version => $Gitalist::VERSION,
173
174 # XXX Move these to a plugin!
4621ecf0 175 time_since => sub {
ef11b09e 176 return 'never' unless $_[0];
4621ecf0 177 return age_string(time - $_[0]->epoch);
178 },
179 short_cmt => sub {
180 my $cmt = shift;
181 my($line) = split /\n/, $cmt;
741e110e 182 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
4621ecf0 183 return $line;
184 },
5cd9b9f9 185 abridged_description => sub {
186 join(' ', grep { defined } (split / /, shift)[0..10]);
187 },
4621ecf0 188 );
04d1d917 189}
d9a9b56b 190
fde5091f 191sub end : ActionClass('RenderView') {
1aad4e81 192 my ($self, $c) = @_;
82bc0f05 193 # Give repository views the current HEAD.
87581f05 194 if ($c->stash->{Repository}) {
195 $c->stash->{HEAD} = $c->stash->{Repository}->head_hash;
1aad4e81 196 }
1fd8159c 197}
d9a9b56b 198
c113db92 199sub error_404 : Action {
4111e151 200 my ($self, $c) = @_;
201 $c->response->status(404);
5232dbdd 202 $c->response->body('Page not found');
4111e151 203}
204
775e96e0 205__PACKAGE__->meta->make_immutable;
206
207__END__
208
d137f7d5 209=head1 NAME
210
211Gitalist::Controller::Root - Root controller for the application
212
213=head1 DESCRIPTION
214
215This controller handles all of the root level paths for the application
216
217=head1 METHODS
218
5bb401c6 219=head2 root
220
221Root of chained actions
222
223=head2 base
224
225Populate the header and footer. Perhaps not the best location.
226
227=head2 index
228
82bc0f05 229Provides the repository listing.
5bb401c6 230
231=head2 end
232
233Attempt to render a view, if needed.
d137f7d5 234
235=head2 blame
236
d137f7d5 237=head2 error_404
238
239=head2 history
240
241=head2 opml
242
82bc0f05 243=head2 repository_index
da8f4f82 244
775e96e0 245=head1 AUTHORS
89de6a33 246
775e96e0 247See L<Gitalist> for authors.
89de6a33 248
249=head1 LICENSE
250
775e96e0 251See L<Gitalist> for the license.
89de6a33 252
253=cut