]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
Merge remote-tracking branch 'origin/help-color'
[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 PublicInbox::SolverGit;
20 use PublicInbox::WwwStream;
21 use PublicInbox::Linkify;
22 use PublicInbox::Hval qw(ascii_html to_filename);
23 my $hl = eval {
24         require PublicInbox::HlMod;
25         PublicInbox::HlMod->new;
26 };
27
28 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
29 my $max_size = 1024 * 1024; # TODO: configurable
30 my $BIN_DETECT = 8000; # same as git
31
32 sub html_page ($$$) {
33         my ($ctx, $code, $strref) = @_;
34         my $wcb = delete $ctx->{-wcb};
35         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
36         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
37                 my ($nr, undef) =  @_;
38                 $nr == 1 ? $$strref : undef;
39         });
40         $wcb ? $wcb->($res) : $res;
41 }
42
43 sub stream_large_blob ($$$$) {
44         my ($ctx, $res, $logref, $fn) = @_;
45         my ($git, $oid, $type, $size, $di) = @$res;
46         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
47         my $qsp = PublicInbox::Qspawn->new($cmd);
48         my @cl = ('Content-Length', $size);
49         my $env = $ctx->{env};
50         $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
51         $qsp->psgi_return($env, undef, sub {
52                 my ($r, $bref) = @_;
53                 if (!defined $r) { # error
54                         html_page($ctx, 500, $logref);
55                 } elsif (index($$bref, "\0") >= 0) {
56                         my $ct = 'application/octet-stream';
57                         [200, ['Content-Type', $ct, @cl ] ];
58                 } else {
59                         my $n = bytes::length($$bref);
60                         if ($n >= $BIN_DETECT || $n == $size) {
61                                 my $ct = 'text/plain; charset=UTF-8';
62                                 return [200, ['Content-Type', $ct, @cl] ];
63                         }
64                         undef; # bref keeps growing
65                 }
66         });
67 }
68
69 sub solve_result {
70         my ($ctx, $res, $log, $hints, $fn) = @_;
71
72         unless (seek($log, 0, 0)) {
73                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
74                 return html_page($ctx, 500, \'seek error');
75         }
76         $log = do { local $/; <$log> };
77
78         my $ref = ref($res);
79         my $l = PublicInbox::Linkify->new;
80         $l->linkify_1($log);
81         $log = '<pre>debug log:</pre><hr /><pre>' .
82                 $l->linkify_2(ascii_html($log)) . '</pre>';
83
84         $res or return html_page($ctx, 404, \$log);
85         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
86
87         my ($git, $oid, $type, $size, $di) = @$res;
88         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
89         my $raw_link = "(<a\nhref=$path>raw</a>)";
90         if ($size > $max_size) {
91                 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
92                 $log = "<pre><b>Too big to show, download available</b>\n" .
93                         "$oid $type $size bytes $raw_link</pre>" . $log;
94                 return html_page($ctx, 500, \$log);
95         }
96
97         my $blob = $git->cat_file($oid);
98         if (!$blob) { # WTF?
99                 my $e = "Failed to retrieve generated blob ($oid)";
100                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
101                 $log = "<pre><b>$e</b></pre>" . $log;
102                 return html_page($ctx, 500, \$log);
103         }
104
105         my $binary = index($$blob, "\0") >= 0;
106         if (defined $fn) {
107                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
108                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
109                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
110         }
111
112         if ($binary) {
113                 $log = "<pre>$oid $type $size bytes (binary)" .
114                         " $raw_link</pre>" . $log;
115                 return html_page($ctx, 200, \$log);
116         }
117
118         # TODO: detect + convert to ensure validity
119         utf8::decode($$blob);
120         my $nl = ($$blob =~ tr/\n/\n/);
121         my $pad = length($nl);
122
123         $l->linkify_1($$blob);
124         my $ok = $hl->do_hl($blob, $path) if $hl;
125         if ($ok) {
126                 $blob = $ok;
127         } else {
128                 $$blob = ascii_html($$blob);
129         }
130
131         # using some of the same CSS class names and ids as cgit
132         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
133                 "<hr /><table\nclass=blob>".
134                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
135                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
136                 } (1..$nl)) . '</pre></td>' .
137                 '<td><pre> </pre></td>'. # pad for non-CSS users
138                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
139                 $l->linkify_2($$blob) .
140                 '</code></pre></td></tr></table>' . $log;
141
142         html_page($ctx, 200, \$log);
143 }
144
145 sub show ($$;$) {
146         my ($ctx, $oid_b, $fn) = @_;
147         my $qp = $ctx->{qp};
148         my $hints = {};
149         while (my ($from, $to) = each %QP_MAP) {
150                 defined(my $v = $qp->{$from}) or next;
151                 $hints->{$to} = $v;
152         }
153
154         open my $log, '+>', undef or die "open: $!";
155         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
156                 solve_result($ctx, $_[0], $log, $hints, $fn);
157         });
158
159         # PSGI server will call this and give us a callback
160         sub {
161                 $ctx->{-wcb} = $_[0]; # HTTP write callback
162                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
163         };
164 }
165
166 1;