1 # Copyright (C) 2019 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
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 {
40 $nr == 1 ? $$strref : undef;
42 $wcb ? $wcb->($res) : $res;
45 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
46 my ($r, $bref, $ctx) = @_;
47 my ($res, $logref) = delete @$ctx{qw(-res -logref)};
48 my ($git, $oid, $type, $size, $di) = @$res;
49 my @cl = ('Content-Length', $size);
50 if (!defined $r) { # error
51 html_page($ctx, 500, $logref);
52 } elsif (index($$bref, "\0") >= 0) {
53 [200, [qw(Content-Type application/octet-stream), @cl] ];
55 my $n = bytes::length($$bref);
56 if ($n >= $BIN_DETECT || $n == $size) {
57 return [200, [ 'Content-Type',
58 'text/plain; charset=UTF-8', @cl ] ];
61 warn "premature EOF on $oid $$logref\n";
62 return html_page($ctx, 500, $logref);
64 undef; # bref keeps growing
68 sub stream_large_blob ($$$$) {
69 my ($ctx, $res, $logref, $fn) = @_;
70 $ctx->{-logref} = $logref;
72 my ($git, $oid, $type, $size, $di) = @$res;
73 my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
74 my $qsp = PublicInbox::Qspawn->new($cmd);
75 my $env = $ctx->{env};
76 $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
77 $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
80 sub show_other_result ($$) {
81 my ($bref, $ctx) = @_;
82 my ($qsp, $logref) = delete @$ctx{qw(-qsp -logref)};
83 if (my $err = $qsp->{err}) {
85 $$logref .= "git show error: $err";
86 return html_page($ctx, 500, $logref);
88 my $l = PublicInbox::Linkify->new;
90 $l->linkify_1($$bref);
91 $$bref = '<pre>'. $l->linkify_2(ascii_html($$bref));
92 $$bref .= '</pre><hr>' . $$logref;
93 html_page($ctx, 200, $bref);
96 sub show_other ($$$$) {
97 my ($ctx, $res, $logref, $fn) = @_;
98 my ($git, $oid, $type, $size) = @$res;
99 if ($size > $MAX_SIZE) {
100 $$logref = "$oid is too big to show\n" . $$logref;
101 return html_page($ctx, 200, $logref);
103 my $cmd = ['git', "--git-dir=$git->{git_dir}",
104 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
105 my $qsp = PublicInbox::Qspawn->new($cmd);
106 my $env = $ctx->{env};
108 $ctx->{-logref} = $logref;
109 $qsp->psgi_qx($env, undef, \&show_other_result, $ctx);
113 my ($ctx, $res, $log, $hints, $fn) = @_;
115 unless (seek($log, 0, 0)) {
116 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
117 return html_page($ctx, 500, \'seek error');
119 $log = do { local $/; <$log> };
122 my $l = PublicInbox::Linkify->new;
124 $log = '<pre>debug log:</pre><hr /><pre>' .
125 $l->linkify_2(ascii_html($log)) . '</pre>';
127 $res or return html_page($ctx, 404, \$log);
128 $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
130 my ($git, $oid, $type, $size, $di) = @$res;
131 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
132 my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
133 my $raw_link = "(<a\nhref=$path>raw</a>)";
134 if ($size > $MAX_SIZE) {
135 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
136 $log = "<pre><b>Too big to show, download available</b>\n" .
137 "$oid $type $size bytes $raw_link</pre>" . $log;
138 return html_page($ctx, 200, \$log);
141 my $blob = $git->cat_file($oid);
143 my $e = "Failed to retrieve generated blob ($oid)";
144 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
145 $log = "<pre><b>$e</b></pre>" . $log;
146 return html_page($ctx, 500, \$log);
149 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
151 my $h = [ 'Content-Length', $size, 'Content-Type' ];
152 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
153 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
157 $log = "<pre>$oid $type $size bytes (binary)" .
158 " $raw_link</pre>" . $log;
159 return html_page($ctx, 200, \$log);
162 # TODO: detect + convert to ensure validity
163 utf8::decode($$blob);
164 my $nl = ($$blob =~ tr/\n/\n/);
165 my $pad = length($nl);
167 $l->linkify_1($$blob);
168 my $ok = $hl->do_hl($blob, $path) if $hl;
172 $$blob = ascii_html($$blob);
175 # using some of the same CSS class names and ids as cgit
176 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
177 "<hr /><table\nclass=blob>".
178 "<tr><td\nclass=linenumbers><pre>" . join('', map {
179 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
180 } (1..$nl)) . '</pre></td>' .
181 '<td><pre> </pre></td>'. # pad for non-CSS users
182 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
183 $l->linkify_2($$blob) .
184 '</code></pre></td></tr></table>' . $log;
186 html_page($ctx, 200, \$log);
190 my ($ctx, $oid_b, $fn) = @_;
193 while (my ($from, $to) = each %QP_MAP) {
194 defined(my $v = $qp->{$from}) or next;
198 my $log = tmpfile("solve.$oid_b");
199 my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
200 solve_result($ctx, $_[0], $log, $hints, $fn);
203 # PSGI server will call this and give us a callback
205 $ctx->{-wcb} = $_[0]; # HTTP write callback
206 $solver->solve($ctx->{env}, $log, $oid_b, $hints);