Got blob highlighting working with the ContentMangler.
[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 };
72
73 =head2 blob
74
75 The blob action i.e the contents of a file.
76
77 =cut
78
79 after blob => sub {
80     my ( $self, $c ) = @_;
81     $c->stash(
82         is_image  => File::Type::WebImages::mime_type($c->stash->{blob}),
83         is_binary => Gitalist::Utils::is_binary($c->stash->{blob}),
84     );
85     $c->forward('Model::ContentMangler');
86 };
87
88 after history => sub {
89     my ($self, $c) = @_;
90     my $repository  = $c->stash->{Repository};
91     my $filename    = $c->stash->{filename};
92
93     my %logargs = (
94        sha1   => $c->stash->{Commit}->sha1,
95        count  => 25, #Gitalist->config->{paging}{log} || 25,
96        ($filename ? (file => $filename) : ())
97     );
98
99     my $file = $repository->get_object(
100         $repository->hash_by_path(
101             $repository->head_hash,
102             $filename
103         )
104     );
105
106     my $page = $c->req->param('pg') || 0;
107     $logargs{skip} = $c->req->param('pg') * $logargs{count}
108         if $c->req->param('pg');
109
110     $c->stash(
111        log_lines => [$repository->list_revs(%logargs)],
112        refs      => $repository->references,
113        filename  => $filename,
114        filetype  => $file->type,
115     );
116 };
117
118 __PACKAGE__->meta->make_immutable;