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_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 {
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 ] ];
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] ];
68 warn "premature EOF on $oid $$logref\n";
69 return html_page($ctx, 500, $logref);
71 undef; # bref keeps growing
76 sub show_other_result ($$) {
77 my ($bref, $ctx) = @_;
78 my ($qsp, $logref) = delete @$ctx{qw(-qsp -logref)};
79 if (my $err = $qsp->{err}) {
81 $$logref .= "git show error: $err";
82 return html_page($ctx, 500, $logref);
84 my $l = PublicInbox::Linkify->new;
86 $l->linkify_1($$bref);
87 $$bref = '<pre>'. $l->linkify_2(ascii_html($$bref));
88 $$bref .= '</pre><hr>' . $$logref;
89 html_page($ctx, 200, $bref);
92 sub show_other ($$$$) {
93 my ($ctx, $res, $logref, $fn) = @_;
94 my ($git, $oid, $type, $size) = @$res;
95 if ($size > $MAX_SIZE) {
96 $$logref = "$oid is too big to show\n" . $$logref;
97 return html_page($ctx, 200, $logref);
99 my $cmd = ['git', "--git-dir=$git->{git_dir}",
100 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
101 my $qsp = PublicInbox::Qspawn->new($cmd);
102 my $env = $ctx->{env};
104 $ctx->{-logref} = $logref;
105 $qsp->psgi_qx($env, undef, \&show_other_result, $ctx);
109 my ($ctx, $res, $log, $hints, $fn) = @_;
111 unless (seek($log, 0, 0)) {
112 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
113 return html_page($ctx, 500, \'seek error');
115 $log = do { local $/; <$log> };
118 my $l = PublicInbox::Linkify->new;
120 $log = '<pre>debug log:</pre><hr /><pre>' .
121 $l->linkify_2(ascii_html($log)) . '</pre>';
123 $res or return html_page($ctx, 404, \$log);
124 $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
126 my ($git, $oid, $type, $size, $di) = @$res;
127 return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
128 my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
129 my $raw_link = "(<a\nhref=$path>raw</a>)";
130 if ($size > $MAX_SIZE) {
131 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
132 $log = "<pre><b>Too big to show, download available</b>\n" .
133 "$oid $type $size bytes $raw_link</pre>" . $log;
134 return html_page($ctx, 200, \$log);
137 my $blob = $git->cat_file($oid);
139 my $e = "Failed to retrieve generated blob ($oid)";
140 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
141 $log = "<pre><b>$e</b></pre>" . $log;
142 return html_page($ctx, 500, \$log);
145 my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
147 my $h = [ 'Content-Length', $size, 'Content-Type' ];
148 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
149 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
153 $log = "<pre>$oid $type $size bytes (binary)" .
154 " $raw_link</pre>" . $log;
155 return html_page($ctx, 200, \$log);
158 # TODO: detect + convert to ensure validity
159 utf8::decode($$blob);
160 my $nl = ($$blob =~ tr/\n/\n/);
161 my $pad = length($nl);
163 $l->linkify_1($$blob);
164 my $ok = $hl->do_hl($blob, $path) if $hl;
168 $$blob = ascii_html($$blob);
171 # using some of the same CSS class names and ids as cgit
172 $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
173 "<hr /><table\nclass=blob>".
174 "<tr><td\nclass=linenumbers><pre>" . join('', map {
175 sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
176 } (1..$nl)) . '</pre></td>' .
177 '<td><pre> </pre></td>'. # pad for non-CSS users
178 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
179 $l->linkify_2($$blob) .
180 '</code></pre></td></tr></table>' . $log;
182 html_page($ctx, 200, \$log);
186 my ($ctx, $oid_b, $fn) = @_;
189 while (my ($from, $to) = each %QP_MAP) {
190 defined(my $v = $qp->{$from}) or next;
194 my $log = tmpfile("solve.$oid_b");
195 my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
196 solve_result($ctx, $_[0], $log, $hints, $fn);
199 # PSGI server will call this and give us a callback
201 $ctx->{-wcb} = $_[0]; # HTTP write callback
202 $solver->solve($ctx->{env}, $log, $oid_b, $hints);