]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
doc: remove completed TODO items
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # show any VCS object, similar to "git show"
5 # FIXME: we only show blobs for now
6 #
7 # This can use a "solver" to reconstruct blobs based on git
8 # patches (with abbreviated OIDs in the header).  However, the
9 # abbreviated OIDs must match exactly what's in the original
10 # email (unless a normal code repo already has the blob).
11 #
12 # In other words, we can only reliably reconstruct blobs based
13 # on links generated by ViewDiff (and only if the emailed
14 # patches apply 100% cleanly to published blobs).
15
16 package PublicInbox::ViewVCS;
17 use strict;
18 use warnings;
19 use Encode qw(find_encoding);
20 use PublicInbox::SolverGit;
21 use PublicInbox::WwwStream;
22 use PublicInbox::Linkify;
23 use PublicInbox::Hval qw(ascii_html to_filename src_escape);
24 my $hl = eval {
25         require PublicInbox::HlMod;
26         PublicInbox::HlMod->new;
27 };
28
29 # we need to trigger highlight::CodeGenerator::deleteInstance
30 # in HlMod::DESTROY before the rest of Perl shuts down to avoid
31 # a segfault at shutdown
32 END { $hl = undef };
33
34 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
35 my $max_size = 1024 * 1024; # TODO: configurable
36 my $enc_utf8 = find_encoding('UTF-8');
37 my $BIN_DETECT = 8000; # same as git
38
39 sub html_page ($$$) {
40         my ($ctx, $code, $strref) = @_;
41         my $wcb = delete $ctx->{-wcb};
42         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
43         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
44                 my ($nr, undef) =  @_;
45                 $nr == 1 ? $$strref : undef;
46         });
47         $wcb ? $wcb->($res) : $res;
48 }
49
50 sub stream_large_blob ($$$$) {
51         my ($ctx, $res, $logref, $fn) = @_;
52         my ($git, $oid, $type, $size, $di) = @$res;
53         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
54         my $qsp = PublicInbox::Qspawn->new($cmd);
55         my @cl = ('Content-Length', $size);
56         my $env = $ctx->{env};
57         $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
58         $qsp->psgi_return($env, undef, sub {
59                 my ($r, $bref) = @_;
60                 if (!defined $r) { # error
61                         html_page($ctx, 500, $logref);
62                 } elsif (index($$bref, "\0") >= 0) {
63                         my $ct = 'application/octet-stream';
64                         [200, ['Content-Type', $ct, @cl ] ];
65                 } else {
66                         my $n = bytes::length($$bref);
67                         if ($n >= $BIN_DETECT || $n == $size) {
68                                 my $ct = 'text/plain; charset=UTF-8';
69                                 return [200, ['Content-Type', $ct, @cl] ];
70                         }
71                         undef; # bref keeps growing
72                 }
73         });
74 }
75
76 sub solve_result {
77         my ($ctx, $res, $log, $hints, $fn) = @_;
78
79         unless (seek($log, 0, 0)) {
80                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
81                 return html_page($ctx, 500, \'seek error');
82         }
83         $log = do { local $/; <$log> };
84
85         my $ref = ref($res);
86         my $l = PublicInbox::Linkify->new;
87         $l->linkify_1($log);
88         $log = '<pre>debug log:</pre><hr /><pre>' .
89                 $l->linkify_2(ascii_html($log)) . '</pre>';
90
91         $res or return html_page($ctx, 404, \$log);
92         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
93
94         my ($git, $oid, $type, $size, $di) = @$res;
95         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
96         my $raw_link = "(<a\nhref=$path>raw</a>)";
97         if ($size > $max_size) {
98                 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
99                 $log = "<pre><b>Too big to show, download available</b>\n" .
100                         "$oid $type $size bytes $raw_link</pre>" . $log;
101                 return html_page($ctx, 500, \$log);
102         }
103
104         my $blob = $git->cat_file($oid);
105         if (!$blob) { # WTF?
106                 my $e = "Failed to retrieve generated blob ($oid)";
107                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
108                 $log = "<pre><b>$e</b></pre>" . $log;
109                 return html_page($ctx, 500, \$log);
110         }
111
112         my $binary = index($$blob, "\0") >= 0;
113         if ($fn) {
114                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
115                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
116                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
117         }
118
119         if ($binary) {
120                 $log = "<pre>$oid $type $size bytes (binary)" .
121                         " $raw_link</pre>" . $log;
122                 return html_page($ctx, 200, \$log);
123         }
124
125         $$blob = $enc_utf8->decode($$blob);
126         my $nl = ($$blob =~ tr/\n/\n/);
127         my $pad = length($nl);
128
129         $l->linkify_1($$blob);
130         my $ok = $hl->do_hl($blob, $path) if $hl;
131         if ($ok) {
132                 $$ok = $enc_utf8->decode($$ok);
133                 src_escape($$ok);
134                 $blob = $ok;
135         } else {
136                 $$blob = ascii_html($$blob);
137         }
138
139         # using some of the same CSS class names and ids as cgit
140         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
141                 "<hr /><table\nclass=blob>".
142                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
143                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
144                 } (1..$nl)) . '</pre></td>' .
145                 '<td><pre> </pre></td>'. # pad for non-CSS users
146                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
147                 $l->linkify_2($$blob) .
148                 '</code></pre></td></tr></table>' . $log;
149
150         html_page($ctx, 200, \$log);
151 }
152
153 sub show ($$;$) {
154         my ($ctx, $oid_b, $fn) = @_;
155         my $qp = $ctx->{qp};
156         my $hints = {};
157         while (my ($from, $to) = each %QP_MAP) {
158                 defined(my $v = $qp->{$from}) or next;
159                 $hints->{$to} = $v;
160         }
161
162         open my $log, '+>', undef or die "open: $!";
163         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
164                 solve_result($ctx, $_[0], $log, $hints, $fn);
165         });
166
167         # PSGI server will call this and give us a callback
168         sub {
169                 $ctx->{-wcb} = $_[0]; # HTTP write callback
170                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
171         };
172 }
173
174 1;