]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
solver: rewrite to use Qspawn->psgi_qx and pi-httpd.async
[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);
24 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
25 my $max_size = 1024 * 1024; # TODO: configurable
26 my $enc_utf8 = find_encoding('UTF-8');
27
28 sub html_page ($$$) {
29         my ($ctx, $code, $strref) = @_;
30         my $wcb = delete $ctx->{-wcb};
31         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
32         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
33                 my ($nr, undef) =  @_;
34                 $nr == 1 ? $$strref : undef;
35         });
36         $wcb->($res);
37 }
38
39 sub solve_result {
40         my ($ctx, $res, $log, $hints, $fn) = @_;
41
42         unless (seek($log, 0, 0)) {
43                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
44                 return html_page($ctx, 500, \'seek error');
45         }
46         $log = do { local $/; <$log> };
47
48         my $ref = ref($res);
49         $log .= $res unless $ref;
50         my $l = PublicInbox::Linkify->new;
51         $l->linkify_1($log);
52         $log = '<pre>debug log:</pre><hr /><pre>' .
53                 $l->linkify_2(ascii_html($log)) . '</pre>';
54
55         $res or return html_page($ctx, 404, \$log);
56         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
57
58         my ($git, $oid, $type, $size, $di) = @$res;
59         if ($size > $max_size) {
60                 # TODO: stream the raw file if it's gigantic, at least
61                 $log = '<pre><b>Too big to show</b></pre>' . $log;
62                 return html_page($ctx, 500, \$log);
63         }
64
65         my $blob = $git->cat_file($oid);
66         if (!$blob) { # WTF?
67                 my $e = "Failed to retrieve generated blob ($oid)";
68                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
69                 $log = "<pre><b>$e</b></pre>" . $log;
70                 return html_page($ctx, 500, \$log);
71         }
72
73         my $binary = index($$blob, "\0") >= 0;
74         if ($fn) {
75                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
76                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
77                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
78         }
79
80         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
81         my $raw_link = "(<a\nhref=$path>raw</a>)";
82         if ($binary) {
83                 $log = "<pre>$oid $type $size bytes (binary)" .
84                         " $raw_link</pre>" . $log;
85                 return html_page($ctx, 200, \$log);
86         }
87
88         $$blob = $enc_utf8->decode($$blob);
89         my $nl = ($$blob =~ tr/\n/\n/);
90         my $pad = length($nl);
91
92         # using some of the same CSS class names and ids as cgit
93         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
94                 "<hr /><table\nclass=blob>".
95                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
96                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
97                 } (1..$nl)) . '</pre></td>' .
98                 '<td><pre> </pre></td>'. # pad for non-CSS users
99                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
100                 ascii_html($$blob) .
101                 '</code></pre></td></tr></table>' . $log;
102
103         html_page($ctx, 200, \$log);
104 }
105
106 sub show ($$;$) {
107         my ($ctx, $oid_b, $fn) = @_;
108         my $qp = $ctx->{qp};
109         my $hints = {};
110         while (my ($from, $to) = each %QP_MAP) {
111                 defined(my $v = $qp->{$from}) or next;
112                 $hints->{$to} = $v;
113         }
114
115         open my $log, '+>', undef or die "open: $!";
116         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
117                 solve_result($ctx, $_[0], $log, $hints, $fn);
118         });
119
120         # PSGI server will call this and give us a callback
121         sub {
122                 $ctx->{-wcb} = $_[0]; # HTTP write callback
123                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
124         };
125 }
126
127 1;