Commit | Line | Data |
2980657b |
1 | package Gitalist::Controller::Fragment::Ref; |
0da7966e |
2 | use Moose; |
3 | use namespace::autoclean; |
4 | |
b6ec181b |
5 | BEGIN { extends 'Gitalist::Controller' } |
b9423b8e |
6 | with qw/ |
7 | Gitalist::URIStructure::Ref |
8 | Gitalist::URIStructure::Fragment::WithLog |
9 | /; |
0da7966e |
10 | |
e172b6b8 |
11 | use File::Type::WebImages (); |
5dc23a14 |
12 | use JSON::XS qw(encode_json); |
e172b6b8 |
13 | |
0da7966e |
14 | sub base : Chained('/fragment/repository/find') PathPart('') CaptureArgs(0) {} |
15 | |
7998de12 |
16 | sub _diff { |
493327c8 |
17 | my ($self, $c) = @_; |
18 | my $commit = $c->stash->{Commit}; |
7998de12 |
19 | my %filename = $c->stash->{filename} ? (filename => $c->stash->{filename}) : (); |
493327c8 |
20 | my($tree, $patch) = $c->stash->{Repository}->diff( |
21 | commit => $commit, |
7998de12 |
22 | parent => $c->stash->{parent}, |
493327c8 |
23 | patch => 1, |
7998de12 |
24 | %filename, |
493327c8 |
25 | ); |
26 | $c->stash( |
27 | diff_tree => $tree, |
28 | diff => $patch, |
29 | # XXX Hack hack hack, see View::SyntaxHighlight |
30 | blobs => [map $_->{diff}, @$patch], |
7998de12 |
31 | %filename, |
493327c8 |
32 | ); |
7998de12 |
33 | } |
493327c8 |
34 | |
d361d955 |
35 | after diff_fancy => sub { |
36 | my ($self, $c) = @_; |
7998de12 |
37 | $self->_diff($c); |
5424d43c |
38 | $c->forward('Model::ContentMangler'); |
d361d955 |
39 | }; |
40 | |
41 | after diff_plain => sub { |
42 | my ($self, $c) = @_; |
7998de12 |
43 | $self->_diff($c); |
493327c8 |
44 | }; |
45 | |
1b33f63b |
46 | after tree => sub { |
b6ec181b |
47 | my ( $self, $c ) = @_; |
1b33f63b |
48 | my $repository = $c->stash->{Repository}; |
49 | my $commit = $c->stash->{Commit}; |
b6ec181b |
50 | my $tree = $c->stash->{filename} |
51 | ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename})) |
1b33f63b |
52 | : $repository->get_object($commit->tree_sha1) |
53 | ; |
54 | $c->stash( |
55 | tree => $tree, |
56 | tree_list => [$repository->list_tree($tree->sha1)], |
1b33f63b |
57 | ); |
58 | }; |
59 | |
571348f6 |
60 | after blame => sub { |
b6ec181b |
61 | my($self, $c) = @_; |
571348f6 |
62 | |
63 | my $repository = $c->stash->{Repository}; |
571348f6 |
64 | # WTF? |
b6ec181b |
65 | my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1); |
571348f6 |
66 | $c->stash( |
67 | blame => $blame, |
571348f6 |
68 | blob => join("\n", map $_->{line}, @$blame), |
69 | ); |
70 | |
b4ecc157 |
71 | $c->forward('Model::ContentMangler'); |
571348f6 |
72 | }; |
73 | |
85aed493 |
74 | =head2 blob |
75 | |
76 | The blob action i.e the contents of a file. |
77 | |
78 | =cut |
79 | |
80 | after blob => sub { |
b6ec181b |
81 | my ( $self, $c ) = @_; |
85aed493 |
82 | $c->stash( |
e172b6b8 |
83 | is_image => File::Type::WebImages::mime_type($c->stash->{blob}), |
53a9d6de |
84 | is_binary => Gitalist::Utils::is_binary($c->stash->{blob}), |
85aed493 |
85 | ); |
b4ecc157 |
86 | $c->forward('Model::ContentMangler'); |
85aed493 |
87 | }; |
88 | |
18fdf3d0 |
89 | after history => sub { |
90 | my ($self, $c) = @_; |
91 | my $repository = $c->stash->{Repository}; |
92 | my $filename = $c->stash->{filename}; |
93 | |
94 | my %logargs = ( |
95 | sha1 => $c->stash->{Commit}->sha1, |
96 | count => 25, #Gitalist->config->{paging}{log} || 25, |
97 | ($filename ? (file => $filename) : ()) |
98 | ); |
99 | |
100 | my $file = $repository->get_object( |
101 | $repository->hash_by_path( |
102 | $repository->head_hash, |
103 | $filename |
104 | ) |
105 | ); |
106 | |
107 | my $page = $c->req->param('pg') || 0; |
108 | $logargs{skip} = $c->req->param('pg') * $logargs{count} |
109 | if $c->req->param('pg'); |
110 | |
111 | $c->stash( |
112 | log_lines => [$repository->list_revs(%logargs)], |
113 | refs => $repository->references, |
114 | filename => $filename, |
115 | filetype => $file->type, |
116 | ); |
117 | }; |
118 | |
5dc23a14 |
119 | after file_commit_info => sub { |
120 | my ($self, $c) = @_; |
121 | |
122 | my $repository = $c->stash->{Repository}; |
123 | |
124 | my($commit) = $repository->list_revs( |
125 | sha1 => $c->stash->{Commit}->sha1, |
126 | count => 1, |
127 | file => $c->stash->{filename}, |
128 | ); |
129 | |
47eed95e |
130 | my $json_obj = !$commit |
131 | ? { } |
132 | : { |
133 | sha1 => $commit->sha1, |
134 | comment => $c->stash->{short_cmt}->($commit->comment), |
135 | age => $c->stash->{time_since}->($commit->authored_time), |
136 | }; |
5dc23a14 |
137 | |
138 | $c->response->content_type('application/json'); |
139 | # XXX Make use of the json branch |
140 | $c->response->body( encode_json $json_obj ); |
141 | }; |
142 | |
0da7966e |
143 | __PACKAGE__->meta->make_immutable; |