b988d4dddab1783feb890b30b3edd9442e693431
[catagits/Gitalist.git] / lib / Gitalist / Controller / Ref.pm
1 package Gitalist::Controller::Ref;
2
3 use Moose;
4 use namespace::autoclean;
5
6 BEGIN { extends 'Gitalist::Controller' }
7 with 'Gitalist::URIStructure::Ref';
8
9 use File::Type;
10 use File::Type::WebImages ();
11
12 sub base : Chained('/repository/find') PathPart('') CaptureArgs(0) {}
13
14 after commit => sub {
15   my($self, $c) = @_;
16
17   $c->stash->{diff_tree} = ($c->stash->{Repository}->diff(
18     commit => $c->stash->{Commit},
19   ))[0];
20 };
21
22 sub raw : Chained('find') Does('FilenameArgs') Args() {
23     my ($self, $c) = @_;
24     $c->forward('find_blob');
25
26     if(-T $c->stash->{blob}) {
27         $c->response->content_type('text/plain; charset=utf-8');
28     } else {
29         my $ft = File::Type->new();
30         $c->response->content_type(
31             File::Type::WebImages::mime_type($c->stash->{blob})
32          || File::Type->new->mime_type($c->stash->{blob})
33         );
34     }
35
36     $c->response->body(delete $c->stash->{blob});
37 }
38
39 =head2 snapshot
40
41 Provides a snapshot of a given commit.
42
43 =cut
44
45 sub snapshot : Chained('find') PathPart('snapshot') Args() {
46     my ($self, $c, $format) = @_;
47     $format ||= 'tgz';
48     my @snap = $c->stash->{Repository}->snapshot(
49         sha1 => $c->stash->{Commit}->sha1,
50         format => $format
51     );
52     $c->response->status(200);
53     $c->response->headers->header( 'Content-Disposition' =>
54                                        "attachment; filename=$snap[0]");
55     $c->response->body($snap[1]);
56 }
57
58 =head2 patch
59
60 A raw patch for a given commit.
61
62 =cut
63
64 sub patch : Chained('find') Args(0) {
65     my ($self, $c) = @_;
66     $c->detach('patches', [1]);
67 }
68
69 =head2 patches
70
71 The patcheset for a given commit ???
72
73 =cut
74
75 sub patches : Chained('find') Args(1) {
76     my ($self, $c, $count) = @_;
77     $count ||= Gitalist->config->{patches}{max};
78     my $commit = $c->stash->{Commit};
79     my $parent = $c->req->param('hp') || undef; # FIXME
80     my $patch = $commit->get_patch( $parent, $count );
81     $c->response->body($patch);
82     $c->response->content_type('text/plain');
83     $c->response->status(200);
84 }
85
86 __PACKAGE__->meta->make_immutable;