Fleshed out reflog action.
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
1 package Gitalist::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Catalyst::Controller' }
6
7 #
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
10 #
11 __PACKAGE__->config->{namespace} = '';
12
13 =head1 NAME
14
15 Gitalist::Controller::Root - Root Controller for Gitalist
16
17 =head1 DESCRIPTION
18
19 [enter your description here]
20
21 =head1 METHODS
22
23 =cut
24
25 =head2 index
26
27 =cut
28
29 use IO::Capture::Stdout;
30 use File::Slurp qw(slurp);
31
32 sub run_gitweb {
33   my ( $self, $c ) = @_;
34
35   # XXX A slippery slope to be sure.
36   if($c->req->param('a')) {
37     my $capture = IO::Capture::Stdout->new();
38     $capture->start();
39     eval {
40       my $action = gitweb::main($c);
41       $action->();
42     };
43     $capture->stop();
44     
45     use Data::Dumper;
46     die Dumper($@)
47       if $@;
48   
49     my $output = join '', $capture->read;
50     $c->stash->{gitweb_output} = $output;
51     $c->stash->{template} = 'gitweb.tt2';
52   }
53 }
54
55 sub index :Path :Args(0) {
56   my ( $self, $c ) = @_;
57
58   # Leave actions up to gitweb at this point.
59   return $self->run_gitweb($c)
60     if $c->req->param('a');
61
62   my $list = $c->model('Git')->list_projects;
63   unless(@$list) {
64     die "No projects found";
65   }
66
67   $c->stash(
68     searchtext => $c->req->param('searchtext') || '',
69     projects   => $list,
70     action     => 'index',
71   );
72 }
73
74 sub blob : Local {
75   my ( $self, $c ) = @_;
76
77   my $git      = $c->model('Git');
78   my $req      = $c->req;
79   my $proj     = $req->param('p');
80   my $filename = $req->param('f')  || $req->param('filename');
81   my $hash     = $req->param('hb') || $git->get_head_hash($proj);
82   my $filehash = $req->param('h')  || $git->get_hash_by_path($proj, $hash, $filename, 'blob');
83   
84   my $blob = $git->run_cmd('cat-file' => blob => $filehash);
85
86   $c->stash(
87       blob   => $blob,
88       action => 'blob',
89   );
90 }
91
92 sub reflog : Local {
93   my ( $self, $c ) = @_;
94
95   my @log = $c->model('Git')->reflog(
96       # XXX The project parameter should probably be passed into the Model.
97       $c->req->param('p'),
98       '--since=yesterday'
99   );
100
101   $c->stash(
102       log    => \@log,
103       action => 'reflog',
104   );
105 }
106
107 sub auto : Private {
108     my($self, $c) = @_;
109
110     # Yes, this is hideous.
111     $self->header($c);
112     $self->footer($c);
113 }
114
115 # XXX This could probably be dropped altogether.
116 use Gitalist::Util qw(to_utf8);
117 # Formally git_header_html
118 sub header {
119   my($self, $c) = @_;
120
121   my $title = $c->config->{sitename};
122
123   my $project   = $c->req->param('project')  || $c->req->param('p');
124   my $action    = $c->req->param('action')   || $c->req->param('a');
125   my $file_name = $c->req->param('filename') || $c->req->param('f');
126   if(defined $project) {
127     $title .= " - " . to_utf8($project);
128     if (defined $action) {
129       $title .= "/$action";
130       if (defined $file_name) {
131         $title .= " - " . $file_name;
132         if ($action eq "tree" && $file_name !~ m|/$|) {
133           $title .= "/";
134         }
135       }
136     }
137   }
138
139   $c->stash->{version}     = $c->config->{version};
140   $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
141   $c->stash->{title}       = $title;
142
143   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
144   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
145
146   $c->stash->{project} = $project;
147   my @links;
148   if($project) {
149     my %href_params = $self->feed_info($c);
150     $href_params{'-title'} ||= 'log';
151
152     foreach my $format qw(RSS Atom) {
153       my $type = lc($format);
154       push @links, {
155         rel   => 'alternate',
156         title => "$project - $href_params{'-title'} - $format feed",
157
158         # XXX A bit hacky and could do with using gitweb::href() features
159         href  => "?a=$type;p=$project",
160         type  => "application/$type+xml"
161         }, {
162         rel   => 'alternate',
163
164         # XXX This duplication also feels a bit awkward
165         title => "$project - $href_params{'-title'} - $format feed (no merges)",
166         href  => "?a=$type;p=$project;opt=--no-merges",
167         type  => "application/$type+xml"
168         };
169     }
170   } else {
171     push @links, {
172       rel => "alternate",
173       title => $c->config->{sitename}." projects list",
174       href => '?a=project_index',
175       type => "text/plain; charset=utf-8"
176       }, {
177       rel => "alternate",
178       title => $c->config->{sitename}." projects feeds",
179       href => '?a=opml',
180       type => "text/plain; charset=utf-8"
181       };
182   }
183
184   $c->stash->{favicon} = $c->config->{favicon};
185
186   # </head><body>
187
188   $c->stash(
189     logo_url      => $c->config->{logo_url},
190     logo_label    => $c->config->{logo_label},
191     logo_img      => $c->config->{logo},
192     home_link     => $c->config->{home_link},
193     home_link_str => $c->config->{home_link_str},
194     );
195
196   if(defined $project) {
197     $c->stash(
198       search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
199       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
200           || $c->req->param('h')  || $c->req->param('hash')
201           || 'HEAD' ),
202       );
203   }
204 }
205
206 # Formally git_footer_html
207 sub footer {
208   my($self, $c) = @_;
209
210   my $feed_class = 'rss_logo';
211
212   my @feeds;
213   my $project = $c->req->param('project')  || $c->req->param('p');
214   if(defined $project) {
215     (my $pstr = $project) =~ s[/?\.git$][];
216     my $descr = $c->model('Git')->project_info($project)->{description};
217     $c->stash->{project_description} = defined $descr
218       ? $descr
219       : '';
220
221     my %href_params = $self->feed_info($c);
222     if (!%href_params) {
223       $feed_class .= ' generic';
224     }
225     $href_params{'-title'} ||= 'log';
226
227     @feeds = [
228       map +{
229         class => $feed_class,
230         title => "$href_params{'-title'} $_ feed",
231         href  => "/?p=$project;a=\L$_",
232         name  => lc $_,
233         }, qw(RSS Atom)
234       ];
235   } else {
236     @feeds = [
237       map {
238         class => $feed_class,
239           title => '',
240           href  => "/?a=$_->[0]",
241           name  => $_->[1],
242         }, [opml=>'OPML'],[project_index=>'TXT'],
243       ];
244   }
245 }
246
247 # XXX This feels wrong here, should probably be refactored.
248 # returns hash to be passed to href to generate gitweb URL
249 # in -title key it returns description of link
250 sub feed_info {
251   my($self, $c) = @_;
252
253   my $format = shift || 'Atom';
254   my %res = (action => lc($format));
255
256   # feed links are possible only for project views
257   return unless $c->req->param('project');
258
259   # some views should link to OPML, or to generic project feed,
260   # or don't have specific feed yet (so they should use generic)
261   return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
262
263   my $branch;
264   my $hash = $c->req->param('h')  || $c->req->param('hash');
265   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
266
267   # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
268   # from tag links; this also makes possible to detect branch links
269   if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
270     (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
271     $branch = $1;
272   }
273
274   # find log type for feed description (title)
275   my $type = 'log';
276   my $file_name = $c->req->param('f') || $c->req->param('filename');
277   if (defined $file_name) {
278     $type  = "history of $file_name";
279     $type .= "/" if $c->req->param('action') eq 'tree';
280     $type .= " on '$branch'" if (defined $branch);
281   } else {
282     $type = "log of $branch" if (defined $branch);
283   }
284
285   $res{-title} = $type;
286   $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
287   $res{'file_name'} = $file_name;
288
289   return %res;
290 }
291 =head2 end
292
293 Attempt to render a view, if needed.
294
295 =cut
296
297 sub end : ActionClass('RenderView') {}
298
299 =head1 AUTHOR
300
301 Dan Brook,,,
302
303 =head1 LICENSE
304
305 This library is free software. You can redistribute it and/or modify
306 it under the same terms as Perl itself.
307
308 =cut
309
310 __PACKAGE__->meta->make_immutable;