Commit | Line | Data |
89de6a33 |
1 | package Gitalist::Controller::Root; |
89de6a33 |
2 | |
c113db92 |
3 | use Moose; |
cce8c9b6 |
4 | use Moose::Autobox; |
f796a861 |
5 | use Sys::Hostname (); |
d17ce39c |
6 | use XML::Atom::Feed; |
7 | use XML::Atom::Entry; |
f796a861 |
8 | use XML::RSS; |
286cea09 |
9 | use XML::OPML::SimpleGen; |
d17ce39c |
10 | |
c113db92 |
11 | use Gitalist::Utils qw/ age_string /; |
89de6a33 |
12 | |
c113db92 |
13 | use namespace::autoclean; |
89de6a33 |
14 | |
c113db92 |
15 | BEGIN { extends 'Catalyst::Controller' } |
89de6a33 |
16 | |
c113db92 |
17 | __PACKAGE__->config->{namespace} = ''; |
89de6a33 |
18 | |
c113db92 |
19 | sub root : Chained('/') PathPart('') CaptureArgs(0) {} |
89de6a33 |
20 | |
832cbc81 |
21 | sub _get_object { |
b4b4d0fd |
22 | my($self, $c, $haveh) = @_; |
9dc3b9a5 |
23 | |
c1f608c8 |
24 | my $h = $haveh || $c->req->param('h') || ''; |
0ee97fec |
25 | my $f = $c->req->param('f'); |
8dbe8024 |
26 | |
87581f05 |
27 | my $m = $c->stash->{Repository}; |
1aad4e81 |
28 | my $pd = $m->path; |
0ee97fec |
29 | |
9dc3b9a5 |
30 | # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD. |
a7cc1ede |
31 | my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h) |
32 | || ($f && $m->hash_by_path($f)) |
33 | || $m->head_hash |
9dc3b9a5 |
34 | # XXX This could definitely use more context. |
35 | || Carp::croak("Couldn't find a hash for the commit object!"); |
36 | |
c046a52f |
37 | my $obj = $m->get_object($hash) |
38 | or Carp::croak("Couldn't find a object for '$hash' in '$pd'!"); |
9dc3b9a5 |
39 | |
c046a52f |
40 | return $obj; |
9dc3b9a5 |
41 | } |
42 | |
5bb401c6 |
43 | sub index : Chained('base') PathPart('') Args(0) { |
7bc165b3 |
44 | my ( $self, $c ) = @_; |
45 | |
46 | $c->detach($c->req->param('a')) |
47 | if $c->req->param('a'); |
48 | |
7bc165b3 |
49 | my $search = $c->req->param('s') || ''; |
7bc165b3 |
50 | |
51 | $c->stash( |
52 | search_text => $search, |
7bc165b3 |
53 | ); |
04d1d917 |
54 | } |
55 | |
b5f3d3e7 |
56 | sub _blob_objs { |
295c9703 |
57 | my ( $self, $c ) = @_; |
82bc0f05 |
58 | my $repository = $c->stash->{Repository}; |
c8870bd3 |
59 | my $h = $c->req->param('h') |
82bc0f05 |
60 | || $repository->hash_by_path($c->req->param('hb'), $c->req->param('f')) |
c8870bd3 |
61 | || die "No file or sha1 provided."; |
62 | my $hb = $c->req->param('hb') |
82bc0f05 |
63 | || $repository->head_hash |
c8870bd3 |
64 | || die "Couldn't discern the corresponding head."; |
65 | |
f5da8e7a |
66 | my $filename = $c->req->param('f') || ''; |
67 | |
82bc0f05 |
68 | my $blob = $repository->get_object($h); |
69 | $blob = $repository->get_object( |
70 | $repository->hash_by_path($h || $hb, $filename) |
b5f3d3e7 |
71 | ) if $blob->type ne 'blob'; |
72 | |
82bc0f05 |
73 | return $blob, $repository->get_object($hb), $filename; |
b5f3d3e7 |
74 | } |
75 | |
b5f3d3e7 |
76 | |
c8a42dd5 |
77 | |
b5f3d3e7 |
78 | =head2 blob_plain |
79 | |
80 | The plain text version of blob, where file is rendered as is. |
81 | |
82 | =cut |
83 | |
5bb401c6 |
84 | sub blob_plain : Chained('base') Args(0) { |
c8a42dd5 |
85 | my($self, $c) = @_; |
86 | |
b5f3d3e7 |
87 | my($blob) = $self->_blob_objs($c); |
c8a42dd5 |
88 | $c->response->content_type('text/plain; charset=utf-8'); |
b5f3d3e7 |
89 | $c->response->body($blob->content); |
90 | $c->response->status(200); |
c8a42dd5 |
91 | } |
92 | |
b5f3d3e7 |
93 | =head2 blobdiff_plain |
94 | |
95 | The plain text version of blobdiff. |
96 | |
97 | =cut |
98 | |
5bb401c6 |
99 | sub blobdiff_plain : Chained('base') Args(0) { |
c8a42dd5 |
100 | my($self, $c) = @_; |
101 | |
102 | $c->stash(no_wrapper => 1); |
103 | $c->response->content_type('text/plain; charset=utf-8'); |
104 | |
105 | $c->forward('blobdiff'); |
295c9703 |
106 | } |
107 | |
6cf4366a |
108 | =head2 blobdiff |
109 | |
110 | Exposes a given diff of a blob. |
111 | |
112 | =cut |
113 | |
5bb401c6 |
114 | sub blobdiff : Chained('base') Args(0) { |
6cf4366a |
115 | my ( $self, $c ) = @_; |
832cbc81 |
116 | my $commit = $self->_get_object($c, $c->req->param('hb')); |
6cf4366a |
117 | my $filename = $c->req->param('f') |
118 | || croak("No file specified!"); |
87581f05 |
119 | my($tree, $patch) = $c->stash->{Repository}->diff( |
ad8884fc |
120 | commit => $commit, |
ad8884fc |
121 | patch => 1, |
c8a42dd5 |
122 | parent => $c->req->param('hpb') || undef, |
123 | file => $filename, |
6cf4366a |
124 | ); |
125 | $c->stash( |
126 | commit => $commit, |
ad8884fc |
127 | diff => $patch, |
592b68ef |
128 | filename => $filename, |
6cf4366a |
129 | # XXX Hack hack hack, see View::SyntaxHighlight |
ad8884fc |
130 | blobs => [$patch->[0]->{diff}], |
6cf4366a |
131 | language => 'Diff', |
6cf4366a |
132 | ); |
133 | |
c8a42dd5 |
134 | $c->forward('View::SyntaxHighlight') |
135 | unless $c->stash->{no_wrapper}; |
6cf4366a |
136 | } |
137 | |
c8a42dd5 |
138 | # For legacy support. |
5bb401c6 |
139 | sub history : Chained('base') Args(0) { |
cce8c9b6 |
140 | my ( $self, $c ) = @_; |
141 | $self->shortlog($c); |
82bc0f05 |
142 | my $repository = $c->stash->{Repository}; |
143 | my $file = $repository->get_object( |
144 | $repository->hash_by_path( |
145 | $repository->head_hash, |
cce8c9b6 |
146 | $c->stash->{filename} |
147 | ) |
148 | ); |
066e9aa4 |
149 | $c->stash( |
cce8c9b6 |
150 | filetype => $file->type, |
151 | ); |
c8a42dd5 |
152 | } |
153 | |
9dc3b9a5 |
154 | =head2 reflog |
155 | |
156 | Expose the local reflog. This may go away. |
157 | |
158 | =cut |
159 | |
5bb401c6 |
160 | sub reflog : Chained('base') Args(0) { |
9dc3b9a5 |
161 | my ( $self, $c ) = @_; |
87581f05 |
162 | my @log = $c->stash->{Repository}->reflog( |
9dc3b9a5 |
163 | '--since=yesterday' |
164 | ); |
165 | |
166 | $c->stash( |
167 | log => \@log, |
9dc3b9a5 |
168 | ); |
169 | } |
170 | |
ea19a20c |
171 | =head2 search |
172 | |
173 | The action for the search form. |
174 | |
175 | =cut |
176 | |
5bb401c6 |
177 | sub search : Chained('base') Args(0) { |
4df2f62f |
178 | my($self, $c) = @_; |
82bc0f05 |
179 | my $repository = $c->stash->{Repository}; |
832cbc81 |
180 | my $commit = $self->_get_object($c); |
4df2f62f |
181 | # Lifted from /shortlog. |
182 | my %logargs = ( |
183 | sha1 => $commit->sha1, |
184 | count => Gitalist->config->{paging}{log}, |
185 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()), |
72d72d4d |
186 | search => { |
187 | type => $c->req->param('type'), |
188 | text => $c->req->param('text'), |
189 | regexp => $c->req->param('regexp') || 0, |
190 | }, |
4df2f62f |
191 | ); |
192 | |
193 | $c->stash( |
194 | commit => $commit, |
82bc0f05 |
195 | results => [$repository->list_revs(%logargs)], |
4df2f62f |
196 | # This could be added - page => $page, |
197 | ); |
14664e1c |
198 | } |
199 | |
ea19a20c |
200 | =head2 search_help |
201 | |
202 | Provides some help for the search form. |
203 | |
204 | =cut |
205 | |
5bb401c6 |
206 | sub search_help : Chained('base') Args(0) { |
2646511e |
207 | my ($self, $c) = @_; |
208 | $c->stash(template => 'search_help.tt2'); |
6cfcd548 |
209 | } |
210 | |
ea19a20c |
211 | =head2 atom |
212 | |
82bc0f05 |
213 | Provides an atom feed for a given repository. |
ea19a20c |
214 | |
215 | =cut |
216 | |
5bb401c6 |
217 | sub atom : Chained('base') Args(0) { |
d17ce39c |
218 | my($self, $c) = @_; |
6cfcd548 |
219 | |
d17ce39c |
220 | my $feed = XML::Atom::Feed->new; |
6cfcd548 |
221 | |
d17ce39c |
222 | my $host = lc Sys::Hostname::hostname(); |
223 | $feed->title($host . ' - ' . Gitalist->config->{name}); |
224 | $feed->updated(~~DateTime->now); |
225 | |
82bc0f05 |
226 | my $repository = $c->stash->{Repository}; |
d17ce39c |
227 | my %logargs = ( |
82bc0f05 |
228 | sha1 => $repository->head_hash, |
d17ce39c |
229 | count => Gitalist->config->{paging}{log} || 25, |
230 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()) |
231 | ); |
232 | |
233 | my $mk_title = $c->stash->{short_cmt}; |
82bc0f05 |
234 | for my $commit ($repository->list_revs(%logargs)) { |
d17ce39c |
235 | my $entry = XML::Atom::Entry->new; |
236 | $entry->title( $mk_title->($commit->comment) ); |
237 | $entry->id($c->uri_for('commit', {h=>$commit->sha1})); |
238 | # XXX Needs work ... |
239 | $entry->content($commit->comment); |
240 | $feed->add_entry($entry); |
241 | } |
242 | |
f796a861 |
243 | $c->response->body($feed->as_xml); |
e75df318 |
244 | $c->response->content_type('application/atom+xml'); |
f796a861 |
245 | $c->response->status(200); |
6cfcd548 |
246 | } |
247 | |
ea19a20c |
248 | =head2 rss |
249 | |
82bc0f05 |
250 | Provides an RSS feed for a given repository. |
ea19a20c |
251 | |
252 | =cut |
253 | |
5bb401c6 |
254 | sub rss : Chained('base') Args(0) { |
f796a861 |
255 | my ($self, $c) = @_; |
256 | |
82bc0f05 |
257 | my $repository = $c->stash->{Repository}; |
f796a861 |
258 | |
259 | my $rss = XML::RSS->new(version => '2.0'); |
260 | $rss->channel( |
261 | title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name}, |
82bc0f05 |
262 | link => $c->uri_for('summary', {p=>$repository->name}), |
f796a861 |
263 | language => 'en', |
82bc0f05 |
264 | description => $repository->description, |
f796a861 |
265 | pubDate => DateTime->now, |
266 | lastBuildDate => DateTime->now, |
267 | ); |
268 | |
269 | my %logargs = ( |
82bc0f05 |
270 | sha1 => $repository->head_hash, |
f796a861 |
271 | count => Gitalist->config->{paging}{log} || 25, |
272 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()) |
273 | ); |
274 | my $mk_title = $c->stash->{short_cmt}; |
82bc0f05 |
275 | for my $commit ($repository->list_revs(%logargs)) { |
f796a861 |
276 | # XXX Needs work .... |
277 | $rss->add_item( |
278 | title => $mk_title->($commit->comment), |
279 | permaLink => $c->uri_for(commit => {h=>$commit->sha1}), |
280 | description => $commit->comment, |
281 | ); |
282 | } |
283 | |
284 | $c->response->body($rss->as_string); |
285 | $c->response->content_type('application/rss+xml'); |
286 | $c->response->status(200); |
14664e1c |
287 | } |
288 | |
5bb401c6 |
289 | sub opml : Chained('base') Args(0) { |
286cea09 |
290 | my($self, $c) = @_; |
291 | |
292 | my $opml = XML::OPML::SimpleGen->new(); |
293 | |
294 | $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name}); |
295 | |
82bc0f05 |
296 | my @list = @{ $c->model()->repositories }; |
297 | die 'No repositories found in '. $c->model->repo_dir |
286cea09 |
298 | unless @list; |
299 | |
300 | for my $proj ( @list ) { |
301 | $opml->insert_outline( |
302 | text => $proj->name. ' - '. $proj->description, |
303 | xmlUrl => $c->uri_for(rss => {p => $proj->name}), |
304 | ); |
305 | } |
306 | |
307 | $c->response->body($opml->as_string); |
308 | $c->response->content_type('application/rss'); |
309 | $c->response->status(200); |
310 | } |
311 | |
ea19a20c |
312 | =head2 patch |
313 | |
314 | A raw patch for a given commit. |
315 | |
316 | =cut |
317 | |
5bb401c6 |
318 | sub patch : Chained('base') Args(0) { |
377bf360 |
319 | my ($self, $c) = @_; |
61ba8635 |
320 | $c->detach('patches', [1]); |
321 | } |
322 | |
ea19a20c |
323 | =head2 patches |
324 | |
325 | The patcheset for a given commit ??? |
326 | |
327 | =cut |
328 | |
5bb401c6 |
329 | sub patches : Chained('base') Args(0) { |
61ba8635 |
330 | my ($self, $c, $count) = @_; |
331 | $count ||= Gitalist->config->{patches}{max}; |
377bf360 |
332 | my $commit = $self->_get_object($c); |
333 | my $parent = $c->req->param('hp') || undef; |
f707d264 |
334 | my $patch = $commit->get_patch( $parent, $count ); |
377bf360 |
335 | $c->response->body($patch); |
336 | $c->response->content_type('text/plain'); |
337 | $c->response->status(200); |
6cfcd548 |
338 | } |
339 | |
ea19a20c |
340 | =head2 snapshot |
341 | |
342 | Provides a snapshot of a given commit. |
343 | |
344 | =cut |
345 | |
5bb401c6 |
346 | sub snapshot : Chained('base') Args(0) { |
30db8f5b |
347 | my ($self, $c) = @_; |
63afe2db |
348 | my $format = $c->req->param('sf') || 'tgz'; |
30db8f5b |
349 | die unless $format; |
2e79039a |
350 | my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1; |
87581f05 |
351 | my @snap = $c->stash->{Repository}->snapshot( |
c0dbe239 |
352 | sha1 => $sha1, |
353 | format => $format |
354 | ); |
30db8f5b |
355 | $c->response->status(200); |
356 | $c->response->headers->header( 'Content-Disposition' => |
c0dbe239 |
357 | "attachment; filename=$snap[0]"); |
358 | $c->response->body($snap[1]); |
6cfcd548 |
359 | } |
360 | |
1625cd5f |
361 | |
5bb401c6 |
362 | sub base : Chained('/root') PathPart('') CaptureArgs(0) { |
4621ecf0 |
363 | my($self, $c) = @_; |
04d1d917 |
364 | |
066e9aa4 |
365 | my $git_version = `git --version`; |
366 | chomp($git_version); |
4621ecf0 |
367 | $c->stash( |
066e9aa4 |
368 | git_version => $git_version, |
da8f4f82 |
369 | version => $Gitalist::VERSION, |
370 | |
371 | # XXX Move these to a plugin! |
4621ecf0 |
372 | time_since => sub { |
ef11b09e |
373 | return 'never' unless $_[0]; |
4621ecf0 |
374 | return age_string(time - $_[0]->epoch); |
375 | }, |
376 | short_cmt => sub { |
377 | my $cmt = shift; |
378 | my($line) = split /\n/, $cmt; |
741e110e |
379 | $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/; |
4621ecf0 |
380 | return $line; |
381 | }, |
5cd9b9f9 |
382 | abridged_description => sub { |
383 | join(' ', grep { defined } (split / /, shift)[0..10]); |
384 | }, |
4621ecf0 |
385 | ); |
04d1d917 |
386 | } |
d9a9b56b |
387 | |
fde5091f |
388 | sub end : ActionClass('RenderView') { |
1aad4e81 |
389 | my ($self, $c) = @_; |
82bc0f05 |
390 | # Give repository views the current HEAD. |
87581f05 |
391 | if ($c->stash->{Repository}) { |
392 | $c->stash->{HEAD} = $c->stash->{Repository}->head_hash; |
1aad4e81 |
393 | } |
1fd8159c |
394 | } |
d9a9b56b |
395 | |
c113db92 |
396 | sub error_404 : Action { |
4111e151 |
397 | my ($self, $c) = @_; |
398 | $c->response->status(404); |
5232dbdd |
399 | $c->response->body('Page not found'); |
4111e151 |
400 | } |
401 | |
775e96e0 |
402 | __PACKAGE__->meta->make_immutable; |
403 | |
404 | __END__ |
405 | |
d137f7d5 |
406 | =head1 NAME |
407 | |
408 | Gitalist::Controller::Root - Root controller for the application |
409 | |
410 | =head1 DESCRIPTION |
411 | |
412 | This controller handles all of the root level paths for the application |
413 | |
414 | =head1 METHODS |
415 | |
5bb401c6 |
416 | =head2 root |
417 | |
418 | Root of chained actions |
419 | |
420 | =head2 base |
421 | |
422 | Populate the header and footer. Perhaps not the best location. |
423 | |
424 | =head2 index |
425 | |
82bc0f05 |
426 | Provides the repository listing. |
5bb401c6 |
427 | |
428 | =head2 end |
429 | |
430 | Attempt to render a view, if needed. |
d137f7d5 |
431 | |
432 | =head2 blame |
433 | |
d137f7d5 |
434 | =head2 error_404 |
435 | |
436 | =head2 history |
437 | |
438 | =head2 opml |
439 | |
82bc0f05 |
440 | =head2 repository_index |
da8f4f82 |
441 | |
775e96e0 |
442 | =head1 AUTHORS |
89de6a33 |
443 | |
775e96e0 |
444 | See L<Gitalist> for authors. |
89de6a33 |
445 | |
446 | =head1 LICENSE |
447 | |
775e96e0 |
448 | See L<Gitalist> for the license. |
89de6a33 |
449 | |
450 | =cut |