Make filename handling a generic actionrole. Fix more nav uris
[catagits/Gitalist.git] / lib / Gitalist / Controller / Fragment / Commit.pm
1 package Gitalist::Controller::Fragment::Commit;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Gitalist::Controller' }
6 with 'Gitalist::URIStructure::Commit';
7
8 sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {}
9
10 after diff => sub {
11     my ($self, $c) = @_;
12     my $commit = $c->stash->{Commit};
13     my($tree, $patch) = $c->stash->{Repository}->diff(
14         commit => $commit,
15         parent => $c->req->param('hp') || undef,
16         patch  => 1,
17     );
18     $c->stash(
19       diff_tree => $tree,
20       diff      => $patch,
21       # XXX Hack hack hack, see View::SyntaxHighlight
22       blobs     => [map $_->{diff}, @$patch],
23       language  => 'Diff',
24     );
25 };
26
27 after diff_fancy => sub {
28     my ($self, $c) = @_;
29     $c->forward('View::SyntaxHighlight');
30 };
31
32 after diff_plain => sub {
33     my ($self, $c) = @_;
34     $c->response->content_type('text/plain; charset=utf-8');
35 };
36
37 after tree => sub {
38     my ( $self, $c ) = @_;
39     my $repository = $c->stash->{Repository};
40     my $commit  = $c->stash->{Commit};
41     my $tree    = $c->stash->{filename}
42       ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
43       : $repository->get_object($commit->tree_sha1)
44     ;
45     $c->stash(
46         tree      => $tree,
47         tree_list => [$repository->list_tree($tree->sha1)],
48         path      => $c->stash->{filename}, # FIXME?
49     );
50 };
51
52 after blame => sub {
53     my($self, $c) = @_;
54
55     my $repository = $c->stash->{Repository};
56                                                       # WTF?
57     my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
58     $c->stash(
59         blame    => $blame,
60         # XXX Hack hack hack, see View::SyntaxHighlight
61         language => ($c->stash->{filename} =~ /\.p[lm]$/i ? 'Perl' : ''),
62         blob     => join("\n", map $_->{line}, @$blame),
63     );
64
65     $c->forward('View::SyntaxHighlight')
66         unless $c->stash->{no_wrapper};
67 };
68
69 =head2 blob
70
71 The blob action i.e the contents of a file.
72
73 =cut
74
75 after blob => sub {
76     my ( $self, $c ) = @_;
77
78     my $repository = $c->stash->{Repository};
79     my $h  =
80           $repository->hash_by_path($c->stash->{Commit}->sha1, $c->stash->{filename})
81           || die "No file or sha1 provided.";
82      my $blob = $repository->get_object($h);
83     $c->stash(
84         blob     => $blob->content,
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 __PACKAGE__->meta->make_immutable;