1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # show any VCS object, similar to "git show"
5 # FIXME: we only show blobs for now
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).
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).
16 package PublicInbox::ViewVCS;
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);
26 require PublicInbox::HlMod;
27 PublicInbox::HlMod->new;
30 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
31 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
32 my $BIN_DETECT = 8000; # same as git
34 sub html_i { # WwwStream::getline callback
36 $nr == 1 ? ${delete $ctx->{obuf}} : undef;
40 my ($ctx, $code, $strref) = @_;
41 my $wcb = delete $ctx->{-wcb};
42 $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
43 $ctx->{obuf} = $strref;
44 my $res = PublicInbox::WwwStream->response($ctx, $code, \&html_i);
45 $wcb ? $wcb->($res) : $res;
48 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
49 my ($r, $bref, $ctx) = @_;
50 my ($res, $logref) = delete @$ctx{qw(-res -logref)};
51 my ($git, $oid, $type, $size, $di) = @$res;
52 my @cl = ('Content-Length', $size);
53 if (!defined $r) { # error
54 html_page($ctx, 500, $logref);
55 } elsif (index($$bref, "\0") >= 0) {
56 [200, [qw(Content-Type application/octet-stream), @cl] ];
58 my $n = bytes::length($$bref);
59 if ($n >= $BIN_DETECT || $n == $size) {
60 return [200, [ 'Content-Type',
61 'text/plain; charset=UTF-8', @cl ] ];
64 warn "premature EOF on $oid $$logref\n";
65 return html_page($ctx, 500, $logref);
67 undef; # bref keeps growing
71 sub stream_large_blob ($$$$) {
72 my ($ctx, $res, $logref, $fn) = @_;
73 $ctx->{-logref} = $logref;
75 my ($git, $oid, $type, $size, $di) = @$res;
76 my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
77 my $qsp = PublicInbox::Qspawn->new($cmd);
78 my $env = $ctx->{env};
79 $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
80 $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
83 sub show_other_result ($$) {
84 my ($bref, $ctx) = @_;
85 my ($qsp, $logref) = delete @$ctx{qw(-qsp -logref)};
86 if (my $err = $qsp->{err}) {
88 $$logref .= "git show error: $err";
89 return html_page($ctx, 500, $logref);
91 my $l = PublicInbox::Linkify->new;
93 $$bref = '<pre>'. $l->to_html($$bref);
94 $$bref .= '</pre><hr>' . $$logref;
95 html_page($ctx, 200, $bref);
98 sub show_other ($$$$) {
99 my ($ctx, $res, $logref, $fn) = @_;
100 my ($git, $oid, $type, $size) = @$res;
101 if ($size > $MAX_SIZE) {
102 $$logref = "$oid is too big to show\n" . $$logref;
103 return html_page($ctx, 200, $logref);
105 my $cmd = ['git', "--git-dir=$git->{git_dir}",
106 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
107 my $qsp = PublicInbox::Qspawn->new($cmd);
108 my $env = $ctx->{env};
110 $ctx->{-logref} = $logref;
111 $qsp->psgi_qx($env, undef, \&show_other_result, $ctx);
114 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
116 my ($res, $ctx) = @_;
117 my ($log, $hints, $fn) = delete @$ctx{qw(log hints fn)};
119 unless (seek($log, 0, 0)) {
120 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
121 return html_page($ctx, 500, \'seek error');
123 $log = do { local $/; <$log> };
126 my $l = PublicInbox::Linkify->new;
127 $log = '<pre>debug log:</pre><hr /><pre>' .
128 $l->to_html($log) . '</pre>';
130 $res or return html_page($ctx, 404, \$log);
131 $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
133 my ($git, $oid, $type, $size, $di) = @$res;
134 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
135 my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
136 my $raw_link = "(<a\nhref=$path>raw</a>)";
137 if ($size > $MAX_SIZE) {
138 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
139 $log = "<pre><b>Too big to show, download available</b>\n" .
140 "$oid $type $size bytes $raw_link</pre>" . $log;
141 return html_page($ctx, 200, \$log);
144 my $blob = $git->cat_file($oid);
146 my $e = "Failed to retrieve generated blob ($oid)";
147 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
148 $log = "<pre><b>$e</b></pre>" . $log;
149 return html_page($ctx, 500, \$log);
152 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
154 my $h = [ 'Content-Length', $size, 'Content-Type' ];
155 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
156 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
160 $log = "<pre>$oid $type $size bytes (binary)" .
161 " $raw_link</pre>" . $log;
162 return html_page($ctx, 200, \$log);
165 # TODO: detect + convert to ensure validity
166 utf8::decode($$blob);
167 my $nl = ($$blob =~ s/\r?\n/\n/sg);
168 my $pad = length($nl);
170 $l->linkify_1($$blob);
171 my $ok = $hl->do_hl($blob, $path) if $hl;
175 $$blob = ascii_html($$blob);
178 # using some of the same CSS class names and ids as cgit
179 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
180 "<hr /><table\nclass=blob>".
181 "<tr><td\nclass=linenumbers><pre>" . join('', map {
182 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
183 } (1..$nl)) . '</pre></td>' .
184 '<td><pre> </pre></td>'. # pad for non-CSS users
185 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
186 $l->linkify_2($$blob) .
187 '</code></pre></td></tr></table>' . $log;
189 html_page($ctx, 200, \$log);
193 my ($ctx, $oid_b, $fn) = @_;
195 my $hints = $ctx->{hints} = {};
196 while (my ($from, $to) = each %QP_MAP) {
197 defined(my $v = $qp->{$from}) or next;
201 $ctx->{'log'} = tmpfile("solve.$oid_b");
203 my $solver = PublicInbox::SolverGit->new($ctx->{-inbox},
204 \&solve_result, $ctx);
205 # PSGI server will call this immediately and give us a callback (-wcb)
207 $ctx->{-wcb} = $_[0]; # HTTP write callback
208 $solver->solve($ctx->{env}, $ctx->{log}, $oid_b, $hints);