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