Provided support for blobdiff_plain legacy URIs.
[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 sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {}
12
13 sub _diff {
14     my ($self, $c) = @_;
15     my $commit = $c->stash->{Commit};
16     my %filename = $c->stash->{filename} ? (filename => $c->stash->{filename}) : ();
17     my($tree, $patch) = $c->stash->{Repository}->diff(
18         commit => $commit,
19         parent => $c->stash->{parent},
20         patch  => 1,
21         %filename,
22     );
23     $c->stash(
24       diff_tree => $tree,
25       diff      => $patch,
26       # XXX Hack hack hack, see View::SyntaxHighlight
27       blobs     => [map $_->{diff}, @$patch],
28       language  => 'Diff',
29       %filename,
30     );
31 }
32
33 after diff_fancy => sub {
34     my ($self, $c) = @_;
35     $self->_diff($c);
36     $c->forward('View::SyntaxHighlight');
37 };
38
39 after diff_plain => sub {
40     my ($self, $c) = @_;
41     $self->_diff($c);
42 };
43
44 after tree => sub {
45     my ( $self, $c ) = @_;
46     my $repository = $c->stash->{Repository};
47     my $commit  = $c->stash->{Commit};
48     my $tree    = $c->stash->{filename}
49       ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
50       : $repository->get_object($commit->tree_sha1)
51     ;
52     $c->stash(
53         tree      => $tree,
54         tree_list => [$repository->list_tree($tree->sha1)],
55         path      => $c->stash->{filename}, # FIXME?
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         # XXX Hack hack hack, see View::SyntaxHighlight
68         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
69         blob     => join("\n", map $_->{line}, @$blame),
70     );
71
72     $c->forward('View::SyntaxHighlight')
73         unless $c->stash->{no_wrapper};
74 };
75
76 =head2 blob
77
78 The blob action i.e the contents of a file.
79
80 =cut
81
82 after blob => sub {
83     my ( $self, $c ) = @_;
84     $c->stash(
85         # XXX Hack hack hack, see View::SyntaxHighlight
86         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
87     );
88
89     $c->forward('View::SyntaxHighlight')
90         unless $c->stash->{no_wrapper};
91 };
92
93 after history => sub {
94     my ($self, $c) = @_;
95     my $repository  = $c->stash->{Repository};
96     my $filename    = $c->stash->{filename};
97
98     my %logargs = (
99        sha1   => $c->stash->{Commit}->sha1,
100        count  => 25, #Gitalist->config->{paging}{log} || 25,
101        ($filename ? (file => $filename) : ())
102     );
103
104     my $file = $repository->get_object(
105         $repository->hash_by_path(
106             $repository->head_hash,
107             $filename
108         )
109     );
110
111     my $page = $c->req->param('pg') || 0;
112     $logargs{skip} = $c->req->param('pg') * $logargs{count}
113         if $c->req->param('pg');
114
115     $c->stash(
116        log_lines => [$repository->list_revs(%logargs)],
117        refs      => $repository->references,
118        filename  => $filename,
119        filetype  => $file->type,
120     );
121 };
122
123 __PACKAGE__->meta->make_immutable;