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