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 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);
24 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
25 my $max_size = 1024 * 1024; # TODO: configurable
26 my $enc_utf8 = find_encoding('UTF-8');
29 my ($ctx, $code, $strref) = @_;
30 my $wcb = delete $ctx->{-wcb};
31 $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
32 my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
34 $nr == 1 ? $$strref : undef;
40 my ($ctx, $res, $log, $hints, $fn) = @_;
42 unless (seek($log, 0, 0)) {
43 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
44 return html_page($ctx, 500, \'seek error');
46 $log = do { local $/; <$log> };
49 my $l = PublicInbox::Linkify->new;
51 $log = '<pre>debug log:</pre><hr /><pre>' .
52 $l->linkify_2(ascii_html($log)) . '</pre>';
54 $res or return html_page($ctx, 404, \$log);
55 $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
57 my ($git, $oid, $type, $size, $di) = @$res;
58 if ($size > $max_size) {
59 # TODO: stream the raw file if it's gigantic, at least
60 $log = '<pre><b>Too big to show</b></pre>' . $log;
61 return html_page($ctx, 500, \$log);
64 my $blob = $git->cat_file($oid);
66 my $e = "Failed to retrieve generated blob ($oid)";
67 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
68 $log = "<pre><b>$e</b></pre>" . $log;
69 return html_page($ctx, 500, \$log);
72 my $binary = index($$blob, "\0") >= 0;
74 my $h = [ 'Content-Length', $size, 'Content-Type' ];
75 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
76 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
79 my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
80 my $raw_link = "(<a\nhref=$path>raw</a>)";
82 $log = "<pre>$oid $type $size bytes (binary)" .
83 " $raw_link</pre>" . $log;
84 return html_page($ctx, 200, \$log);
87 $$blob = $enc_utf8->decode($$blob);
88 my $nl = ($$blob =~ tr/\n/\n/);
89 my $pad = length($nl);
91 # using some of the same CSS class names and ids as cgit
92 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
93 "<hr /><table\nclass=blob>".
94 "<tr><td\nclass=linenumbers><pre>" . join('', map {
95 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
96 } (1..$nl)) . '</pre></td>' .
97 '<td><pre> </pre></td>'. # pad for non-CSS users
98 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
100 '</code></pre></td></tr></table>' . $log;
102 html_page($ctx, 200, \$log);
106 my ($ctx, $oid_b, $fn) = @_;
109 while (my ($from, $to) = each %QP_MAP) {
110 defined(my $v = $qp->{$from}) or next;
114 open my $log, '+>', undef or die "open: $!";
115 my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
116 solve_result($ctx, $_[0], $log, $hints, $fn);
119 # PSGI server will call this and give us a callback
121 $ctx->{-wcb} = $_[0]; # HTTP write callback
122 $solver->solve($ctx->{env}, $log, $oid_b, $hints);