00e7dbdf421543d6aed65622abbd6a7b97ecad59
[catagits/Gitalist.git] / lib / Gitalist / Controller / Repository.pm
1 package Gitalist::Controller::Repository;
2 use Moose;
3 use Sys::Hostname qw/hostname/;
4 use namespace::autoclean;
5
6 BEGIN { extends 'Gitalist::Controller' }
7 with 'Gitalist::URIStructure::Repository';
8
9 sub base : Chained('/base') PathPart('') CaptureArgs(0) {}
10
11 =head2 search
12
13 The action for the search form.
14
15 =cut
16
17 sub search : Chained('find') Args(0) {
18   my($self, $c) = @_;
19   my $repository = $c->stash->{Repository};
20   # Lifted from /shortlog.
21   my %logargs = (
22     sha1   => $repository->head_hash,
23 #    count  => Gitalist->config->{paging}{log},
24 #    ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
25     search => {
26       type   => $c->req->param('type'),
27       text   => $c->req->param('text'),
28       regexp => $c->req->param('regexp') || 0,
29     },
30   );
31
32   $c->stash(
33 #      commit  => $commit,
34       results => [$repository->list_revs(%logargs)],
35           # This could be added - page      => $page,
36   );
37 }
38
39 =head2 tree
40
41 Provide a simple redirect to C</ref/tree>.
42
43 =cut
44
45 sub tree : Chained('find') Args(0) {
46     my($self, $c) = @_;
47     $c->res->redirect($c->uri_for_action('/ref/tree', [$c->stash->{Repository}->name, 'HEAD']));
48     $c->res->status(301);
49 }
50
51 =head2 atom
52
53 Provides an atom feed for a given repository.
54
55 =cut
56
57 sub atom : Chained('find') Does('FilenameArgs') Args() {
58     my ($self, $c) = @_;
59
60     my $host = lc hostname();
61     $c->stash(
62         title => $host . ' - ' . Gitalist->config->{name},
63         updated => DateTime->now
64     );
65
66     my $repository = $c->stash->{Repository};
67     my %logargs = (
68         sha1     => $repository->head_hash,
69         count    => Gitalist->config->{paging}{log} || 25,
70         ($c->stash->{filename} ? (file => $c->stash->{filename}) : ()),
71     );
72
73     my @revs;
74     my $mk_title = $c->stash->{short_cmt};
75     for my $commit ($repository->list_revs(%logargs)) {
76         my $entry = {};
77         $entry->{title} = $mk_title->($commit->comment);
78         $entry->{id} = $c->uri_for_action('/ref/commit', [$repository->name, $commit->sha1]);
79         # XXX FIXME Needs work ...
80         $entry->{content} = $commit->comment;
81         push(@revs, $entry);
82     }
83     $c->stash(
84         Commits => \@revs,
85         no_wrapper => 1,
86     );
87     $c->response->content_type('application/atom+xml');
88 }
89
90 =head2 rss
91
92 Provides an RSS feed for a given repository.
93
94 =cut
95
96 sub rss : Chained('find') Does('FilenameArgs') Args() {
97   my ($self, $c) = @_;
98
99   my $repository = $c->stash->{Repository};
100
101   $c->stash(
102     title          => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
103     language       => 'en',
104     pubDate        => DateTime->now,
105     lastBuildDate  => DateTime->now,
106     no_wrapper     => 1,
107   );
108
109   my %logargs = (
110       sha1   => $repository->head_hash,
111       count  => Gitalist->config->{paging}{log} || 25,
112       ($c->stash->{filename} ? (file => $c->stash->{filename}) : ()),
113   );
114   my @revs;
115   my $mk_title = $c->stash->{short_cmt};
116   for my $commit ($repository->list_revs(%logargs)) {
117     # XXX FIXME Needs work ....
118     push(@revs, {
119         title       => $mk_title->($commit->comment),
120         permaLink   => $c->uri_for_action('/ref/commit', [$repository->name, $commit->sha1]),
121         description => $commit->comment,
122     });
123   }
124   $c->stash(Commits => \@revs);
125   $c->response->content_type('application/rss+xml');
126 }
127
128 __PACKAGE__->meta->make_immutable;