A manual cherry-picking of f012b0, making use of syn_engines work mostly.
[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 %diff_args = ( patch => 1 );
18     foreach my $arg qw/filename parent/ {
19         if (defined $c->stash->{$arg}) {
20             $diff_args{$arg} = $c->stash->{$arg};
21         };
22     };
23     my ($tree, $patch) = $c->stash->{Commit}->diff(
24         %diff_args,
25     );
26     $c->stash(
27       diff_tree => $tree,
28       diff      => $patch,
29       # XXX Hack hack hack, see View::SyntaxHighlight
30       blobs     => [map $_->{diff}, @$patch],
31       %filename,
32     );
33 }
34
35 after diff_fancy => sub {
36     my ($self, $c) = @_;
37     $self->_diff($c);
38     $c->forward('Model::ContentMangler');
39 };
40
41 after diff_plain => sub {
42     my ($self, $c) = @_;
43     $self->_diff($c);
44 };
45
46 after tree => sub {
47     my ( $self, $c ) = @_;
48     my $repository = $c->stash->{Repository};
49     my $commit  = $c->stash->{Commit};
50     my $tree    = $c->stash->{filename}
51       ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
52       : $repository->get_object($commit->tree_sha1)
53     ;
54     $c->stash(
55         tree      => $tree,
56         tree_list => [$repository->list_tree($tree->sha1)],
57     );
58 };
59
60 after blame => sub {
61     my($self, $c) = @_;
62
63     my $repository = $c->stash->{Repository};
64                                                       # WTF?
65     my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
66     $c->stash(
67         blame    => $blame,
68         blob     => join("\n", map $_->{line}, @$blame),
69     );
70
71     $c->forward('Model::ContentMangler')
72         unless $c->stash->{no_wrapper};
73 };
74
75 =head2 blob
76
77 The blob action i.e the contents of a file.
78
79 =cut
80
81 after blob => sub {
82     my ( $self, $c ) = @_;
83     $c->stash(
84         is_image  => File::Type::WebImages::mime_type($c->stash->{blob}),
85         is_binary => Gitalist::Utils::is_binary($c->stash->{blob}),
86     );
87
88     $c->forward('Model::ContentMangler')
89         unless $c->stash->{no_wrapper};
90 };
91
92 after history => sub {
93     my ($self, $c) = @_;
94     my $repository  = $c->stash->{Repository};
95     my $filename    = $c->stash->{filename};
96
97     my %logargs = (
98        sha1   => $c->stash->{Commit}->sha1,
99        count  => 25, #Gitalist->config->{paging}{log} || 25,
100        ($filename ? (file => $filename) : ())
101     );
102
103     my $file = $repository->get_object(
104         $repository->hash_by_path(
105             $repository->head_hash,
106             $filename
107         )
108     );
109
110     my $page = $c->req->param('pg') || 0;
111     $logargs{skip} = $c->req->param('pg') * $logargs{count}
112         if $c->req->param('pg');
113
114     $c->stash(
115        log_lines => [$repository->list_revs(%logargs)],
116        refs      => $repository->references,
117        filename  => $filename,
118        filetype  => $file->type,
119     );
120 };
121
122 __PACKAGE__->meta->make_immutable;