e03bb22cc8ba1d8845ca7b764eb0bdb906079433
[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 use JSON::XS qw(encode_json);
13
14 sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {}
15
16 sub _diff {
17     my ($self, $c) = @_;
18     my %diff_args = ( patch => 1 );
19     foreach my $arg qw/filename parent/ {
20         if (defined $c->stash->{$arg}) {
21             $diff_args{$arg} = $c->stash->{$arg};
22         };
23     };
24     my ($tree, $patch) = $c->stash->{Commit}->diff(
25         %diff_args,
26     );
27     $c->stash(
28       diff_tree => $tree,
29       diff      => $patch,
30       # XXX Hack hack hack, see View::SyntaxHighlight
31       blobs     => [map $_->{diff}, @$patch],
32       %diff_args,
33     );
34 }
35
36 after diff_fancy => sub {
37     my ($self, $c) = @_;
38     $self->_diff($c);
39     $c->forward('Model::ContentMangler');
40 };
41
42 after diff_plain => sub {
43     my ($self, $c) = @_;
44     $self->_diff($c);
45 };
46
47 after tree => sub {
48     my ( $self, $c ) = @_;
49     my $repository = $c->stash->{Repository};
50     my $commit  = $c->stash->{Commit};
51     my $tree    = $c->stash->{filename}
52       ? $repository->get_object($commit->sha_by_path($c->stash->{filename}))
53       : $repository->get_object($commit->tree_sha1)
54     ;
55     $c->stash(
56         tree      => $tree,
57         tree_list => [$repository->list_tree($tree->sha1)],
58     );
59 };
60
61 after blame => sub {
62     my($self, $c) = @_;
63
64     my $repository = $c->stash->{Repository};
65                                                       # WTF?
66     my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1);
67     $c->stash(
68         blame    => $blame,
69         blob     => join("\n", map $_->{line}, @$blame),
70     );
71
72     $c->forward('Model::ContentMangler');
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     $c->forward('Model::ContentMangler');
88 };
89
90 after history => sub {
91     my ($self, $c) = @_;
92     my $repository  = $c->stash->{Repository};
93     my $filename    = $c->stash->{filename};
94
95     my %logargs = (
96        sha1   => $c->stash->{Commit}->sha1,
97        count  => 25, #Gitalist->config->{paging}{log} || 25,
98        ($filename ? (file => $filename) : ())
99     );
100
101     my $commit = $repository->get_object('HEAD');
102     my $file = $repository->get_object($commit->sha_by_path($filename));
103
104     my $page = $c->req->param('pg') || 0;
105     $logargs{skip} = $c->req->param('pg') * $logargs{count}
106         if $c->req->param('pg');
107
108     $c->stash(
109        log_lines => [$repository->list_revs(%logargs)],
110        refs      => $repository->references,
111        filename  => $filename,
112        filetype  => $file->type,
113     );
114 };
115
116 after file_commit_info => sub {
117     my ($self, $c) = @_;
118
119     my $repository  = $c->stash->{Repository};
120
121     my($commit) = $repository->list_revs(
122        sha1   => $c->stash->{Commit}->sha1,
123        count  => 1,
124        file   => $c->stash->{filename},
125     );
126
127     my $json_obj = !$commit
128                  ? { }
129                  : {
130                      sha1    => $commit->sha1,
131                      comment => $c->stash->{short_cmt}->($commit->comment),
132                      age     => $c->stash->{time_since}->($commit->authored_time),
133                  };
134
135     $c->response->content_type('application/json');
136     # XXX Make use of the json branch
137     $c->response->body( encode_json $json_obj );
138 };
139
140 __PACKAGE__->meta->make_immutable;