549e4a32a997d61da2c8c652033d35481ba61282
[catagits/Gitalist.git] / lib / Gitalist / Controller / Fragment / Ref.pm
1 package Gitalist::Controller::Fragment::Ref;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Gitalist::Controller' }
6 with qw/
7     Gitalist::URIStructure::Ref
8     Gitalist::URIStructure::Fragment::WithLog
9 /;
10
11 use File::Type::WebImages ();
12
13 sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {}
14
15 sub _diff {
16     my ($self, $c) = @_;
17     my $commit = $c->stash->{Commit};
18     my %filename = $c->stash->{filename} ? (filename => $c->stash->{filename}) : ();
19     my($tree, $patch) = $c->stash->{Repository}->diff(
20         commit => $commit,
21         parent => $c->stash->{parent},
22         patch  => 1,
23         %filename,
24     );
25     $c->stash(
26       diff_tree => $tree,
27       diff      => $patch,
28       # XXX Hack hack hack, see View::SyntaxHighlight
29       blobs     => [map $_->{diff}, @$patch],
30       %filename,
31     );
32 }
33
34 after diff_fancy => sub {
35     my ($self, $c) = @_;
36     $self->_diff($c);
37     $c->forward('Model::ContentMangler');
38 };
39
40 after diff_plain => sub {
41     my ($self, $c) = @_;
42     $self->_diff($c);
43 };
44
45 after tree => sub {
46     my ( $self, $c ) = @_;
47     my $repository = $c->stash->{Repository};
48     my $commit  = $c->stash->{Commit};
49     my $tree    = $c->stash->{filename}
50       ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
51       : $repository->get_object($commit->tree_sha1)
52     ;
53     $c->stash(
54         tree      => $tree,
55         tree_list => [$repository->list_tree($tree->sha1)],
56     );
57 };
58
59 after blame => sub {
60     my($self, $c) = @_;
61
62     my $repository = $c->stash->{Repository};
63                                                       # WTF?
64     my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
65     $c->stash(
66         blame    => $blame,
67         blob     => join("\n", map $_->{line}, @$blame),
68     );
69
70     $c->forward('Model::ContentMangler')
71         unless $c->stash->{no_wrapper};
72 };
73
74 =head2 blob
75
76 The blob action i.e the contents of a file.
77
78 =cut
79
80 after blob => sub {
81     my ( $self, $c ) = @_;
82     $c->stash(
83         is_image  => File::Type::WebImages::mime_type($c->stash->{blob}),
84         is_binary => Gitalist::Utils::is_binary($c->stash->{blob}),
85     );
86
87     $c->forward('Model::ContentMangler')
88         unless $c->stash->{no_wrapper};
89 };
90
91 after history => sub {
92     my ($self, $c) = @_;
93     my $repository  = $c->stash->{Repository};
94     my $filename    = $c->stash->{filename};
95
96     my %logargs = (
97        sha1   => $c->stash->{Commit}->sha1,
98        count  => 25, #Gitalist->config->{paging}{log} || 25,
99        ($filename ? (file => $filename) : ())
100     );
101
102     my $file = $repository->get_object(
103         $repository->hash_by_path(
104             $repository->head_hash,
105             $filename
106         )
107     );
108
109     my $page = $c->req->param('pg') || 0;
110     $logargs{skip} = $c->req->param('pg') * $logargs{count}
111         if $c->req->param('pg');
112
113     $c->stash(
114        log_lines => [$repository->list_revs(%logargs)],
115        refs      => $repository->references,
116        filename  => $filename,
117        filetype  => $file->type,
118     );
119 };
120
121 __PACKAGE__->meta->make_immutable;