Make the log work for commits and symbolic refs also.
[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 after diff => sub {
14     my ($self, $c) = @_;
15     my $commit = $c->stash->{Commit};
16     my($tree, $patch) = $c->stash->{Repository}->diff(
17         commit => $commit,
18         parent => $c->req->param('hp') || undef,
19         patch  => 1,
20     );
21     $c->stash(
22       diff_tree => $tree,
23       diff      => $patch,
24       # XXX Hack hack hack, see View::SyntaxHighlight
25       blobs     => [map $_->{diff}, @$patch],
26       language  => 'Diff',
27     );
28 };
29
30 after diff_fancy => sub {
31     my ($self, $c) = @_;
32     $c->forward('View::SyntaxHighlight');
33 };
34
35 after diff_plain => sub {
36     my ($self, $c) = @_;
37     $c->response->content_type('text/plain; charset=utf-8');
38 };
39
40 after tree => sub {
41     my ( $self, $c ) = @_;
42     my $repository = $c->stash->{Repository};
43     my $commit  = $c->stash->{Commit};
44     my $tree    = $c->stash->{filename}
45       ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
46       : $repository->get_object($commit->tree_sha1)
47     ;
48     $c->stash(
49         tree      => $tree,
50         tree_list => [$repository->list_tree($tree->sha1)],
51         path      => $c->stash->{filename}, # FIXME?
52     );
53 };
54
55 after blame => sub {
56     my($self, $c) = @_;
57
58     my $repository = $c->stash->{Repository};
59                                                       # WTF?
60     my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
61     $c->stash(
62         blame    => $blame,
63         # XXX Hack hack hack, see View::SyntaxHighlight
64         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
65         blob     => join("\n", map $_->{line}, @$blame),
66     );
67
68     $c->forward('View::SyntaxHighlight')
69         unless $c->stash->{no_wrapper};
70 };
71
72 =head2 blob
73
74 The blob action i.e the contents of a file.
75
76 =cut
77
78 after blob => sub {
79     my ( $self, $c ) = @_;
80     $c->stash(
81         # XXX Hack hack hack, see View::SyntaxHighlight
82         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
83     );
84
85     $c->forward('View::SyntaxHighlight')
86         unless $c->stash->{no_wrapper};
87 };
88
89 __PACKAGE__->meta->make_immutable;