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 (); |
9912b062 |
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) = @_; |
586572a7 |
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, |
493327c8 |
26 | ); |
27 | $c->stash( |
28 | diff_tree => $tree, |
29 | diff => $patch, |
30 | # XXX Hack hack hack, see View::SyntaxHighlight |
31 | blobs => [map $_->{diff}, @$patch], |
0fb64eae |
32 | %filename, |
493327c8 |
33 | ); |
7998de12 |
34 | } |
493327c8 |
35 | |
d361d955 |
36 | after diff_fancy => sub { |
37 | my ($self, $c) = @_; |
7998de12 |
38 | $self->_diff($c); |
0fb64eae |
39 | $c->forward('Model::ContentMangler'); |
d361d955 |
40 | }; |
41 | |
42 | after diff_plain => sub { |
43 | my ($self, $c) = @_; |
7998de12 |
44 | $self->_diff($c); |
493327c8 |
45 | }; |
46 | |
1b33f63b |
47 | after tree => sub { |
b6ec181b |
48 | my ( $self, $c ) = @_; |
1b33f63b |
49 | my $repository = $c->stash->{Repository}; |
50 | my $commit = $c->stash->{Commit}; |
b6ec181b |
51 | my $tree = $c->stash->{filename} |
52 | ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename})) |
1b33f63b |
53 | : $repository->get_object($commit->tree_sha1) |
54 | ; |
55 | $c->stash( |
56 | tree => $tree, |
57 | tree_list => [$repository->list_tree($tree->sha1)], |
1b33f63b |
58 | ); |
59 | }; |
60 | |
571348f6 |
61 | after blame => sub { |
b6ec181b |
62 | my($self, $c) = @_; |
571348f6 |
63 | |
64 | my $repository = $c->stash->{Repository}; |
571348f6 |
65 | # WTF? |
b6ec181b |
66 | my $blame = $c->stash->{Commit}->blame($c->stash->{filename}, $c->stash->{Commit}->sha1); |
571348f6 |
67 | $c->stash( |
68 | blame => $blame, |
571348f6 |
69 | blob => join("\n", map $_->{line}, @$blame), |
70 | ); |
71 | |
1064e5b8 |
72 | $c->forward('Model::ContentMangler'); |
571348f6 |
73 | }; |
74 | |
85aed493 |
75 | =head2 blob |
76 | |
77 | The blob action i.e the contents of a file. |
78 | |
79 | =cut |
80 | |
81 | after blob => sub { |
b6ec181b |
82 | my ( $self, $c ) = @_; |
85aed493 |
83 | $c->stash( |
e172b6b8 |
84 | is_image => File::Type::WebImages::mime_type($c->stash->{blob}), |
53a9d6de |
85 | is_binary => Gitalist::Utils::is_binary($c->stash->{blob}), |
85aed493 |
86 | ); |
1064e5b8 |
87 | $c->forward('Model::ContentMangler'); |
85aed493 |
88 | }; |
89 | |
18fdf3d0 |
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 $file = $repository->get_object( |
102 | $repository->hash_by_path( |
103 | $repository->head_hash, |
104 | $filename |
105 | ) |
106 | ); |
107 | |
108 | my $page = $c->req->param('pg') || 0; |
109 | $logargs{skip} = $c->req->param('pg') * $logargs{count} |
110 | if $c->req->param('pg'); |
111 | |
112 | $c->stash( |
113 | log_lines => [$repository->list_revs(%logargs)], |
114 | refs => $repository->references, |
115 | filename => $filename, |
116 | filetype => $file->type, |
117 | ); |
118 | }; |
119 | |
9912b062 |
120 | after file_commit_info => sub { |
121 | my ($self, $c) = @_; |
122 | |
123 | my $repository = $c->stash->{Repository}; |
124 | |
125 | my($commit) = $repository->list_revs( |
126 | sha1 => $c->stash->{Commit}->sha1, |
127 | count => 1, |
128 | file => $c->stash->{filename}, |
129 | ); |
130 | |
e404e9a6 |
131 | my $json_obj = !$commit |
132 | ? { } |
133 | : { |
134 | sha1 => $commit->sha1, |
135 | comment => $c->stash->{short_cmt}->($commit->comment), |
136 | age => $c->stash->{time_since}->($commit->authored_time), |
137 | }; |
9912b062 |
138 | |
139 | $c->response->content_type('application/json'); |
140 | # XXX Make use of the json branch |
141 | $c->response->body( encode_json $json_obj ); |
142 | }; |
143 | |
0da7966e |
144 | __PACKAGE__->meta->make_immutable; |