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